XQ0001 排班异常识别 / XQ0002 雇主险增减操作 / XQ0012 ERP 付款申请 / XQ0013 社保费用申报 / XQ0042 抖音平台核对 / XQ0084 银行卡更新:逻辑与界面 bundle 更新(含各自版本号变更)
180 lines
6.9 KiB
Python
180 lines
6.9 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""店长解析(花名册)与固定路由。
|
||
|
||
规则(与影刀版一致):
|
||
- 工号已由离职过滤保证在花名册内;
|
||
- 固定路由优先:``现职位序列`` 命中 ``FIXED_ROUTE_RULES`` 直接归指定处理人;
|
||
- 否则按异常人员工号取花名册部门链(四级→三级→所属→二级),逐级找同名门店下岗位含「店长」的人;
|
||
- 仍解析不到时交给调用方按默认处理人归属,否则只留档不推送。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
from typing import Dict, List, Optional, Tuple
|
||
|
||
import pandas as pd
|
||
|
||
from .. import config
|
||
from .rules import clean, to_text
|
||
|
||
|
||
def read_roster(path: str, log=print) -> pd.DataFrame:
|
||
"""读花名册并定位表头行(含「工号」列的那一行)。"""
|
||
target = os.path.abspath(path)
|
||
if not os.path.exists(target):
|
||
raise RuntimeError(f"未找到花名册:{target}(请先用 SDK 导出或指定路径)")
|
||
probe = pd.read_excel(target, header=None, nrows=10)
|
||
header_row = None
|
||
for idx in range(len(probe)):
|
||
if (probe.iloc[idx].astype(str).str.strip() == config.COL_CODE).any():
|
||
header_row = idx
|
||
break
|
||
if header_row is None:
|
||
raise ValueError(f"花名册未找到表头行(缺少「{config.COL_CODE}」列):{target}")
|
||
frame = pd.read_excel(target, header=header_row, dtype=str)
|
||
frame.columns = [str(c).strip() for c in frame.columns]
|
||
missing = [c for c in config.ROSTER_REQUIRED if c not in frame.columns]
|
||
if missing:
|
||
raise ValueError(f"花名册缺少必需列:{missing}({target})")
|
||
log(f"[INFO] 花名册已读取:{os.path.basename(target)}({len(frame)} 行)")
|
||
return frame
|
||
|
||
|
||
def dept_values(row) -> List[str]:
|
||
"""按「四级→三级→所属→二级」取该行所有非空部门名(由深到浅、去重)。"""
|
||
values: List[str] = []
|
||
for column in config.ROSTER_DEPT_COLUMNS:
|
||
if not hasattr(row, "get"):
|
||
continue
|
||
value = to_text(row.get(column))
|
||
if value and value not in values:
|
||
values.append(value)
|
||
return values
|
||
|
||
|
||
def manager_flags(frame: pd.DataFrame):
|
||
"""返回 ``(是否店长 Series, 店长门店名 Series)``(门店名取第一个非空层级)。"""
|
||
is_manager = frame[config.COL_POSITION].astype(str).str.contains(
|
||
config.MANAGER_KEYWORD, na=False)
|
||
store = pd.Series([""] * len(frame), index=frame.index, dtype=object)
|
||
for column in config.ROSTER_DEPT_COLUMNS:
|
||
if column not in frame.columns:
|
||
continue
|
||
values = frame[column].map(to_text)
|
||
fill = is_manager & store.eq("") & values.ne("")
|
||
store = store.mask(fill, values)
|
||
return is_manager, store
|
||
|
||
|
||
def find_manager_by_dept(frame: pd.DataFrame, dept_name: str) -> Tuple[Optional[str], Optional[str]]:
|
||
"""按门店名找店长(岗位含「店长」),多个取第一个。"""
|
||
name = clean(dept_name)
|
||
if not name:
|
||
return None, None
|
||
is_manager, store = manager_flags(frame)
|
||
candidates = frame[is_manager & (store == name)]
|
||
if candidates.empty:
|
||
return None, None
|
||
first = candidates.iloc[0]
|
||
return to_text(first[config.COL_CODE]) or None, to_text(first[config.COL_NAME]) or ""
|
||
|
||
|
||
def find_manager_by_code(frame: pd.DataFrame, code: str, log=print
|
||
) -> Tuple[Optional[str], Optional[str]]:
|
||
"""按异常人员工号逐级回溯门店并解析店长。"""
|
||
code = to_text(code)
|
||
if not code:
|
||
return None, None
|
||
hit = frame[frame[config.COL_CODE].map(to_text) == code]
|
||
if hit.empty:
|
||
log(f"[WARN] 花名册中无该工号:{code}")
|
||
return None, None
|
||
depts = dept_values(hit.iloc[0])
|
||
if not depts:
|
||
log(f"[WARN] 员工(工号 {code})无部门信息,无法解析店长")
|
||
return None, None
|
||
for dept in depts:
|
||
manager_code, manager_name = find_manager_by_dept(frame, dept)
|
||
if manager_code or manager_name:
|
||
return manager_code, manager_name
|
||
log(f"[WARN] 员工(工号 {code})在部门 {' / '.join(depts)} 均未找到店长")
|
||
return None, None
|
||
|
||
|
||
def resolve_manager(frame: pd.DataFrame, row: dict, log=print
|
||
) -> Tuple[Optional[str], Optional[str]]:
|
||
return find_manager_by_code(frame, row.get("工号", ""), log)
|
||
|
||
|
||
def lookup_dept4_by_code(frame: pd.DataFrame, code: str) -> str:
|
||
"""按工号回填四级部门(预入职台账模板无该列)。"""
|
||
code = to_text(code)
|
||
if not code:
|
||
return ""
|
||
hit = frame[frame[config.COL_CODE].map(to_text) == code]
|
||
if hit.empty:
|
||
return ""
|
||
return to_text(hit.iloc[0][config.COL_DEPT4])
|
||
|
||
|
||
def dept1_map(frame: pd.DataFrame) -> Dict[str, str]:
|
||
"""工号 → 一级部门(花名册无「一级部门」列时返回空 dict)。"""
|
||
if config.COL_DEPT1 not in frame.columns:
|
||
return {}
|
||
return {to_text(code): to_text(dept1) for code, dept1
|
||
in zip(frame[config.COL_CODE], frame[config.COL_DEPT1])}
|
||
|
||
|
||
def dept1_excluded(dept1: str) -> bool:
|
||
"""一级部门命中剔除关键字(如「后勤」)→ True。"""
|
||
text = clean(dept1)
|
||
return any(keyword in text for keyword in config.EXCLUDE_DEPT1_KEYWORDS)
|
||
|
||
|
||
def match_fixed_route(row: dict):
|
||
"""命中固定路由返回 ``((工号, 姓名), 范围描述)``,未命中返回 ``None``。"""
|
||
seq = to_text(row.get("现职位序列"))
|
||
for keywords, manager, scope in config.FIXED_ROUTE_RULES:
|
||
if any(keyword in seq for keyword in keywords):
|
||
return manager, scope
|
||
return None
|
||
|
||
|
||
def resolve_manager_identity(frame: pd.DataFrame, keyword: str
|
||
) -> Tuple[str, str]:
|
||
"""把「工号 or 姓名」归一化为 ``(工号, 姓名)``(默认处理人场景)。"""
|
||
keyword = to_text(keyword).strip()
|
||
if not keyword:
|
||
return "", ""
|
||
hit = frame[frame[config.COL_CODE].map(to_text) == keyword]
|
||
if hit.empty:
|
||
hit = frame[frame[config.COL_NAME].map(to_text) == keyword]
|
||
if not hit.empty:
|
||
return to_text(hit.iloc[0][config.COL_CODE]), to_text(hit.iloc[0][config.COL_NAME])
|
||
if keyword.isdigit():
|
||
return keyword, ""
|
||
return "", keyword
|
||
|
||
|
||
def find_store_by_manager(frame: pd.DataFrame, manager_code: str,
|
||
manager_name: str) -> str:
|
||
"""按店长工号/姓名查其门店名(部门链第一个非空值)。"""
|
||
if manager_code:
|
||
hit = frame[frame[config.COL_CODE].map(to_text) == manager_code]
|
||
elif manager_name:
|
||
hit = frame[frame[config.COL_NAME].map(to_text) == manager_name]
|
||
else:
|
||
return ""
|
||
if hit.empty:
|
||
return ""
|
||
values = dept_values(hit.iloc[0])
|
||
return values[0] if values else ""
|
||
|
||
|
||
def manager_label(manager_code: str, manager_name: str) -> str:
|
||
"""店长/处理人显示标签:姓名/工号(缺一则显示另一个)。"""
|
||
if manager_code and manager_name:
|
||
return f"{manager_name}/{manager_code}"
|
||
return manager_code or manager_name
|