feat(xq0041): 美团/抖音核对迁出至 XQ0085,保留导表与到账统计核对(v1.3.13)
download=只导打款/团购/预订收益明细;check_settlement=到账统计核对(并台账/门店归属/更新核对统计);金色印象与金色湖畔独立浏览器档案;新增 CRM 门店映射、基础信息同步与贴总表逻辑。
This commit is contained in:
@@ -1,19 +1,21 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""XQ0041 新美大平台核对 —— webhost 场景插件入口。
|
||||
|
||||
执行过程由 action 决定(UI 的两个开关派生):
|
||||
action='download' → 只下载:登录经营宝(界面配了账号密码则自动填写提交,验证码/选商家仍需人工),
|
||||
按勾选下载打款记录 / 团购收益明细 / 预订收益明细;
|
||||
action='reconcile' → 只核对:**不开浏览器**,直接用本机最新的台账与收益明细并入台账、
|
||||
更新核对统计(适合数据已下载、只想重算的场景);
|
||||
action='all' → 下载后再核对(默认)。
|
||||
两个独立动作(action),互不混跑:
|
||||
|
||||
* ``download`` → 只导表:登录经营宝下载打款记录 / 团购收益明细 / 预订收益明细;
|
||||
* ``check_settlement`` → 到账统计核对:新美大到账统计的核对
|
||||
(并入台账 → 门店归属 → 更新核对统计)。
|
||||
|
||||
美团核对 / 抖音核对已迁到 XQ0085 团购数据核对插件,本插件不再做。
|
||||
|
||||
``download_before`` 可在核对前先下载;不勾则读本机已有表(只校验文件存在)。
|
||||
|
||||
多公司(对齐 xq0042 抖音平台核对):金色印象(台账那家)与金色湖畔在平台上是两套登录,
|
||||
各自用独立的浏览器档案与调试端口(browser_sessions/xq0041 与 xq0041_b,端口 9222 / 9333),
|
||||
登录态互不干扰,避免同浏览器反复切号触发风控。
|
||||
run_mode='full' → 只导金色印象 + 并入台账 + 更新核对统计(默认,历史行为)
|
||||
run_mode='export_only' → 按 export_companies 勾选的公司各导一份(可只导一家),不写任何表
|
||||
(湖畔文件名带「金色湖畔_」前缀)
|
||||
run_mode='full' → 下载时只导金色印象(默认)
|
||||
run_mode='export_only' → 下载时按 export_companies 勾选的公司各导一份(不写表)
|
||||
并入台账 / 更新核对统计只认金色印象那份明细(湖畔文件天然落在台账 glob 之外)。
|
||||
|
||||
签名说明:平台接口的签名参数由页面自身的前端组件生成,插件只在页面上下文发起请求,
|
||||
@@ -26,7 +28,7 @@ from webhost.backend.plugins.base import PLUGINS, ScenePlugin
|
||||
|
||||
from . import config
|
||||
from .data import browser, meituan_login, payment_download, profit_download
|
||||
from .logic import merge_ledger, stat_links
|
||||
from .logic import merge_ledger, stat_links, store_map
|
||||
|
||||
|
||||
def _parse_day(s):
|
||||
@@ -54,38 +56,41 @@ def _resolve_pair(raw_from, raw_to):
|
||||
|
||||
|
||||
def _resolve_action(fields):
|
||||
"""决定本次做什么:download / reconcile / all。
|
||||
"""解析动作:download / check_settlement(美团核对已迁到 xq0085)。"""
|
||||
raw = str(fields.get('action') or '').strip().lower()
|
||||
if raw in config.ACTIONS:
|
||||
return raw
|
||||
if raw in ('all', 'reconcile', 'check_meituan'):
|
||||
return config.ACTION_CHECK_SETTLEMENT
|
||||
return config.ACTION_DOWNLOAD
|
||||
|
||||
UI 会直接给 action;老配置(无 action)按开关推导,缺省视为全流程。
|
||||
"""
|
||||
action = str(fields.get('action') or '').strip().lower()
|
||||
if action in ('download', 'reconcile', 'all'):
|
||||
return action
|
||||
do_download = fields.get('do_download')
|
||||
do_reconcile = fields.get('do_reconcile')
|
||||
do_download = True if do_download is None else bool(do_download)
|
||||
do_reconcile = True if do_reconcile is None else bool(do_reconcile)
|
||||
if do_download and do_reconcile:
|
||||
return 'all'
|
||||
if do_download:
|
||||
return 'download'
|
||||
if do_reconcile:
|
||||
return 'reconcile'
|
||||
return ''
|
||||
|
||||
def _missing_inputs(action, work_dir):
|
||||
"""不下载时校验核对必需的本机文件(只看存在性,不读内容)。"""
|
||||
import glob
|
||||
|
||||
absent = []
|
||||
|
||||
def has(pattern):
|
||||
return bool(glob.glob(os.path.join(work_dir, pattern)))
|
||||
|
||||
if action == config.ACTION_CHECK_SETTLEMENT:
|
||||
if not has(config.LEDGER_GLOB):
|
||||
absent.append(config.LEDGER_GLOB)
|
||||
if not has(config.DETAIL_GLOB):
|
||||
absent.append(config.DETAIL_GLOB)
|
||||
return absent
|
||||
|
||||
|
||||
class Xq0041Plugin(ScenePlugin):
|
||||
id = 'xq0041'
|
||||
name = '新美大平台核对'
|
||||
version = '1.3.11'
|
||||
description = ('美团经营宝账单自动下载与台账核对:按需下载打款记录、团购收益明细、'
|
||||
'预订收益明细,把新产生的收益按验券时间并入月份台账,'
|
||||
'并按日期把核对统计表整行同源取数公式(资金组取数)换成当天共享总表的链接;'
|
||||
'界面可配账号密码自动填好并勾选用户协议、浏览器内浮层提示你点登录'
|
||||
'(登录按钮/验证码/选门店仍人工),并自动选「非餐饮商家版」;'
|
||||
'执行模式分「含写表」(只导金色印象 + 并台账 + 更新核对统计)与'
|
||||
'「仅导出」(金色印象 / 金色湖畔按需勾选,可只导一家,不写表),'
|
||||
'两家各自独立浏览器档案与调试端口,登录态互不干扰')
|
||||
version = '1.3.13'
|
||||
description = ('美团数据导出与到账统计核对:两个独立动作——「只导表」(下载打款记录/团购/'
|
||||
'预订收益明细)、「到账统计核对」(新美大到账统计:并台账 → 门店归属 → '
|
||||
'更新核对统计)。美团核对与抖音核对已迁到 XQ0085 团购数据核对插件。'
|
||||
'界面可配账号密码自动填好,登录/验证码/选商家/选版本由人工完成;'
|
||||
'金色印象与金色湖畔各自独立浏览器档案与调试端口,登录态互不干扰')
|
||||
platform = '' # 插件自带登录(经营宝 SSO + 选商家),不用底座共享登录态
|
||||
uses_month = False
|
||||
capabilities = ['xq0041']
|
||||
@@ -102,10 +107,10 @@ class Xq0041Plugin(ScenePlugin):
|
||||
'enum': [config.RUN_MODE_FULL, config.RUN_MODE_EXPORT_ONLY]},
|
||||
'export_companies': {'type': 'array', 'title': '导出范围(仅导出模式生效)',
|
||||
'default': ['a', 'b'], 'items': {'type': 'string'}},
|
||||
'action': {'type': 'string', 'title': '执行过程', 'default': 'all',
|
||||
'enum': ['all', 'download', 'reconcile']},
|
||||
'do_download': {'type': 'boolean', 'title': '下载数据', 'default': True},
|
||||
'do_reconcile': {'type': 'boolean', 'title': '更新核对', 'default': True},
|
||||
'action': {'type': 'string', 'title': '执行动作',
|
||||
'default': config.ACTION_CHECK_SETTLEMENT, 'enum': list(config.ACTIONS)},
|
||||
'download_before': {'type': 'boolean',
|
||||
'title': '核对前先下载(不选则用本机已有表)', 'default': False},
|
||||
'pay_enabled': {'type': 'boolean', 'title': '下载打款记录', 'default': True},
|
||||
'pay_date_from': {'type': 'string', 'title': '打款记录-开始日期', 'default': ''},
|
||||
'pay_date_to': {'type': 'string', 'title': '打款记录-结束日期', 'default': ''},
|
||||
@@ -116,6 +121,7 @@ class Xq0041Plugin(ScenePlugin):
|
||||
'save_dir': {'type': 'string', 'title': '保存目录', 'default': ''},
|
||||
'force_login': {'type': 'boolean', 'title': '强制重新登录', 'default': False},
|
||||
'run_merge': {'type': 'boolean', 'title': '并入台账', 'default': True},
|
||||
'run_store_map': {'type': 'boolean', 'title': '门店归属(CRM 券号)', 'default': True},
|
||||
'run_links': {'type': 'boolean', 'title': '更新核对统计', 'default': True},
|
||||
'dry_run': {'type': 'boolean', 'title': '只核对不写入', 'default': False},
|
||||
'debug_mode': {'type': 'boolean', 'title': '调试日志', 'default': False},
|
||||
@@ -128,18 +134,18 @@ class Xq0041Plugin(ScenePlugin):
|
||||
config.set_debug(fields.get('debug_mode'))
|
||||
|
||||
# ---------- 参数 ----------
|
||||
raw_action = str(fields.get('action') or '').strip().lower()
|
||||
action = _resolve_action(fields)
|
||||
run_mode = config.normalize_run_mode(fields.get('run_mode'))
|
||||
if run_mode == config.RUN_MODE_EXPORT_ONLY:
|
||||
# 仅导出:两家公司各导一份,不写任何表(对齐 xq0042 的语义)
|
||||
if action == 'reconcile':
|
||||
raise RuntimeError('「仅导出」模式不做核对;要并台账请把执行模式切成「含写表」')
|
||||
action = 'download'
|
||||
if not action:
|
||||
raise RuntimeError('请至少选择一个执行过程(下载数据 / 更新核对)')
|
||||
do_download = action in ('all', 'download')
|
||||
do_reconcile = action in ('all', 'reconcile')
|
||||
export_only = run_mode == config.RUN_MODE_EXPORT_ONLY
|
||||
if export_only and action != config.ACTION_DOWNLOAD:
|
||||
raise RuntimeError('「仅导出」模式不做核对;要核对请把执行模式切成「含写表」')
|
||||
|
||||
only_download = action == config.ACTION_DOWNLOAD
|
||||
check_settlement = action == config.ACTION_CHECK_SETTLEMENT
|
||||
download_before = bool(fields.get('download_before', False))
|
||||
do_download = only_download or download_before
|
||||
do_check = check_settlement
|
||||
|
||||
codes = []
|
||||
if fields.get('profit_group', True):
|
||||
@@ -150,6 +156,7 @@ class Xq0041Plugin(ScenePlugin):
|
||||
|
||||
dry_run = bool(fields.get('dry_run'))
|
||||
run_merge = bool(fields.get('run_merge', True))
|
||||
run_store_map = bool(fields.get('run_store_map', True))
|
||||
run_links = bool(fields.get('run_links', True))
|
||||
force_login = bool(fields.get('force_login'))
|
||||
# 保存目录同时是台账目录:并入台账 / 更新核对统计都读它
|
||||
@@ -167,8 +174,16 @@ class Xq0041Plugin(ScenePlugin):
|
||||
if do_download and not pay_on and not codes:
|
||||
raise RuntimeError('请至少勾选一项要下载的内容(打款记录 / 团购 / 预订)')
|
||||
|
||||
# 不下载就核对:只校验本机必需文件是否存在,缺了明确提示先导表
|
||||
if not do_download and do_check:
|
||||
absent = _missing_inputs(action, config.WORK_DIR)
|
||||
if absent:
|
||||
raise RuntimeError(
|
||||
'未勾选「核对前先下载」,且本机缺少核对所需文件:' + '、'.join(absent)
|
||||
+ f'(目录 {config.WORK_DIR});请先执行「只导表」,或勾「核对前先下载」')
|
||||
|
||||
# 并入台账依赖团购收益明细(台账只收团购口径)
|
||||
if do_reconcile and run_merge and not codes and do_download:
|
||||
if check_settlement and run_merge and not codes and do_download:
|
||||
config.log('未勾选团购收益明细,本次跳过「并入台账」', 'WARN')
|
||||
run_merge = False
|
||||
|
||||
@@ -178,33 +193,38 @@ class Xq0041Plugin(ScenePlugin):
|
||||
os.makedirs(save_dir, exist_ok=True)
|
||||
|
||||
companies = config.companies_for_mode(run_mode, fields.get('export_companies'))
|
||||
plan = []
|
||||
labels = {config.ACTION_DOWNLOAD: '只导表',
|
||||
config.ACTION_CHECK_SETTLEMENT: '到账统计核对'}
|
||||
plan = [labels[action]]
|
||||
if do_download:
|
||||
plan.append('公司 ' + ' + '.join(c['short'] for c in companies))
|
||||
plan.append('先下载' if do_check else
|
||||
'公司 ' + ' + '.join(c['short'] for c in companies))
|
||||
if pay_on:
|
||||
plan.append(f'打款记录 {pay_from}~{pay_to}')
|
||||
if codes:
|
||||
name = '/'.join(n for c, n in ((1, '团购'), (11, '预订')) if c in codes)
|
||||
plan.append(f'{name}收益明细 {profit_from}~{profit_to}')
|
||||
if do_reconcile:
|
||||
if check_settlement:
|
||||
plan.append('并入台账' if run_merge else '')
|
||||
plan.append('门店归属(CRM 券号)' if run_store_map else '')
|
||||
plan.append('更新核对统计' if run_links else '')
|
||||
plan = [p for p in plan if p]
|
||||
config.log(('本次执行:' + ';'.join(plan) if plan else '本次没有可执行的动作')
|
||||
+ f';目录 {save_dir}'
|
||||
config.log('本次执行:' + ';'.join(plan) + f';目录 {save_dir}'
|
||||
+ ('(只核对不写入)' if dry_run else ''))
|
||||
config.log('执行模式:' + ('仅导出(金色印象 + 金色湖畔,不写表)' if export_only
|
||||
else '含写表(只导金色印象 + 并台账 + 更新核对统计)'))
|
||||
config.log('下载:' + ('先下载再核对' if download_before else
|
||||
('只导表' if only_download else '不下载,用本机已有表')))
|
||||
|
||||
detail = {'action': action, 'run_mode': run_mode, 'pay': None, 'profit': None,
|
||||
'merge': None, 'links': None, 'results': {},
|
||||
'companies': [c['short'] for c in companies]}
|
||||
detail = {'action': action, 'action_label': labels[action],
|
||||
'download_before': download_before, 'run_mode': run_mode,
|
||||
'pay': None, 'profit': None, 'merge': None, 'store': None,
|
||||
'links': None,
|
||||
'results': {}, 'companies': [c['short'] for c in companies]}
|
||||
files = []
|
||||
|
||||
# ---------- 1~3:登录 + 下载(逐家公司:各自独立浏览器档案 + 端口) ----------
|
||||
if do_download:
|
||||
jobs = [(comp, *creds.get(comp['id'], ('', ''))) for comp in companies]
|
||||
dl_end = 60 if do_reconcile else 95
|
||||
dl_end = 60 if do_check else 95
|
||||
span = (dl_end - 2) / len(jobs)
|
||||
errors = []
|
||||
try:
|
||||
@@ -217,9 +237,10 @@ class Xq0041Plugin(ScenePlugin):
|
||||
config.log(f'—— {label}:档案 {comp["profile"]} / 端口 {comp["port"]} ——')
|
||||
page = browser.open_page(force_login=force_login)
|
||||
try:
|
||||
config.log(f'{label} 登录方式:' + ('界面配置的账号密码自动登录'
|
||||
config.log(f'{label} 登录方式:' + ('界面配置的账号密码自动填入登录页'
|
||||
if (user and pwd) else '手工登录'))
|
||||
ok, msg = meituan_login.ensure_login(page, user, pwd)
|
||||
ok, msg = meituan_login.ensure_login(page, user, pwd,
|
||||
force_login=force_login)
|
||||
config.log(msg)
|
||||
if not ok:
|
||||
raise RuntimeError(f'{label} 登录失败:{msg}')
|
||||
@@ -250,6 +271,8 @@ class Xq0041Plugin(ScenePlugin):
|
||||
if comp.get('ledger'):
|
||||
detail['profit'] = r2
|
||||
ctx.progress(int(base + span))
|
||||
# 下载期间站点可能轮换 Cookie,收尾再落盘一次
|
||||
meituan_login.save_cookies(page)
|
||||
detail['results'] = detail.get('results') or {}
|
||||
detail['results'][label] = {'ok': True}
|
||||
except Exception as e:
|
||||
@@ -266,16 +289,31 @@ class Xq0041Plugin(ScenePlugin):
|
||||
if errors:
|
||||
raise RuntimeError(';'.join(errors))
|
||||
|
||||
# ---------- 4~5:并入台账 + 更新核对(不依赖浏览器) ----------
|
||||
if do_reconcile:
|
||||
ctx.progress(65 if do_download else 10)
|
||||
# ---------- 核对:到账统计(不依赖浏览器;美团/抖音核对已迁到 xq0085) ----------
|
||||
if do_check:
|
||||
ctx.progress(62 if do_download else 5)
|
||||
|
||||
if check_settlement:
|
||||
# 之前的核对:新美大到账统计
|
||||
if run_merge:
|
||||
m = merge_ledger.run(dry_run=dry_run)
|
||||
config.log(m.get('message') or '')
|
||||
detail['merge'] = m
|
||||
if not m.get('ok'):
|
||||
raise RuntimeError(f'并入台账失败:{m.get("message")}')
|
||||
ctx.progress(85 if do_download else 60)
|
||||
if run_store_map:
|
||||
# 门店归属:券号 join CRM,结果单列输出(不动台账)
|
||||
ctx.progress(72 if do_download else 45)
|
||||
sm = store_map.run(dry_run=dry_run,
|
||||
progress_from=(72 if do_download else 45),
|
||||
progress_to=(84 if do_download else 58))
|
||||
config.log(sm.get('message') or '')
|
||||
detail['store'] = sm
|
||||
if not sm.get('ok'):
|
||||
# CRM 未登录 / 未挂载 SDK 时只告警:不影响台账与核对统计
|
||||
config.log(f'门店归属未完成:{sm.get("message")}', 'WARN')
|
||||
|
||||
ctx.progress(88 if do_download else 65)
|
||||
|
||||
if run_links:
|
||||
# 纯核对时让后端按本机最新明细文件的日期推断(跟随实际数据);
|
||||
@@ -290,13 +328,16 @@ class Xq0041Plugin(ScenePlugin):
|
||||
ctx.progress(100)
|
||||
|
||||
# ---------- 汇总 ----------
|
||||
parts = []
|
||||
parts = [detail.get('action_label') or '执行完成']
|
||||
if detail['pay'] and detail['pay'].get('empty'):
|
||||
parts.append(f'打款记录 {pay_from}~{pay_to} 无数据')
|
||||
if files:
|
||||
parts.append(f'下载 {len(files)} 个文件')
|
||||
if detail['merge'] and detail['merge'].get('appended'):
|
||||
parts.append(f'台账并入 {detail["merge"]["appended"]} 行')
|
||||
if detail['store'] and detail['store'].get('ok'):
|
||||
parts.append(f'门店归属 {detail["store"]["mapped"]} 行 / '
|
||||
f'{detail["store"]["stores"]} 家店')
|
||||
if detail['links'] and detail['links'].get('rows'):
|
||||
parts.append(f'核对统计更新 {len(detail["links"]["rows"])} 行')
|
||||
if not parts:
|
||||
|
||||
Reference in New Issue
Block a user