Commit 0587f16f by 林冲

_process_audit_finance性能优化

1 parent 5f342069
...@@ -1352,66 +1352,86 @@ class SceneSplitProcessor: ...@@ -1352,66 +1352,86 @@ class SceneSplitProcessor:
settle_table = 'dj_car_life_merchant_settle_account_rule' settle_table = 'dj_car_life_merchant_settle_account_rule'
division_table = 'dj_car_life_merchant_division_rule' division_table = 'dj_car_life_merchant_division_rule'
# 查询表1和表6的映射关系 # 从dj_car_life_rule_audit出发,查询需要拆分的旧数据
query_mapping_sql = f""" query_old_audit_sql = f"""
SELECT DISTINCT
m1.old_id as old_settle_id,
m1.new_id as new_settle_id,
m6.old_id as old_division_id,
m6.new_id as new_division_id,
m1.base_scene_code
FROM tmp_rule_id_mapping m1
JOIN tmp_rule_id_mapping m6
ON m6.table_name = '{division_table}'
AND m6.base_scene_code = m1.base_scene_code
WHERE m1.table_name = '{settle_table}'
AND m1.old_id != m1.new_id
AND m6.old_id != m6.new_id
ORDER BY m1.old_id, m1.base_scene_code;
"""
mappings = self.db.execute_query(query_mapping_sql)
self.logger.info(f" 找到 {len(mappings)} 条财务规则映射关系需要处理")
copy_count = 0
for mapping in mappings:
old_settle_id = mapping[0]
new_settle_id = mapping[1]
old_division_id = mapping[2]
new_division_id = mapping[3]
base_scene_code = mapping[4]
# 查询原审核记录
query_sql = f"""
SELECT id, merchant_id, merchant_name, scene_type, rule_type, SELECT id, merchant_id, merchant_name, scene_type, rule_type,
rule_id, division_rule_id, effect_date_type, effect_start_date, rule_id, division_rule_id, effect_date_type, effect_start_date,
audit_status, audit_by, auditor_name, audit_date, audit_status, audit_by, auditor_name, audit_date,
creator_name, enabled_flag, created_by, created_date, updated_by, updated_date creator_name, enabled_flag, created_by, created_date, updated_by, updated_date
FROM {table_name} FROM {table_name}
WHERE rule_type = 1 WHERE scene_type = 0
AND rule_id = {old_settle_id} AND rule_type = 1
AND division_rule_id = {old_division_id} AND deleted_flag = 'N'
AND deleted_flag = 'N'; ORDER BY id;
""" """
records = self.db.execute_query(query_sql) old_audit_records = self.db.execute_query(query_old_audit_sql)
self.logger.info(f" 找到 {len(old_audit_records)} 条需要拆分的财务审核记录")
for record in records: copy_count = 0
old_id = record[0] skip_count = 0
merchant_id = record[1]
merchant_name = record[2] for old_record in old_audit_records:
scene_type = record[3] old_id = old_record[0]
rule_type = record[4] merchant_id = old_record[1]
effect_date_type = record[7] merchant_name = old_record[2]
effect_start_date = f"'{record[8]}'" if record[8] is not None else 'NULL' scene_type = old_record[3]
audit_status = record[9] rule_type = old_record[4]
audit_by = f"'{record[10]}'" if record[10] is not None else 'NULL' old_rule_id = old_record[5]
auditor_name = f"'{record[11]}'" if record[11] is not None else 'NULL' old_division_rule_id = old_record[6]
audit_date = f"'{record[12]}'" if record[12] is not None else 'NULL' effect_date_type = old_record[7]
creator_name = record[13] effect_start_date = f"'{old_record[8]}'" if old_record[8] is not None else 'NULL'
enabled_flag = record[14] audit_status = old_record[9]
created_by = record[15] audit_by = f"'{old_record[10]}'" if old_record[10] is not None else 'NULL'
created_date = record[16] auditor_name = f"'{old_record[11]}'" if old_record[11] is not None else 'NULL'
updated_by = record[17] audit_date = f"'{old_record[12]}'" if old_record[12] is not None else 'NULL'
updated_date = record[18] creator_name = old_record[13]
enabled_flag = old_record[14]
created_by = old_record[15]
created_date = old_record[16]
updated_by = old_record[17]
updated_date = old_record[18]
# 查询rule_id对应的拆分后的数据
query_settle_mapping_sql = f"""
SELECT new_id, base_scene_code
FROM tmp_rule_id_mapping
WHERE table_name = '{settle_table}'
AND old_id = {old_rule_id}
ORDER BY old_id, base_scene_code;
"""
settle_mappings = self.db.execute_query(query_settle_mapping_sql)
# 查询division_rule_id对应的拆分后的数据
division_mappings = []
if old_division_rule_id is not None:
query_division_mapping_sql = f"""
SELECT new_id, base_scene_code
FROM tmp_rule_id_mapping
WHERE table_name = '{division_table}'
AND old_id = {old_division_rule_id}
ORDER BY old_id, base_scene_code;
"""
division_mappings = self.db.execute_query(query_division_mapping_sql)
# 检查两个映射列表的长度是否一致
if len(settle_mappings) == 0:
self.logger.warning(f" 审核记录 id={old_id} 的 rule_id={old_rule_id} 没有找到映射关系,跳过")
skip_count += 1
continue
if old_division_rule_id is not None and len(division_mappings) != len(settle_mappings):
self.logger.warning(f" 审核记录 id={old_id} 的 settle 和 division 映射数量不一致,跳过")
skip_count += 1
continue
# 按顺序组装新记录
for i in range(len(settle_mappings)):
new_settle_id = settle_mappings[i][0]
base_scene_code = settle_mappings[i][1]
new_division_id = 'NULL'
if division_mappings and i < len(division_mappings):
new_division_id = division_mappings[i][0]
insert_sql = f""" insert_sql = f"""
INSERT INTO {table_name} INSERT INTO {table_name}
...@@ -1434,9 +1454,9 @@ class SceneSplitProcessor: ...@@ -1434,9 +1454,9 @@ class SceneSplitProcessor:
new_id = result[0] new_id = result[0]
self.id_mapper.add_mapping(table_name, old_id, new_id, base_scene_code) self.id_mapper.add_mapping(table_name, old_id, new_id, base_scene_code)
copy_count += 1 copy_count += 1
self.logger.debug(f" 复制财务审核: old_id={old_id} -> new_id={new_id}") self.logger.debug(f" 复制财务审核: old_id={old_id} -> new_id={new_id}, base_scene_code={base_scene_code}")
self.logger.info(f" 财务审核记录复制完成,共 {copy_count} 条") self.logger.info(f" 财务审核记录复制完成,共 {copy_count} 条,跳过 {skip_count} 条")
def _process_audit_reconciliation(self): def _process_audit_reconciliation(self):
"""处理对账审核记录(rule_type=3)""" """处理对账审核记录(rule_type=3)"""
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!