- 配置由 save_dir / standard_path / roster_path 收敛为 work_dir:线下标准表在 该目录内按文件名关键词(起调薪薪资标准/薪资标准)与日期自动取最新, 处理结果 xlsx 同目录输出 - 星级改为按待办工号实时查询,失败再回退全量花名册导出 - 处理窗口留空=全部待办(原先默认当前自然月);界面、规则说明、README 同步
152 lines
5.7 KiB
Python
152 lines
5.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""花名册查询:职位序列(决定是否处理与工资账套)与星级(标准表匹配用)。
|
|
|
|
按待办工号实时查询在职/返聘人员(queryRealTimeData.mc,登录一次只取这批人,逐个补查时限流),
|
|
查询失败才回退全量导出在职花名册。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import time
|
|
from typing import List
|
|
|
|
import pandas as pd
|
|
|
|
from .. import config
|
|
|
|
COL_CODE = ("工号", "base_code")
|
|
COL_STAR = ("星级", "ext_star_level")
|
|
COL_SEQ = ("职位序列", "ext_sequence_name", "sequence")
|
|
DEFAULT_TTL = 1800
|
|
|
|
ROSTER_ID = "1116da64-b094-11ea-b588-14187743d052"
|
|
ROSTER_TABLE_ID = "assemble-hr_staff_base"
|
|
ROSTER_MENU_NAME = "员工花名册"
|
|
ROSTER_QUERY_URL = f"https://hr.37du.co/RedseaPlatform/hrRoster/{ROSTER_ID}/queryRealTimeData.mc"
|
|
PAGE_SIZE = 500
|
|
THROTTLE_SECONDS = 0.5
|
|
|
|
|
|
def _cell(row: dict, key: str) -> str:
|
|
"""兼容实时查询返回的裸键与 hr_staff_base / hr_staff_base_ext 前缀键。"""
|
|
return config.text(row.get(key)
|
|
or row.get(f"hr_staff_base.{key}")
|
|
or row.get(f"hr_staff_base_ext.{key}"))
|
|
|
|
|
|
def _col(columns, aliases) -> str:
|
|
for alias in aliases:
|
|
if alias in columns:
|
|
return alias
|
|
return ""
|
|
|
|
|
|
def load(path: str, log=None) -> dict:
|
|
"""读取花名册 → {工号: {"star": 星级, "sequence": 职位序列}}。"""
|
|
path = config.text(path)
|
|
if not path or not os.path.exists(path):
|
|
return {}
|
|
frame = pd.read_excel(path, dtype=str)
|
|
if not _col(frame.columns, COL_CODE):
|
|
frame = pd.read_excel(path, dtype=str, header=1)
|
|
col_code = _col(frame.columns, COL_CODE)
|
|
col_star = _col(frame.columns, COL_STAR)
|
|
col_seq = _col(frame.columns, COL_SEQ)
|
|
if not col_code:
|
|
if log:
|
|
log(f"[WARN] 花名册缺少「工号」列:{list(frame.columns)}", "WARNING")
|
|
return {}
|
|
result = {}
|
|
for _, row in frame.iterrows():
|
|
code = config.text(row.get(col_code))
|
|
if not code:
|
|
continue
|
|
result[code] = {
|
|
"star": config.text(row.get(col_star)) if col_star else "",
|
|
"sequence": config.text(row.get(col_seq)) if col_seq else "",
|
|
}
|
|
if log:
|
|
log(f"[INFO] 花名册载入 {len(result)} 人(职位序列/星级):{os.path.basename(path)}")
|
|
return result
|
|
|
|
|
|
def _index(rows: List[dict]) -> dict:
|
|
result = {}
|
|
for row in rows:
|
|
code = _cell(row, "base_code")
|
|
if not code or code in result:
|
|
continue
|
|
result[code] = {
|
|
"star": _cell(row, "ext_star_level"),
|
|
"sequence": _cell(row, "ext_sequence_name"),
|
|
}
|
|
return result
|
|
|
|
|
|
def _status_conditions() -> List[dict]:
|
|
"""在职+返聘(与花名册页面口径一致)。"""
|
|
from hr_sdk.query import condition
|
|
return [condition("`hr_staff_base.person_status`", "in", "1,4", query_text="1,4",
|
|
left_bracket="(", right_bracket=")", input_type="multipleSelect")]
|
|
|
|
|
|
def _query(client, conditions: List[dict]) -> List[dict]:
|
|
from hr_sdk.config import base_params
|
|
from hr_sdk.query import encode
|
|
|
|
params = base_params(ROSTER_MENU_NAME, ROSTER_TABLE_ID, ROSTER_ID)
|
|
params["commonQuery"] = encode(conditions)
|
|
params["baseHrefPath"] = "http://hr.37du.co:80RedseaPlatform"
|
|
params["pageNo"] = "1"
|
|
params["pageSize"] = str(PAGE_SIZE)
|
|
data = client.request_json("POST", ROSTER_QUERY_URL, data=params, label=ROSTER_MENU_NAME)
|
|
client.check_state(data, ROSTER_MENU_NAME)
|
|
return data.get("datas") or []
|
|
|
|
|
|
def query_by_codes(client, codes: List[str], log=None) -> dict:
|
|
"""按工号批量实时查询花名册(只取这批人的职位序列/星级)。"""
|
|
from hr_sdk.query import condition
|
|
|
|
codes = [code for code in dict.fromkeys(codes) if code]
|
|
if not codes:
|
|
return {}
|
|
batch = _status_conditions() + [condition(
|
|
"`hr_staff_base.base_code`", "in", ",".join(codes), query_text=",".join(codes),
|
|
left_bracket="(", right_bracket=")", input_type="text")]
|
|
result = _index(_query(client, batch))
|
|
if log:
|
|
log(f"[INFO] 花名册按工号实时查询 {len(codes)} 人,命中 {len(result)} 人")
|
|
missing = [code for code in codes if code not in result]
|
|
if missing:
|
|
# 批量 in 未覆盖时逐个 like 补查;逐个请求限流,避免连续打 HR 接口
|
|
for code in missing:
|
|
time.sleep(THROTTLE_SECONDS)
|
|
single = _status_conditions() + [condition(
|
|
"`hr_staff_base.base_code`", "like", code, query_text=code)]
|
|
result.update(_index(_query(client, single)))
|
|
if log:
|
|
log(f"[INFO] 花名册逐个补查 {len(missing)} 人(限流 {THROTTLE_SECONDS}s/次),"
|
|
f"累计命中 {len(result)} 人")
|
|
return result
|
|
|
|
|
|
def ensure(client, save_dir: str, log=None, base_codes: List[str] = None) -> dict:
|
|
"""返回花名册索引:按待办工号实时查询;查询失败再回退全量导出。"""
|
|
if base_codes:
|
|
try:
|
|
return query_by_codes(client, base_codes, log)
|
|
except Exception as exc: # noqa: BLE001 - 查询失败不阻断
|
|
if log:
|
|
log(f"[WARN] 花名册实时查询失败,回退全量导出:{exc}", "WARNING")
|
|
try:
|
|
from hr_sdk import roster as roster_sdk
|
|
target = os.path.join(save_dir, "花名册_在职.xlsx")
|
|
path = roster_sdk.export(client, statuses=["在职"], output=target, ttl=DEFAULT_TTL)
|
|
return load(path, log)
|
|
except Exception as exc: # noqa: BLE001 - 查询失败不阻断,后续按待核对
|
|
if log:
|
|
log(f"[WARN] 自动导出花名册失败,职位序列/星级将缺失:{exc}", "WARNING")
|
|
return {}
|