feat(xq0041): 美团/抖音核对迁出至 XQ0085,保留导表与到账统计核对(v1.3.13)
download=只导打款/团购/预订收益明细;check_settlement=到账统计核对(并台账/门店归属/更新核对统计);金色印象与金色湖畔独立浏览器档案;新增 CRM 门店映射、基础信息同步与贴总表逻辑。
This commit is contained in:
@@ -0,0 +1,207 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""基础信息补维护:报表里出现新的消费门店 / 新的套餐(项目)时,补进【基础信息】。
|
||||
|
||||
背景(共享表里的公式链)::
|
||||
|
||||
报表 J = IF(B=0,"",VLOOKUP(A,基础信息!$A$3:$B$1973,2,0)) # 美团门店名 → 内部门店
|
||||
报表 K = IF(B=0,"",VLOOKUP(B,基础信息!$E$2:$F$12924,2,0)) # 套餐名 → 品项名称
|
||||
|
||||
VLOOKUP 查不到就显示 #N/A(人工看到的是"乱码")。所以每天要检查报表里有没有
|
||||
新的门店名 / 新的套餐名,补进【基础信息】的 A~C(门店映射)与 E~F(品项映射)。
|
||||
|
||||
自动补的来源:
|
||||
|
||||
* **内部门店 / 品牌**:优先用 ``门店映射表.json``(CRM 券号 join 出来的,最准);
|
||||
没有则按名称相似度给候选,标「待确认」;
|
||||
* **品项名称**:套餐名里的 ``【...】`` 就是品项(如「【龙手足道】80分钟…」→ 龙手足道);
|
||||
提取不到再给空值待人工。
|
||||
|
||||
写入只在**本地副本**上做(共享盘只读),人工确认后自行同步回共享盘。
|
||||
"""
|
||||
import difflib
|
||||
import json
|
||||
import os
|
||||
|
||||
import openpyxl
|
||||
|
||||
from .. import config
|
||||
|
||||
COL_BI_A, COL_BI_B, COL_BI_C = 1, 2, 3 # 基础信息:消费门店名称 / 门店 / 品牌
|
||||
COL_BI_E, COL_BI_F = 5, 6 # 基础信息:套餐名 / 品项名称
|
||||
ITEM_MARK = ('【', '】')
|
||||
|
||||
|
||||
def _norm(name):
|
||||
return str(name or '').strip()
|
||||
|
||||
|
||||
def _is_blank(value):
|
||||
"""报表里透视多出来的 0 行:A/B 为 '0' 或空,不算真实门店/套餐。"""
|
||||
text = str(value if value is not None else '').strip()
|
||||
return text in ('', '0', 'None')
|
||||
|
||||
|
||||
def _read_report_pairs(ws):
|
||||
"""读【报表】A(消费门店名)/ B(套餐名),返回去重后的 (门店名集合, 套餐名集合)。"""
|
||||
shops, items = [], []
|
||||
seen_shop, seen_item = set(), set()
|
||||
last = 0
|
||||
for r in range(config.REPORT_DATA_START, int(ws.max_row) + 1):
|
||||
shop = ws.cell(r, 1).value
|
||||
item = ws.cell(r, 2).value
|
||||
if not _is_blank(shop) or not _is_blank(item):
|
||||
last = r
|
||||
for r in range(config.REPORT_DATA_START, last + 1):
|
||||
shop = _norm(ws.cell(r, 1).value)
|
||||
item = _norm(ws.cell(r, 2).value)
|
||||
if not _is_blank(shop) and shop not in seen_shop:
|
||||
seen_shop.add(shop)
|
||||
shops.append(shop)
|
||||
if not _is_blank(item) and item not in seen_item:
|
||||
seen_item.add(item)
|
||||
items.append(item)
|
||||
return shops, items
|
||||
|
||||
|
||||
def _column_values(ws, col, start_row):
|
||||
"""取某列从 start_row 起的非空值(去首尾空白)与真实末行。"""
|
||||
values = set()
|
||||
last = start_row - 1
|
||||
for r in range(start_row, int(ws.max_row) + 1):
|
||||
value = ws.cell(r, col).value
|
||||
if value not in (None, ''):
|
||||
last = r
|
||||
values.add(_norm(value))
|
||||
return values, last
|
||||
|
||||
|
||||
def _load_shop_map():
|
||||
"""读门店映射表(CRM 券号 join 的产物):美团门店名 → {shop, brand, shop_id}。"""
|
||||
path = os.path.join(config.WORK_DIR, config.STORE_MAP_FILE)
|
||||
if not os.path.exists(path):
|
||||
return {}
|
||||
try:
|
||||
with open(path, encoding='utf-8') as fp:
|
||||
data = json.load(fp)
|
||||
except (OSError, ValueError):
|
||||
return {}
|
||||
shops = data.get('shops') if isinstance(data, dict) else None
|
||||
return shops if isinstance(shops, dict) else {}
|
||||
|
||||
|
||||
def _guess_shop(shop_name, known, mapping):
|
||||
"""新门店 → 内部门店:先查 CRM 映射表,再按名称相似度给候选。"""
|
||||
hit = mapping.get(shop_name)
|
||||
if hit and hit.get('shop'):
|
||||
return hit['shop'], hit.get('brand', ''), 'CRM券号映射'
|
||||
best = difflib.get_close_matches(shop_name, list(known), n=1, cutoff=0.5)
|
||||
if best:
|
||||
return best[0], '', '名称相近(待确认)'
|
||||
return '', '', '待确认'
|
||||
|
||||
|
||||
def _guess_item(item_name):
|
||||
"""套餐名 → 品项名称:取 ``【...】`` 里的词(共享表的人工口径就是这个)。"""
|
||||
text = str(item_name or '')
|
||||
start = text.find(ITEM_MARK[0])
|
||||
end = text.find(ITEM_MARK[1], start + 1)
|
||||
if start >= 0 and end > start:
|
||||
return text[start + 1:end].strip(), '套餐名【】提取'
|
||||
return '', '待人工'
|
||||
|
||||
|
||||
def run(table_path='', dry_run=False):
|
||||
"""检查【报表】里有、而【基础信息】里没有的门店/套餐,并补进本地副本。
|
||||
|
||||
Args:
|
||||
table_path: 共享表的本地副本路径(必填,共享盘只读)
|
||||
dry_run: 只检测不写
|
||||
|
||||
Returns:
|
||||
``{"ok", "message", "new_shops": [...], "new_items": [...], "path"}``
|
||||
"""
|
||||
result = {'ok': False, 'message': '', 'new_shops': [], 'new_items': [],
|
||||
'path': table_path or ''}
|
||||
if not table_path or not os.path.exists(table_path):
|
||||
result['message'] = '没有可用的本地副本(共享盘只读,需先拉一份到本地)'
|
||||
return result
|
||||
|
||||
wb = openpyxl.load_workbook(table_path)
|
||||
try:
|
||||
if config.BASE_SHEET not in wb.sheetnames:
|
||||
result['message'] = f'{os.path.basename(table_path)} 缺少「{config.BASE_SHEET}」sheet'
|
||||
return result
|
||||
|
||||
report = wb[config.REPORT_SHEET] if config.REPORT_SHEET in wb.sheetnames else None
|
||||
if report is None:
|
||||
result['message'] = f'缺少「{config.REPORT_SHEET}」sheet,跳过基础信息校验'
|
||||
return result
|
||||
|
||||
shops, items = _read_report_pairs(report)
|
||||
base = wb[config.BASE_SHEET]
|
||||
known_shops, last_a = _column_values(base, COL_BI_A, config.BASE_INFO_ROW)
|
||||
known_items, last_e = _column_values(base, COL_BI_E, config.BASE_ITEM_ROW)
|
||||
|
||||
# 只检查不修改:VLOOKUP 范围是写死的行号,末尾逼近上界时提醒人工扩范围
|
||||
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')
|
||||
|
||||
new_shops = [s for s in shops if s not in known_shops]
|
||||
new_items = [i for i in items if i not in known_items]
|
||||
result['new_shops'] = new_shops
|
||||
result['new_items'] = new_items
|
||||
|
||||
if not new_shops and not new_items:
|
||||
result.update(ok=True, message=f'基础信息已覆盖报表里的 {len(shops)} 家门店 / '
|
||||
f'{len(items)} 个套餐,无需补维护')
|
||||
return result
|
||||
|
||||
mapping = _load_shop_map()
|
||||
shop_rows = []
|
||||
for name in new_shops:
|
||||
shop, brand, source = _guess_shop(name, known_shops, mapping)
|
||||
shop_rows.append({'name': name, 'shop': shop, 'brand': brand, 'source': source})
|
||||
item_rows = []
|
||||
for name in new_items:
|
||||
item, source = _guess_item(name)
|
||||
item_rows.append({'name': name, 'item': item, 'source': source})
|
||||
|
||||
if not dry_run:
|
||||
row = last_a + 1
|
||||
for info in shop_rows:
|
||||
base.cell(row, COL_BI_A).value = info['name']
|
||||
base.cell(row, COL_BI_B).value = info['shop']
|
||||
base.cell(row, COL_BI_C).value = info['brand']
|
||||
row += 1
|
||||
row = last_e + 1
|
||||
for info in item_rows:
|
||||
base.cell(row, COL_BI_E).value = info['name']
|
||||
base.cell(row, COL_BI_F).value = info['item']
|
||||
row += 1
|
||||
wb.save(table_path)
|
||||
|
||||
auto_shop = sum(1 for x in shop_rows if x['source'] == 'CRM券号映射')
|
||||
auto_item = sum(1 for x in item_rows if x['source'] == '套餐名【】提取')
|
||||
result.update(ok=True, shop_rows=shop_rows, item_rows=item_rows,
|
||||
message=(f'{"[演练] " if dry_run else ""}基础信息待补:'
|
||||
f'新增门店 {len(shop_rows)} 个(自动补 {auto_shop},其余待确认)、'
|
||||
f'新增套餐 {len(item_rows)} 个(自动提取品项 {auto_item});'
|
||||
+ ('已写入本地副本,确认后同步回共享盘' if not dry_run
|
||||
else '未写入')))
|
||||
config.log(result['message'])
|
||||
return result
|
||||
finally:
|
||||
wb.close()
|
||||
Reference in New Issue
Block a user