feat: 启用考勤异常直接推送,重构钉钉推送适配逻辑
1. 启用app版考勤异常推送:移除push_abnormal中的注释,直接调用钉钉推送接口,不再仅打印日志
2. 更新钉钉推送文档与适配逻辑:
- 重构dingtalk_sender参数为search_keyword/verify_keyword替代target/display_name
- 更新考勤异常推送分组与参数格式,适配新的OCR推送逻辑
3. 移除影刀审批脚本的单聊推送支持,统一使用群机器人推送
4. 优化缺勤分析缓存逻辑,移除历史月缓存限制,改用懒加载缓存
5. 为影刀考勤异常脚本新增联调测试模式,避免联调期间打扰真实员工
This commit is contained in:
@@ -10,9 +10,9 @@
|
||||
往前连续)均没出勤者纳入名单(统计月内已回来出勤的不放入名单);
|
||||
若连续段触及查询月首则一个月一个月往前推(按月全量缓存/单人查询两种策略),
|
||||
直到上一个上班日(跨月追溯)→ 确定请休假起止日期与天数 → 导出 20 列 Excel 名单。
|
||||
历史月数据缓存:考勤/请假「上上月及更早」已彻底月结不会变动,接口原始结果按
|
||||
「月/月+工号」落盘缓存(%LOCALAPPDATA%\37duAutomation\absence_cache),再次运行
|
||||
同月任务命中缓存不重复请求接口;上月/当月仍在校正,始终实时拉取。
|
||||
数据缓存:统计月(上月)始终实时调接口;跨月追溯往前的历史月走懒缓存(先查
|
||||
本地缓存,未命中才调接口,结果按「月/月+工号」落盘到 %LOCALAPPDATA%\37du
|
||||
Automation\absence_cache),下次直接复用;删除缓存文件即强制刷新。
|
||||
@Version: 2.2.0
|
||||
|
||||
场景定位(月度定时任务,建议每月 1 号由影刀调度触发,传上月月份参数):
|
||||
@@ -38,7 +38,6 @@
|
||||
absence_month=(可选)查询月份 "YYYY-MM",默认上月
|
||||
absence_roster_path=(可选)花名册文件路径,默认 D:\资料\人资\门店上月请休假异常人员名单梳理\员工花名册.xlsx
|
||||
(由「37du花名册导出」指令导出的固定文件)
|
||||
absence_no_cache=(可选,默认关闭)置为 1 时跳过历史月缓存读取,强制重新请求接口并刷新缓存
|
||||
|
||||
运行:
|
||||
- 影刀「调用模块」:main({"save_dir": ..., "month": "2026-07", "roster_path": ...})
|
||||
@@ -182,13 +181,11 @@ NOT_BACK_TEXT = "未归岗,时间待定"
|
||||
# 业务脚本只读取该文件,不做浏览器导出、不加载共享模块(影刀指令拖拽机制下无法按文件路径加载)。
|
||||
DEFAULT_ROSTER_PATH = r"D:\资料\人资\请休假异常\员工花名册.xlsx"
|
||||
|
||||
# ---------- 历史月数据缓存(避免重复调用接口) ----------
|
||||
# 考勤/请假一经彻底月结不再变动,把接口返回的原始结果按「月 / 月+工号」落盘缓存,
|
||||
# 再次运行同月任务时直接命中缓存,不再重复请求接口。
|
||||
# 仅缓存「上上月及更早」的月份(上月数据仍可能在校正、当月尚未结束,两者始终实时拉取);
|
||||
# 环境变量 absence_no_cache=1 时跳过缓存读取(强制重新请求接口),新数据仍写回缓存以便刷新。
|
||||
# ---------- 接口数据懒缓存(避免重复调用接口) ----------
|
||||
# 统计月(上月)始终实时调接口;跨月追溯往前的历史月走缓存旁路:
|
||||
# 每次先查本地缓存,没有才调接口,调完按「月 / 月+工号」落盘,下次直接复用;
|
||||
# 缓存以 月/工号 为键互不影响,如需强制刷新删除对应缓存文件即可。
|
||||
CACHE_DIR = os.path.join(_APP_DATA_DIR, "absence_cache")
|
||||
_FORCE_REFRESH = os.getenv("absence_no_cache") == "1"
|
||||
|
||||
# ==================== 登录态(共享 cookies 文件) ====================
|
||||
# 登录态统一由「37duHR登录」影刀自定义指令维护(见 yingdao/share/hr_login.py),
|
||||
@@ -396,23 +393,7 @@ def fetch_all_leave(session, user_id, base_code, start_time, end_time, treecode=
|
||||
return all_rows
|
||||
|
||||
|
||||
# ==================== 历史月数据缓存(按月落盘,命中不重复请求接口) ====================
|
||||
def _month_is_historical(month_key):
|
||||
"""判断月份(YYYYMM 或 YYYY-MM)是否「上上月及更早」。
|
||||
|
||||
上上月及更早的考勤/请假已彻底月结不会变动,才允许走缓存;
|
||||
上月数据仍可能在校正、当月尚未结束,两者始终实时拉取。
|
||||
"""
|
||||
key = str(month_key).replace("-", "")
|
||||
try:
|
||||
m_year, m_mon = int(key[:4]), int(key[4:6])
|
||||
except (TypeError, ValueError, IndexError):
|
||||
return False
|
||||
today = date.today()
|
||||
prev_first = (today.replace(day=1) - timedelta(days=1)).replace(day=1) # 上月第一天
|
||||
return date(m_year, m_mon, 1) < prev_first
|
||||
|
||||
|
||||
# ==================== 接口数据懒缓存(按月落盘,命中不重复请求接口) ====================
|
||||
def _cache_path(kind, month_key, code=""):
|
||||
"""构造缓存文件路径。kind ∈ {att, leave};month_key 为 "YYYYMM";code 空表示全量。"""
|
||||
fname = f"{kind}_{month_key}" + (f"_{code}" if code else "")
|
||||
@@ -420,8 +401,8 @@ def _cache_path(kind, month_key, code=""):
|
||||
|
||||
|
||||
def _read_cache(path):
|
||||
"""读取缓存列表;不存在/损坏/强制刷新返回 None。"""
|
||||
if _FORCE_REFRESH or not os.path.exists(path):
|
||||
"""读取缓存列表;不存在/损坏返回 None。"""
|
||||
if not os.path.exists(path):
|
||||
return None
|
||||
try:
|
||||
with open(path, encoding="utf-8") as f:
|
||||
@@ -443,39 +424,35 @@ def _write_cache(path, records):
|
||||
|
||||
|
||||
def _cached_attendance(session, user_id, month_key, base_code=""):
|
||||
"""按月缓存整月考勤统计报表(历史月命中缓存,不重复请求接口)。
|
||||
"""按月缓存整月考勤统计报表:先查缓存,未命中才调接口并落盘,下次直接复用。
|
||||
|
||||
month_key:月份 "YYYYMM";base_code 非空时为该员工单人报表(跨月追溯单人模式)。
|
||||
"""
|
||||
path = _cache_path("att", month_key, base_code) if _month_is_historical(month_key) else None
|
||||
if path is not None:
|
||||
cached = _read_cache(path)
|
||||
if cached is not None:
|
||||
logger.info("命中考勤报表缓存:%s(%d 条)", os.path.basename(path), len(cached))
|
||||
return cached
|
||||
path = _cache_path("att", month_key, base_code)
|
||||
cached = _read_cache(path)
|
||||
if cached is not None:
|
||||
logger.info("命中考勤报表缓存:%s(%d 条)", os.path.basename(path), len(cached))
|
||||
return cached
|
||||
records = fetch_all_attendance_stat(session, user_id, month_key, base_code=base_code)
|
||||
if path is not None:
|
||||
_write_cache(path, records)
|
||||
_write_cache(path, records)
|
||||
return records
|
||||
|
||||
|
||||
def _cached_leave(session, user_id, base_code, month_str):
|
||||
"""按月缓存整月请假记录(历史月命中缓存,不重复请求接口)。
|
||||
"""按月缓存整月请假记录:先查缓存,未命中才调接口并落盘,下次直接复用。
|
||||
|
||||
month_str:月份 "YYYY-MM";base_code 空为全集团,非空为该员工(跨月追溯单人模式)。
|
||||
"""
|
||||
month_key = month_str.replace("-", "")
|
||||
path = _cache_path("leave", month_key, base_code) if _month_is_historical(month_key) else None
|
||||
if path is not None:
|
||||
cached = _read_cache(path)
|
||||
if cached is not None:
|
||||
logger.info("命中请假记录缓存:%s(%d 条)", os.path.basename(path), len(cached))
|
||||
return cached
|
||||
path = _cache_path("leave", month_key, base_code)
|
||||
cached = _read_cache(path)
|
||||
if cached is not None:
|
||||
logger.info("命中请假记录缓存:%s(%d 条)", os.path.basename(path), len(cached))
|
||||
return cached
|
||||
mb, me = _month_range(month_str)
|
||||
records = fetch_all_leave(session, user_id, base_code,
|
||||
mb.strftime("%Y-%m-%d"), me.strftime("%Y-%m-%d"))
|
||||
if path is not None:
|
||||
_write_cache(path, records)
|
||||
_write_cache(path, records)
|
||||
return records
|
||||
|
||||
|
||||
@@ -828,8 +805,11 @@ def _latest_leave_end(session, user_id, code, begin, end):
|
||||
|
||||
情况A(月末仍缺席)判定:仅当存在请假截止日期 > 统计期最后一天(请假仍在进行/跨出统计期)
|
||||
时,以其截止日作为请休假结束日期;否则视为未归岗(返回 None,外部填「未归岗,时间待定」)。
|
||||
统计月始终实时调接口,不走缓存。
|
||||
"""
|
||||
leaves = _cached_leave(session, user_id, code, begin.strftime("%Y-%m"))
|
||||
leaves = fetch_all_leave(
|
||||
session, user_id, code, begin.strftime("%Y-%m-%d"), end.strftime("%Y-%m-%d")
|
||||
)
|
||||
ongoing = [d for r in leaves if (d := _parse_date(r.get("end_time"))) and d > end]
|
||||
if not ongoing:
|
||||
return None
|
||||
@@ -1001,14 +981,16 @@ def export_absence_list(save_dir=None, roster_path=None, month=None):
|
||||
session, user_id = _validate_session(session)
|
||||
|
||||
# 3. 拉取上月全集团考勤统计报表,按报表判定「休息/旷工」,再并入整天请假(leave_days>=1)
|
||||
# 统计月(上月)始终实时调接口,不走缓存;跨月追溯的历史月才走懒缓存。
|
||||
seen = {}
|
||||
records = _cached_attendance(session, user_id, month_str)
|
||||
records = fetch_all_attendance_stat(session, user_id, month_str)
|
||||
logger.info("考勤统计报表共 %d 人", len(records))
|
||||
if not records:
|
||||
logger.error("考勤统计报表无数据,请检查月份或权限")
|
||||
return ""
|
||||
month_status = _report_records_to_status(records, begin, end, seen)
|
||||
leaves = _cached_leave(session, user_id, "", begin.strftime("%Y-%m"))
|
||||
leaves = fetch_all_leave(
|
||||
session, user_id, "", begin.strftime("%Y-%m-%d"), end.strftime("%Y-%m-%d"))
|
||||
_merge_leave_status(month_status, leaves, begin, end)
|
||||
logger.info("全集团请假记录 %d 条(整天请假已并入缺勤判定)", len(leaves))
|
||||
_log_statuses(seen)
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
- 本脚本不内联任何发送逻辑,也不 import 共享推送模块;
|
||||
- 运行后会同步把待推送的 messages(字典列表)写入影刀全局变量
|
||||
glv['messages'](无数据时置为空列表),由流程拖「37du钉钉推送」指令
|
||||
完成发送(含 target 走 OCR 单聊,不含 target 走群机器人);
|
||||
完成发送(含 search_keyword 走 OCR 单聊,不含则走群机器人);
|
||||
- 处理汇总报表文本写入全局变量 glv['report'],由流程推送给 人资专用号。
|
||||
|
||||
依赖安装(影刀版启动校验:所有库必须预先装齐,任一缺失启动即报错退出,不懒加载):
|
||||
@@ -131,6 +131,16 @@ DINGTALK_PUSH_ENABLED = "true"
|
||||
# 人资专用号:处理汇总报表推送对象;未配置则跳过汇总推送
|
||||
DINGTALK_NOTIFY_TARGET = "夏一平"
|
||||
|
||||
# ===== 联调测试模式(临时,联调完成后请改回 False) =====
|
||||
# 启用时:仅推送前 PUSH_TEST_LIMIT 条当事人消息,且全部重定向到测试接收人,
|
||||
# 避免联调期间真的给真实员工发钉钉消息。
|
||||
# search_keyword 为钉钉搜索关键字(用工号避免同名误选);verify_keyword 为会话标题校验关键字(姓名)。
|
||||
PUSH_TEST_MODE = True
|
||||
PUSH_TEST_LIMIT = 10
|
||||
PUSH_TEST_SEARCH_KEYWORD = "37036361" # 测试接收人工号(搜索关键字)
|
||||
PUSH_TEST_VERIFY_KEYWORD = "夏一平" # 测试接收人姓名(会话标题校验关键字)
|
||||
# ======================================================
|
||||
|
||||
|
||||
# ==================== 登录态(共享 cookies 文件) ====================
|
||||
# 登录态统一由「37duHR登录」影刀自定义指令维护(见 yingdao/share/hr_login.py),
|
||||
@@ -353,8 +363,8 @@ def export_excel(data, save_dir, month_str):
|
||||
def push_abnormal(items):
|
||||
"""按「工号-姓名」分组,合并为 messages 字典列表,写入影刀全局变量 messages,
|
||||
由流程拖「37du钉钉推送」指令发送(本脚本不内联发送,保持单文件自包含)。
|
||||
推送目标(target)使用工号作为钉钉搜索关键字,避免同名人员误发;
|
||||
会话标题校验名(display_name)传姓名(钉钉标题显示姓名而非工号)。
|
||||
search_keyword(搜索关键字)使用工号,避免同名人员误发;
|
||||
verify_keyword(会话标题校验关键字)传姓名(钉钉标题显示姓名而非工号)。
|
||||
返回 (待推送条数, 失败数=0)。
|
||||
"""
|
||||
groups = {}
|
||||
@@ -366,10 +376,20 @@ def push_abnormal(items):
|
||||
key = (it.get("base_code", "") or "", name)
|
||||
groups.setdefault(key, []).append(it)
|
||||
|
||||
messages = [
|
||||
{"target": _code or _name, "display_name": _name, "text": build_notify_text(group)}
|
||||
for (_code, _name), group in groups.items()
|
||||
]
|
||||
# 联调测试模式:仅推送前 N 条,且全部重定向到测试接收人
|
||||
pairs = list(groups.items())
|
||||
if PUSH_TEST_MODE:
|
||||
pairs = pairs[:PUSH_TEST_LIMIT]
|
||||
logger.warning("【联调测试模式】仅推送前 %d 条,全部重定向到 %s(%s)(不打扰真实员工)",
|
||||
len(pairs), PUSH_TEST_VERIFY_KEYWORD, PUSH_TEST_SEARCH_KEYWORD)
|
||||
|
||||
messages = []
|
||||
for (_code, _name), group in pairs:
|
||||
if PUSH_TEST_MODE:
|
||||
search_keyword, verify_keyword = PUSH_TEST_SEARCH_KEYWORD, PUSH_TEST_VERIFY_KEYWORD
|
||||
else:
|
||||
search_keyword, verify_keyword = _code or _name, _name
|
||||
messages.append({"search_keyword": search_keyword, "verify_keyword": verify_keyword, "text": build_notify_text(group)})
|
||||
# 写入影刀全局变量,供流程「37du钉钉推送」指令读取(无数据也置空,避免 NameError)
|
||||
_set_glv('messages', messages)
|
||||
logger.info("当事人消息已生成并写入全局变量 messages:共 %d 条(由流程推送)", len(messages))
|
||||
|
||||
Reference in New Issue
Block a user