feat(xq0085): 新增团购数据核对插件(v1.0.0)
美团/抖音两条独立分支共享盘只读取数并本地副本处理:团购明细贴总表、明细/透视/报表多口径合计校验、报表基础信息补维护、按值贴入平台总表。
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""基础信息补维护:检查【报表】里 VLOOKUP 补不到(#N/A)的新增门店/新增套餐。
|
||||
|
||||
比对【报表】A/B 列与【基础信息】A/E 列,新增项写入本地副本。
|
||||
同时检查固定 VLOOKUP 范围上界:到 90% 提示扩范围,超界提示人工改公式(不改表)。
|
||||
"""
|
||||
import glob
|
||||
import json
|
||||
import os
|
||||
from difflib import SequenceMatcher
|
||||
|
||||
import openpyxl
|
||||
|
||||
from .. import config
|
||||
|
||||
COL_BI_A = 1
|
||||
COL_BI_B = 2
|
||||
COL_BI_E = 5
|
||||
COL_BI_F = 6
|
||||
|
||||
|
||||
def _read_report_pairs(ws):
|
||||
pairs = []
|
||||
seen = set()
|
||||
for r in range(config.REPORT_DATA_START, int(ws.max_row or 0) + 1):
|
||||
shop = str(ws.cell(r, 1).value or '').strip()
|
||||
item = str(ws.cell(r, 2).value or '').strip()
|
||||
if shop and shop != '0' and (shop, item) not in seen:
|
||||
seen.add((shop, item))
|
||||
pairs.append((shop, item))
|
||||
return pairs
|
||||
|
||||
|
||||
def _column_values(ws, col, start_row):
|
||||
values = set()
|
||||
last = start_row - 1
|
||||
for r in range(start_row, int(ws.max_row or 0) + 1):
|
||||
v = str(ws.cell(r, col).value or '').strip()
|
||||
if v and v != '0':
|
||||
values.add(v)
|
||||
last = r
|
||||
return values, last
|
||||
|
||||
|
||||
def _load_shop_map():
|
||||
path = os.path.join(config.WORK_DIR, config.STORE_MAP_FILE)
|
||||
try:
|
||||
with open(path, encoding='utf-8-sig') as fp:
|
||||
data = json.load(fp)
|
||||
except (OSError, ValueError):
|
||||
return {}
|
||||
return data if isinstance(data, dict) else {}
|
||||
|
||||
|
||||
def _guess_shop(name, existing, shop_map):
|
||||
for crm, info in shop_map.items():
|
||||
if not isinstance(info, dict):
|
||||
continue
|
||||
if str(info.get('shop') or info.get('门店') or '').strip() in existing:
|
||||
if crm and (crm in name or name in crm):
|
||||
return str(info.get('shop') or info.get('门店')).strip(), \
|
||||
str(info.get('brand') or info.get('品牌') or '').strip()
|
||||
best, score = '', 0.0
|
||||
for e in existing:
|
||||
s = SequenceMatcher(None, name, e).ratio()
|
||||
if s > score:
|
||||
best, score = e, s
|
||||
return (best if score >= 0.45 else ''), ''
|
||||
|
||||
|
||||
def _guess_item(name):
|
||||
if '【' in name and '】' in name:
|
||||
return name.split('【', 1)[1].split('】', 1)[0].strip()
|
||||
return ''
|
||||
|
||||
|
||||
def run(table_path='', dry_run=False):
|
||||
src = str(table_path or '').strip()
|
||||
if not src:
|
||||
cands = glob.glob(os.path.join(config.WORK_DIR, config.DAILY_WORK_DIR, '美团*.xlsx'))
|
||||
src = cands[0] if cands else ''
|
||||
result = {'ok': False, 'message': '', 'path': src,
|
||||
'new_shops': [], 'new_items': [], 'range_warnings': []}
|
||||
if not src or not os.path.isfile(src):
|
||||
result['message'] = '未找到本地美团核对表副本'
|
||||
return result
|
||||
|
||||
wb = openpyxl.load_workbook(src, data_only=False)
|
||||
try:
|
||||
if config.BASE_SHEET not in wb.sheetnames or config.REPORT_SHEET not in wb.sheetnames:
|
||||
result['message'] = f'{os.path.basename(src)} 缺少【{config.BASE_SHEET}】或【{config.REPORT_SHEET}】'
|
||||
return result
|
||||
base = wb[config.BASE_SHEET]
|
||||
report = wb[config.REPORT_SHEET]
|
||||
pairs = _read_report_pairs(report)
|
||||
shops, last_a = _column_values(base, COL_BI_A, config.BASE_INFO_ROW)
|
||||
items, last_e = _column_values(base, COL_BI_E, config.BASE_ITEM_ROW)
|
||||
shop_map = _load_shop_map()
|
||||
|
||||
new_shops, new_items = [], []
|
||||
for shop, item in pairs:
|
||||
if shop not in shops:
|
||||
internal, brand = _guess_shop(shop, shops, shop_map)
|
||||
new_shops.append({'crm': shop, 'internal': internal, 'brand': brand})
|
||||
if item:
|
||||
internal_item = _guess_item(item)
|
||||
if internal_item and internal_item not in items:
|
||||
new_items.append({'crm': item, 'internal': internal_item})
|
||||
|
||||
warnings = []
|
||||
for name, last, limit in (
|
||||
('门店映射(A~B)', last_a, config.BASE_INFO_VLOOKUP_MAX),
|
||||
('品项映射(E~F)', last_e, config.BASE_ITEM_VLOOKUP_MAX),
|
||||
):
|
||||
ratio = last / float(limit) if limit else 0.0
|
||||
if last > limit:
|
||||
warnings.append(f'{name}已到第 {last} 行,超出报表公式上界 {limit};'
|
||||
f'超出部分查不到,需要人工把 VLOOKUP 范围改大')
|
||||
elif ratio >= config.BASE_RANGE_WARN_RATIO:
|
||||
warnings.append(f'{name}已到第 {last} 行(上界 {limit},'
|
||||
f'已用 {ratio:.0%}),建议尽快扩范围')
|
||||
result['range_warnings'] = warnings
|
||||
for warning in warnings:
|
||||
config.log(warning, 'WARN')
|
||||
|
||||
result.update(new_shops=new_shops, new_items=new_items)
|
||||
if dry_run:
|
||||
result.update(ok=True, message=f'[演练] 新增门店 {len(new_shops)} / '
|
||||
f'套餐 {len(new_items)}')
|
||||
return result
|
||||
|
||||
changed = 0
|
||||
r = last_a + 1
|
||||
for entry in new_shops:
|
||||
internal = entry['internal'] or entry['crm']
|
||||
if internal in shops or not entry['crm']:
|
||||
continue
|
||||
base.cell(r, COL_BI_A, entry['crm'])
|
||||
base.cell(r, COL_BI_B, internal)
|
||||
if entry['brand']:
|
||||
base.cell(r, 3, entry['brand'])
|
||||
shops.add(entry['crm'])
|
||||
r += 1
|
||||
changed += 1
|
||||
r = last_e + 1
|
||||
for entry in new_items:
|
||||
if entry['internal'] in items:
|
||||
continue
|
||||
base.cell(r, COL_BI_E, entry['crm'])
|
||||
base.cell(r, COL_BI_F, entry['internal'])
|
||||
items.add(entry['internal'])
|
||||
r += 1
|
||||
changed += 1
|
||||
if changed:
|
||||
wb.save(src)
|
||||
result.update(ok=True, path=src,
|
||||
message=f'基础信息补维护:新增门店 {len(new_shops)} / '
|
||||
f'套餐 {len(new_items)},写入 {changed} 行到本地副本')
|
||||
return result
|
||||
finally:
|
||||
wb.close()
|
||||
Reference in New Issue
Block a user