File size: 8,711 Bytes
68c8519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""
測試訂單查詢功能 - 基於實際銷售訂單資料
"""

def test_order_number_extraction():
    """測試訂單編號提取功能"""
    
    def extract_order_number_simple(message: str) -> str:
        """簡化版訂單編號提取"""
        import re
        
        # 尋找 SO- 格式的訂單編號
        so_pattern = r'SO-\d{8}-\d{3}'
        match = re.search(so_pattern, message.upper())
        if match:
            return match.group()
        
        # 尋找日期格式的編號 (20250706-001)
        date_pattern = r'\d{8}-\d{3}'
        match = re.search(date_pattern, message)
        if match:
            return f"SO-{match.group()}"
        
        # 尋找純數字編號
        number_pattern = r'\d{6,}'
        match = re.search(number_pattern, message)
        if match:
            return match.group()
        
        return None
    
    print("🔍 測試訂單編號提取")
    print("=" * 50)
    
    test_queries = [
        "查詢訂單 SO-20250706-001",
        "我的訂單 SO-20250708-001 狀態如何?",
        "20250706-001 這個訂單",
        "訂單編號 20250708-001",
        "SO20250706001",  # 沒有連字符
        "查詢我的訂單",  # 沒有編號
    ]
    
    for query in test_queries:
        order_number = extract_order_number_simple(query)
        print(f"'{query}' → 訂單編號: {order_number}")

def test_order_status_extraction():
    """測試訂單狀態提取功能"""
    
    def extract_order_status_simple(message: str) -> str:
        """簡化版訂單狀態提取"""
        message_lower = message.lower()
        
        # 中文狀態關鍵字
        status_keywords = {
            '已交付': 'DELIVERED',
            '已出貨': 'SHIPPED',
            '出貨': 'SHIPPED',
            '交付': 'DELIVERED',
            '處理中': 'PROCESSING',
            '已取消': 'CANCELLED',
            '取消': 'CANCELLED',
            '待處理': 'PENDING',
            '已確認': 'CONFIRMED',
            '已完成': 'COMPLETED'
        }
        
        for chinese, english in status_keywords.items():
            if chinese in message_lower:
                return english
        
        # 英文狀態關鍵字
        english_keywords = ['delivered', 'shipped', 'processing', 'cancelled', 'pending', 'confirmed', 'completed']
        for keyword in english_keywords:
            if keyword in message_lower:
                return keyword.upper()
        
        return None
    
    print("\n📊 測試訂單狀態提取")
    print("=" * 50)
    
    test_queries = [
        "查詢已交付的訂單",
        "已出貨訂單有哪些?",
        "處理中的訂單",
        "DELIVERED 狀態的訂單",
        "shipped orders",
        "我的訂單狀態",  # 沒有狀態
    ]
    
    for query in test_queries:
        status = extract_order_status_simple(query)
        print(f"'{query}' → 狀態: {status}")

def test_order_query_simulation():
    """模擬訂單查詢功能"""
    
    # 模擬的銷售訂單資料(來自您的 Supabase)
    orders = [
        {
            "id": 1,
            "so_number": "SO-20250706-001",
            "sales_date": "2025-07-06",
            "customer_id": 1,
            "salesperson_id": 1,
            "payment_term": "MONTHLY",
            "status": "DELIVERED",
            "subtotal": 0,
            "tax_amount": 0,
            "discount_amount": 0,
            "total_amount": 0,
            "created_at": "2025-07-06 14:53:23",
            "updated_at": "2025-07-06 14:55:18"
        },
        {
            "id": 2,
            "so_number": "SO-20250708-001",
            "sales_date": "2025-07-08",
            "customer_id": 1,
            "salesperson_id": 1,
            "payment_term": "CASH",
            "status": "SHIPPED",
            "subtotal": 0,
            "tax_amount": 0,
            "discount_amount": 0,
            "total_amount": 0,
            "created_at": "2025-07-08 06:24:34",
            "updated_at": "2025-07-10 07:08:12"
        }
    ]
    
    def search_orders_simulation(order_number=None, status=None):
        """模擬訂單搜尋"""
        results = []
        
        for order in orders:
            match = True
            
            # 訂單編號篩選
            if order_number:
                if order_number not in order["so_number"]:
                    match = False
            
            # 狀態篩選
            if status:
                if status.upper() != order["status"]:
                    match = False
            
            if match:
                results.append(order)
        
        return results
    
    def get_status_display(status):
        """狀態中文顯示"""
        status_mapping = {
            'DELIVERED': '已交付',
            'SHIPPED': '已出貨',
            'PROCESSING': '處理中',
            'CANCELLED': '已取消',
            'PENDING': '待處理'
        }
        return status_mapping.get(status, status)
    
    print("\n🛍️ 測試訂單查詢模擬")
    print("=" * 50)
    
    test_scenarios = [
        {"query": "查詢訂單 SO-20250706-001", "order_number": "SO-20250706-001", "status": None},
        {"query": "已交付的訂單", "order_number": None, "status": "DELIVERED"},
        {"query": "已出貨訂單", "order_number": None, "status": "SHIPPED"},
        {"query": "我的所有訂單", "order_number": None, "status": None},
        {"query": "SO-20250708-001 狀態", "order_number": "SO-20250708-001", "status": None},
    ]
    
    for scenario in test_scenarios:
        print(f"\n查詢: '{scenario['query']}'")
        results = search_orders_simulation(
            order_number=scenario["order_number"],
            status=scenario["status"]
        )
        
        print(f"找到 {len(results)} 個訂單:")
        for order in results:
            status_display = get_status_display(order["status"])
            print(f"  ✅ {order['so_number']} - {status_display} - {order['sales_date']} - 付款: {order['payment_term']}")

def test_natural_language_processing():
    """測試自然語言處理"""
    
    print("\n🤖 測試自然語言訂單查詢")
    print("=" * 50)
    
    def extract_order_number_simple(message: str) -> str:
        import re
        so_pattern = r'SO-\d{8}-\d{3}'
        match = re.search(so_pattern, message.upper())
        if match:
            return match.group()
        
        date_pattern = r'\d{8}-\d{3}'
        match = re.search(date_pattern, message)
        if match:
            return f"SO-{match.group()}"
        
        return None
    
    def extract_order_status_simple(message: str) -> str:
        message_lower = message.lower()
        status_keywords = {
            '已交付': 'DELIVERED',
            '已出貨': 'SHIPPED',
            '出貨': 'SHIPPED',
            '交付': 'DELIVERED',
            '處理中': 'PROCESSING'
        }
        
        for chinese, english in status_keywords.items():
            if chinese in message_lower:
                return english
        
        return None
    
    natural_queries = [
        "請問我的訂單 SO-20250706-001 現在是什麼狀態?",
        "查詢已交付的所有訂單",
        "20250708-001 這個訂單出貨了嗎?",
        "我有哪些已出貨的訂單?",
        "訂單狀態查詢"
    ]
    
    for query in natural_queries:
        print(f"\n自然語言查詢: '{query}'")
        order_number = extract_order_number_simple(query)
        status = extract_order_status_simple(query)
        
        print(f"  提取的訂單編號: {order_number}")
        print(f"  提取的狀態: {status}")
        
        # 判斷查詢類型
        if order_number and status:
            query_type = "特定訂單狀態查詢"
        elif order_number:
            query_type = "特定訂單查詢"
        elif status:
            query_type = "狀態篩選查詢"
        else:
            query_type = "一般訂單查詢"
        
        print(f"  查詢類型: {query_type}")

def main():
    """主函數"""
    print("🚀 開始訂單查詢功能測試\n")
    
    test_order_number_extraction()
    test_order_status_extraction()
    test_order_query_simulation()
    test_natural_language_processing()
    
    print("\n" + "=" * 50)
    print("✅ 測試完成!")
    print("\n💡 分析結果:")
    print("1. 訂單編號提取邏輯能正確識別 SO- 格式")
    print("2. 狀態提取支援中文和英文")
    print("3. 查詢模擬能正確篩選訂單")
    print("4. 自然語言處理能提取關鍵資訊")

if __name__ == "__main__":
    main()