XQ0001 排班异常识别 / XQ0002 雇主险增减操作 / XQ0012 ERP 付款申请 / XQ0013 社保费用申报 / XQ0042 抖音平台核对 / XQ0084 银行卡更新:逻辑与界面 bundle 更新(含各自版本号变更)
243 lines
11 KiB
Python
243 lines
11 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""XQ0006 EHR系统周考勤异常处理 插件入口。
|
||
|
||
流程编排(由影刀版 attendance_abnormal.py 移植,登录态改由底座注入):
|
||
1) 会话校验:底座注入 HR session → getCurUserInfo 校验(失效提示重新登录);
|
||
2) 拉取查询月份(默认 1 号查上月,否则当月)全部「未调整」考勤异常;
|
||
3) 导出 Excel 底稿(试算模式只做这一步);
|
||
4) 自动确认:范围=查询范围内全部数据(分批调用确认接口);
|
||
5) 钉钉推送(口径与影刀版一致):仅推送上周职能序列 + 推送范围单位
|
||
(默认总办/集团财经中心/金色湖畔事业部,部门路径不含「大区」)的严重异常给当事人,
|
||
并把条数汇总推送到群机器人。
|
||
"""
|
||
from webhost.backend.plugins.base import ScenePlugin, PLUGINS
|
||
|
||
from . import config
|
||
from .data import hr_api
|
||
from .data.hr_api import HRSessionExpiredError
|
||
from .logic import attendance_rules as rules
|
||
from .report import attendance_report
|
||
from .report import dingtalk_push
|
||
|
||
|
||
def _as_bool(value, default=False):
|
||
"""字段值转布尔(兼容 JSON 布尔与 "true"/"false" 字符串)。"""
|
||
if value is None or value == "":
|
||
return default
|
||
if isinstance(value, bool):
|
||
return value
|
||
return str(value).strip().lower() in ("1", "true", "yes", "on")
|
||
|
||
|
||
def _split_units(value):
|
||
"""把「总办, 集团财经中心」这类多值文本拆成单位列表(支持中英文逗号/顿号/分号)。"""
|
||
raw = str(value or "").strip()
|
||
if not raw:
|
||
return []
|
||
for sep in (",", "、", ";", ";", "|", "\n"):
|
||
raw = raw.replace(sep, ",")
|
||
return [s.strip() for s in raw.split(",") if s.strip()]
|
||
|
||
|
||
class WeeklyAttendanceAbnormalPlugin(ScenePlugin):
|
||
"""EHR系统周考勤异常处理(需求 XQ0006)。"""
|
||
|
||
id = "xq0006"
|
||
name = "EHR系统周考勤异常处理"
|
||
version = "1.2.0"
|
||
description = ("拉取查询月份未调整考勤异常并导出 Excel,整月全量自动确认;"
|
||
"上周职能序列及推送范围单位(默认总办/集团财经中心/金色湖畔事业部/"
|
||
"集团管理中心/集团营建中心/金色印象事业部,不含大区)的严重异常按人合并推送;"
|
||
"支持试算模式(只出表、不确认不推送)。")
|
||
platform = "hr"
|
||
uses_month = True
|
||
capabilities = ["xq0006"]
|
||
permissions = {"platforms": ["hr"]}
|
||
fields = {
|
||
"ref_month": {
|
||
"type": "string", "title": "执行月份",
|
||
"default": "",
|
||
"description": "留空=本月(1 号查上月);格式 YYYY-MM(如 2026-11)",
|
||
},
|
||
"save_dir": {
|
||
"type": "string", "title": "输出目录",
|
||
"default": config.DEFAULT_SAVE_DIR,
|
||
"description": "考勤异常 Excel 底稿存放目录",
|
||
},
|
||
"dry_run": {
|
||
"type": "boolean", "title": "试算模式", "default": False,
|
||
"description": "开启后只导出 Excel 并统计,不自动确认、不发送任何钉钉消息",
|
||
},
|
||
"auto_confirm": {
|
||
"type": "boolean", "title": "自动确认", "default": True,
|
||
"description": "开启后对查询范围内全部异常调用确认接口(分批提交);试算模式下强制不确认",
|
||
},
|
||
"push_enabled": {
|
||
"type": "boolean", "title": "钉钉推送", "default": True,
|
||
"description": "开启后推送上周职能序列及推送范围单位的严重异常给当事人,并推送条数汇总到钉钉群;试算模式下强制不推送",
|
||
},
|
||
"push_units": {
|
||
"type": "string", "title": "推送范围单位",
|
||
"default": "总办,集团财经中心,金色湖畔事业部,集团管理中心,集团营建中心,金色印象事业部",
|
||
"description": "除职能序列外,这些所属单位也推送(逗号/顿号分隔);留空=只推职能序列",
|
||
},
|
||
"push_exclude_keyword": {
|
||
"type": "string", "title": "推送排除关键字", "default": "大区",
|
||
"description": "部门路径包含该关键字则不推送;留空表示不按部门路径排除",
|
||
},
|
||
"dingtalk_webhook": {
|
||
"type": "string", "title": "钉钉 Webhook", "default": "",
|
||
"description": "留空使用内置默认机器人;需要发到其他群时填写完整 webhook 地址",
|
||
},
|
||
"dingtalk_secret": {
|
||
"type": "string", "title": "钉钉加签密钥", "default": "",
|
||
"description": "机器人启用加签安全设置时填写,留空表示不加签",
|
||
},
|
||
}
|
||
|
||
def run(self, fields: dict, ctx):
|
||
try:
|
||
return self._run(fields, ctx)
|
||
except HRSessionExpiredError as e:
|
||
ctx.log(f"HR 登录态已失效,请重新登录后再执行:{e}", "ERROR")
|
||
raise RuntimeError("HR 登录态已失效,请重新登录 HR 后再执行") from e
|
||
|
||
def _run(self, fields: dict, ctx):
|
||
# ---- 注入运行上下文 ----
|
||
config.set_log(ctx.log)
|
||
config.set_progress(ctx.progress)
|
||
config.set_session(ctx.get_session("hr"))
|
||
session = config.require_session()
|
||
save_dir = (fields.get("save_dir") or "").strip() or config.DEFAULT_SAVE_DIR
|
||
auto_confirm = _as_bool(fields.get("auto_confirm"), True)
|
||
push_enabled = _as_bool(fields.get("push_enabled"), True)
|
||
dry_run = _as_bool(fields.get("dry_run"), config.DEFAULT_DRY_RUN)
|
||
if dry_run: # 试算:只导出,不产生任何写入/外发动作
|
||
auto_confirm, push_enabled = False, False
|
||
push_units = _split_units(fields.get("push_units"))
|
||
if fields.get("push_units") is None:
|
||
push_units = config.DEFAULT_PUSH_EXTRA_UNITS
|
||
push_exclude = (fields.get("push_exclude_keyword") or "").strip()
|
||
if fields.get("push_exclude_keyword") is None:
|
||
push_exclude = config.DEFAULT_PUSH_EXCLUDE_KEYWORD
|
||
webhook = (fields.get("dingtalk_webhook") or "").strip()
|
||
secret = (fields.get("dingtalk_secret") or "").strip()
|
||
|
||
try:
|
||
month = rules.resolve_month(fields.get("ref_month"))
|
||
except ValueError as e:
|
||
raise RuntimeError(str(e)) from e
|
||
begin, end = rules.month_range(month)
|
||
ctx.log(f"===== 周考勤异常处理启动:{month} 整月({begin} ~ {end}),"
|
||
f"试算模式={'是(只导出)' if dry_run else '否'},"
|
||
f"自动确认={'开(全量)' if auto_confirm else '关'},"
|
||
f"钉钉推送={'开' if push_enabled else '关'} =====", "INFO")
|
||
|
||
# ---- 步骤1:会话校验 ----
|
||
ctx.progress(5)
|
||
user_id, treecode = hr_api.validate_session(session)
|
||
config.set_user_id(user_id)
|
||
ctx.progress(8)
|
||
|
||
# ---- 步骤2:拉取整月考勤异常 ----
|
||
data = hr_api.fetch_all_attendance_abnormal(session, user_id, treecode, begin, end)
|
||
ctx.log(f"考勤异常共获取 {len(data)} 条(调整状态:{config.IS_ADJUSTED or '所有'})", "INFO")
|
||
if not data:
|
||
ctx.log(f"{month} 无考勤异常数据,跳过导出与推送", "INFO")
|
||
ctx.progress(100)
|
||
return {
|
||
"summary": f"{month} 无考勤异常数据,未导出、未确认、未推送",
|
||
"detail": {"total": 0},
|
||
"path": "",
|
||
"save_dir": save_dir,
|
||
}
|
||
ctx.progress(30)
|
||
|
||
# ---- 步骤3:导出 Excel 底稿 ----
|
||
path = attendance_report.export_excel(data, save_dir, month, dry_run=dry_run)
|
||
ctx.progress(35)
|
||
|
||
# ---- 步骤4:自动确认(整月全量) ----
|
||
split = rules.split_data(data, push_units, push_exclude)
|
||
auto_count = len([it for it in data if it.get("abnormal_id")])
|
||
confirmed_count, failed_ids = 0, []
|
||
if auto_confirm:
|
||
ctx.log(f"自动确认(整月全量){auto_count} 条 …", "INFO")
|
||
_, confirmed_count, failed_ids = hr_api.auto_confirm_all(
|
||
session, data,
|
||
on_progress=lambda done, total: ctx.progress(
|
||
35 + int(35 * done / max(total, 1))))
|
||
else:
|
||
if dry_run:
|
||
ctx.log(f"试算模式:仅统计待确认 {auto_count} 条,未执行确认", "INFO")
|
||
else:
|
||
ctx.log(f"自动确认未启用,仅统计待确认 {auto_count} 条,未执行确认", "WARN")
|
||
ctx.progress(72)
|
||
|
||
# ---- 步骤5:钉钉推送(只通知,不改口径) ----
|
||
messages = rules.group_messages(split["func_list"])
|
||
push_result = {"sent": 0, "failed": []}
|
||
if push_enabled:
|
||
if messages:
|
||
push_result = dingtalk_push.push_person_messages(
|
||
messages,
|
||
on_progress=lambda done, total: ctx.progress(
|
||
72 + int(15 * done / max(total, 1))))
|
||
else:
|
||
ctx.log("无需推送的异常,跳过当事人钉钉通知", "INFO")
|
||
else:
|
||
if dry_run:
|
||
ctx.log("试算模式:未发送任何钉钉消息", "INFO")
|
||
else:
|
||
ctx.log("钉钉推送未启用,未发送任何钉钉消息", "INFO")
|
||
ctx.progress(90)
|
||
|
||
# ---- 步骤6:汇总日志 + 群条数汇总 ----
|
||
group_message = rules.build_group_summary(
|
||
len(data), auto_count, confirmed_count, len(split["non_func_list"]),
|
||
len(split["func_list"]), len(messages), len(split["other_list"]),
|
||
month, begin, end)
|
||
group_sent = False
|
||
if push_enabled:
|
||
group_sent = dingtalk_push.send_group_text(
|
||
group_message, webhook=webhook, secret=secret)
|
||
|
||
ctx.log("=" * 60, "INFO")
|
||
ctx.log(group_message, "INFO")
|
||
ctx.log(f"导出文件:{path}", "INFO")
|
||
if failed_ids:
|
||
ctx.log(f"自动确认失败 {len(failed_ids)} 条:{','.join(str(i) for i in failed_ids[:20])}", "WARN")
|
||
if push_result["failed"]:
|
||
ctx.log(f"当事人推送失败 {len(push_result['failed'])} 人:"
|
||
f"{';'.join(push_result['failed'][:10])}", "WARN")
|
||
ctx.log("=" * 60, "INFO")
|
||
|
||
counts = {
|
||
"total": len(data),
|
||
"auto_confirm_total": auto_count,
|
||
"confirmed": confirmed_count,
|
||
"confirm_failed": len(failed_ids),
|
||
"func_lastweek": len(split["func_list"]),
|
||
"non_func_lastweek": len(split["non_func_list"]),
|
||
"other": len(split["other_list"]),
|
||
"push_people": len(messages),
|
||
"push_sent": push_result["sent"],
|
||
"push_failed": len(push_result["failed"]),
|
||
"group_pushed": group_sent,
|
||
}
|
||
summary = f"{month} 周考勤异常处理完成:共 {counts['total']} 条"
|
||
if dry_run:
|
||
summary += ",试算模式(仅导出,未确认、未推送)"
|
||
elif auto_confirm:
|
||
summary += f",自动确认 {confirmed_count}/{auto_count} 条"
|
||
else:
|
||
summary += ",自动确认已关闭"
|
||
if push_enabled:
|
||
summary += f",推送 {counts['push_people']} 人(成功 {counts['push_sent']})"
|
||
summary += f";导出:{path}"
|
||
ctx.progress(100)
|
||
return {"summary": summary, "detail": counts, "path": path, "save_dir": save_dir}
|
||
|
||
|
||
PLUGINS.append(WeeklyAttendanceAbnormalPlugin())
|