# -*- coding: utf-8 -*- """XQ0037 步骤4:改签流程「离职电子签办理」环节发起离职电子签。 处理 HR「改签离职电子签」流程已推进到「离职电子签办理」环节、 办理人含「人资专用号」且任务状态 WAITING/ACCEPTED 的记录: 1. 仅处理 step2 置位时登记的「待办工号列表.json」内工号(本批次作用域闸门; 人工发起的单子/其他批次人员不在列表,永不触碰) 2. 复用 XQ0035 step1_trial_period_change.initiate_resign_esign: msca getLastContract 取签署单位 → mscaCreateSign 发起 (个人 autoSign=0 + 企业 autoSign=1 双签,第三方 CA 承载,向员工发短信) 3. 幂等双保险:先查签署记录 is_initiate_sign(1=签署中/2=已完成, 非空即已发起,人工/此前发起均跳过);msca 报「已发起或已完成」也按已完成 跳过;其余发起失败写入「改签异常名单.xlsx」 4. 发起成功后保留在待办工号列表(移除职责在链路最后一步 step5 回写 AI 表完成时) 职责边界(与 XQ0035 一致,2026-10-09 用户澄清):发起即终点; 「离职电子签办理」任务的提交办结由 XQ0020 在员工 CA 签完后自动审核。 【插件版移植说明】本模块由影刀版原样移植为执行机插件 data 层(xq0037): - XQ0035 公共函数改从本插件内嵌副本(xq35_step1/xq35_step2)相对导入; - HR 会话改经底座注入(config.get_session -> ctx.get_session("hr")); - print 日志由插件入口统一改道 ctx.log 推送前端。 """ import os from .xq35_step2_sign_new_contract import ( # noqa: E402 HR_BASE_URL, _request_retry, query_process_tasks) from .xq35_step1_trial_period_change import initiate_resign_esign # noqa: E402 from .step2_transfer_initiate import _hr_session, load_pending_list # noqa: E402 from .step3_transfer_todo import ( # noqa: E402 WORKER_NAME, WORKFORM_FORM_ID, _log, get_business_data, list_running_workflows, write_abnormal) ACTIVITY_ESIGN = "离职电子签办理" # 试算开关:True=试算(只打印,不发起电子签);False=实跑发起 DRY_RUN = False # 电子签真实发起状态查询(XQ0020 同款接口;按流程 businessKey/main_id 查签署记录) SIGN_STATUS_URL = (f"{HR_BASE_URL}/RedseaPlatform/getContentLinkage/redApi/" "rf_hr_staff_contract_signature_select_sp_cq37du/WorkformEngine.mc") def query_sign_status(session, contract_id): """签署记录查询 -> {is_initiate_sign, sign_status_yg, sign_status_qy, file}。 该合同无电子签记录时服务端业务报错 -> 返回空 dict(按未发起处理)。 """ resp = _request_retry("POST", SIGN_STATUS_URL, session, data={"contract_id": contract_id, "inParamFormId": "", "colCode": "main_id"}) d = resp.json() result = d.get("result") if str(d.get("state")) != "1" or not isinstance(result, dict): return {} return result def find_esign_candidates(session, pending): """yield (flow, base_code, name):离职电子签办理环节、人资专用号待办、本批待办内。 待办列表外(人工单/其他批次)静默跳过;本环节无自己任务(未走到/已走过)跳过。 """ for flow in list_running_workflows(session): tasks = query_process_tasks(session, flow.get("processId")) mine = [t for t in tasks if str(t.get("activityName") or "") == ACTIVITY_ESIGN and str(t.get("participantName") or "") == WORKER_NAME] if not any(str(t.get("currentState") or "") in ("WAITING", "ACCEPTED") for t in mine): continue biz = get_business_data(session, flow.get("businessKey"), WORKFORM_FORM_ID) code = str(biz.get("base_code") or "").strip() name = str(biz.get("staff_name") or "").strip() if code not in pending: continue yield flow, code, name def process_one(session, flow, code, name, dry_run): title = str(flow.get("title") or "") status = query_sign_status(session, flow.get("businessKey")) if str(status.get("is_initiate_sign") or "") not in ("", "0"): _log(f"[跳过] {code} {name} | 电子签此前已发起(is_initiate_sign=" f"{status.get('is_initiate_sign')}),不重复发起") return "already" if dry_run: _log(f"[试算·不发起] {code} {name} | {title}") return "dry" ok, msg = initiate_resign_esign( session, {"base_code": code}, {"business_key": flow.get("businessKey"), "process_id": flow.get("processId")}) if ok and "此前已完成" in msg: _log(f"[跳过] {code} {name} | {msg}") return "already" if ok: _log(f"[发起] {code} {name} | {msg}") return "initiated" if "已发起或已完成" in msg: # msca 侧重复发起保护(签署记录可能滞后):按已完成处理,不算异常 _log(f"[跳过] {code} {name} | {msg}(按此前已发起处理)") return "already" _log(f"[失败] {code} {name} | {msg}") write_abnormal(code, name, title, "电子签发起失败", msg, dry_run) return "error" def main(dry_run=None): dry_run = DRY_RUN if dry_run is None else dry_run pending = load_pending_list() if not pending: _log("[待办] 待办工号列表为空(本批次无人待办),无事可做") return {"initiated": 0, "already": 0, "error": 0, "dry": 0} _log("[待办] 本批待办 {} 人:{}".format( len(pending), "、".join(f"{c}{n}" for c, n in sorted(pending.items())))) session = _hr_session() stats = {"initiated": 0, "already": 0, "error": 0, "dry": 0} for flow, code, name in find_esign_candidates(session, pending): stats[process_one(session, flow, code, name, dry_run)] += 1 _log(f"[汇总] 发起={stats['initiated']} 此前已完成={stats['already']} " f"失败={stats['error']} 试算={stats['dry']}" f"{'(试算模式,未发起)' if dry_run else ''}" f"(待办列表由 step5 回写 AI 表后移除)") return stats