generateDetailData.py
21.1 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import sys
import os
import argparse
# 将当前目录添加到 sys.path
current_dir = os.path.dirname(os.path.abspath(__file__))
if current_dir not in sys.path:
sys.path.append(current_dir)
from calc_dj_income_2021 import *
from handle_unmatch_delivery_device import *
def merge(startTime,report_year):
print("merge start==")
bizConn = MySQL(g_pro_biz_dbcfg)
sqlList = []
# 记录仪&车充合并
updateSql = f"""
insert into dj_2021_settle_details (`channel`,`is_virtual`,`sn`,`iccid`,`device_type`,`material_code`,`delivery_order`,`delivery_time`,`delivery_merchant`,`active_time`,`active_merchant`,`pk_name`,
`pk_period`,`settle_merchant`,`settle_order`,`settle_time`,`pk_price`,`pk_price_tax`,`hw_price`,`hw_price_tax`,`install_price`,`install_price_tax`,`cost`,`flag`,`return`,`hw_cost`,`install_cost`)
SELECT `channel`,`is_virtual`,`sn`,`iccid`,`device_type`,`material_code`,`delivery_order`,`delivery_time`,`delivery_merchant`,`active_time`,`active_merchant`,`pk_name`,
`pk_period`,`settle_merchant`,`settle_order`,`settle_time`,`pk_price`,`pk_price_tax`,`hw_price`,`hw_price_tax`,`install_price`,`install_price_tax`,`cost`,`flag`,`return`,`hw_cost`,`install_cost`
from dj_{report_year}_settle_details_record where settle_time >='{startTime}';
"""
print("updateSql===", updateSql)
sqlList.append(updateSql)
# 追踪器合并
updateSql01 = f"""
insert into dj_2021_settle_details (`channel`,`is_virtual`,`sn`,`iccid`,`device_type`,`material_code`,`delivery_order`,`delivery_time`,`delivery_merchant`,`active_time`,`active_merchant`,`pk_name`,
`pk_period`,`settle_merchant`,`settle_order`,`settle_time`,`pk_price`,`pk_price_tax`,`hw_price`,`hw_price_tax`,`install_price`,`install_price_tax`,`cost`,`flag`,`return`,`hw_cost`,`install_cost`)
SELECT `channel`,`is_virtual`,`sn`,`iccid`,`device_type`,`material_code`,`delivery_order`,`delivery_time`,`delivery_merchant`,`active_time`,`active_merchant`,`pk_name`,
`pk_period`,`settle_merchant`,`settle_order`,`settle_time`,`pk_price`,`pk_price_tax`,`hw_price`,`hw_price_tax`,`install_price`,`install_price_tax`,`cost`,`flag`,`return`,`hw_cost`,`install_cost`
from dj_{report_year}_settle_details where settle_time >='{startTime}';
"""
print("updateSql01===", updateSql01)
sqlList.append(updateSql01)
if not bizConn.updateList(sqlList):
logger.error("handle_return_device fail, sql=%s", ",".join(sqlList))
print("merge end==")
def deal_data(startTime, endTime, report_year):
# # 导入GPS OBD 车充设备
query_supplychain_delivery_device(startTime, endTime, devType=8)
# 导入记录仪 车充设备
query_supplychain_delivery_device(startTime, endTime, devType=6)
# 更新供应链出库设备的结算客户 6表示: 记录仪 车充
update_delivery_device_settle_merchant(6, 2021)
update_delivery_device_settle_merchant(8, 2021)
# GPS OBD 处理直销 (原代销)
match_not_direct_settlement_delivery_order(startTime, report_year, devType=8, mode=0)
# GPS OBD 匹配客户渠道 (原直销)
match_direct_settlement_delivery_order(startTime, report_year, devType=8, findmode=0)
# 记录仪直销 (原代销)
match_not_direct_settlement_delivery_order(startTime, report_year, devType=6, mode=0)
# 记录仪渠道 (原直销)
match_direct_settlement_delivery_order(startTime, report_year, devType=6, findmode=0)
# 车充直销 (原代销)
match_not_direct_settlement_delivery_order(startTime, report_year, devType=12501, mode=0)
# 车充渠道 (原直销)
match_direct_settlement_delivery_order(startTime, report_year, devType=12501, findmode=0)
# 生成未匹配的设备数据
# 追踪器设备
match_gen_sn_settlement_delivery_order(startTime, report_year, devType=8, prefix="19")
# 记录仪设备
match_gen_sn_settlement_delivery_order(startTime, report_year, devType=6, prefix="SH23G")
# 'SH26H', 'SH23G', 'SH24G'
# 车充设备
match_gen_sn_settlement_delivery_order(startTime, report_year, devType=12501, prefix="864")
# 合并汇总数据
merge(startTime, report_year)
# 处理退货
# 直销追踪器退货 -- 一般不会有直销的退货
# 追踪器
handle_return_device("直销", report_year, data=2021, devType=8)
# 记录仪
handle_return_device("直销", report_year, data=2021, devType=6)
# 车充退货
handle_return_device("直销", report_year, data=2021, devType=12501)
# 车充退货
handle_return_device("渠道", report_year, data=2021, devType=12501)
# 渠道追踪器退货
handle_return_device("渠道", report_year, data=2021, devType=8)
# 记录仪
handle_return_device("渠道", report_year, data=2021, devType=6)
# 将新增的数据拷贝到生成环境同步的表中
def copyNewDataToProTable(startTime):
bizConn = MySQL(g_pro_biz_dbcfg)
sqlList = []
# 1. 先删除旧数据
delSql = f"DELETE FROM dj_device_details WHERE settle_time >= '{startTime}';"
print("copyNewDataToProTable delSql==" + delSql)
sqlList.append(delSql)
# 2. 插入新数据
insertSql = f"""
insert into dj_device_details (channel,sn,device_type,delivery_time,delivery_merchant,active_time,
active_merchant,pk_name,pk_period,settle_merchant,settle_order,settle_time,pk_price,hw_price,install_price,hw_cost,
install_cost)
SELECT channel,sn,device_type,delivery_time,delivery_merchant,active_time,
active_merchant,pk_name,pk_period,settle_merchant,settle_order,settle_time,pk_price,hw_price,install_price,hw_cost,
install_cost from dj_2021_settle_details where settle_time >= '{startTime}';
"""
print("copyNewDataToProTable insertSql==" + insertSql)
sqlList.append(insertSql)
if not bizConn.updateList(sqlList):
logger.error("copyNewDataToProTable fail, sql=%s", ",".join(sqlList))
def active(report_year):
# 记录仪渠道激活
# active_direct_record_device(report_year)
# 追踪器渠道测算激活
active_direct_merchant_device(report_year)
def rollback(startTime, endTime, report_year):
update_device_alloc(8, startTime, endTime, report_year)
update_device_alloc(6, startTime, endTime, report_year)
bizConn = MySQL(g_pro_biz_dbcfg)
sqlList = []
# 退货标记还原
#OK
updateSql = f"""
update dj_2021_settle_details set `return` = 0 where sn in (SELECT sn from dj_{report_year}_settle_details_return where settle_time >= '{startTime}');
"""
print("updateSql==" + updateSql)
sqlList.append(updateSql)
# 整体
updateSql01 = f"""
DELETE from dj_2021_settle_details where settle_time >= '{startTime}';
"""
print("updateSql01==" + updateSql01)
sqlList.append(updateSql01)
# GPS
updateSql02 = f"""
DELETE from dj_{report_year}_settle_details where settle_time >= '{startTime}';
"""
print("updateSql02==" + updateSql02)
sqlList.append(updateSql02)
# 记录仪
updateSql03 = f"""
DELETE from dj_{report_year}_settle_details_record where settle_time >= '{startTime}' ;
"""
print("updateSql03==" + updateSql03)
sqlList.append(updateSql03)
# 退货表
updateSql04 = f"""
DELETE from dj_{report_year}_settle_details_return where settle_time >= '{startTime}' ;
"""
print("updateSql04==" + updateSql04)
sqlList.append(updateSql04)
# 商务表
updateSql05 = f"""
update dj_{report_year}_settlment_order set match_total=NULL,`status`=0 where finance_time >= '{startTime}' and finance_time <= '{endTime}';
"""
print("updateSql05==" + updateSql05)
sqlList.append(updateSql05)
# updateSql06 = f"""
# truncate TABLE dj_2023_settle_details;
# """
# print("updateSql06==" + updateSql06)
# sqlList.append(updateSql06)
# 删除同步到生产库中的表数据
# updateSql11 = f"""
# DELETE FROM dj_device_details WHERE settle_time >= '{startTime}';
# """
# print("updateSql11==" + updateSql11)
# sqlList.append(updateSql11)
if not bizConn.updateList(sqlList):
logger.error("handle_return_device fail, sql=%s", ",".join(sqlList))
def pre_deal(startTime, endTime, report_year):
bizConn = MySQL(g_pro_biz_dbcfg)
sqlList = []
updateSql = f"""
UPDATE dj_{report_year}_settlment_order_finance SET device_type = '其他' WHERE product_name LIKE '%无忧卡%' AND finance_time >= '{startTime}' AND finance_time <= '{endTime}';
"""
sqlList.append(updateSql)
updateSql01 = f"""
UPDATE dj_{report_year}_settlment_order SET device_type = '其他' WHERE product_name LIKE '%无忧卡%' AND finance_time >= '{startTime}' AND finance_time <= '{endTime}';
"""
sqlList.append(updateSql01)
updateSql02 = f"""
UPDATE dj_{report_year}_settlment_order SET cus_name = '遵义千汇汽车销售服务有限公司' WHERE cus_name = '遵义市千汇汽车销售服务有限公司' AND finance_time >= '{startTime}' AND finance_time <= '{endTime}';
"""
sqlList.append(updateSql02)
updateSql03 = f"""
UPDATE dj_{report_year}_settlment_order_finance SET cus_name = '遵义千汇汽车销售服务有限公司' WHERE cus_name = '遵义市千汇汽车销售服务有限公司' AND finance_time >= '{startTime}' AND finance_time <= '{endTime}';
"""
sqlList.append(updateSql03)
if not bizConn.updateList(sqlList):
logger.error("handle_return_device fail, sql=%s", ",".join(sqlList))
def importData():
filePath = "F:\\审计\\"
fileName = filePath + "嘀加激活测算(2024年1-6月)0704.xlsx"
sheetName = "Sheet1"
tableName = "dj_2022_direct_cus_active"
"""将Excel表数据导入mysql"""
conn_updata = pymysql.Connect(
host="192.168.5.2",
port=3306,
user="biz_glsx_data",
passwd="unEKczDc",
db="glsx_biz_data",
charset="utf8",
)
data = pd.read_excel(fileName, sheet_name=sheetName, header=0)
newData = data[
[
"客户",
"2024年12底未激活数据",
"2024年1月结算",
"2024年1月累计未激活",
"2024年1月激活",
"2024年2月结算",
"2024年2月累计未激活",
"2024年2月激活",
"2024年3月结算",
"2024年3月累计未激活",
"2024年3月激活",
"2024年4月结算",
"2024年4月累计未激活",
"2024年4月激活",
"2024年5月结算",
"2024年5月累计未激活",
"2024年5月激活",
"2024年6月结算",
"2024年6月累计未激活",
"2024年6月激活",
"2024年7月结算",
"2024年7月累计未激活",
"2024年7月激活",
"2024年8月结算",
"2024年8月累计未激活",
"2024年8月激活",
"2024年9月结算",
"2024年9月累计未激活",
"2024年9月激活",
"2024年10月结算",
"2024年10月累计未激活",
"2024年10月激活",
"2024年11月结算",
"2024年11月累计未激活",
"2024年11月激活",
"2024年12月结算",
"2024年12月累计未激活",
"2024年12月激活",
"本年未激活",
"本年结算+去年未激活",
"本年激活",
]
]
newData["2024年12底未激活数据"].fillna(0, inplace=True)
newData["2024年1月结算"].fillna(0, inplace=True)
newData["2024年1月累计未激活"].fillna(0, inplace=True)
newData["2024年1月激活"].fillna(0, inplace=True)
newData["2024年2月结算"].fillna(0, inplace=True)
newData["2024年2月累计未激活"].fillna(0, inplace=True)
newData["2024年2月激活"].fillna(0, inplace=True)
newData["2024年3月结算"].fillna(0, inplace=True)
newData["2024年3月累计未激活"].fillna(0, inplace=True)
newData["2024年3月激活"].fillna(0, inplace=True)
newData["2024年4月结算"].fillna(0, inplace=True)
newData["2024年4月累计未激活"].fillna(0, inplace=True)
newData["2024年4月激活"].fillna(0, inplace=True)
newData["2024年5月结算"].fillna(0, inplace=True)
newData["2024年5月累计未激活"].fillna(0, inplace=True)
newData["2024年5月激活"].fillna(0, inplace=True)
newData["2024年6月结算"].fillna(0, inplace=True)
newData["2024年6月累计未激活"].fillna(0, inplace=True)
newData["2024年6月激活"].fillna(0, inplace=True)
newData["2024年7月结算"].fillna(0, inplace=True)
newData["2024年7月累计未激活"].fillna(0, inplace=True)
newData["2024年7月激活"].fillna(0, inplace=True)
newData["2024年8月结算"].fillna(0, inplace=True)
newData["2024年8月累计未激活"].fillna(0, inplace=True)
newData["2024年8月激活"].fillna(0, inplace=True)
newData["2024年9月结算"].fillna(0, inplace=True)
newData["2024年9月累计未激活"].fillna(0, inplace=True)
newData["2024年9月激活"].fillna(0, inplace=True)
newData["2024年10月结算"].fillna(0, inplace=True)
newData["2024年10月累计未激活"].fillna(0, inplace=True)
newData["2024年10月激活"].fillna(0, inplace=True)
newData["2024年11月结算"].fillna(0, inplace=True)
newData["2024年11月累计未激活"].fillna(0, inplace=True)
newData["2024年11月激活"].fillna(0, inplace=True)
newData["2024年12月结算"].fillna(0, inplace=True)
newData["2024年12月累计未激活"].fillna(0, inplace=True)
newData["2024年12月激活"].fillna(0, inplace=True)
newData["本年未激活"].fillna(0, inplace=True)
# newData["本年结算+去年未激活"].fillna(0, inplace=True)
newData["本年激活"].fillna(0, inplace=True)
valueArr = []
for idx in newData.index:
value = (
newData.loc[idx, "客户"].strip(),
newData.loc[idx, "2024年12底未激活数据"],
newData.loc[idx, "2024年1月结算"],
newData.loc[idx, "2024年1月累计未激活"],
newData.loc[idx, "2024年1月激活"],
newData.loc[idx, "2024年2月结算"],
newData.loc[idx, "2024年2月累计未激活"],
newData.loc[idx, "2024年2月激活"],
newData.loc[idx, "2024年3月结算"],
newData.loc[idx, "2024年3月累计未激活"],
newData.loc[idx, "2024年3月激活"],
newData.loc[idx, "2024年4月结算"],
newData.loc[idx, "2024年4月累计未激活"],
newData.loc[idx, "2024年4月激活"],
newData.loc[idx, "2024年5月结算"],
newData.loc[idx, "2024年5月累计未激活"],
newData.loc[idx, "2024年5月激活"],
newData.loc[idx, "2024年6月结算"],
newData.loc[idx, "2024年6月累计未激活"],
newData.loc[idx, "2024年6月激活"],
newData.loc[idx, "2024年7月结算"],
newData.loc[idx, "2024年7月累计未激活"],
newData.loc[idx, "2024年7月激活"],
newData.loc[idx, "2024年8月结算"],
newData.loc[idx, "2024年8月累计未激活"],
newData.loc[idx, "2024年8月激活"],
newData.loc[idx, "2024年9月结算"],
newData.loc[idx, "2024年9月累计未激活"],
newData.loc[idx, "2024年9月激活"],
newData.loc[idx, "2024年10月结算"],
newData.loc[idx, "2024年10月累计未激活"],
newData.loc[idx, "2024年10月激活"],
newData.loc[idx, "2024年11月结算"],
newData.loc[idx, "2024年11月累计未激活"],
newData.loc[idx, "2024年11月激活"],
newData.loc[idx, "2024年12月结算"],
newData.loc[idx, "2024年12月累计未激活"],
newData.loc[idx, "2024年12月激活"],
newData.loc[idx, "本年未激活"],
# newData.loc[idx, "本年结算+去年未激活"],
newData.loc[idx, "本年激活"],
)
print(value)
# valueArr.append(value)
#
# cs = conn_updata.cursor()
# sql = (
# "insert into "
# + tableName
# + "(customer,unactive_202012,settle_202101,unactive_202101,active_202101,settle_202102,unactive_202102,active_202102,settle_202103,unactive_202103,active_202103,settle_202104,unactive_202104,active_202104,settle_202105,unactive_202105,active_202105,settle_202106,unactive_202106,active_202106,settle_202107,unactive_202107,active_202107,settle_202108,unactive_202108,active_202108,settle_202109,unactive_202109,active_202109,settle_202110,unactive_202110,active_202110,settle_202111,unactive_202111,active_202111,settle_202112,unactive_202112,active_202112,AM,AN) "
# "values "
# "(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,s%,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
# )
#
# print(sql)
# cs.executemany(sql, value)
# conn_updata.commit()
def update_dj_device_details_active_time():
print("start update dj_device_details active_time==")
bizConn = MySQL(g_pro_biz_dbcfg)
updateSql = """
UPDATE dj_device_details d
INNER JOIN dj_2021_settle_details s ON d.sn = s.sn
SET d.active_time = s.active_time
WHERE d.active_time IS NULL AND s.active_time IS NOT NULL;
"""
print("updateSql===", updateSql)
if not bizConn.update(updateSql):
logger.error("update_dj_device_details_active_time fail, sql=%s", updateSql)
print("end update dj_device_details active_time==")
def update_dj_basic_device_info_active_time_from_2021_settle():
print("start update dj_basic_device_info active_time from dj_2021_settle_details==")
bizConn = MySQL(g_pro_biz_dbcfg)
updateSql = """
UPDATE dj_basic_device_info d
INNER JOIN dj_2021_settle_details s ON d.sn = s.sn
SET d.active_time = s.active_time
WHERE d.active_time IS NULL AND s.active_time IS NOT NULL;
"""
print("updateSql===", updateSql)
if not bizConn.update(updateSql):
logger.error("update_dj_basic_device_info_active_time_from_2021_settle fail, sql=%s", updateSql)
print("end update dj_basic_device_info active_time from dj_2021_settle_details==")
def update_dj_year_basic_device_info_active_time_from_2021_settle(report_year):
print(f"start update dj_{report_year}_basic_device_info active_time from dj_2021_settle_details==")
bizConn = MySQL(g_pro_biz_dbcfg)
updateSql = f"""
UPDATE dj_{report_year}_basic_device_info d
INNER JOIN dj_2021_settle_details s ON d.sn = s.sn
SET d.active_time = s.active_time
WHERE d.active_time IS NULL AND s.active_time IS NOT NULL;
"""
print("updateSql===", updateSql)
if not bizConn.update(updateSql):
logger.error("update_dj_year_basic_device_info_active_time_from_2021_settle fail, sql=%s", updateSql)
print(f"end update dj_{report_year}_basic_device_info active_time from dj_2021_settle_details==")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate DJ detail data.")
parser.add_argument("--year", type=int, default=2025, help="The report year (e.g., 2025)")
parser.add_argument("--start_time", type=str, default="2025-08-01", help="Start time (YYYY-MM-DD)")
parser.add_argument("--end_time", type=str, default="2025-09-01", help="End time (YYYY-MM-DD)")
parser.add_argument("--action", type=str, default="generate", choices=["generate", "rollback", "copy", "copy_all", "copy_basic_all", "copy_basic_year"], help="Action to perform: generate, rollback, copy, copy_all, copy_basic_all, or copy_basic_year")
args = parser.parse_args()
report_year = args.year
startTime = args.start_time
endTime = args.end_time
action = args.action
if action == "generate":
# 将财务和商务表导入到数据库
# runScript()
# pre_deal(startTime, endTime, report_year)
# # 寻找差异
# # 比较财务和商务的差异
# # 手动处理数据
deal_data(startTime, endTime, report_year)
# # 激活数据
# # 导入财务数据
# # importData()
active(report_year)
elif action == "rollback":
# 回滚数据
rollback(startTime, endTime, report_year)
elif action == "copy":
# 将新增的数据拷贝到生成环境同步的表中
copyNewDataToProTable(startTime)
elif action == "copy_all":
# 更新 dj_device_details 使其和 dj_2021_settle_details 保持一致
# 前置条件为两个表都没有重复数据,且设备号一致
# 通过查找 dj_device_details 中active_time为空的数据,用sn号匹配,去 dj_2021_settle_details 拿数据,更新 dj_device_details 里面的active_time
update_dj_device_details_active_time()
elif action == "copy_basic_all":
# 同步 dj_2021_settle_details 到 dj_basic_device_info
update_dj_basic_device_info_active_time_from_2021_settle()
elif action == "copy_basic_year":
# 同步 dj_2021_settle_details 到 dj_{year}_basic_device_info
update_dj_year_basic_device_info_active_time_from_2021_settle(report_year)
else:
print("end===")
print(report_year, startTime, endTime)