feat(xq0002): 新增HR自动导入功能,优化花名册源表新鲜度校验

```

或者更贴合中文项目习惯的表述:
```msg
feat(xq0002): 新增HR自动导入,优化花名册源表新鲜度校验
This commit is contained in:
2026-09-29 13:54:32 +08:00
parent 701aa2a2cf
commit bf4f72cb01
58 changed files with 5080 additions and 694 deletions
@@ -2,23 +2,25 @@
"""XQ0002 雇主险增减操作 插件入口。
流程编排(替代影刀版 4 个脚本的人工串行,WPS COM 替代人工 Ctrl+S 刷新公式):
1) 花名册确保:源表缺失且开启 auto_export 时自动从 HR 接口导出(TTL 15 分钟复用);
1) 花名册确保:源表缺失或超过 TTL(15 分钟)时自动从 HR 接口导出,新鲜度内复用;
2) 筛选复制:按类别 include/exclude 过滤花名册数据写入各目标表(F列工号强制文本);
3) WPS/Excel 后台重算公式(替代人工打开文件 Ctrl+S,供调度读取公式缓存值);
4) 三阶段名额调度:更新「手动维护-在参名单」+ 精细更新「变更清单」+ 备份;
5) 再次重算公式(刷新变更清单/在参名单公式列缓存);
6) 生成增员/减员导入文件 + 导出变更清单独立工作簿(公式转值)。
6) 生成增员/减员导入文件 + 导出变更清单独立工作簿(公式转值);
7) HR 自动导入(可选开关,默认开启):浏览器自动化把调度生成的导入文件导入
HR「待增减人员」(先全部减员再全部增员,仅导入本次调度成功的类别)。
运行上下文:底座注入 HR 会话与 userId(config 全局钩子),平台 = hr,uses_month = False。
字段(fields)由 plugin.json 权威定义:base_dir / ref_date / categories / roster_path /
auto_export / enable_validation / wps_recalc / export_change_sheet / gen_import_files。
enable_validation / wps_recalc / export_change_sheet / gen_import_files / import_to_hr。
"""
import os
from webhost.backend.plugins.base import ScenePlugin, PLUGINS
from . import config
from .logic import roster_filter, schedule
from .logic import roster_filter, schedule, hr_import
from .logic.wps_recalc import recalc_target, ComRecalcError
from .report import output
@@ -28,10 +30,10 @@ class EmployerInsurancePlugin(ScenePlugin):
id = "xq0002"
name = "雇主险增减操作"
version = "1.0.1"
version = "1.0.4"
description = ("雇主险名额调度全流程:花名册导出→筛选复制→WPS/Excel 后台重算公式→"
"三阶段名额调度→更新在参名单与变更清单→生成增员/减员导入文件,"
"替代人工 Ctrl+S 刷新公式环节")
"三阶段名额调度→更新在参名单与变更清单→生成增员/减员导入文件"
"(可选自动导入 HR 系统),替代人工 Ctrl+S 刷新公式与人工上传导入环节")
platform = "hr"
uses_month = False
capabilities = ["xq0002"]
@@ -55,11 +57,14 @@ class EmployerInsurancePlugin(ScenePlugin):
cat_names = fields.get("categories") or []
categories = [config.get_category(n) for n in cat_names] if cat_names else list(config.CATEGORIES)
roster_path = (fields.get("roster_path") or "").strip()
auto_export = bool(fields.get("auto_export", True))
enable_validation = bool(fields.get("enable_validation", True))
wps_recalc = bool(fields.get("wps_recalc", True))
export_change_sheet = bool(fields.get("export_change_sheet", True))
gen_import_files = bool(fields.get("gen_import_files", True))
import_to_hr = bool(fields.get("import_to_hr", True))
import_on = import_to_hr and gen_import_files
if import_to_hr and not gen_import_files:
ctx.log("已开启「自动导入HR系统」但「生成导入文件」未开启,本次无可导入文件,导入步骤将跳过", "WARN")
if not categories:
raise ValueError("未选择任何类别")
@@ -70,7 +75,7 @@ class EmployerInsurancePlugin(ScenePlugin):
# ---- 步骤1:花名册确保(5% ~ 10%) ----
ctx.progress(5)
try:
roster_path = roster_filter.ensure_employee_file(roster_path, auto_export, session)
roster_path = roster_filter.ensure_employee_file(roster_path, session)
ctx.log(f"源表就绪:{roster_path}", "INFO")
except Exception as e:
notes.append(f"花名册准备失败(筛选复制与调度将受影响):{e}")
@@ -109,10 +114,15 @@ class EmployerInsurancePlugin(ScenePlugin):
to_schedule = ok_categories
ctx.progress(60)
# ---- 步骤4:三阶段名额调度(60% ~ 88%) ----
# ---- 步骤4:三阶段名额调度 ----
# 进度边界:开启 HR 导入时压缩为 60~80(步骤5/6/7 依次 80~84 / 84~90 / 90~100),
# 关闭时沿用原 60~88 / 88~95 / 95~100
s4_end, s4_span = (80, 20) if import_on else (88, 28)
s5_end, s5_span = (84, 4) if import_on else (95, 7)
s6_end, s6_span = (90, 6) if import_on else (100, 5)
scheduled = []
for i, category in enumerate(to_schedule):
ctx.progress(60 + int(28 * i / max(len(to_schedule), 1)))
ctx.progress(60 + int(s4_span * i / max(len(to_schedule), 1)))
try:
schedule.schedule_category(category,
enable_validation=enable_validation,
@@ -124,28 +134,48 @@ class EmployerInsurancePlugin(ScenePlugin):
except Exception as e:
notes.append(f"类别[{category['name']}] 调度失败:{e}")
ctx.log(f"类别[{category['name']}] 调度失败:{e}", "WARN")
ctx.progress(88)
ctx.progress(s4_end)
# ---- 步骤5:再次重算公式(88% ~ 95%,刷新变更清单/在参名单公式列缓存) ----
# ---- 步骤5:再次重算公式(刷新变更清单/在参名单公式列缓存) ----
if wps_recalc:
for i, category in enumerate(scheduled):
ctx.progress(88 + int(7 * i / max(len(scheduled), 1)))
ctx.progress(s5_end - s5_span + int(s5_span * i / max(len(scheduled), 1)))
try:
recalc_target(config.category_target_file(category))
except ComRecalcError as e:
notes.append(f"类别[{category['name']}] 调度后重算失败(变更清单独立工作簿将取不到公式值):{e}")
ctx.log(f"类别[{category['name']}] 调度后重算失败:{e}", "WARN")
ctx.progress(95)
ctx.progress(s5_end)
# ---- 步骤6:导出变更清单独立工作簿(95% ~ 100%) ----
# ---- 步骤6:导出变更清单独立工作簿 ----
if export_change_sheet:
for i, category in enumerate(scheduled):
ctx.progress(95 + int(5 * i / max(len(scheduled), 1)))
ctx.progress(s6_end - s6_span + int(s6_span * i / max(len(scheduled), 1)))
try:
output.export_change_sheet_workbook(category)
except Exception as e:
notes.append(f"类别[{category['name']}] 导出变更清单工作簿失败:{e}")
ctx.log(f"类别[{category['name']}] 导出变更清单工作簿失败:{e}", "WARN")
ctx.progress(s6_end)
# ---- 步骤7:HR 自动导入(90% ~ 100%,仅本次调度成功的类别;先全部减员再全部增员) ----
import_results = []
if import_on:
if not scheduled:
ctx.log("无调度成功的类别,跳过 HR 自动导入", "WARN")
else:
def _on_import_progress(done, total):
ctx.progress(90 + int(10 * done / max(total, 1)))
ctx.log(f"开始 HR 自动导入:先全部【减员】后【增员】,共 {len(scheduled) * 2} 个导入单元")
try:
import_results = hr_import.import_all(scheduled, on_progress=_on_import_progress)
except hr_import.LoginRequiredError as e:
notes.append(f"HR 自动导入未执行(登录态缺失):{e}")
ctx.log(f"HR 自动导入未执行:{e}", "ERROR")
except Exception as e:
notes.append(f"HR 自动导入整体失败:{e}")
ctx.log(f"HR 自动导入整体失败:{e}", "ERROR")
ctx.progress(100)
# ---- 汇总 ----
@@ -154,11 +184,21 @@ class EmployerInsurancePlugin(ScenePlugin):
"筛选复制成功": len(ok_categories),
"调度成功": len(scheduled),
}
if import_on:
if import_results:
import_lines = hr_import.summarize_results(import_results)
import_text = ";".join(import_lines) if import_lines else "无导入结果"
else:
import_text = "未执行(登录态缺失或整体失败,详见注意事项与日志)"
tail = ("增/减员导入文件与变更清单独立工作簿已生成在各类别文件夹。"
f"HR 导入结果:{import_text}。")
else:
tail = ("增/减员导入文件与变更清单独立工作簿已生成在各类别文件夹,"
"请先执行【减员】导入再执行【增员】导入。")
summary = ("雇主险增减操作完成({date}):处理 {total} 个类别,筛选复制 {filtered} 个、"
"调度成功 {scheduled} 个。增/减员导入文件与变更清单独立工作簿已生成在各类别文件夹,"
"请先执行【减员】导入再执行【增员】导入。").format(
"调度成功 {scheduled} 个。{tail}").format(
date=config.today_date(), total=len(categories),
filtered=len(ok_categories), scheduled=len(scheduled))
filtered=len(ok_categories), scheduled=len(scheduled), tail=tail)
if notes:
summary += "\n注意事项:\n- " + "\n- ".join(notes)
return {"summary": summary, "notes": notes, "detail": detail,