Commit d19bf3e0 by 林冲

_batch_update_settle_rule_goods_type_group_id 优化为批量查询

1 parent fdb19049
......@@ -150,3 +150,41 @@ class IDMappingManager:
except Exception as e:
self.logger.error(f"查询映射数量失败: {table_name}, 错误: {e}")
return 0
def batch_get_new_ids_by_scene(self, table_name, old_id_scene_pairs):
"""
批量根据原ID和场景编码获取新ID
Args:
table_name: 表名
old_id_scene_pairs: 原ID和场景编码对列表,格式 [(old_id, base_scene_code), ...]
Returns:
dict: 映射字典,key为(old_id, base_scene_code),value为new_id
"""
if not old_id_scene_pairs:
return {}
# 构建批量查询条件
conditions = []
for old_id, base_scene_code in old_id_scene_pairs:
conditions.append(f"(old_id = {old_id} AND base_scene_code = '{base_scene_code}')")
query_sql = f"""
SELECT old_id, base_scene_code, new_id
FROM {self.mapping_table}
WHERE table_name = '{table_name}'
AND ({' OR '.join(conditions)});
"""
try:
results = self.db.execute_query(query_sql)
# 构建映射字典
mapping_dict = {}
for row in results:
old_id, base_scene_code, new_id = row
mapping_dict[(old_id, base_scene_code)] = new_id
return mapping_dict
except Exception as e:
self.logger.error(f"批量查询映射失败: {table_name}, 错误: {e}")
return {}
......@@ -808,15 +808,28 @@ class SceneSplitProcessor:
settle_rule_table = 'dj_car_life_merchant_goods_type_group_settle_rule'
goods_type_group_table = 'dj_car_life_goods_type_group'
# 从映射表获取所有新ID
update_batch = []
# 准备批量查询的参数:收集所有 (old_id, base_scene_code) 对
old_id_scene_pairs = []
item_map = {} # 用于后续匹配:key=(old_id, base_scene_code), value=new_settle_rule_group_id
for item in insert_batch:
old_id = item['old_id']
base_scene_code = item['base_scene_code']
new_settle_rule_group_id = item['new_settle_rule_group_id']
# 从映射表查询新ID
new_goods_type_group_id = self.id_mapper.get_new_id_by_scene(goods_type_group_table, old_id, base_scene_code)
pair = (old_id, base_scene_code)
old_id_scene_pairs.append(pair)
item_map[pair] = new_settle_rule_group_id
# 批量查询所有新ID
new_id_mapping = self.id_mapper.batch_get_new_ids_by_scene(
goods_type_group_table, old_id_scene_pairs
)
# 构建更新批次
update_batch = []
for pair, new_settle_rule_group_id in item_map.items():
new_goods_type_group_id = new_id_mapping.get(pair)
if new_goods_type_group_id:
update_batch.append({
'settle_rule_id': new_settle_rule_group_id,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!