feat(xq0085): 团购核对支持对比表与门店归属(v1.1.0)
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""核对前置预检:导出明细 / 核对表 / 马敏总表 / CRM 支付数据齐不齐、日期对不对。
|
||||
|
||||
预检只读目录与文件名,不打开、也不写任何表。任一项 error 不通过,调用方应立即中止该分支——
|
||||
否则后续「先清空【订单收入明细表】/【核销明细】再重贴」会在缺文件时把表清空。
|
||||
|
||||
判定规则:
|
||||
|
||||
* 必填项缺失 → error;
|
||||
* 选填项(预订收益明细,账号无预订业务时本就没有)缺失 → 仅 WARN;
|
||||
* 明细文件名必须能解析出日期,且营业日落在区间内(单日即等于营业日);
|
||||
* 同一类明细出现 2 份及以上 → error。`daily_detail.run()` 会把匹配到的文件**全部合并**,
|
||||
目录里多留一份历史明细就会串天,所以要求只留当天那份。
|
||||
"""
|
||||
import os
|
||||
import re
|
||||
from datetime import date, datetime
|
||||
|
||||
from .. import config
|
||||
from . import share_copy
|
||||
|
||||
BRANCH_MEITUAN = 'check_meituan'
|
||||
BRANCH_DOUYIN = 'check_douyin'
|
||||
|
||||
# 分支 → ((名称, glob, 是否必填), ...)
|
||||
_BRANCH_SOURCES = {
|
||||
BRANCH_MEITUAN: (
|
||||
('团购收益明细', '团购收益明细_*.xlsx', True),
|
||||
('预订收益明细', '预订收益明细_*.xlsx', False),
|
||||
),
|
||||
BRANCH_DOUYIN: (
|
||||
('核销明细', '核销明细*.xlsx', True),
|
||||
),
|
||||
}
|
||||
|
||||
_DATE_RE = re.compile(r'(\d{4})\D?(\d{1,2})\D?(\d{1,2})')
|
||||
|
||||
|
||||
def _as_date(value):
|
||||
if isinstance(value, datetime):
|
||||
return value.date()
|
||||
if isinstance(value, date):
|
||||
return value
|
||||
text = str(value or '').strip()[:10]
|
||||
for fmt in ('%Y-%m-%d', '%Y/%m/%d', '%Y%m%d'):
|
||||
try:
|
||||
return datetime.strptime(text, fmt).date()
|
||||
except ValueError:
|
||||
continue
|
||||
return None
|
||||
|
||||
|
||||
def dates_in_name(name):
|
||||
"""从文件名里取出所有日期(升序去重),取不到返回空列表。"""
|
||||
out = []
|
||||
for m in _DATE_RE.finditer(os.path.basename(str(name))):
|
||||
try:
|
||||
d = date(int(m.group(1)), int(m.group(2)), int(m.group(3)))
|
||||
except ValueError:
|
||||
continue
|
||||
if 2000 <= d.year <= 2100 and d not in out:
|
||||
out.append(d)
|
||||
return sorted(out)
|
||||
|
||||
|
||||
def _item(name, ok, level, message, **extra):
|
||||
item = {'name': name, 'ok': bool(ok), 'level': level, 'message': message}
|
||||
item.update(extra)
|
||||
return item
|
||||
|
||||
|
||||
def _check_table(name, path):
|
||||
exists = os.path.isfile(path)
|
||||
return _item(name, exists, 'ok' if exists else 'error',
|
||||
'' if exists else f'未找到{name}:{path}', path=path)
|
||||
|
||||
|
||||
def _check_files(name, pattern, day, required=True):
|
||||
paths = config.glob_files(pattern)
|
||||
if not paths:
|
||||
level = 'error' if required else 'warn'
|
||||
tail = '' if required else '(选填,缺省视为当天无预订业务)'
|
||||
return _item(name, not required, level,
|
||||
f'未找到{name}({pattern}){tail};搜索目录:'
|
||||
+ ';'.join(config.search_dirs()),
|
||||
pattern=pattern, files=[])
|
||||
files = [os.path.basename(p) for p in paths]
|
||||
if len(paths) > 1:
|
||||
return _item(name, False, 'error',
|
||||
f'{name}有 {len(paths)} 份,合并时会串天;只保留营业日当天那一份:'
|
||||
+ '、'.join(files), pattern=pattern, files=files)
|
||||
|
||||
dates = dates_in_name(files[0])
|
||||
if not dates:
|
||||
return _item(name, True, 'warn',
|
||||
f'{name}文件名解析不出日期,无法校验是否属于营业日:{files[0]}',
|
||||
pattern=pattern, files=files)
|
||||
start, end = dates[0], dates[-1]
|
||||
if start <= day <= end:
|
||||
return _item(name, True, 'ok', '',
|
||||
pattern=pattern, files=files, range=[start.isoformat(), end.isoformat()])
|
||||
return _item(name, False, 'error',
|
||||
f'{name}日期 {start}~{end} 不含营业日 {day}:{files[0]}',
|
||||
pattern=pattern, files=files, range=[start.isoformat(), end.isoformat()])
|
||||
|
||||
|
||||
def _crm_path(day, crm_dir=''):
|
||||
root = str(crm_dir or config.CRM_EXPORT_DIR).strip().strip('"')
|
||||
return os.path.join(root, f'{config.CRM_GROUP_PAY_PREFIX}-{day:%Y%m%d}.xlsx')
|
||||
|
||||
|
||||
def run(day=None, action=BRANCH_MEITUAN, crm_dir=''):
|
||||
"""执行预检。
|
||||
|
||||
Args:
|
||||
day: 营业日(date / str),空则今天。
|
||||
action: ``check_meituan`` / ``check_douyin``。
|
||||
crm_dir: CRM 导出目录,空则用 ``config.CRM_EXPORT_DIR``。
|
||||
|
||||
Returns:
|
||||
dict: ``ok`` / ``day`` / ``action`` / ``checks`` / ``errors`` / ``warns`` / ``message``
|
||||
"""
|
||||
target = _as_date(day) or date.today()
|
||||
act = action if action in _BRANCH_SOURCES else BRANCH_MEITUAN
|
||||
keyword = config.SHARE_TABLE_KEYWORD if act == BRANCH_MEITUAN else config.DOUYIN_SHARE_KEYWORD
|
||||
|
||||
checks = [
|
||||
_check_table(f'{keyword}核对表副本',
|
||||
os.path.join(config.WORK_DIR,
|
||||
share_copy.table_name(keyword, target, config.SHARE_TABLE_EXT))),
|
||||
_check_table('马敏团购总表副本',
|
||||
os.path.join(config.WORK_DIR,
|
||||
f'{config.LINK_BOOK}{target.month}.{target.day}.xlsx')),
|
||||
]
|
||||
for name, pattern, required in _BRANCH_SOURCES[act]:
|
||||
checks.append(_check_files(name, pattern, target, required))
|
||||
if act == BRANCH_MEITUAN:
|
||||
checks.append(_check_table('CRM 团购支付数据', _crm_path(target, crm_dir)))
|
||||
|
||||
errors = [c for c in checks if not c['ok']]
|
||||
warns = [c for c in checks if c['ok'] and c['level'] == 'warn']
|
||||
ok = not errors
|
||||
if ok:
|
||||
message = '前置预检通过:' + '、'.join(c['name'] for c in checks) \
|
||||
+ (f';{len(warns)} 项提示见上' if warns else '')
|
||||
else:
|
||||
message = f'前置预检未通过({len(errors)} 项):' + ';'.join(
|
||||
c['message'] for c in errors)
|
||||
return {'ok': ok, 'day': target.isoformat(), 'action': act,
|
||||
'checks': checks, 'errors': errors, 'warns': warns, 'message': message}
|
||||
Reference in New Issue
Block a user