Files
37duAutomation/plugins/XQ0001-0100/XQ0006 EHR系统周考勤异常处理/logic/attendance_rules.py
T
puddy a2f4e756ae feat: 更新3个插件版本并优化XQ0006考勤异常处理逻辑
- XQ0036门店员工起调薪处理:版本升至0.1.6,优化纳税单位解析逻辑,优先使用接口返回值,新增金色印象、金色湖畔、后浪会馆的纳税单位兜底规则
- XQ0047微盟数据导出:版本升至1.0.5,新增等待生成上限配置项,优化轮询超时处理逻辑
- XQ0006 EHR系统周考勤异常处理:版本升至1.4.1,重构推送逻辑,支持组织架构范围选择,新增dws推送通道,优化自动确认范围为整月轻微异常,新增组织架构同步功能
2026-11-20 13:41:03 +08:00

144 lines
5.9 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 逻辑层:日期范围、分级分类、通知文本与汇总口径(规则与影刀版一致)。"""
import calendar
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):
"""整月查询范围:当月 1 号 ~ 当月最后一天(按真实天数,避免出现 -31)。"""
year, mon = (int(x) for x in str(month).split("-")[:2])
last = calendar.monthrange(year, mon)[1]
return f"{month}-01", f"{month}-{last:02d}"
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_mild_abnormal(item):
"""轻微异常:迟到/早退 且 异常时长 < 30 分钟(自动确认范围,不分职能序列)。"""
return not classify_abnormal(item)
def in_org_scope(item, scope_paths):
"""数据行是否落在所选组织节点内(节点本身或其下级,按组织全路径前缀匹配)。"""
path = str(item.get("ORG_FULL_PATH", "") or "").strip()
if not path:
return False
for p in scope_paths or ():
p = str(p or "").strip()
if p and (path == p or path.startswith(p + "-")):
return True
return False
def split_data(data, scope_paths=None):
"""按推送口径切分:只看组织架构勾选范围(含下级);未勾选=不推送当事人。"""
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 in_org_scope(it, scope_paths)],
"non_func_list": [it for it in push_list if not in_org_scope(it, scope_paths)],
"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} 条"
)