Files
37duAutomation/plugins/XQ0001-0100/XQ0006 EHR系统周考勤异常处理/logic/attendance_rules.py
T
puddy 50fb138e1b feat(plugin): 新增 XQ0006 周考勤异常处理、XQ0083 调动流程生效确认;其余插件累积改动
XQ0001 排班异常识别 / XQ0002 雇主险增减操作 / XQ0012 ERP 付款申请 / XQ0013 社保费用申报 / XQ0042 抖音平台核对 / XQ0084 银行卡更新:逻辑与界面 bundle 更新(含各自版本号变更)
2026-11-18 18:55:59 +08:00

147 lines
6.1 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 -*-
"""XQ0006 逻辑层:日期范围、分级分类、通知文本与汇总口径(规则与影刀版一致)。"""
from datetime import date, datetime, timedelta
from .. import config
# ==================== 日期范围 ====================
def query_month(today=None):
"""默认查询月份:1 号(2 号之前,不含 2 号)查上月,否则查当月。"""
today = today or date.today()
if today.day < 2:
return (today.replace(day=1) - timedelta(days=1)).strftime("%Y-%m")
return today.strftime("%Y-%m")
def resolve_month(value):
""""YYYY-MM" / "YYYYMM" / 留空 → 查询月份;格式非法抛错。"""
raw = str(value or "").strip()
if not raw:
return query_month()
digits = raw.replace("-", "").replace("/", "")
if len(digits) != 6 or not digits.isdigit():
raise ValueError(f"查询月份格式不正确:{value}(应为 YYYY-MM)")
try:
datetime.strptime(digits + "01", "%Y%m%d")
except ValueError:
raise ValueError(f"查询月份格式不正确:{value}(应为 YYYY-MM)")
return f"{digits[:4]}-{digits[4:]}"
def month_range(month):
"""整月查询范围(与影刀版一致:-01 ~ -31)。"""
return f"{month}-01", f"{month}-31"
def last_week_range(today=None):
"""返回上周范围:(上周一, 上周日)。"""
today = today or date.today()
this_monday = today - timedelta(days=today.weekday())
return ((this_monday - timedelta(days=7)).strftime("%Y-%m-%d"),
(this_monday - timedelta(days=1)).strftime("%Y-%m-%d"))
def in_last_week(item, push_begin, push_end):
"""按 work_day 判断是否落在上周范围;缺失返回 False。"""
work_day = str(item.get("work_day") or "")[:10]
return bool(work_day) and push_begin <= work_day <= push_end
# ==================== 分级分类 ====================
def classify_abnormal(item):
"""是否需要钉钉推送:迟到/早退<30 分钟为轻微(不推送),其余需推送。"""
abnormal = str(item.get("abnormal", ""))
try:
minute = int(item.get("abnormal_minute", 0) or 0)
except (TypeError, ValueError):
minute = 0
if ("迟到" in abnormal or "早退" in abnormal) and minute < 30:
return False
return True
def is_func_staff(item):
"""是否职能序列(first_type_name 含「职能」才推送)。"""
return "职能" in str(item.get("first_type_name", "") or "")
def is_push_scope(item, extra_units=None, exclude_keyword=None):
"""是否属于当事人推送范围:
职能序列,或所属单位在「推送范围单位」内且部门路径不含排除关键字。
"""
if is_func_staff(item):
return True
units = config.DEFAULT_PUSH_EXTRA_UNITS if extra_units is None else extra_units
keyword = config.DEFAULT_PUSH_EXCLUDE_KEYWORD if exclude_keyword is None \
else exclude_keyword
if not units:
return False
unit = str(item.get("BELONG_UNIT_ORG_NAME", "") or "").strip()
return unit in units and (not keyword
or keyword not in str(item.get("ORG_FULL_PATH", "") or ""))
def split_data(data, push_units=None, push_exclude_keyword=None):
"""按推送口径切分;自动确认范围不在此切分(由 run 全量确认)。"""
push_begin, push_end = last_week_range()
push_list = [it for it in data
if classify_abnormal(it) and in_last_week(it, push_begin, push_end)]
other_list = [it for it in data
if classify_abnormal(it) and not in_last_week(it, push_begin, push_end)]
return {
"push_list": push_list,
"other_list": other_list,
"func_list": [it for it in push_list
if is_push_scope(it, push_units, push_exclude_keyword)],
"non_func_list": [it for it in push_list
if not is_push_scope(it, push_units, push_exclude_keyword)],
"push_begin": push_begin,
"push_end": push_end,
}
def group_messages(items):
"""按「工号-姓名」合并为 [(姓名, 工号, 通知文本)],一人一条。"""
groups = {}
for it in items:
name = it.get("STAFF_NAME", "") or ""
if not name:
config.logger.warning("异常记录缺少姓名,跳过:%s", it)
continue
groups.setdefault((it.get("base_code", "") or "", name), []).append(it)
return [(name, code, build_notify_text(group)) for (code, name), group in groups.items()]
def build_notify_text(items):
"""按人员合并拼接钉钉通知文本(内容与影刀版完全一致)。"""
first = items[0]
lines = [
"【考勤异常提醒】",
f"姓名:{first.get('STAFF_NAME', '')}",
f"工号:{first.get('base_code', '')}",
f"共{len(items)}条考勤异常,请在2日内主动对接人事处理考勤异常确认/请假审批/漏打卡监控查询等,"
"超时未对接,将视为考勤异常直接封存",
]
for idx, item in enumerate(items, 1):
abnormal = str(item.get("abnormal", "") or "").strip() or "考勤异常"
minute = item.get("abnormal_minute") or "0"
lines.append(
f"{idx}、考勤日期:{item.get('work_day', '')}|异常类型:{abnormal}|异常时长:{minute}分钟"
)
return "\n".join(lines)
def build_group_summary(total_count, auto_count, confirmed_count, non_func_count, func_count,
push_count, other_count, month, begin, end):
"""群汇总文本(推送段落口径不变;自动确认段落为整月全量)。"""
return (
"【考勤异常处理汇总】\n\n"
f"查询范围:{begin} ~ {end}({month} 整月,推送仅上周)\n\n"
f"考勤异常总数:{total_count} 条\n\n"
f"自动确认(整月全量):{auto_count} 条,确认成功 {confirmed_count} 条\n\n"
f"待通知(职能及指定单位,上周):{func_count} 条,合并为 {push_count} 条推送\n\n"
f"上周非职能(不推送):{non_func_count} 条\n\n"
f"本月其他严重异常(非上周,仅导出留痕):{other_count} 条"
)