feat(xq0037): 升级调动改签离职电子签流程优化数据范围与逻辑

- 调整台账导出范围为近三月、筛选时间为近4天
- 将名单表从「本月发起名单」统一改为「已发起名单」
- 新增三类人员群提醒、分页查询流程优化
- 修复单位名称匹配逻辑,新增选项集获取
- 替换COM写入为openpyxl避免时区问题
This commit is contained in:
2026-10-21 08:45:55 +08:00
parent c0fd432fc8
commit b76e703dba
11 changed files with 1708 additions and 1052 deletions
@@ -2,7 +2,7 @@
"""XQ0037 步骤5:HR 已发起工号回写钉钉 AI 表格「改签离职电子签跟进-日更新」(新增行)。
链路最后一步,与 step2/3/4 同一作用域闸门:仅处理「待办工号列表.json」内工号
(step2 置位时登记,step4 发起后保留),字段从当天推送表「本月发起名单」取。
(step2 置位时登记,step4 发起后保留),字段从当天推送表「已发起名单」取。
发起成功以 HR 系统为准:
1. HR 核查:遍历运行中「改签离职电子签」流程按表单 base_code 匹配工号,
再按流程主键查签署记录,is_initiate_sign 非空非 0 即已发起
@@ -14,7 +14,7 @@
4. 新增行:姓名/工号/调动时间/现单位名称/现岗位/现技师工号/原单位名称/
原岗位/原技师工号 + 是否已发起改签离职=是 + 发起时间=当天;
5. 检测到已发起(AI 表已有本笔调动记录,或本次回写成功)-> 回填推送表
「本月发起名单」对应行「发起日期」列(openpyxl 直写,试算模式不写);
「已发起名单」对应行「发起日期」列(AI/35,openpyxl 直写,试算模式不写);
6. 回写完成(新增成功或 AI 表已有)-> 从待办工号列表移除,列表清空即链路收尾。
字段映射注意:AI 表「现单位名称/原单位名称」是 singleSelect 且选项为会馆名
@@ -41,7 +41,7 @@ import openpyxl
from .xq35_step2_sign_new_contract import ( # noqa: E402
AITABLE_BASE_ID, AITABLE_OPERATOR_UNIONID, _aitable_request)
from .step2_transfer_initiate import ( # noqa: E402
_hr_session, load_pending_list, remove_pending)
TARGET_SHEET, _hr_session, load_pending_list, remove_pending)
from .step3_transfer_todo import ( # noqa: E402
WORKFORM_FORM_ID, _find_push_table, _log, get_business_data,
list_running_workflows)
@@ -54,17 +54,17 @@ RECORDS_LIST_URL = (f"https://api.dingtalk.com/v1.0/notable/bases/{AITABLE_BASE_
RECORDS_ADD_URL = (f"https://api.dingtalk.com/v1.0/notable/bases/{AITABLE_BASE_ID}"
f"/sheets/{TABLE_ID}/records")
# 名单表「本月发起名单」所需列(按表头名解析,列序无关)
# 名单表「已发起名单」所需列(按表头名解析,列序无关)
ROSTER_COLS = ("姓名", "工号", "调动时间",
"现四级部门", "现岗位", "现技师工号",
"原四级部门", "原岗位", "原技师工号")
"现二级部门", "现三级部门", "现四级部门", "现岗位", "现技师工号",
"原二级部门", "原三级部门", "原四级部门", "原岗位", "原技师工号")
FIELD_INITIATED = "是否已发起改签离职"
FIELD_INIT_TIME = "发起时间"
# 试算开关:True=试算(只打印,不写 AI 表);False=实跑写入
DRY_RUN = False
FIELD_INIT_DATE = "发起日期" # 「本月发起名单」值列(第38列),已发起时回填
FIELD_INIT_DATE = "发起日期" # 「已发起名单」AI(35) 列(按表头名定位),已发起时回填
DATE_FMT = "yyyy/mm/dd" # 回填显示格式
@@ -95,15 +95,46 @@ def _ms_day(value):
# ---------- 数据源:待办列表 + 名单表字段 ----------
def fetch_field_choices():
"""「现/原单位名称」字段选项集(两字段选项一致,取并集兜底)。
fields 接口响应结构 {"value": [field...]},单选选项在 property.choices。
"""
url = (f"https://api.dingtalk.com/v1.0/notable/bases/{AITABLE_BASE_ID}"
f"/sheets/{TABLE_ID}/fields")
d = _aitable_request("GET", url, params={"operatorId": AITABLE_OPERATOR_UNIONID})
names = set()
for f in d.get("value") or []:
if f.get("name") in ("现单位名称", "原单位名称"):
names |= {c["name"] for c in (f.get("property") or {}).get("choices") or []
if c.get("name")}
return names
def pick_unit(row, prefix, choices):
"""单位名称候选依次 四级 -> 二级-三级 -> 二级 -> 三级,首个命中选项集者。
金色印象系四级=会馆名直接命中;石桥铺会馆直管体系选项为
「石桥铺会馆-前厅部」式路径名,由 二级-三级 拼接命中。
"""
l2 = row[f"{prefix}二级部门"]
l3 = row[f"{prefix}三级部门"]
l4 = row[f"{prefix}四级部门"]
for cand in (l4, f"{l2}-{l3}" if l2 and l3 else "", l2, l3):
if cand and cand in choices:
return cand
return ""
def load_roster_rows():
"""名单表数据行 -> {工号(str): 9 个回写字段 + _row(行号,发起日期回填用)};
"""名单表数据行 -> {工号(str): 回写字段 + _row(行号,发起日期回填用)};
同工号多行取后一行。"""
path, header = _find_push_table()
miss = [c for c in ROSTER_COLS if c not in header]
if miss:
raise KeyError(f"{os.path.basename(path)}「本月发起名单」缺列:{miss}")
raise KeyError(f"{os.path.basename(path)}「{TARGET_SHEET}」缺列:{miss}")
wb = openpyxl.load_workbook(path, read_only=True, data_only=True)
ws = wb["本月发起名单"]
ws = wb[TARGET_SHEET]
rows = {}
for r in range(2, ws.max_row + 1):
code = _s(ws.cell(r, header["工号"]).value)
@@ -113,16 +144,20 @@ def load_roster_rows():
"姓名": _s(ws.cell(r, header["姓名"]).value),
"工号": code,
"调动时间": ws.cell(r, header["调动时间"]).value,
"现二级部门": _s(ws.cell(r, header["现二级部门"]).value),
"现三级部门": _s(ws.cell(r, header["现三级部门"]).value),
"现四级部门": _s(ws.cell(r, header["现四级部门"]).value),
"现岗位": _s(ws.cell(r, header["现岗位"]).value),
"现技师工号": _s(ws.cell(r, header["现技师工号"]).value),
"原二级部门": _s(ws.cell(r, header["原二级部门"]).value),
"原三级部门": _s(ws.cell(r, header["原三级部门"]).value),
"原四级部门": _s(ws.cell(r, header["原四级部门"]).value),
"原岗位": _s(ws.cell(r, header["原岗位"]).value),
"原技师工号": _s(ws.cell(r, header["原技师工号"]).value),
"_row": r,
}
wb.close()
_log(f"[名单] {os.path.basename(path)}「本月发起名单」共 {len(rows)} 条")
_log(f"[名单] {os.path.basename(path)}「{TARGET_SHEET}」共 {len(rows)} 条")
return rows
@@ -169,7 +204,7 @@ def append_follow_rows(items):
def backfill_init_dates(rows, items):
"""已发起回填:发起日期写入「本月发起名单」对应行「发起日期」列(显示 yyyy/mm/dd)。
"""已发起回填:发起日期写入「已发起名单」对应行「发起日期」列(显示 yyyy/mm/dd)。
Args:
rows: load_roster_rows() 返回(借 _row 定位行)
@@ -178,14 +213,14 @@ def backfill_init_dates(rows, items):
实际更新行数(单元格已是同一天则跳过,跨天重跑幂等)
openpyxl 直写保留公式(同 step2 风格),保存清公式缓存无碍
(step1 下次跑时 WPS 重算恢复)。
(公式保留,打开自动重算显示)。
"""
path, header = _find_push_table()
if FIELD_INIT_DATE not in header:
raise KeyError(f"{os.path.basename(path)}「本月发起名单」缺「{FIELD_INIT_DATE}」列")
raise KeyError(f"{os.path.basename(path)}「{TARGET_SHEET}」缺「{FIELD_INIT_DATE}」列")
col = header[FIELD_INIT_DATE]
wb = openpyxl.load_workbook(path)
ws = wb["本月发起名单"]
ws = wb[TARGET_SHEET]
done = 0
for code, ms in items:
row = rows.get(code)
@@ -209,10 +244,11 @@ def backfill_init_dates(rows, items):
return done
def build_fields(row, today_ms):
"""名单行 + 发起时间 -> AI 表新增行 fields(空值字段不写)。
def build_fields(row, today_ms, choices):
"""名单行 + 发起时间 + 选项集 -> AI 表新增行 fields(空值字段不写)。
singleSelect(是否已发起改签离职/现单位名称/原单位名称)写入传选项名字符串。
singleSelect(是否已发起改签离职/现单位名称/原单位名称)写入传选项名字符串;
单位名称经 pick_unit 匹配选项集,未命中不写该字段(留空告警)。
"""
fields = {
"姓名": row["姓名"],
@@ -223,14 +259,16 @@ def build_fields(row, today_ms):
t = _ms_day(row["调动时间"])
if t:
fields["调动时间"] = t
if row["现四级部门"]:
fields["现单位名称"] = row["现四级部门"]
u = pick_unit(row, "现", choices)
if u:
fields["现单位名称"] = u
if row["现岗位"]:
fields["现岗位"] = row["现岗位"]
if row["现技师工号"]:
fields["现技师工号"] = str(row["现技师工号"])
if row["原四级部门"]:
fields["原单位名称"] = row["原四级部门"]
u = pick_unit(row, "原", choices)
if u:
fields["原单位名称"] = u
if row["原岗位"]:
fields["原岗位"] = row["原岗位"]
if row["原技师工号"]:
@@ -300,6 +338,9 @@ def main(dry_run=None):
f"{'(试算模式)' if dry_run else ''}")
return stats
choices = fetch_field_choices()
if not choices:
raise RuntimeError("未能获取「现/原单位名称」字段选项集,终止回写以免单位名称写空")
add_items, done_codes, init_items = [], [], []
today_ms = _ms_day(datetime.now())
for code in sorted(initiated):
@@ -318,9 +359,11 @@ def main(dry_run=None):
done_codes.append(code)
init_items.append((code, dup[2]))
continue
fields = build_fields(row, today_ms)
_log(f"[回写] {code} {row['姓名']} | 现单位 {row['现四级部门'] or '空'} | "
f"原单位 {row['原四级部门'] or '空'} | 调动 {row['调动时间']}")
fields = build_fields(row, today_ms, choices)
warn = "" if ("现单位名称" in fields and "原单位名称" in fields) \
else "(存在无匹配选项的单位名称,已留空待人工核对)"
_log(f"[回写] {code} {row['姓名']} | 现单位 {fields.get('现单位名称') or '空'} | "
f"原单位 {fields.get('原单位名称') or '空'} | 调动 {row['调动时间']}{warn}")
add_items.append(fields)
if add_items and not dry_run: