# -*- coding: utf-8 -*- """XQ0011 考勤月报封存检查 插件入口。 流程编排(数据全部预处理就绪后,再执行检查,检查阶段零接口请求): 1) 花名册导出(每次强制重导,不缓存复用)、调动台账导出(自动); 2) 封存数据导出与预处理(全集团 → 白名单单位 → 按花名册剔除学校/高管 → 固定路径封存表, 并预读进进程缓存供检查1~6 复用); 3) 排班数据预处理(一次拉取,产出 工号→最早排班日期/排班方案 映射,供检查1/2 复用); 4) 依次执行检查1~6(在职天数/出勤排休/封存最新值/全勤验证/未封存/延时加班); 5) 单工作簿输出(汇总 + 检查1~6 共 7 个 sheet)。 运行上下文:底座注入 HR 会话与 userId(config 全局钩子),平台 = hr,uses_month = True (前端页眉月份选择器经 payload.ref_month 注入)。 """ import os from webhost.backend.plugins.base import ScenePlugin, PLUGINS from . import config from .data.seal_data_export import export_seal_data, load_shift_data, load_seal_rows from .data.roster_export_api import export_roster from .data.transfer_roster_export_api import export_transfer_roster from .report.monthly_report_check import run_all_checks, write_workbook, _normalize_month class KqSealCheckPlugin(ScenePlugin): """考勤月报封存检查(需求 XQ0011)。""" id = "xq0011" name = "考勤月报封存检查" version = "1.2.1" description = ("考勤月报封存后自动检查:拉取当月全集团封存数据并按白名单单位预处理," "自动导出花名册/调动台账,依次执行 6 项检查(在职天数排班/出勤及排休/" "封存最新值/全勤验证/未封存名单/延时加班),输出单工作簿异常名单") platform = "hr" uses_month = True capabilities = ["xq0011"] permissions = {"platforms": ["hr"]} fields = { "ref_month": { "type": "string", "title": "执行月份", "default": "", "description": "留空=上月;格式 YYYY-MM", }, "save_dir": { "type": "string", "title": "输出目录", "default": r"D:\资料\人资\考勤月报封存检查", "description": "封存表/花名册/调动台账/汇总工作簿统一输出目录", }, "roster_path": { "type": "string", "title": "花名册路径", "default": "", "description": "留空=输出目录/员工花名册.xlsx(每次执行自动重导,不缓存复用)", }, "transfer_path": { "type": "string", "title": "调动台账路径", "default": "", "description": "留空=输出目录/调动台账.xlsx(每次执行自动导出)", }, "checks": { "type": "array", "title": "检查项(可多选)", "default": ["检查1", "检查2", "检查3", "检查4", "检查5", "检查6"], "description": "勾选需要执行的检查项;留空=全部执行", }, } def run(self, fields: dict, ctx): # ---- 注入运行上下文:日志 / HR 会话 / userId ---- config.set_log(ctx.log) config.set_session(ctx.get_session("hr")) config.set_user_id(ctx.get_user_id("hr")) month = _normalize_month(fields.get("ref_month") or fields.get("month") or "") save_dir = (fields.get("save_dir") or "").strip() or config.DEFAULT_SAVE_DIR roster_path = (fields.get("roster_path") or "").strip() or os.path.join(save_dir, "员工花名册.xlsx") transfer_path = (fields.get("transfer_path") or "").strip() or os.path.join(save_dir, "调动台账.xlsx") checks = [str(c).strip() for c in (fields.get("checks") or []) if str(c).strip()] or None config.set_roster_path(roster_path) session = config.get_session() ctx.log(f"===== 考勤月报封存检查启动:月份 {month},输出目录 {save_dir} =====", "INFO") notes: list = [] # ---- 步骤1:花名册导出(强制重导,不做缓存复用;5% ~ 30%) ---- # 必须放在封存导出之前:封存预处理需按花名册剔除学校/高管、回填部门 ctx.progress(5) try: export_roster(output_path=roster_path, download_dir=os.path.join(config.CACHE_DIR, "roster_tmp"), session=session, ttl=0) except Exception as e: notes.append(f"花名册导出失败(检查5/6 与部门回填/学校剔除将受影响):{e}") ctx.log(f"花名册导出失败:{e}", "WARN") ctx.progress(30) # ---- 步骤2:调动台账导出(30% ~ 45%) ---- try: export_transfer_roster(output_path=transfer_path, session=session) except Exception as e: notes.append(f"调动台账导出失败(检查4 将受影响):{e}") ctx.log(f"调动台账导出失败:{e}", "WARN") ctx.progress(45) # ---- 步骤3:封存数据导出与预处理(45% ~ 60%) ---- ctx.progress(45) export_seal_data(month, save_dir) # 预读封存行到进程缓存(检查1~6 复用,避免各自重复读大文件) load_seal_rows(month, save_dir) ctx.progress(60) # ---- 步骤3.5:排班数据预处理(仅所选检查涉及排班时拉取;全部数据就绪后再执行检查) ---- need_shift = not checks or bool({"检查1", "检查2"} & set(checks)) if need_shift: try: earliest_map, staff_team = load_shift_data(month) config.set_shift_data(earliest_map, staff_team) except Exception as e: notes.append(f"排班数据预处理失败(检查1 二次入职识别与检查2 将受影响):{e}") ctx.log(f"排班数据预处理失败:{e}", "WARN") ctx.progress(70) # ---- 步骤4:依次执行所选检查(70% ~ 90%) ---- result = run_all_checks(month, save_dir, roster_path, transfer_path, checks=checks) ctx.progress(90) # ---- 步骤5:单工作簿输出(90% ~ 100%) ---- save_path = write_workbook(result, save_dir) ctx.progress(100) detail = {it["no"]: len(it["rows"]) for it in result["results"]} summary = ("考勤月报封存检查完成({ym}),异常合计 {total} 人:{detail}。" "汇总工作簿:{path}").format( ym=result["ym"], total=result["total"], detail="、".join(f"{k}:{v}" for k, v in detail.items()), path=save_path) if notes: summary += "\n注意事项:\n- " + "\n- ".join(notes) return {"summary": summary, "notes": notes, "detail": detail, "path": save_path, "ym": result["ym"]} PLUGINS.append(KqSealCheckPlugin())