upload
This commit is contained in:
+229
@@ -0,0 +1,229 @@
|
||||
import multiprocessing
|
||||
import json
|
||||
import newsetting
|
||||
import time
|
||||
|
||||
# 数据库配置
|
||||
DB_HOST = '192.168.5.2'
|
||||
DB_PORT = 3307
|
||||
DB_NAME = 'mt_newretail'
|
||||
# workid = '20260409'
|
||||
# workid = '20260409_test3'
|
||||
workid = '20261116'
|
||||
|
||||
# 进程私有变量
|
||||
process_tent = None
|
||||
poi_filter_config = {} # 只存配置表中的店铺
|
||||
|
||||
|
||||
def init_worker():
|
||||
global process_tent, poi_filter_config
|
||||
process_tent = newsetting.All_Setting(DB_HOST, DB_PORT, DB_NAME)
|
||||
poi_filter_config = load_poi_filters()
|
||||
|
||||
|
||||
def load_poi_filters():
|
||||
"""加载配置表中的店铺筛选配置"""
|
||||
temp_tent = newsetting.All_Setting(DB_HOST, DB_PORT, DB_NAME)
|
||||
|
||||
# 读取配置表(workid=20260302)
|
||||
sql = f"""
|
||||
SELECT poi_id_str, shop_name, tag1, tag1_name, tag2, tag2_name
|
||||
FROM poi_category_filter
|
||||
WHERE workid = '{workid}'
|
||||
"""
|
||||
result = temp_tent.get_shoppingmalls(sql)
|
||||
|
||||
# 建立 shop_name -> 分类配置
|
||||
filters_by_name = {}
|
||||
for row in result:
|
||||
poi_id, shop_name, tag1, tag1_name, tag2, tag2_name = row
|
||||
shop_key = shop_name.strip() if shop_name else ''
|
||||
|
||||
if shop_key not in filters_by_name:
|
||||
filters_by_name[shop_key] = []
|
||||
|
||||
filters_by_name[shop_key].append({
|
||||
'tag1': tag1,
|
||||
'tag1_name': tag1_name.strip() if tag1_name else None,
|
||||
'tag2': tag2,
|
||||
'tag2_name': tag2_name.strip() if tag2_name else None
|
||||
})
|
||||
|
||||
print(f"配置表中共有 {len(filters_by_name)} 个不同店名")
|
||||
|
||||
# 匹配当前workid中的poi_id_str
|
||||
filters = {}
|
||||
if filters_by_name:
|
||||
shop_names = "','".join([k.replace("'", "''") for k in filters_by_name.keys()])
|
||||
poi_sql = f"""
|
||||
SELECT DISTINCT poi_id_str, shop_name
|
||||
FROM food_table
|
||||
WHERE workid like '%{workid}%' AND shop_name IN ('{shop_names}')
|
||||
"""
|
||||
poi_result = temp_tent.get_shoppingmalls(poi_sql)
|
||||
|
||||
for poi_id, shop_name in poi_result:
|
||||
shop_key = shop_name.strip() if shop_name else ''
|
||||
if shop_key in filters_by_name:
|
||||
filters[poi_id] = {
|
||||
'shop_name': shop_name,
|
||||
'categories': filters_by_name[shop_key]
|
||||
}
|
||||
|
||||
print(f"已匹配到 {len(filters)} 个门店需要筛选")
|
||||
return filters
|
||||
|
||||
|
||||
def match_category(cat_tag1_name, cat_tag2_name, config):
|
||||
"""按分类名字匹配,'全部'或None表示不限制"""
|
||||
cfg_tag1_name = config.get('tag1_name')
|
||||
cfg_tag2_name = config.get('tag2_name')
|
||||
|
||||
match_tag1 = (cfg_tag1_name is None) or (cfg_tag1_name == '全部') or \
|
||||
(cat_tag1_name and cat_tag1_name.strip() == cfg_tag1_name)
|
||||
|
||||
match_tag2 = (cfg_tag2_name is None) or (cfg_tag2_name == '全部') or \
|
||||
(cat_tag2_name and cat_tag2_name.strip() == cfg_tag2_name)
|
||||
|
||||
return match_tag1 and match_tag2
|
||||
|
||||
|
||||
def get_data_batch(poi_ids):
|
||||
"""批量获取数据"""
|
||||
poi_str = "','".join(poi_ids)
|
||||
|
||||
cat_sql = f"""
|
||||
SELECT t1.parent_id,t1.workid,t1.business_code,t1.mt_org_id,t1.poi_id_str,t1.shop_name,t1.address,t1.shipping_fee_tip,t1.min_price_tip,t1.packet_fee_tip,t1.shipping_time,t2.tag1,t2.tag1_name,t2.tag2,t2.tag2_name,t2.tag_type,t2.product_count,t2.allSortedSpuId
|
||||
from (SELECT * FROM food_table WHERE workid like '%{workid}%' and poi_id_str IN ('{poi_str}')) AS t1
|
||||
INNER JOIN (SELECT * FROM categorylist WHERE workid like '%{workid}%' AND is_ok = 1 and poi_id_str IN ('{poi_str}')) as t2
|
||||
ON t1.poi_id_str = t2.poi_id_str AND t1.mt_org_id <=> t2.mt_org_id
|
||||
"""
|
||||
categories = process_tent.get_shoppingmalls(cat_sql)
|
||||
|
||||
prod_sql = f"""
|
||||
SELECT poi_id_str, product_id,product_name,description,praise_content,month_saled_content,skus_id,spec,actual_price,price,origin_price,activity_price,min_order_count,quota_per_order,dynamic_text,dynamic_url,upccode,want_to_buy_content,praise_rate,picture,promotion_info,tag,activity_tag_id,stock_label,real_stock,skus_count,product_label,vague_picture,create_time
|
||||
FROM `mt_newretail`.`productslist`
|
||||
where workid like '%{workid}%' and poi_id_str IN ('{poi_str}')
|
||||
"""
|
||||
products = process_tent.get_shoppingmalls(prod_sql)
|
||||
|
||||
# 筛选分类:配置表有的按配置筛,没有的不筛(保留全部)
|
||||
filtered_categories = []
|
||||
for cat in categories:
|
||||
poi_id = cat[4]
|
||||
cat_tag1_name = cat[12] if cat[12] else None
|
||||
cat_tag2_name = cat[14] if cat[14] else None
|
||||
|
||||
# 配置表中没有这个店铺 -> 保留所有分类(原逻辑)
|
||||
if poi_id not in poi_filter_config:
|
||||
filtered_categories.append(cat)
|
||||
continue
|
||||
|
||||
# 配置表中有这个店铺 -> 按配置筛选
|
||||
category_configs = poi_filter_config[poi_id]['categories']
|
||||
for cfg in category_configs:
|
||||
if match_category(cat_tag1_name, cat_tag2_name, cfg):
|
||||
filtered_categories.append(cat)
|
||||
break
|
||||
|
||||
return filtered_categories, products
|
||||
|
||||
|
||||
def process_batch_task(poi_ids):
|
||||
"""处理一批门店的数据"""
|
||||
try:
|
||||
categories, products = get_data_batch(poi_ids)
|
||||
if not categories or not products:
|
||||
return poi_ids, []
|
||||
|
||||
# 建立商品索引
|
||||
product_index = {}
|
||||
for p in products:
|
||||
poi_id = p[0]
|
||||
prod_data = p[1:]
|
||||
if poi_id not in product_index:
|
||||
product_index[poi_id] = {}
|
||||
product_index[poi_id][str(prod_data[0])] = prod_data
|
||||
|
||||
local_results = []
|
||||
for cat in categories:
|
||||
poi_id = cat[4]
|
||||
if poi_id not in product_index:
|
||||
continue
|
||||
|
||||
try:
|
||||
all_spu_json = cat[-1]
|
||||
spu_ids = json.loads(all_spu_json)
|
||||
for spu_id in spu_ids:
|
||||
spu_id_str = str(spu_id)
|
||||
if spu_id_str in product_index[poi_id]:
|
||||
prod = product_index[poi_id][spu_id_str]
|
||||
local_results.append(cat + prod)
|
||||
except:
|
||||
continue
|
||||
|
||||
print(f'[{time.strftime("%H:%M:%S")}] 批次完成: {len(poi_ids)} 门店, 匹配: {len(local_results)} 条')
|
||||
return poi_ids, local_results
|
||||
except Exception as e:
|
||||
print(f"批次处理出错: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return poi_ids, []
|
||||
|
||||
|
||||
def main():
|
||||
global poi_filter_config
|
||||
|
||||
# 1. 加载筛选配置(只包含有配置的店铺)
|
||||
poi_filter_config = load_poi_filters()
|
||||
|
||||
# 2. 获取所有门店(原逻辑)
|
||||
temp_tent = newsetting.All_Setting(DB_HOST, DB_PORT, DB_NAME)
|
||||
task = temp_tent.get_shoppingmalls(f"SELECT DISTINCT poi_id_str FROM food_table WHERE workid like '%{workid}%' and business_code ='chengrenyongpin' ")
|
||||
# task = temp_tent.get_shoppingmalls(f"SELECT DISTINCT poi_id_str FROM food_table WHERE workid = '{workid}' ")
|
||||
all_poi_ids = [i[0] for i in task]
|
||||
print(
|
||||
f"总共有 {len(all_poi_ids)} 个门店,其中 {len(poi_filter_config)} 个需要筛选,{len(all_poi_ids) - len(poi_filter_config)} 个按原逻辑处理")
|
||||
|
||||
# 3. 分组处理所有门店
|
||||
batch_size = 20
|
||||
poi_batches = [all_poi_ids[i:i + batch_size] for i in range(0, len(all_poi_ids), batch_size)]
|
||||
print(f"共分成 {len(poi_batches)} 个批次")
|
||||
|
||||
# 4. 准备写入
|
||||
final_tent = newsetting.All_Setting(DB_HOST, DB_PORT, DB_NAME)
|
||||
insert_sql = """
|
||||
insert into ready_list2_copy1 (parent_id,workid,business_code,mt_org_id, poi_id_str, shop_name, address, shipping_fee_tip, min_price_tip, packet_fee_tip, shipping_time, tag1, tag1_name, tag2, tag2_name, tag_type, product_count, allSortedSpuId,product_id, product_name, description, praise_content, month_saled_content, skus_id,spec, actual_price, price, origin_price, activity_price, min_order_count, quota_per_order, dynamic_text, dynamic_url, upccode, want_to_buy_content, praise_rate, picture, promotion_info, tag, activity_tag_id, stock_label, real_stock, skus_count, product_label, vague_picture,create_time) 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,%s,%s,%s,%s,%s,%s)
|
||||
"""
|
||||
|
||||
# 5. 启动进程池
|
||||
num_processes = 32
|
||||
total_written = 0
|
||||
|
||||
with multiprocessing.Pool(processes=num_processes, initializer=init_worker) as pool:
|
||||
for handled_ids, res_list in pool.imap_unordered(process_batch_task, poi_batches):
|
||||
try:
|
||||
ids_str = "','".join(handled_ids)
|
||||
update_sql = f"UPDATE food_table SET is_ok = 1 WHERE workid like '%{workid}%' AND poi_id_str IN ('{ids_str}')"
|
||||
final_tent.update_data(update_sql)
|
||||
except Exception as e:
|
||||
print(f"更新状态失败: {e}")
|
||||
if res_list:
|
||||
try:
|
||||
final_tent.write_data(insert_sql, res_list)
|
||||
total_written += len(res_list)
|
||||
print(f'>>> 已写入数据库: {len(res_list)} 条 (累计: {total_written} 条)')
|
||||
del res_list
|
||||
except Exception as e:
|
||||
print(f"写入数据库失败: {e}")
|
||||
print(f"[{time.strftime('%H:%M:%S')}] 完成,共计 {total_written} 条数据。")
|
||||
|
||||
# temp_tent.update_data(f"UPDATE ready_list2_copy1 SET business_code ='SURPERMARKET' WHERE business_code = 'SURPERMARKET1' and workid ='{workid}'")
|
||||
# print('月报更新')
|
||||
|
||||
|
||||
|
||||
# 110809
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user