# -*- coding: utf-8 -*- """XQ0085 团购数据核对 —— 美团核对 + 抖音核对,两条独立分支。 从 XQ0041 抽出核对部分,插件自包含、不开浏览器、不做下载。共享盘全程只读: 先把当天的核对表从共享盘复制到本地 ``WORK_DIR/_work/``,后续所有处理只改本地副本。 * ``check_meituan``:复制共享表 → 订单收入明细合并+合计 → 基础信息补维护 → 报表按值贴入本地马敏总表副本(平台-新美大 + CRM 团购统计); * ``check_douyin``:复制共享表 → 核销明细 A~BR 重贴 → 四口径预计收入合计校验 → 基础信息补维护 → 抖音报表 N~Z 按值贴入本地总表副本(平台-抖音)。 """ import os from datetime import date, datetime, timedelta from webhost.backend.plugins.base import PLUGINS, ScenePlugin from . import config from .logic import base_info_sync, daily_detail, douyin_daily, paste_to_master, share_copy ACTION_CHECK_MEITUAN = 'check_meituan' ACTION_CHECK_DOUYIN = 'check_douyin' ACTIONS = (ACTION_CHECK_MEITUAN, ACTION_CHECK_DOUYIN) def _parse_day(s): for fmt in ('%Y-%m-%d', '%Y/%m/%d', '%Y%m%d'): try: return datetime.strptime(str(s).strip(), fmt).date() except (ValueError, TypeError, AttributeError): continue return None def _resolve_action(fields): raw = str(fields.get('action') or '').strip().lower() if raw in ACTIONS: return raw return ACTION_CHECK_MEITUAN def _check_day(fields): """核对营业日:界面指定优先,否则按收益明细结束日期,再否则今天。""" day = _parse_day(fields.get('daily_day')) or _parse_day(fields.get('profit_date_to')) return day or date.today() class Xq0085Plugin(ScenePlugin): id = 'xq0085' name = '团购数据核对' version = '1.0.0' description = ('团购数据核对(美团 + 抖音两条独立分支,共享盘只读):' '先把当天的核对表从共享盘复制到本地 _work,再全部在本地副本上处理。' '美团核对:团购+预订收益明细合并贴入【订单收入明细表】、明细/透视/报表三口径合计校验、' '报表 #N/A 基础信息补维护、报表按值贴入马敏总表【平台-新美大】与 CRM【团购统计】。' '抖音核对:导出核销明细 A~BR 重贴【核销明细】、明细/透视/报表/转换四口径预计收入合计校验、' '报表 O/P 基础信息补维护、报表按值贴入【平台-抖音】。') platform = '' uses_month = False capabilities = ['xq0085'] permissions = {'platforms': []} fields = { 'action': {'type': 'string', 'title': '核对分支', 'default': ACTION_CHECK_MEITUAN, 'enum': list(ACTIONS)}, 'daily_day': {'type': 'string', 'title': '核对营业日', 'default': '', 'description': 'YYYY-MM-DD;留空=今天'}, 'save_dir': {'type': 'string', 'title': '本地工作目录', 'default': ''}, 'share_root': {'type': 'string', 'title': '共享核对表根目录', 'default': ''}, 'copy_share_table': {'type': 'boolean', 'title': '从共享盘复制当天的表到本地(只读)', 'default': True}, 'run_daily_detail': {'type': 'boolean', 'title': '美团-订单收入明细(合并+合计)', 'default': True}, 'run_base_info': {'type': 'boolean', 'title': '美团-基础信息补维护(报表 #N/A)', 'default': True}, 'run_paste_master': {'type': 'boolean', 'title': '按值贴入马敏总表(只写本地副本)', 'default': True}, 'run_douyin_daily': {'type': 'boolean', 'title': '抖音-核销明细重贴+四口径合计', 'default': True}, 'run_douyin_base': {'type': 'boolean', 'title': '抖音-基础信息补维护(报表 O/P)', 'default': True}, 'use_com_refresh': {'type': 'boolean', 'title': '用 WPS/Excel COM 刷新透视(推荐)', 'default': True}, 'dry_run': {'type': 'boolean', 'title': '只核对不写入', 'default': False}, 'debug_mode': {'type': 'boolean', 'title': '调试日志', 'default': False}, } def run(self, fields, ctx): fields = fields or {} config.set_log(ctx.log) config.set_progress(ctx.progress) config.set_debug(fields.get('debug_mode')) config.set_work_dir(fields.get('save_dir')) action = _resolve_action(fields) day = _check_day(fields) dry = bool(fields.get('dry_run')) use_com = bool(fields.get('use_com_refresh', True)) share_root = str(fields.get('share_root') or '').strip() copy_share = bool(fields.get('copy_share_table', True)) detail = {'action': action, 'day': day.isoformat(), 'share': None, 'daily': None, 'base_info': None, 'paste': None, 'douyin': None, 'results': {}} config.log(f'== {self.name} {action} 营业日 {day.isoformat()} ' + ('(只核对不写入)' if dry else '') + ' ==') ctx.progress(3) # ---------- 第 1 步:共享盘只读复制当天表到本地 ---------- if copy_share: keywords = ([config.SHARE_TABLE_KEYWORD, config.LINK_BOOK] if action == ACTION_CHECK_MEITUAN else [config.DOUYIN_SHARE_KEYWORD, config.LINK_BOOK]) sc = share_copy.copy_prev_tables(share_root=share_root, day=day, keywords=keywords, dry_run=dry) config.log(sc.get('message') or '') detail['share'] = sc if not sc.get('ok'): config.log(f'共享表复制不完整:{sc.get("message")}', 'WARN') ctx.progress(20) if action == ACTION_CHECK_MEITUAN: self._run_meituan(fields, day, dry, use_com, detail, ctx) else: self._run_douyin(fields, day, dry, use_com, detail, ctx) ctx.progress(100) parts = [f'美团核对' if action == ACTION_CHECK_MEITUAN else '抖音核对'] if detail['daily'] and detail['daily'].get('ok'): d = detail['daily'] parts.append(f'明细{d["detail_rows"]}行 合计{d["total"]:.2f}') if detail['douyin'] and detail['douyin'].get('ok'): d = detail['douyin'] parts.append(f'核销明细{d["detail_rows"]}行 四口径' f'{d["detail_total"]:.2f}/{d["pivot_total"]:.2f}/' f'{d["report_total"]:.2f}/{d["convert_total"]:.2f}') if detail['paste'] and detail['paste'].get('ok'): p = detail['paste'] parts.append(f'总表粘贴 新美大{p["meituan_rows"]}/团购{p["group_rows"]}/' f'抖音{p["douyin_rows"]}行') config.log('完成:' + ';'.join(parts)) return {'ok': True, 'message': ';'.join(parts), 'detail': detail} def _run_meituan(self, fields, day, dry, use_com, detail, ctx): if bool(fields.get('run_daily_detail', True)): ctx.progress(35) d = daily_detail.run(day=day, dry_run=dry, use_com=use_com) config.log(d.get('message') or '') detail['daily'] = d if not d.get('ok'): config.log(f'订单收入明细未完成:{d.get("message")}', 'WARN') if bool(fields.get('run_base_info', True)): ctx.progress(55) local = (detail['daily'] or {}).get('path') or '' bi = base_info_sync.run(local, dry_run=dry) config.log(bi.get('message') or '') detail['base_info'] = bi if bi.get('ok') and (bi.get('new_shops') or bi.get('new_items')): config.log(f'基础信息需补维护:新增门店 {len(bi["new_shops"])} / ' f'套餐 {len(bi["new_items"])}', 'WARN') if bool(fields.get('run_paste_master', True)): ctx.progress(75) pm = paste_to_master.run(day=day, dry_run=dry, jobs_filter=['meituan', 'group']) config.log(pm.get('message') or '') detail['paste'] = pm if not pm.get('ok'): config.log(f'马敏总表粘贴未完成:{pm.get("message")}', 'WARN') def _run_douyin(self, fields, day, dry, use_com, detail, ctx): if bool(fields.get('run_douyin_daily', True)): ctx.progress(35) dy = douyin_daily.run(day=day, dry_run=dry, use_com=use_com) config.log(dy.get('message') or '') detail['douyin'] = dy if not dy.get('ok'): config.log(f'抖音核对未完成:{dy.get("message")}', 'WARN') if bool(fields.get('run_douyin_base', True)): # 基础信息检查已含在 douyin_daily 里;这里只把结果单独记一份 detail['base_info'] = (detail['douyin'] or {}).get('base_info') if bool(fields.get('run_paste_master', True)): ctx.progress(75) pm = paste_to_master.run(day=day, dry_run=dry, jobs_filter=['douyin']) config.log(pm.get('message') or '') detail['paste'] = pm if not pm.get('ok'): config.log(f'平台-抖音粘贴未完成:{pm.get("message")}', 'WARN') PLUGINS.append(Xq0085Plugin)