feat(plugin): XQ0084 0.1.1 插件界面 + 核对通过自动导入 HR 工资卡并回写台账
- 新增 ui-src(Vue3+Element Plus,v3 规范)与构建产物 ui/,plugin.json 开启 ui - 接入 hr_sdk.wage_import:核对通过记录按身份证匹配导入工资卡,成功后回写「是否更新HR系统=是」 - 委托发放/缺开户银行记录跳过并注明原因,现金发放仍不导入;导入失败不中断出表与推送 - 工作簿新增 HR导入/说明列,推送正文补充导入统计;_pack 排除 ui-src 并校验 ui/index.html
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""XQ0084 流程编排:拉取待处理行 → 下载影像 → 识别 → 判定 → 输出/提醒。"""
|
||||
"""XQ0084 流程编排:拉取待处理行 → 下载影像 → 识别 → 判定 → 导入 HR → 回写/输出/提醒。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
@@ -11,9 +11,10 @@ from ..report.notify import send_markdown
|
||||
from ..report.workbook import write_workbook
|
||||
from .recognize import recognize
|
||||
from .verify import verify
|
||||
from .wage import import_passed
|
||||
|
||||
|
||||
def _push_text(rows, stats, path):
|
||||
def _push_text(rows, stats, path, hr=None):
|
||||
lines = [
|
||||
"### 银行卡变更影像核对(XQ0084)",
|
||||
f"- 待处理:**{stats['total']}** 条;通过 **{stats['passed']}**、"
|
||||
@@ -24,6 +25,9 @@ def _push_text(rows, stats, path):
|
||||
stats["trigger"][config.F_REG_DATE],
|
||||
),
|
||||
]
|
||||
if hr and hr.get("enabled"):
|
||||
lines.append(f"- HR 工资卡导入:成功 **{hr['imported']}**、失败 **{hr['failed']}**、"
|
||||
f"跳过 **{hr['skipped']}**;台账已回写 **{hr['written']}** 条")
|
||||
manual = [r for r in rows if r["conclusion"] == "需人工核实"]
|
||||
if manual:
|
||||
lines.append(f"#### 需人工核实({len(manual)})")
|
||||
@@ -31,12 +35,20 @@ def _push_text(rows, stats, path):
|
||||
lines.append(f"- {r['holder']}({r['emp_no']}):{r['notes'][:120]}")
|
||||
if len(manual) > 20:
|
||||
lines.append(f"- …… 其余 {len(manual) - 20} 条见结果文件")
|
||||
failed = [r for r in rows if r.get("hr_status") == "导入失败"]
|
||||
if failed:
|
||||
lines.append(f"#### HR 导入失败({len(failed)})")
|
||||
for r in failed[:10]:
|
||||
lines.append(f"- {r['holder']}({r['emp_no']}):{r.get('hr_note', '')[:120]}")
|
||||
if len(failed) > 10:
|
||||
lines.append(f"- …… 其余 {len(failed) - 10} 条见结果文件")
|
||||
lines.append(f"- 结果文件:{path}")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def run_verify(save_dir, keep_files=config.DEFAULT_KEEP_FILES, push_webhook="",
|
||||
push_secret="", limit_rows=0, progress=None):
|
||||
push_secret="", limit_rows=0, write_hr=False, hr_session=None,
|
||||
progress=None):
|
||||
p = progress or (lambda _pct: None)
|
||||
stamp = time.strftime("%Y%m%d_%H%M%S")
|
||||
image_batch = os.path.join(save_dir, config.IMAGE_DIR_NAME, stamp)
|
||||
@@ -52,6 +64,7 @@ def run_verify(save_dir, keep_files=config.DEFAULT_KEEP_FILES, push_webhook="",
|
||||
config.logger.info("按配置仅处理前 %d 条", len(items))
|
||||
if not items:
|
||||
return {"path": "", "rows": [], "stats": _stats([], []),
|
||||
"hr": {"enabled": bool(write_hr)},
|
||||
"push": {"success": False, "message": "无待处理记录"},
|
||||
"cleaned": [], "batch_dir": image_batch}
|
||||
|
||||
@@ -78,19 +91,30 @@ def run_verify(save_dir, keep_files=config.DEFAULT_KEEP_FILES, push_webhook="",
|
||||
p(25 + int(65 * (i + 1) / len(items)))
|
||||
|
||||
stats = _stats(rows, items)
|
||||
|
||||
# 3) 闭环:核对通过 → 导入 HR 工资卡 → 回写 AI 表格(关闭时只核对出表)
|
||||
hr = {"enabled": bool(write_hr)}
|
||||
if write_hr:
|
||||
if not hr_session:
|
||||
raise RuntimeError("未获取到 HR 登录态,无法导入工资卡;请先在壳子登录 HR")
|
||||
p(88)
|
||||
hr = import_passed(rows, hr_session, progress=p)
|
||||
hr["enabled"] = True
|
||||
|
||||
path = write_workbook(rows, config.build_result_path(save_dir))
|
||||
p(94)
|
||||
p(98)
|
||||
|
||||
cleaned = config.cleanup_old_results(save_dir, keep_files, protect=path)
|
||||
config.cleanup_old_images(os.path.join(save_dir, config.IMAGE_DIR_NAME), keep_files,
|
||||
protect=image_batch)
|
||||
|
||||
abnormal = bool(stats["manual"] or any(r.get("hr_status") == "导入失败" for r in rows))
|
||||
push = {"success": False, "message": "无需推送(无异常)"}
|
||||
if stats["manual"] and push_webhook:
|
||||
if abnormal and push_webhook:
|
||||
push = send_markdown(push_webhook, push_secret, "银行卡变更影像核对异常",
|
||||
_push_text(rows, stats, path))
|
||||
_push_text(rows, stats, path, hr))
|
||||
p(100)
|
||||
return {"path": path, "rows": rows, "stats": stats, "push": push,
|
||||
return {"path": path, "rows": rows, "stats": stats, "hr": hr, "push": push,
|
||||
"cleaned": cleaned, "batch_dir": image_batch}
|
||||
|
||||
|
||||
|
||||
@@ -79,11 +79,15 @@ def verify(item, ocr):
|
||||
done = config.text(row.get(config.F_DONE))
|
||||
|
||||
base = {
|
||||
"record_id": item.get("record_id") or "",
|
||||
"emp_no": config.text(row.get(config.F_EMP_NO)),
|
||||
"emp_name": config.text(row.get(config.F_EMP_NAME)),
|
||||
"holder": holder or config.text(row.get(config.F_EMP_NAME)),
|
||||
"trust": config.text(row.get(config.F_TRUST)),
|
||||
"reason": reason,
|
||||
"triggers": "、".join(item.get("triggers") or []),
|
||||
"table_bank": table_bank,
|
||||
"table_branch": config.text(row.get(config.F_BRANCH)),
|
||||
"table_card": table_card,
|
||||
"table_id": table_id,
|
||||
"ocr_id": "",
|
||||
@@ -95,6 +99,8 @@ def verify(item, ocr):
|
||||
"card_status": "-",
|
||||
"bank_status": "-",
|
||||
"self_check": "",
|
||||
"hr_status": "",
|
||||
"hr_note": "",
|
||||
"conclusion": "",
|
||||
"notes": "",
|
||||
"image_dir": item.get("image_dir") or "",
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""XQ0084 闭环层:核对通过记录导入 HR 工资卡,成功后回写 AI 表格。
|
||||
|
||||
调用链:``hr_sdk.wage_import.import_cards``(浏览器上下文 + WS 读结果)→
|
||||
按服务端 ``errorList.line`` 回填每条记录的导入结果 →
|
||||
对导入成功的记录把 AI 表格「是否更新HR系统」回写为「是」。
|
||||
|
||||
SDK 懒加载:hr_sdk 由执行机按 plugin.json.requires.sdks 挂载,不在模块顶层 import。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from .. import config
|
||||
|
||||
|
||||
def _log(msg, level="INFO"):
|
||||
config.log(msg, level)
|
||||
|
||||
|
||||
def build_wage_rows(rows):
|
||||
"""挑出可自动导入的记录并生成模板数据行。
|
||||
|
||||
Returns:
|
||||
(targets, data_rows):targets 与 data_rows 一一对应;
|
||||
不可自动导入的记录(委托发放/缺字段)已就地写入 hr_status 说明。
|
||||
"""
|
||||
targets, data_rows = [], []
|
||||
for r in rows:
|
||||
if r.get("conclusion") != "通过":
|
||||
continue
|
||||
if r.get("trust") == config.HR_TRUST_YES:
|
||||
r["hr_status"] = "跳过"
|
||||
r["hr_note"] = "委托发放,工资卡开户人与员工不一致,需人工在 HR 维护"
|
||||
continue
|
||||
if not (r.get("table_bank") and r.get("table_card") and r.get("table_id")):
|
||||
r["hr_status"] = "跳过"
|
||||
r["hr_note"] = "缺开户银行/银行卡号/身份证号,无法自动导入"
|
||||
continue
|
||||
data_rows.append([
|
||||
r.get("emp_name") or r.get("holder"), # *姓名(员工)
|
||||
r.get("table_id"), # *身份证号
|
||||
r.get("holder"), # *开户人姓名
|
||||
config.WAGE_IS_MAIN, # *是否主卡
|
||||
r.get("table_id"), # *开户人身份证号
|
||||
config.WAGE_CARD_TYPE, # 银行卡类型
|
||||
r.get("table_bank"), # *开卡银行
|
||||
r.get("table_branch") or "", # 开户支行
|
||||
r.get("table_card"), # *银行卡号
|
||||
config.WAGE_REMARK, # 备注
|
||||
])
|
||||
targets.append(r)
|
||||
return targets, data_rows
|
||||
|
||||
|
||||
def import_passed(rows, session, progress=None):
|
||||
"""把核对通过记录导入 HR 工资卡,结果逐条写回 ``rows`` 的 hr_status/hr_note。"""
|
||||
p = progress or (lambda _pct: None)
|
||||
targets, data_rows = build_wage_rows(rows)
|
||||
stats = {"imported": 0, "failed": 0, "skipped": 0,
|
||||
"written": 0, "message": ""}
|
||||
stats["skipped"] = sum(1 for r in rows if r.get("hr_status") == "跳过")
|
||||
if not targets:
|
||||
stats["message"] = "无核对通过记录,跳过 HR 导入"
|
||||
_log("[HR导入] " + stats["message"], "WARNING")
|
||||
return stats
|
||||
|
||||
_log(f"[HR导入] 开始导入 {len(targets)} 条工资卡(按身份证匹配)")
|
||||
try:
|
||||
from hr_sdk import wage_import # SDK 下发,懒加载
|
||||
res = wage_import.import_cards(
|
||||
data_rows, session=session, fit_type=config.WAGE_FIT_TYPE,
|
||||
timeout=config.WAGE_IMPORT_TIMEOUT, log=lambda m: _log(f"[HR导入] {m}"))
|
||||
except Exception as e: # noqa: BLE001 导入失败不中断出表
|
||||
_log(f"[HR导入] 导入失败:{e}", "ERROR")
|
||||
for r in targets:
|
||||
r["hr_status"] = "导入失败"
|
||||
r["hr_note"] = f"HR 导入失败:{e}"
|
||||
stats["failed"] = len(targets)
|
||||
stats["message"] = f"HR 导入失败:{e}"
|
||||
return stats
|
||||
p(94)
|
||||
|
||||
errors = {}
|
||||
for item in res.get("errorList") or []:
|
||||
try:
|
||||
errors[int(item.get("line") or 0)] = str(item.get("meg") or item.get("msg") or "")
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
for idx, r in enumerate(targets):
|
||||
line = idx + 2 # 模板第 1 行为表头
|
||||
if not errors and res.get("result") == 1:
|
||||
r["hr_status"] = "已导入"
|
||||
r["hr_note"] = "HR 工资卡导入成功"
|
||||
elif line in errors:
|
||||
r["hr_status"] = "导入失败"
|
||||
r["hr_note"] = errors[line] or "HR 返回导入失败"
|
||||
elif errors:
|
||||
# 服务端有失败行但未命中本行:按成功处理,仍需人工复核导入结果
|
||||
r["hr_status"] = "已导入"
|
||||
r["hr_note"] = "未在错误列表命中,请复核 HR 工资卡"
|
||||
else:
|
||||
r["hr_status"] = "导入失败"
|
||||
r["hr_note"] = f"HR 返回 result={res.get('result')},未成功"
|
||||
stats["imported"] = sum(1 for r in targets if r["hr_status"] == "已导入")
|
||||
stats["failed"] = len(targets) - stats["imported"]
|
||||
_log(f"[HR导入] 完成:成功 {stats['imported']}、失败 {stats['failed']}、"
|
||||
f"跳过 {stats['skipped']}(服务端 total={res.get('total')} "
|
||||
f"successNum={res.get('successNum')} errorNum={res.get('errorNum')})")
|
||||
|
||||
stats["written"] = write_back(targets)
|
||||
return stats
|
||||
|
||||
|
||||
def write_back(rows):
|
||||
"""把已导入成功的记录回写 AI 表格「是否更新HR系统=是」。"""
|
||||
done = 0
|
||||
pending = [r for r in rows if r.get("hr_status") == "已导入" and r.get("record_id")]
|
||||
if not pending:
|
||||
return 0
|
||||
from ..data.ai_table import open_client # 复用数据层凭证与客户端
|
||||
try:
|
||||
with open_client() as cli:
|
||||
for r in pending:
|
||||
try:
|
||||
cli.update_fields(config.AI_BASE, config.AI_SHEET, r["record_id"],
|
||||
{config.F_DONE: "是"})
|
||||
r["hr_note"] = f"{r['hr_note']};台账已标记已更新HR系统"
|
||||
done += 1
|
||||
except Exception as e: # noqa: BLE001 单条失败不影响其它
|
||||
r["hr_note"] = f"{r['hr_note']};台账回写失败:{e}"
|
||||
_log(f"[回写] {r.get('emp_no')} {r.get('holder')} 回写失败:{e}", "ERROR")
|
||||
except Exception as e: # noqa: BLE001
|
||||
_log(f"[回写] 打开 AI 表格失败:{e}", "ERROR")
|
||||
return 0
|
||||
_log(f"[回写] 已回写 {done}/{len(pending)} 条「{config.F_DONE}=是」")
|
||||
return done
|
||||
Reference in New Issue
Block a user