feat: 新增银行卡检查插件及相关依赖与配置文件

This commit is contained in:
2026-10-29 14:21:30 +08:00
parent 0a32518060
commit c77a26cd99
494 changed files with 66923 additions and 2714 deletions
+177
View File
@@ -0,0 +1,177 @@
# -*- coding: utf-8 -*-
"""XQ0009 插件 - 报告编排层:主流程 + Excel 导出(由影刀版 L1353-1490 移植)。
主流程(与影刀版 export_absence_list 一致,删除钉钉推送/影刀全局变量交接):
1) 会话校验:底座注入 HR 会话 → getCurUserInfo 校验取 userId(失效抛错提示重新登录);
2) 花名册:字段指定已有文件,或由 data/roster_export_api 自动导出到保存目录
(员工花名册.xlsx,15 分钟 TTL 缓存复用);
3) 收银会话:统计月或当前月 >= CASHIER_MIGRATE_MONTH(2026-09) 时初始化;
提供凭据登录失败直接中止(避免静默缺技师数据出错名单),无凭据降级仅用 HR;
4) 统计月数据(实时):考勤统计报表 → 报表缺勤映射 → 技师收银覆盖 → 全集团请假并入;
5) 返岗判定数据:统计月已结束时补拉当前月 1 日~今天(实时),技师出勤日集合走收银;
6) analyze 计算月末往前连续未出勤段(跨月追溯逐月重估)→ export_excel 导出 20 列名单。
"""
import os
from datetime import date, datetime
import pandas as pd
from ..config import (
logger, DEFAULT_SAVE_DIR, COLUMNS, MIN_CONSECUTIVE_DAYS, require_session,
)
from ..data import hr_api, cashier_api, roster_export_api
from ..logic import absence_analysis as core
def resolve_month(ref_month):
"""解析统计月份:"YYYY-MM"/"YYYYMM";留空=上月完整月份。返回 (begin, end, month_str="YYYYMM")。"""
ref = (ref_month or "").strip()
if ref:
begin, end = core.month_range(ref)
else:
begin, end = core.last_month_range()
return begin, end, f"{begin:%Y%m}"
def export_excel(rows, save_dir, month_str):
"""将名单行导出为 .xlsx(无数据时也保留表头,便于对照)。"""
os.makedirs(save_dir, exist_ok=True)
df = pd.DataFrame(rows, columns=COLUMNS)
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
save_path = os.path.abspath(
os.path.join(save_dir, f"门店上月请休假异常名单_{month_str}_{ts}.xlsx"))
try:
df.to_excel(save_path, index=False)
except PermissionError:
raise RuntimeError(
f"导出失败:文件正被占用,请先关闭已打开的 Excel"
f"「{os.path.basename(save_path)}」后重试。无需重查数据。")
except OSError as e:
raise RuntimeError(
f"导出失败:无法写入文件「{save_path}」({e}),请检查磁盘空间或目录权限")
logger.info("名单数据:%d 条", len(rows))
logger.info("文件已保存:%s", save_path)
return save_path
def _prepare_roster(roster_path, save_dir, session):
"""花名册准备:指定路径直接用;否则自动导出到保存目录/员工花名册.xlsx(15 分钟 TTL 缓存)。"""
roster_path = (roster_path or "").strip()
if roster_path:
logger.info("使用指定花名册:%s", roster_path)
return roster_path
output_path = os.path.join(save_dir, "员工花名册.xlsx")
return roster_export_api.export_roster(download_dir=save_dir, output_path=output_path,
session=session)
def build_absence_report(save_dir=None, roster_path=None, ref_month="",
cashier_username="", cashier_password="", progress=None):
"""梳理上月请休假异常名单并导出 Excel。
Returns:
dict:{path 导出文件路径, count 名单人数, month 统计月 "YYYYMM", begin, end 统计范围}
"""
def _step(pct):
if progress:
try:
progress(pct)
except Exception:
pass
save_dir = (save_dir or "").strip() or DEFAULT_SAVE_DIR
os.makedirs(save_dir, exist_ok=True)
begin, end, month_str = resolve_month(ref_month)
today = date.today()
today_m = today.strftime("%Y-%m")
logger.info("===== 请休假异常名单梳理,统计范围:%s ~ %s =====",
begin.strftime("%Y-%m-%d"), end.strftime("%Y-%m-%d"))
# 1. HR 会话校验(底座注入,失效抛 HRSessionExpiredError 提示重新登录)
_step(5)
session = require_session()
user_id = hr_api.validate_session(session)
# 2. 花名册
roster_file = _prepare_roster(roster_path, save_dir, session)
df = core.load_roster(roster_file)
emp = core.build_emp_map(df)
_step(10)
# 2.5 收银系统会话(技师考勤源):统计月或当前月落在迁移月及以后时初始化;
# 无凭据且无有效缓存登录态时降级仅用 HR(不阻断流程);
# 提供凭据后登录失败由 ensure_session 抛 RuntimeError 中止(避免错名单)。
cashier_ready = False
if cashier_api.is_cashier_month(month_str) or cashier_api.is_cashier_month(today_m):
cashier_ready = cashier_api.ensure_session(cashier_username, cashier_password)
_step(15)
# 3. 统计月数据(实时调接口,不走缓存)
seen = {}
records = hr_api.fetch_all_attendance_stat(session, user_id, month_str)
logger.info("考勤统计报表共 %d 人", len(records))
if not records:
raise RuntimeError("考勤统计报表无数据,请检查月份或权限")
month_status = core.report_records_to_status(records, begin, end, seen)
# 统计月 >= 2026-09:HR + 收银两份,技师未出勤以收银为准覆盖(HR 该月起无技师数据)
if cashier_ready and cashier_api.is_cashier_month(month_str):
try:
tech_map = cashier_api.cashier_tech_status(month_str)
for code, daymap in tech_map.items():
month_status[code] = daymap
logger.info("收银技师考勤并入统计月 %d 人", len(tech_map))
except Exception as e:
logger.warning("收银技师考勤拉取失败(技师数据可能缺失):%s", e)
leaves = hr_api.fetch_all_leave(
session, user_id, "", begin.strftime("%Y-%m-%d"), end.strftime("%Y-%m-%d"))
core.merge_leave_status(month_status, leaves, begin, end)
logger.info("全集团请假记录 %d 条(整天请假已并入缺勤判定)", len(leaves))
core.log_statuses(seen)
_step(40)
# 3.5 返岗判定数据:统计月已结束且当前月 != 统计月时,补拉当前月 1 日~今天 的考勤报表
# +请假记录(实时),用于判断名单人员是否已返岗(不再只靠请假判断)。
cur_ctx = None
if end < today:
cur_begin = today.replace(day=1)
cur_end = today
cur_records = hr_api.fetch_all_attendance_stat(session, user_id, today.strftime("%Y%m"))
cur_status = core.cur_month_status(cur_records, cur_begin, cur_end)
cur_leaves = hr_api.fetch_all_leave(
session, user_id, "",
cur_begin.strftime("%Y-%m-%d"), cur_end.strftime("%Y-%m-%d"))
core.merge_leave_status(cur_status, cur_leaves, cur_begin, cur_end)
cur_rows = {core.clean(r.get("base_code")): r
for r in cur_records if core.clean(r.get("base_code"))}
# 当前月 >= 2026-09:技师返岗判定用收银「出勤日集合」(HR 当前月无技师数据,row 为空会漏判)
cashier_cur = {}
if cashier_ready and cashier_api.is_cashier_month(today_m):
try:
# 先确保当前月收银映射(含出勤日 month_on)已加载
cashier_api.cashier_tech_status(today_m)
cashier_cur = dict(cashier_api.state["month_on"].get(today_m, {}))
logger.info("收银技师当前月返岗判定:%d 名技师有出勤日", len(cashier_cur))
except Exception as e:
logger.warning("收银技师当前月数据拉取失败(返岗判定降级):%s", e)
cur_ctx = (cur_status, cur_rows, cur_begin, cur_end, cashier_cur)
logger.info("统计月 %s 已结束,补拉当前月 %s ~ %s 考勤用于返岗判定",
month_str, cur_begin.strftime("%Y-%m-%d"), cur_end.strftime("%Y-%m-%d"))
_step(55)
# 4. 计算月末往前连续未出勤段(跨月追溯「逐月重估」:批量/单人自适应)
rows = core.analyze(emp, month_status, session, user_id, begin, end, cur_ctx)
_step(95)
# 5. 导出
path = export_excel(rows, save_dir, month_str)
_step(100)
logger.info("=" * 60)
logger.info("【请休假异常名单汇总】")
logger.info(" 统计范围:%s ~ %s", begin.strftime("%Y-%m-%d"), end.strftime("%Y-%m-%d"))
logger.info(" 连续未出勤 >= %d 天:%d 人", MIN_CONSECUTIVE_DAYS, len(rows))
logger.info(" 导出文件:%s", path)
logger.info("=" * 60)
return {"path": path, "count": len(rows), "month": month_str,
"begin": begin.strftime("%Y-%m-%d"), "end": end.strftime("%Y-%m-%d")}