test_business_unit_dashboard_apis.py
10.3 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
事业部驾驶舱接口测试脚本
"""
import requests
import json
from datetime import datetime
# API基础URL
BASE_URL = "http://localhost:2011"
# 登录接口获取Token
def login():
"""登录获取Token"""
url = f"{BASE_URL}/api/oauth/Login"
data = {
"account": "admin",
"password": "e10adc3949ba59abbe56e057f20f883e"
}
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
try:
response = requests.post(url, data=data, headers=headers)
response.raise_for_status()
result = response.json()
if result.get("code") == 200:
token = result.get("data", {}).get("token")
print(f"✅ 登录成功,Token: {token[:50]}...")
return token
else:
print(f"❌ 登录失败: {result.get('msg')}")
return None
except Exception as e:
print(f"❌ 登录异常: {str(e)}")
return None
# 测试GetStatistics接口
def test_get_statistics(token, business_unit_id=None, store_ids=None, statistics_month="202512"):
"""测试获取统计数据"""
url = f"{BASE_URL}/api/Extend/LqBusinessUnitDashboard/GetStatistics"
headers = {
"Content-Type": "application/json",
"Authorization": token
}
data = {
"statisticsMonth": statistics_month
}
if business_unit_id:
data["businessUnitId"] = business_unit_id
elif store_ids:
data["storeIds"] = store_ids
try:
response = requests.post(url, json=data, headers=headers)
response.raise_for_status()
result = response.json()
if result.get("code") == 200 or "billingPerformance" in str(result):
print("\n✅ GetStatistics 测试成功")
print(f" 开单业绩: {result.get('billingPerformance', 0):,.2f}")
print(f" 消耗业绩: {result.get('consumePerformance', 0):,.2f}")
print(f" 净业绩: {result.get('netPerformance', 0):,.2f}")
print(f" 目标业绩: {result.get('targetPerformance', 0):,.2f}")
print(f" 完成率: {result.get('completionRate', 0):.2f}%")
print(f" 管理的门店数: {result.get('managedStoreCount', 0)}")
print(f" 活跃门店数: {result.get('activeStoreCount', 0)}")
return True
else:
print(f"\n❌ GetStatistics 测试失败: {result}")
return False
except Exception as e:
print(f"\n❌ GetStatistics 测试异常: {str(e)}")
return False
# 测试GetPerformanceTrend接口
def test_get_performance_trend(token, business_unit_id=None, statistics_month="202512", month_count=12):
"""测试获取业绩趋势"""
url = f"{BASE_URL}/api/Extend/LqBusinessUnitDashboard/GetPerformanceTrend"
headers = {
"Content-Type": "application/json",
"Authorization": token
}
data = {
"statisticsMonth": statistics_month,
"monthCount": month_count
}
if business_unit_id:
data["businessUnitId"] = business_unit_id
try:
response = requests.post(url, json=data, headers=headers)
response.raise_for_status()
result = response.json()
if result.get("code") == 200 or "trendData" in str(result):
trend_data = result.get("trendData", [])
print(f"\n✅ GetPerformanceTrend 测试成功")
print(f" 趋势数据点数量: {len(trend_data)}")
if trend_data:
latest = trend_data[-1]
print(f" 最新月份: {latest.get('month')}")
print(f" 最新开单业绩: {latest.get('billingPerformance', 0):,.2f}")
return True
else:
print(f"\n❌ GetPerformanceTrend 测试失败: {result}")
return False
except Exception as e:
print(f"\n❌ GetPerformanceTrend 测试异常: {str(e)}")
return False
# 测试GetStoreRanking接口
def test_get_store_ranking(token, business_unit_id=None, statistics_month="202512", ranking_type="Billing", top_count=10):
"""测试获取门店排行"""
url = f"{BASE_URL}/api/Extend/LqBusinessUnitDashboard/GetStoreRanking"
headers = {
"Content-Type": "application/json",
"Authorization": token
}
data = {
"statisticsMonth": statistics_month,
"rankingType": ranking_type,
"topCount": top_count
}
if business_unit_id:
data["businessUnitId"] = business_unit_id
try:
response = requests.post(url, json=data, headers=headers)
response.raise_for_status()
result = response.json()
if result.get("code") == 200 or "rankingData" in str(result):
ranking_data = result.get("rankingData", [])
print(f"\n✅ GetStoreRanking 测试成功")
print(f" 排行数据数量: {len(ranking_data)}")
if ranking_data:
top1 = ranking_data[0]
print(f" 第1名: {top1.get('storeName')} - 开单业绩: {top1.get('billingPerformance', 0):,.2f}")
return True
else:
print(f"\n❌ GetStoreRanking 测试失败: {result}")
return False
except Exception as e:
print(f"\n❌ GetStoreRanking 测试异常: {str(e)}")
return False
# 测试GetOperationStatistics接口
def test_get_operation_statistics(token, business_unit_id=None, statistics_month="202512"):
"""测试获取运营统计"""
url = f"{BASE_URL}/api/Extend/LqBusinessUnitDashboard/GetOperationStatistics"
headers = {
"Content-Type": "application/json",
"Authorization": token
}
data = {
"statisticsMonth": statistics_month
}
if business_unit_id:
data["businessUnitId"] = business_unit_id
try:
response = requests.post(url, json=data, headers=headers)
response.raise_for_status()
result = response.json()
if result.get("code") == 200 or "billingAnalysis" in str(result):
billing = result.get("billingAnalysis", {})
consume = result.get("consumeAnalysis", {})
refund = result.get("refundAnalysis", {})
print(f"\n✅ GetOperationStatistics 测试成功")
print(f" 开单次数: {billing.get('billingCount', 0)}")
print(f" 平均开单金额: {billing.get('averageBillingAmount', 0):,.2f}")
print(f" 消耗次数: {consume.get('consumeCount', 0)}")
print(f" 消耗率: {consume.get('consumeRate', 0):.2f}%")
print(f" 退卡次数: {refund.get('refundCount', 0)}")
return True
else:
print(f"\n❌ GetOperationStatistics 测试失败: {result}")
return False
except Exception as e:
print(f"\n❌ GetOperationStatistics 测试异常: {str(e)}")
return False
# 测试GetStoreDetailList接口
def test_get_store_detail_list(token, business_unit_id=None, statistics_month="202512", current_page=1, page_size=10):
"""测试获取门店明细列表"""
url = f"{BASE_URL}/api/Extend/LqBusinessUnitDashboard/GetStoreDetailList"
headers = {
"Content-Type": "application/json",
"Authorization": token
}
data = {
"statisticsMonth": statistics_month,
"currentPage": current_page,
"pageSize": page_size
}
if business_unit_id:
data["businessUnitId"] = business_unit_id
try:
response = requests.post(url, json=data, headers=headers)
response.raise_for_status()
result = response.json()
if result.get("code") == 200 or "list" in str(result):
total = result.get("total", 0)
list_data = result.get("list", [])
print(f"\n✅ GetStoreDetailList 测试成功")
print(f" 总记录数: {total}")
print(f" 当前页数据: {len(list_data)}")
if list_data:
first = list_data[0]
print(f" 第1条: {first.get('storeName')} - 开单业绩: {first.get('billingPerformance', 0):,.2f}")
return True
else:
print(f"\n❌ GetStoreDetailList 测试失败: {result}")
return False
except Exception as e:
print(f"\n❌ GetStoreDetailList 测试异常: {str(e)}")
return False
# 主测试函数
def main():
print("=" * 60)
print("事业部驾驶舱接口测试")
print("=" * 60)
# 1. 登录获取Token
token = login()
if not token:
print("❌ 无法获取Token,测试终止")
return
# 测试用的事业部ID(需要根据实际数据调整)
business_unit_id = "734725299018663173"
statistics_month = "202512"
# 2. 测试所有接口
test_results = []
print("\n" + "=" * 60)
print("开始测试接口...")
print("=" * 60)
# 测试GetStatistics
test_results.append(("GetStatistics", test_get_statistics(token, business_unit_id=business_unit_id, statistics_month=statistics_month)))
# 测试GetPerformanceTrend
test_results.append(("GetPerformanceTrend", test_get_performance_trend(token, business_unit_id=business_unit_id, statistics_month=statistics_month, month_count=6)))
# 测试GetStoreRanking
test_results.append(("GetStoreRanking", test_get_store_ranking(token, business_unit_id=business_unit_id, statistics_month=statistics_month, ranking_type="Billing", top_count=5)))
# 测试GetOperationStatistics
test_results.append(("GetOperationStatistics", test_get_operation_statistics(token, business_unit_id=business_unit_id, statistics_month=statistics_month)))
# 测试GetStoreDetailList
test_results.append(("GetStoreDetailList", test_get_store_detail_list(token, business_unit_id=business_unit_id, statistics_month=statistics_month, current_page=1, page_size=5)))
# 3. 输出测试结果汇总
print("\n" + "=" * 60)
print("测试结果汇总")
print("=" * 60)
success_count = sum(1 for _, result in test_results if result)
total_count = len(test_results)
for name, result in test_results:
status = "✅ 通过" if result else "❌ 失败"
print(f"{status} - {name}")
print(f"\n总计: {success_count}/{total_count} 通过")
print("=" * 60)
if __name__ == "__main__":
main()