41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
from ..models.product import Product
|
|
|
|
|
|
class InventoryAlertService:
|
|
@staticmethod
|
|
def get_low_stock_products():
|
|
"""獲取庫存低於警戒值的商品"""
|
|
return Product.query.filter(
|
|
Product.quantity <= Product.min_quantity
|
|
).all()
|
|
|
|
@staticmethod
|
|
def get_stock_status():
|
|
"""獲取所有商品的庫存狀態"""
|
|
products = Product.query.all()
|
|
return [{
|
|
'product': product,
|
|
'status': 'danger' if product.quantity <= product.min_quantity else 'normal',
|
|
'margin': product.quantity - product.min_quantity,
|
|
'percentage': round((product.quantity / product.min_quantity * 100)
|
|
if product.min_quantity > 0 else 100)
|
|
} for product in products]
|
|
|
|
@staticmethod
|
|
def get_alert_summary():
|
|
"""獲取警告統計摘要"""
|
|
total_products = Product.query.count()
|
|
low_stock_count = Product.query.filter(
|
|
Product.quantity <= Product.min_quantity
|
|
).count()
|
|
|
|
return {
|
|
'total_products': total_products,
|
|
'low_stock_count': low_stock_count,
|
|
'alert_percentage': round(
|
|
(low_stock_count / total_products * 100)
|
|
if total_products > 0 else 0,
|
|
1
|
|
)
|
|
}
|