feat: 新增部门排班查询接口与排班异常校验优化
1. 新增按部门查询排班集合的接口能力,替换原模糊匹配排班名的校验逻辑 2. 新增门店技师序列的排除规则,优化异常判定范围 3. 修复导入统计分页截断问题,完善全量数据抓取逻辑 4. 更新钉钉推送模板与测试账号配置 5. 补全离职电子签流程相关文档
This commit is contained in:
@@ -8,10 +8,13 @@
|
||||
判断口径:
|
||||
- 调动台账:按「调动时间」筛选近 9 天,对每人查询当日完整排班名
|
||||
({日期}_team_name,形如「印象-{上级部门}-技师岗」)。当「现职位序列」含「职能」
|
||||
时按「现单位」比对,其余按「现四级部门」比对;当日有排班且完整排班名包含比对部门
|
||||
才视为正常,否则为排班异常(无排班 / 排班未更新);
|
||||
时按「现单位」比对,其余按「现四级部门」比对;用 rf_s_kq_work_date_query
|
||||
接口按比对部门查出该部门全部排班集合,当日排班属于该集合才视为正常
|
||||
(接口查不到集合时兜底原「完整排班名包含部门名」判断),否则为排班异常
|
||||
(无排班 / 排班未更新);
|
||||
- 预入职台账:按「操作时间」筛选近 9 天,对每人查询「预入职日期」当日完整排班名,
|
||||
当日有完整排班信息即为正常,无排班信息则为排班异常。有排班信息即可,不校验部门。
|
||||
不纳入异常:现职位序列命中 经营系管理序列 / 职能系专业序列 / 门店技师序列 的剔除(调动+预入职均生效)。
|
||||
底层能力复用:hr_session(会话管理)、hr_api.shift(排班查询)、roster 导出。
|
||||
@Version: 1.0.0
|
||||
|
||||
@@ -42,7 +45,7 @@ if _root not in sys.path:
|
||||
from app.utils.logger import get_logger, setup_logging # noqa: E402
|
||||
from app.service.hrbp.common.hr_session import build_hr_session # noqa: E402
|
||||
from app.service.hrbp.ehr.common.hr_api import fetch_cur_user_info # noqa: E402
|
||||
from app.service.hrbp.ehr.common.hr_api.shift import fetch_all_shift # noqa: E402
|
||||
from app.service.hrbp.ehr.common.hr_api.shift import fetch_all_shift, fetch_all_work_date # noqa: E402
|
||||
|
||||
try:
|
||||
import pandas as pd
|
||||
@@ -71,6 +74,9 @@ COLUMNS: List[str] = [
|
||||
"现上级部门", "现四级部门", "调动/入职时间", "操作时间", "当日排班", "异常原因",
|
||||
]
|
||||
|
||||
# 现职位序列命中以下序列的不纳入异常(不统计、不导出)
|
||||
EXCLUDE_SEQ_KEYWORDS = ("经营系管理序列", "职能系专业序列", "门店技师序列")
|
||||
|
||||
|
||||
# ----------------------------------------------
|
||||
# 1. 通用工具
|
||||
@@ -92,6 +98,20 @@ def _date_part(v) -> str:
|
||||
return _clean(v)[:10]
|
||||
|
||||
|
||||
def _seq_excluded(seq: str) -> bool:
|
||||
"""现职位序列命中剔除序列(经营系管理序列/职能系专业序列/门店技师序列)返回 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 _parse_date(v) -> Optional[date]:
|
||||
"""解析日期字符串为 date,失败返回 None。"""
|
||||
s = _date_part(v)
|
||||
@@ -173,10 +193,53 @@ 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
|
||||
|
||||
|
||||
# ----------------------------------------------
|
||||
# 3. 调动台账判定
|
||||
# ----------------------------------------------
|
||||
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("调动时间"))
|
||||
@@ -187,6 +250,8 @@ def _check_transfer_row(fetcher: "_ShiftFetcher", r, begin: date, end: date):
|
||||
|
||||
# 判断字段:现职位序列含「职能」按现单位判断,否则按现四级部门判断
|
||||
seq = _clean(r.get("现职位序列"))
|
||||
if _seq_excluded(seq):
|
||||
return None
|
||||
if "职能" in seq:
|
||||
target_field, target_name = "现单位", _clean(r.get("现单位"))
|
||||
else:
|
||||
@@ -196,10 +261,10 @@ def _check_transfer_row(fetcher: "_ShiftFetcher", r, begin: date, end: date):
|
||||
|
||||
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,
|
||||
@@ -222,7 +287,7 @@ def _check_transfer_row(fetcher: "_ShiftFetcher", r, begin: date, end: date):
|
||||
# ----------------------------------------------
|
||||
# 4. 预入职台账判定
|
||||
# ----------------------------------------------
|
||||
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("操作时间")) # 筛选字段:操作时间
|
||||
@@ -230,6 +295,8 @@ def _check_preentry_row(fetcher: "_ShiftFetcher", r, begin: date, end: date):
|
||||
return None
|
||||
if not (begin <= op_day <= end):
|
||||
return None
|
||||
if _seq_excluded(r.get("职位序列")):
|
||||
return None
|
||||
|
||||
# 排班检查日期:预入职日期(筛选按操作时间,查询排班按预入职日期)
|
||||
pj_day = _parse_date(r.get("预入职日期"))
|
||||
@@ -262,7 +329,7 @@ def _check_preentry_row(fetcher: "_ShiftFetcher", r, begin: date, end: date):
|
||||
# 5. 主流程
|
||||
# ----------------------------------------------
|
||||
def _collect_rows(ledger_path: str, check_row, fetcher: "_ShiftFetcher",
|
||||
end: date) -> List[dict]:
|
||||
dept_fetcher: "_DeptShiftFetcher", end: date) -> List[dict]:
|
||||
"""通用判定流程:读台账 → 逐行判定,返回异常行列表(未写文件)。"""
|
||||
df = _load_ledger(ledger_path)
|
||||
begin, end_date = _recent_window(end)
|
||||
@@ -270,7 +337,7 @@ def _collect_rows(ledger_path: str, check_row, fetcher: "_ShiftFetcher",
|
||||
|
||||
rows: List[dict] = []
|
||||
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))
|
||||
@@ -298,15 +365,16 @@ def run_shift_abnormal_check(
|
||||
session = build_hr_session()
|
||||
session, user_id = _validate_session(session)
|
||||
fetcher = _ShiftFetcher(session, user_id)
|
||||
dept_fetcher = _DeptShiftFetcher(session)
|
||||
|
||||
rows: List[dict] = []
|
||||
if os.path.exists(transfer_path):
|
||||
rows.extend(_collect_rows(transfer_path, _check_transfer_row, fetcher, end))
|
||||
rows.extend(_collect_rows(transfer_path, _check_transfer_row, fetcher, dept_fetcher, end))
|
||||
else:
|
||||
logger.warning("跳过调动台账:文件不存在 %s", transfer_path)
|
||||
|
||||
if os.path.exists(preentry_path):
|
||||
rows.extend(_collect_rows(preentry_path, _check_preentry_row, fetcher, end))
|
||||
rows.extend(_collect_rows(preentry_path, _check_preentry_row, fetcher, dept_fetcher, end))
|
||||
else:
|
||||
logger.warning("跳过预入职台账:文件不存在 %s", preentry_path)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user