Files
37duAutomation/plugins/XQ0001-0100/XQ0011 考勤月报封存检查/logic/working_days_shift_check.py
T
puddy d1af551eac chore: 清理废弃插件与冗余文档,重构项目目录结构
1.  删除旧版xq0031_xq0032插件的冗余UI源码与配置文件
2.  迁移并标准化XQ0001-0100系列需求的插件、文档与工作流目录
3.  清理过期的需求列表、评分文档与流程底稿
4.  更新.gitignore匹配规则,适配新的目录结构
2026-09-21 16:59:05 +08:00

112 lines
4.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""XQ0011 考勤月报封存检查 插件 - 检查1「在职天数是否均排班验证」(由影刀版移植)。
当月考勤封存完毕后执行,检查范围为「上月完整月份」。
在职天数 = 月初1日~末日 与 入职日起~离职日止 闭区间交集的自然天数(含首尾);
差值 = 在职天数 - (在职应勤工作日 + 休息天);差值非 0 视为异常。
"""
from datetime import date
from typing import Dict, List, Optional, Tuple
from ..config import logger, DEFAULT_SAVE_DIR
from ..data.seal_data_export import (
enrich_dept_fields, load_seal_rows,
_clean, _num, _text, _parse_date, _prev_month, _month_range,
)
# 异常类型(对应需求文档检查1)
ABNORMAL_TYPE = "当月在职天数有差缺排班"
# 封存记录字段(aa.md 列定义)
SEAL_CODE_FIELDS = ("base_code", "seal_base_code")
SEAL_NAME_FIELD = "staff_name"
SEAL_JOIN_FIELD = "join_date" # aa.md:入职日期
SEAL_LEAVE_FIELD = "leaveOffice_date" # aa.md:离职时间
SEAL_WORK_FIELD = "kq_stat_month_yq_work_day_in" # aa.md:_在职应勤工作日
SEAL_REST_FIELD = "kq_stat_cal_ext9" # aa.md:休息天
# 输出列(对应需求文档检查1输出字段)
OUTPUT_COLUMNS = [
"37工号", "姓名", "月报检查月份", "异常类型",
"在职天数", "应勤工作日", "休息天", "差值",
"所属单位", "一级部门", "二级部门", "三级部门", "四级部门", "五级部门",
]
def _working_days(first: date, last: date, join: Optional[date], leave: Optional[date]) -> int:
"""计算员工当月在职自然天数(月区间 ∩ 在职区间闭区间,含首尾)。"""
lo = first if not join else (join if join >= first else first)
hi = last if not leave else (leave if leave <= last else last)
if lo > hi:
return 0
return (hi - lo).days + 1
def _build_seal_map(rows: List[dict]) -> Dict[str, dict]:
"""按工号建立 封存记录索引(一员工一条)。"""
mapping: Dict[str, dict] = {}
for rec in rows:
code = ""
for f in SEAL_CODE_FIELDS:
code = _text(rec.get(f))
if code:
break
if code:
mapping.setdefault(code, rec)
return mapping
def run_working_days_shift_check(month: str = None, save_dir: str = None) -> List[dict]:
"""执行检查1「在职天数是否均排班验证」,返回异常名单行列表。
Args:
month: 检查月份 "YYYY-MM",默认上月。
save_dir: 输出目录(仅用于读取预处理封存表)。
Returns:
异常名单行列表(每行为 OUTPUT_COLUMNS 对应字段的 dict)。
"""
today = date.today()
if month:
try:
first, last = _month_range(month)
except (ValueError, IndexError, TypeError):
raise ValueError(f"月份格式应为 YYYY-MM,收到:{month}")
ym = month
else:
py, pm = _prev_month(today.year, today.month)
first, last = _month_range(f"{py:04d}-{pm:02d}")
ym = f"{py:04d}-{pm:02d}"
save_dir = save_dir or DEFAULT_SAVE_DIR
logger.info("===== 检查1「在职天数是否均排班验证」月份:%s(%s ~ %s) =====", ym, first, last)
seal_rows = load_seal_rows(ym, save_dir)
if not seal_rows:
raise RuntimeError(f"当月「{ym}」预处理封存表为空,请确认该月已封存并已运行导出模块。")
seal_map = _build_seal_map(seal_rows)
logger.info("考勤封存加载完成,共 %d 人:%s", len(seal_map), ym)
abnormal: List[dict] = []
for code, rec in seal_map.items():
join = _parse_date(rec.get(SEAL_JOIN_FIELD))
leave = _parse_date(rec.get(SEAL_LEAVE_FIELD))
wd = _working_days(first, last, join, leave)
if wd <= 0:
continue # 当月无在职日,跳过
work = _num(rec.get(SEAL_WORK_FIELD))
rest = _num(rec.get(SEAL_REST_FIELD))
diff = wd - (work + rest)
if diff != 0:
abnormal.append(enrich_dept_fields({
"37工号": code,
"姓名": _text(rec.get(SEAL_NAME_FIELD)),
"月报检查月份": ym,
"异常类型": ABNORMAL_TYPE,
"在职天数": wd,
"应勤工作日": int(work),
"休息天": int(rest),
"差值": diff,
}, code, rec))
logger.info("检查1命中异常 %d 人(在职天数≠应勤+休息)", len(abnormal))
return abnormal