Commit
·
9e4ca7c
1
Parent(s):
4c0ff30
fixed issue...
Browse files
backend/services/database_service.py
CHANGED
@@ -5,7 +5,8 @@ from sqlalchemy import text, and_, or_
|
|
5 |
from backend.models.schemas import NLPAnalysisResult, DatabaseResult, QueryType
|
6 |
from backend.config import settings
|
7 |
from backend.database.connection import get_database_session, close_database_session
|
8 |
-
from backend.database.models import User, Product,
|
|
|
9 |
import logging
|
10 |
|
11 |
logger = logging.getLogger(__name__)
|
@@ -17,37 +18,32 @@ class DatabaseService:
|
|
17 |
self.model_mapping = {
|
18 |
"users": User,
|
19 |
"products": Product,
|
20 |
-
"
|
21 |
-
"
|
22 |
-
"line_messages": LineMessage
|
23 |
-
"user_sessions": UserSession
|
24 |
}
|
25 |
|
26 |
# 定義資料表結構
|
27 |
self.table_schemas = {
|
28 |
"users": {
|
29 |
"model": User,
|
30 |
-
"searchable": ["name", "email"
|
31 |
},
|
32 |
-
"
|
33 |
-
"model":
|
34 |
-
"searchable": ["
|
|
|
|
|
|
|
|
|
35 |
},
|
36 |
"products": {
|
37 |
"model": Product,
|
38 |
-
"searchable": ["
|
39 |
},
|
40 |
"line_messages": {
|
41 |
"model": LineMessage,
|
42 |
"searchable": ["message", "user_id"]
|
43 |
-
},
|
44 |
-
"order_items": {
|
45 |
-
"model": OrderItem,
|
46 |
-
"searchable": []
|
47 |
-
},
|
48 |
-
"user_sessions": {
|
49 |
-
"model": UserSession,
|
50 |
-
"searchable": ["user_id"]
|
51 |
}
|
52 |
}
|
53 |
|
@@ -493,8 +489,8 @@ class DatabaseService:
|
|
493 |
# 如果有查詢文字,進行模糊搜尋
|
494 |
if query_text:
|
495 |
search_filter = or_(
|
496 |
-
Product.
|
497 |
-
Product.
|
498 |
)
|
499 |
query = query.filter(search_filter)
|
500 |
|
@@ -510,12 +506,13 @@ class DatabaseService:
|
|
510 |
for product in products:
|
511 |
product_data = {
|
512 |
"id": product.id,
|
513 |
-
"
|
514 |
-
"
|
515 |
-
"
|
516 |
-
"
|
517 |
-
"stock": product.stock
|
518 |
-
"
|
|
|
519 |
}
|
520 |
data.append(product_data)
|
521 |
|
@@ -546,11 +543,11 @@ class DatabaseService:
|
|
546 |
|
547 |
# 根據商品名稱查詢
|
548 |
if product_name:
|
549 |
-
query = query.filter(Product.
|
550 |
|
551 |
-
# 類別篩選
|
552 |
if category:
|
553 |
-
query = query.
|
554 |
|
555 |
products = query.all()
|
556 |
|
@@ -558,12 +555,13 @@ class DatabaseService:
|
|
558 |
inventory_data = []
|
559 |
for product in products:
|
560 |
stock_info = {
|
561 |
-
"product_name": product.
|
562 |
-
"
|
563 |
-
"
|
564 |
-
"
|
565 |
-
"
|
566 |
-
"
|
|
|
567 |
}
|
568 |
inventory_data.append(stock_info)
|
569 |
|
@@ -589,15 +587,16 @@ class DatabaseService:
|
|
589 |
try:
|
590 |
db = get_database_session()
|
591 |
|
592 |
-
|
|
|
593 |
|
594 |
-
# 用戶篩選
|
595 |
if user_id:
|
596 |
-
query = query.filter(
|
597 |
|
598 |
# 狀態篩選
|
599 |
if status:
|
600 |
-
query = query.filter(
|
601 |
|
602 |
orders = query.limit(limit).all()
|
603 |
|
@@ -605,9 +604,11 @@ class DatabaseService:
|
|
605 |
order_data = []
|
606 |
for order in orders:
|
607 |
order_info = {
|
608 |
-
"order_id": order.
|
609 |
-
"
|
610 |
-
"
|
|
|
|
|
611 |
"total_amount": float(order.total_amount) if order.total_amount else 0.0,
|
612 |
"created_at": order.created_at.isoformat() if order.created_at else None,
|
613 |
"updated_at": order.updated_at.isoformat() if order.updated_at else None
|
@@ -647,10 +648,12 @@ class DatabaseService:
|
|
647 |
data = []
|
648 |
for product in low_stock_products:
|
649 |
product_data = {
|
650 |
-
"product_name": product.
|
651 |
-
"
|
652 |
-
"
|
653 |
-
"
|
|
|
|
|
654 |
"status": "庫存不足"
|
655 |
}
|
656 |
data.append(product_data)
|
@@ -679,18 +682,18 @@ class DatabaseService:
|
|
679 |
|
680 |
# 基本統計
|
681 |
total_products = db.query(Product).count()
|
682 |
-
|
|
|
683 |
total_users = db.query(User).count()
|
684 |
|
685 |
# 計算低庫存商品數量
|
686 |
-
low_stock_count =
|
687 |
-
if hasattr(Product, 'stock'):
|
688 |
-
low_stock_count = db.query(Product).filter(Product.stock <= 10).count()
|
689 |
|
690 |
# 準備摘要資料
|
691 |
summary_data = [{
|
692 |
"total_products": total_products,
|
693 |
-
"
|
|
|
694 |
"total_users": total_users,
|
695 |
"low_stock_items": low_stock_count,
|
696 |
"report_date": datetime.now().isoformat()
|
|
|
5 |
from backend.models.schemas import NLPAnalysisResult, DatabaseResult, QueryType
|
6 |
from backend.config import settings
|
7 |
from backend.database.connection import get_database_session, close_database_session
|
8 |
+
from backend.database.models import User, Product, PurchaseOrder, SalesOrder, LineMessage, Category
|
9 |
+
from datetime import datetime
|
10 |
import logging
|
11 |
|
12 |
logger = logging.getLogger(__name__)
|
|
|
18 |
self.model_mapping = {
|
19 |
"users": User,
|
20 |
"products": Product,
|
21 |
+
"purchase_orders": PurchaseOrder,
|
22 |
+
"sales_orders": SalesOrder,
|
23 |
+
"line_messages": LineMessage
|
|
|
24 |
}
|
25 |
|
26 |
# 定義資料表結構
|
27 |
self.table_schemas = {
|
28 |
"users": {
|
29 |
"model": User,
|
30 |
+
"searchable": ["name", "email"]
|
31 |
},
|
32 |
+
"purchase_orders": {
|
33 |
+
"model": PurchaseOrder,
|
34 |
+
"searchable": ["po_number", "status"]
|
35 |
+
},
|
36 |
+
"sales_orders": {
|
37 |
+
"model": SalesOrder,
|
38 |
+
"searchable": ["so_number", "status"]
|
39 |
},
|
40 |
"products": {
|
41 |
"model": Product,
|
42 |
+
"searchable": ["productName", "productCode", "barcode"]
|
43 |
},
|
44 |
"line_messages": {
|
45 |
"model": LineMessage,
|
46 |
"searchable": ["message", "user_id"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
}
|
48 |
}
|
49 |
|
|
|
489 |
# 如果有查詢文字,進行模糊搜尋
|
490 |
if query_text:
|
491 |
search_filter = or_(
|
492 |
+
Product.productName.ilike(f"%{query_text}%"),
|
493 |
+
Product.productCode.ilike(f"%{query_text}%")
|
494 |
)
|
495 |
query = query.filter(search_filter)
|
496 |
|
|
|
506 |
for product in products:
|
507 |
product_data = {
|
508 |
"id": product.id,
|
509 |
+
"product_code": product.productCode,
|
510 |
+
"product_name": product.productName,
|
511 |
+
"unit": product.unit,
|
512 |
+
"warehouse": product.warehouse,
|
513 |
+
"stock": product.stock,
|
514 |
+
"category_name": product.category.name if product.category else None,
|
515 |
+
"created_at": product.createdAt.isoformat() if product.createdAt else None
|
516 |
}
|
517 |
data.append(product_data)
|
518 |
|
|
|
543 |
|
544 |
# 根據商品名稱查詢
|
545 |
if product_name:
|
546 |
+
query = query.filter(Product.productName.ilike(f"%{product_name}%"))
|
547 |
|
548 |
+
# 類別篩選 (通過關聯查詢)
|
549 |
if category:
|
550 |
+
query = query.join(Product.category).filter(Category.name.ilike(f"%{category}%"))
|
551 |
|
552 |
products = query.all()
|
553 |
|
|
|
555 |
inventory_data = []
|
556 |
for product in products:
|
557 |
stock_info = {
|
558 |
+
"product_name": product.productName,
|
559 |
+
"product_code": product.productCode,
|
560 |
+
"category": product.category.name if product.category else None,
|
561 |
+
"current_stock": product.stock,
|
562 |
+
"unit": product.unit,
|
563 |
+
"warehouse": product.warehouse,
|
564 |
+
"last_updated": product.updatedAt.isoformat() if product.updatedAt else None
|
565 |
}
|
566 |
inventory_data.append(stock_info)
|
567 |
|
|
|
587 |
try:
|
588 |
db = get_database_session()
|
589 |
|
590 |
+
# 使用 SalesOrder 作為主要的訂單查詢
|
591 |
+
query = db.query(SalesOrder)
|
592 |
|
593 |
+
# 用戶篩選 (銷售人員)
|
594 |
if user_id:
|
595 |
+
query = query.filter(SalesOrder.salesperson_id == user_id)
|
596 |
|
597 |
# 狀態篩選
|
598 |
if status:
|
599 |
+
query = query.filter(SalesOrder.status.ilike(f"%{status}%"))
|
600 |
|
601 |
orders = query.limit(limit).all()
|
602 |
|
|
|
604 |
order_data = []
|
605 |
for order in orders:
|
606 |
order_info = {
|
607 |
+
"order_id": order.so_number,
|
608 |
+
"sales_date": order.sales_date.isoformat() if order.sales_date else None,
|
609 |
+
"customer_name": order.customer.customerName if order.customer else None,
|
610 |
+
"salesperson_name": order.salesperson.name if order.salesperson else None,
|
611 |
+
"status": order.status.value if order.status else None,
|
612 |
"total_amount": float(order.total_amount) if order.total_amount else 0.0,
|
613 |
"created_at": order.created_at.isoformat() if order.created_at else None,
|
614 |
"updated_at": order.updated_at.isoformat() if order.updated_at else None
|
|
|
648 |
data = []
|
649 |
for product in low_stock_products:
|
650 |
product_data = {
|
651 |
+
"product_name": product.productName,
|
652 |
+
"product_code": product.productCode,
|
653 |
+
"current_stock": product.stock,
|
654 |
+
"unit": product.unit,
|
655 |
+
"warehouse": product.warehouse,
|
656 |
+
"category": product.category.name if product.category else None,
|
657 |
"status": "庫存不足"
|
658 |
}
|
659 |
data.append(product_data)
|
|
|
682 |
|
683 |
# 基本統計
|
684 |
total_products = db.query(Product).count()
|
685 |
+
total_sales_orders = db.query(SalesOrder).count()
|
686 |
+
total_purchase_orders = db.query(PurchaseOrder).count()
|
687 |
total_users = db.query(User).count()
|
688 |
|
689 |
# 計算低庫存商品數量
|
690 |
+
low_stock_count = db.query(Product).filter(Product.stock <= 10).count()
|
|
|
|
|
691 |
|
692 |
# 準備摘要資料
|
693 |
summary_data = [{
|
694 |
"total_products": total_products,
|
695 |
+
"total_sales_orders": total_sales_orders,
|
696 |
+
"total_purchase_orders": total_purchase_orders,
|
697 |
"total_users": total_users,
|
698 |
"low_stock_items": low_stock_count,
|
699 |
"report_date": datetime.now().isoformat()
|