feat: 新增部门排班查询接口与排班异常校验优化
1. 新增按部门查询排班集合的接口能力,替换原模糊匹配排班名的校验逻辑 2. 新增门店技师序列的排除规则,优化异常判定范围 3. 修复导入统计分页截断问题,完善全量数据抓取逻辑 4. 更新钉钉推送模板与测试账号配置 5. 补全离职电子签流程相关文档
This commit is contained in:
@@ -13,12 +13,13 @@
|
||||
|
||||
判断口径(与 app 版 shift_abnormal_check.py 一致):
|
||||
- 调动台账:按「调动时间」近 {DAYS} 天;「现职位序列」含「职能」按「现单位」比对、
|
||||
其余按「现四级部门」比对;当日有排班且完整排班名({日期}_team_name)包含比对部门才正常,
|
||||
否则为排班异常(无排班 / 排班未更新);
|
||||
其余按「现四级部门」比对;用 rf_s_kq_work_date_query 接口按比对部门查出该部门
|
||||
全部排班集合,当日排班({日期}_team_name)属于该集合才正常(接口查不到集合时兜底
|
||||
原「完整排班名包含部门名」判断),否则为排班异常(无排班 / 排班未更新);
|
||||
- 预入职台账:按「操作时间」近 {DAYS} 天;查询「预入职日期」当日完整排班名,有即正常,否则异常。
|
||||
|
||||
不推送过滤(不纳入异常、不推送):
|
||||
- 「现职位序列」命中 经营系管理序列 / 职能系专业序列 的,直接剔除;
|
||||
- 「现职位序列」命中 经营系管理序列 / 职能系专业序列 / 门店技师序列 的,直接剔除;
|
||||
- 花名册查不到工号的按离职处理剔除(调动+预入职均生效)。
|
||||
|
||||
店长解析(花名册):
|
||||
@@ -108,6 +109,12 @@ SHARED_HR_COOKIE_FILE = os.path.join(_SESSIONS_DIR, 'hr_cookies.json')
|
||||
HR_BASE_URL = "https://hr.37du.co"
|
||||
GET_CUR_USER_INFO_URL = f"{HR_BASE_URL}/RedseaPlatform/PtUsers.mc?method=getCurUserInfo"
|
||||
SHIFT_URL = "https://hr.37du.co/RedseaPlatform/redmagicapi/rf_p_kq_count_SelectBanci/getExecutePageResult.mc"
|
||||
# 按部门查询排班集合(aa.md 提供):org_name=部门名,返回该部门全部排班
|
||||
# (data.list 记录含 work_date_name 排班名、org_name 部门名,逐条记录 = 一个排班)
|
||||
WORK_DATE_QUERY_URL = f"{HR_BASE_URL}/RedseaPlatform/redmagicapi/rf_s_kq_work_date_query/redApiExec.mc"
|
||||
# 集团根组织 struId(aa.md 提供,与考勤统计报表共用)
|
||||
COMPANY_STRU_ID = "596bae33-50e9-11ec-b526-822ff8ed5b28"
|
||||
WORK_DATE_PAGE_SIZE = 50
|
||||
PAGE_MAX_SIZE = 500
|
||||
|
||||
# 输出目录 & 第二步导出的台账文件名(表头均在第 2 行)
|
||||
@@ -129,7 +136,7 @@ COL_POSITION = "岗位"
|
||||
MANAGER_KEYWORD = "店长"
|
||||
|
||||
# 现职位序列命中以下序列的异常不推送(不纳入异常、不推送)
|
||||
EXCLUDE_SEQ_KEYWORDS = ("经营系管理序列", "职能系专业序列")
|
||||
EXCLUDE_SEQ_KEYWORDS = ("经营系管理序列", "职能系专业序列", "门店技师序列")
|
||||
|
||||
# 无法解析店长时归入默认处理人(多个英文逗号分隔则轮流分配;空则不归并、仅记录不推送)
|
||||
DEFAULT_MANAGER = os.getenv("shift_abnormal_default_manager", "")
|
||||
@@ -262,6 +269,58 @@ def fetch_all_shift(session, user_id, base_code, begin_date, end_date):
|
||||
return all_rows
|
||||
|
||||
|
||||
def fetch_work_date_page(session, org_name, page_no=1, page_size=WORK_DATE_PAGE_SIZE):
|
||||
"""请求单页「某部门排班集合」。data.list 记录含 work_date_name(排班名)、org_name(部门名)。"""
|
||||
params = {
|
||||
"stru_tree_code": "cq37du",
|
||||
"struId": COMPANY_STRU_ID,
|
||||
"containLower": "",
|
||||
"work_date_name": "",
|
||||
"paiban_type": "",
|
||||
"holiday_plan_name": "",
|
||||
"org_name": org_name,
|
||||
"work_status": "1",
|
||||
"start": str((page_no - 1) * page_size),
|
||||
"sortType": "desc",
|
||||
"sortName": "create_time",
|
||||
"pageNo": page_no,
|
||||
"pageSize": page_size,
|
||||
}
|
||||
return hr_request_json(session, "POST", WORK_DATE_QUERY_URL, params, "部门排班")
|
||||
|
||||
|
||||
def fetch_all_work_date(session, org_name):
|
||||
"""分页抓取某部门的全部排班名列表(work_date_name)。登录态失效自动补登后续抓。"""
|
||||
names = []
|
||||
page_no = 1
|
||||
while True:
|
||||
try:
|
||||
data = fetch_work_date_page(session, org_name, page_no)
|
||||
except HRSessionExpiredError:
|
||||
logger.warning("部门排班 HR 登录态过期,自动补登录后续抓")
|
||||
session = _relogin_get_user_id()[0]
|
||||
data = fetch_work_date_page(session, org_name, page_no)
|
||||
if not data.get("success"):
|
||||
logger.error("部门排班第 %d 页请求失败:%s", page_no, data.get("message") or data.get("meg"))
|
||||
break
|
||||
data_block = data.get("data", {}) or {}
|
||||
page_list = data_block.get("list", []) or []
|
||||
if not page_list:
|
||||
logger.info("部门排班无更多数据")
|
||||
break
|
||||
for rec in page_list:
|
||||
name = _clean(rec.get("work_date_name"))
|
||||
if name:
|
||||
names.append(name)
|
||||
total_page = data_block.get("pageCount", 0)
|
||||
logger.info("部门排班已抓取 %d/%d 页,累计 %d 条", page_no, total_page, len(names))
|
||||
if page_no >= total_page:
|
||||
break
|
||||
page_no += 1
|
||||
time.sleep(0.2)
|
||||
return names
|
||||
|
||||
|
||||
# ==================== 通用工具 ====================
|
||||
def _clean(v) -> str:
|
||||
"""转字符串并去掉常见杂质:None、浮点 .0 尾缀、pandas 空占位(nan/native等)。"""
|
||||
@@ -276,11 +335,19 @@ def _clean(v) -> str:
|
||||
|
||||
|
||||
def _seq_excluded(seq: str) -> bool:
|
||||
"""现职位序列命中剔除序列(经营系管理序列/职能系专业序列)返回 True。"""
|
||||
"""现职位序列命中剔除序列(经营系管理序列/职能系专业序列/门店技师序列)返回 True。"""
|
||||
seq = _clean(seq)
|
||||
return any(kw in seq for kw in EXCLUDE_SEQ_KEYWORDS)
|
||||
|
||||
|
||||
def _shift_name(s: str) -> str:
|
||||
"""归一化排班名:兼容 {日期}_team_name 可能带 "YYYY-MM-DD_" 前缀,去掉前缀便于与接口 work_date_name 比对。"""
|
||||
s = _clean(s)
|
||||
if len(s) > 11 and s[4] == "-" and s[7] == "-" and s[10] == "_":
|
||||
return s[11:]
|
||||
return s
|
||||
|
||||
|
||||
def _date_part(v) -> str:
|
||||
"""取日期字符串前 10 位(兼容 "YYYY-MM-DD HH:MM:SS")。"""
|
||||
return _clean(v)[:10]
|
||||
@@ -356,8 +423,51 @@ class _ShiftFetcher:
|
||||
return ""
|
||||
|
||||
|
||||
class _DeptShiftFetcher:
|
||||
"""按部门名缓存该部门的排班名集合(work_date_name),复用同一会话,定期校验登录态。
|
||||
|
||||
同一部门可能配置多个排班(技师岗/服务岗/管理岗…),通过 rf_s_kq_work_date_query
|
||||
接口按部门查出全部排班名,用于判断员工当日排班是否属于该部门。
|
||||
"""
|
||||
|
||||
def __init__(self, session):
|
||||
self.session = session
|
||||
self.cache: Dict[str, set] = {}
|
||||
self.calls = 0
|
||||
|
||||
def fetch(self, dept_name: str) -> set:
|
||||
"""返回某部门的排班名集合;部门名为空或查不到返回空集合。"""
|
||||
dept_name = _clean(dept_name)
|
||||
if not dept_name:
|
||||
return set()
|
||||
if dept_name not in self.cache:
|
||||
# 定期重新校验登录态,避免绑定过期会话
|
||||
self.calls += 1
|
||||
if self.calls % SESSION_REVALIDATE_EVERY == 1:
|
||||
self.session, _ = _ensure_user_id(self.session, None)
|
||||
self.cache[dept_name] = {_shift_name(n) for n in fetch_all_work_date(self.session, dept_name)}
|
||||
time.sleep(0.2)
|
||||
return self.cache[dept_name]
|
||||
|
||||
|
||||
def _shift_belongs(dept_fetcher: "_DeptShiftFetcher", dept_name: str, shift: str) -> bool:
|
||||
"""判断某排班是否属于某部门。
|
||||
|
||||
优先用新接口(rf_s_kq_work_date_query)按部门查出排班集合做成员判断;
|
||||
接口查不到集合(如「现单位」无直属排班)时兜底原「完整排班名包含部门名」判断。
|
||||
"""
|
||||
dept_name = _clean(dept_name)
|
||||
shift_norm = _shift_name(shift)
|
||||
if not dept_name or not shift_norm:
|
||||
return False
|
||||
dept_shifts = dept_fetcher.fetch(dept_name)
|
||||
if dept_shifts:
|
||||
return shift_norm in dept_shifts
|
||||
return dept_name in shift
|
||||
|
||||
|
||||
# ==================== 异常判定(与 app 版一致) ====================
|
||||
def _check_transfer_row(fetcher: "_ShiftFetcher", r, begin: date, end: date):
|
||||
def _check_transfer_row(fetcher: "_ShiftFetcher", dept_fetcher: "_DeptShiftFetcher", r, begin: date, end: date):
|
||||
"""对单行调动记录判定,返回名单行 dict;属正常则返回 None。"""
|
||||
code = _clean(r.get("工号"))
|
||||
day = _parse_date(r.get("调动时间"))
|
||||
@@ -377,10 +487,10 @@ def _check_transfer_row(fetcher: "_ShiftFetcher", r, begin: date, end: date):
|
||||
shift = fetcher.fetch(code, day)
|
||||
if not shift:
|
||||
reason, present = "调动时间无排班", ""
|
||||
elif target_name and target_name not in shift:
|
||||
elif target_name and not _shift_belongs(dept_fetcher, target_name, shift):
|
||||
reason, present = f"排班未更新为{target_field}({target_name})", shift
|
||||
else:
|
||||
return None # 当日有排班且完整排班名包含所需部门 → 正常
|
||||
return None # 当日有排班且排班属于所需部门 → 正常
|
||||
|
||||
return {
|
||||
"序号": 0,
|
||||
@@ -400,7 +510,7 @@ def _check_transfer_row(fetcher: "_ShiftFetcher", r, begin: date, end: date):
|
||||
}
|
||||
|
||||
|
||||
def _check_preentry_row(fetcher: "_ShiftFetcher", r, begin: date, end: date):
|
||||
def _check_preentry_row(fetcher: "_ShiftFetcher", dept_fetcher: "_DeptShiftFetcher", r, begin: date, end: date):
|
||||
"""对单行预入职记录判定,返回名单行 dict;有排班信息则返回 None。"""
|
||||
code = _clean(r.get("工号"))
|
||||
op_day = _parse_date(r.get("操作时间")) # 筛选字段:操作时间
|
||||
@@ -437,7 +547,7 @@ def _check_preentry_row(fetcher: "_ShiftFetcher", r, begin: date, end: date):
|
||||
}
|
||||
|
||||
|
||||
def _collect_rows(ledger_path: str, check_row, fetcher: "_ShiftFetcher", end: date,
|
||||
def _collect_rows(ledger_path: str, check_row, fetcher: "_ShiftFetcher", dept_fetcher: "_DeptShiftFetcher", end: date,
|
||||
days: int = DAYS) -> List[dict]:
|
||||
"""通用判定流程:读台账 → 逐行判定,返回异常行列表。"""
|
||||
df = _load_ledger(ledger_path)
|
||||
@@ -445,7 +555,7 @@ def _collect_rows(ledger_path: str, check_row, fetcher: "_ShiftFetcher", end: da
|
||||
logger.info("筛选范围:%s ~ %s(近 %d 天)", begin, end_date, days)
|
||||
rows = []
|
||||
for _, r in df.iterrows():
|
||||
row = check_row(fetcher, r, begin, end_date)
|
||||
row = check_row(fetcher, dept_fetcher, r, begin, end_date)
|
||||
if row:
|
||||
rows.append(row)
|
||||
logger.info("台账 %s 排班异常人数:%d", os.path.basename(ledger_path), len(rows))
|
||||
@@ -536,15 +646,18 @@ def resolve_manager(roster_df, row: dict) -> Tuple[str, str]:
|
||||
|
||||
# ==================== 消息构造 ====================
|
||||
def _item_line(row: dict) -> str:
|
||||
"""异常明细一行(钉钉 markdown 纯文本,<br> 做单行换行),调动/新入职两种文案。
|
||||
"""异常明细一行(钉钉 markdown 纯文本,<br> 做单行换行),按异常原因区分文案:
|
||||
|
||||
- 调动:门店员工{姓名}({工号})至{调动时间}日调入本店超期未排班,请核实处理;
|
||||
- 调动-无排班:门店员工{姓名}({工号})至{调动时间}日调入本店超期未排班,请核实处理;
|
||||
- 调动-排班未更新:门店员工{姓名}({工号})至{调动时间}日调入本店排班未更新,请核实处理;
|
||||
- 新入职:门店员工{姓名}({工号})自{预入职日期}日入职本店超期未排班,请核实处理。
|
||||
"""
|
||||
name = row.get("姓名", "")
|
||||
code = row.get("工号", "")
|
||||
day = row.get("调动/入职时间", "")
|
||||
if row.get("类别") == "调动":
|
||||
if "排班未更新" in _clean(row.get("异常原因")):
|
||||
return f"门店员工{name}({code})至{day}日调入本店排班未更新,请核实处理"
|
||||
return f"门店员工{name}({code})至{day}日调入本店超期未排班,请核实处理"
|
||||
return f"门店员工{name}({code})自{day}日入职本店超期未排班,请核实处理"
|
||||
|
||||
@@ -620,7 +733,7 @@ def build_messages(rows: List[dict], roster_df) -> List[tuple]:
|
||||
f"【排班异常提醒】<br>"
|
||||
f"{dept}店长您好,人力资源部在近期排班核查中发现您的门店有 {len(items)} 名员工排班异常<br>"
|
||||
f"{line_items}<br>"
|
||||
f"如有疑问请联系陈杰/徐红丽/赵晓雪或者经营中心人行"
|
||||
f"如有疑问请联系经营中心人行"
|
||||
)
|
||||
messages.append(("text", mn, mc, content, ""))
|
||||
logger.info("已构造店长[%s%s]单聊推送消息,含 %d 条异常", mn, f"/{mc}" if mc else "", len(items))
|
||||
@@ -660,18 +773,19 @@ def run_shift_abnormal_check(
|
||||
session = build_hr_session()
|
||||
session, user_id = _ensure_user_id(session, None)
|
||||
fetcher = _ShiftFetcher(session, user_id)
|
||||
dept_fetcher = _DeptShiftFetcher(session)
|
||||
else:
|
||||
raise RuntimeError(
|
||||
"调动台账、预入职台账均不存在,请先运行第二步导出后再执行本模块。\n"
|
||||
f"(调动:{transfer_path},预入职:{preentry_path})")
|
||||
|
||||
if os.path.exists(transfer_path):
|
||||
rows.extend(_collect_rows(transfer_path, _check_transfer_row, fetcher, end, days))
|
||||
rows.extend(_collect_rows(transfer_path, _check_transfer_row, fetcher, dept_fetcher, end, days))
|
||||
else:
|
||||
logger.warning("跳过调动台账:文件不存在 %s", transfer_path)
|
||||
|
||||
if os.path.exists(preentry_path):
|
||||
rows.extend(_collect_rows(preentry_path, _check_preentry_row, fetcher, end, days))
|
||||
rows.extend(_collect_rows(preentry_path, _check_preentry_row, fetcher, dept_fetcher, end, days))
|
||||
else:
|
||||
logger.warning("跳过预入职台账:文件不存在 %s", preentry_path)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user