feat: 周考勤异常处理全流程优化与功能增强

1. 新增考勤异常自动确认功能:支持轻微异常(迟到/早退<30分钟)自动确认,总开关可控
2. 完善钉钉推送能力:新增推送记录持久化、会话切换缓存优化、OCR逻辑重构
3. 重构考勤异常处理业务逻辑,统一app与影刀版代码逻辑
4. 新增测试用例与调试脚本,优化文档与配置项
5. 清理废弃调试文件,更新导出字段映射与展示顺序
This commit is contained in:
2026-08-24 22:21:32 +08:00
parent 7526c142aa
commit 1489a9cf69
15 changed files with 1155 additions and 408 deletions
Binary file not shown.

Before

Width:  |  Height:  |  Size: 181 KiB

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 207 KiB

After

Width:  |  Height:  |  Size: 106 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 177 KiB

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 203 KiB

After

Width:  |  Height:  |  Size: 107 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 6.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 6.1 KiB

@@ -41,7 +41,7 @@ from app.utils.logger import get_logger, setup_logging # noqa: E402
from app.service.hrbp.ehr.common.hr_api import ( # noqa: E402
fetch_cur_user_info,
fetch_all_attendance_abnormal,
# adjust_abnormal, # 自动确认异常能力暂未启用,需要时取消注释
adjust_abnormal,
HRSessionExpiredError,
)
from app.service.hrbp.common.hr_session import ( # noqa: E402
@@ -59,6 +59,10 @@ logger = get_logger(__name__)
# ===================== 配置区(自行修改) =====================
# 默认保存目录
SAVE_DIR = r"D:\资料\人资\考勤异常"
# 推送记录保存目录(按场景传入,不写死;空串=不记录)
PUSH_RECORD_DIR = os.path.join(SAVE_DIR, "推送记录")
# 自动确认总开关(默认关闭:置 "true" 才批量确认轻微异常,避免误确认真实记录,便于测试时关闭)
AUTO_CONFIRM_ENABLED = "false"
# 英文字段 → 中文字段映射(aa.md 提供的考勤异常字段,其余字段保留原样)
COL_MAP: Dict[str, str] = {
@@ -74,7 +78,7 @@ COL_MAP: Dict[str, str] = {
# 自定义列展示顺序(核心字段放最前面,其余字段自动排在后面)
SHOW_ORDER = [
"异常ID", "员工姓名", "工号", "职位序列", "考勤日期", "班次", "异常类型", "异常时长(分钟)",
"异常ID", "员工姓名", "工号", "职位序列", "考勤日期", "班次", "异常类型", "异常时长(分钟)", "处理方式",
]
# 考勤异常调整状态过滤(本次只查询「未调整」)
@@ -126,6 +130,10 @@ def export_excel(data: List[dict], save_dir: str, month_str: str) -> str:
raise ValueError("未查询到任何考勤异常数据,无法导出")
df = pd.DataFrame(data)
# 处理方式:轻微异常(自动确认)/ 需核实(钉钉推送)
df["处理方式"] = df.apply(
lambda r: "自动确认" if not classify_abnormal(r) else "钉钉推送", axis=1
)
# 替换英文列为中文
df.rename(columns=COL_MAP, inplace=True)
# 过滤表中实际存在的列,再拼接剩余其他字段
@@ -224,7 +232,7 @@ def push_abnormal(items: List[dict]) -> int:
for (_name, _code), group in groups.items():
text = build_notify_text(group)
send_to_person(_name, text=text)
send_to_person(_name, text=text, keyword=_code, record_dir=PUSH_RECORD_DIR)
logger.info("已向 %s 推送考勤异常通知(%d 条合并)", _name, len(group))
return len(groups)
@@ -262,22 +270,64 @@ def _fetch_abnormal_data(month: str = None) -> tuple:
# ----------------------------------------------
# 6. 对外便捷入口
# ----------------------------------------------
# ---- 自动确认异常能力暂未启用,需要时取消以下注释 ----
# def confirm_abnormal(abnormal_ids, type_code: int = 1, session=None):
# """确认/调整考勤异常单据(按 abnormal_id 唯一编号)。
#
# Args:
# abnormal_ids: 异常单据 id,可为单个 id 或 id 列表
# type_code: 处理类型,1=自动确认异常(默认)
# session: 已登录会话;为空时自动构建并校验
#
# Returns:
# 接口返回的 JSON 字典(state=1 表示处理成功)
# """
# if session is None:
# session = build_hr_session()
# session, _, _ = _validate_session(session)
# return adjust_abnormal(session, abnormal_ids, type_code)
def confirm_abnormal(abnormal_ids, type_code: int = 1, session=None):
"""确认/调整考勤异常单据(按 abnormal_id 唯一编号)。
Args:
abnormal_ids: 异常单据 id,可为单个 id 或 id 列表
type_code: 处理类型,1=自动确认异常(默认)
session: 已登录会话;为空时自动构建并校验
Returns:
接口返回的 JSON 字典(state=1 表示处理成功)
"""
if session is None:
session = build_hr_session()
session, _, _ = _validate_session(session)
return adjust_abnormal(session, abnormal_ids, type_code)
def is_mild_abnormal(item: dict) -> bool:
"""轻微异常:迟到/早退 且 异常时长 < 30 分钟(自动确认范围,不分职能序列)。"""
return not classify_abnormal(item)
def auto_confirm_mild(data, session=None) -> int:
"""自动确认轻微异常(迟到/早退 < 30 分钟,不分职能序列)。
Args:
data: 未调整考勤异常记录列表
session: 已登录会话;为空时自动构建并校验
Returns:
确认成功的条数
"""
mild = [it for it in data if is_mild_abnormal(it) and it.get("abnormal_id")]
if not mild:
logger.info("无轻微异常(迟到/早退<30分钟),跳过自动确认")
return 0
ids = [it["abnormal_id"] for it in mild]
logger.info("自动确认轻微异常 %d 条(不分职能序列)", len(ids))
result = confirm_abnormal(ids, session=session)
if result.get("state") == "1":
logger.info("自动确认接口返回处理成功,共 %d 条", len(ids))
return len(ids)
logger.error("自动确认失败:%s", result.get("meg") or result)
return 0
def build_group_summary(total_count: int, mild_count: int, confirmed_count: int,
non_func_count: int, func_count: int, push_count: int,
begin: str, end: str) -> str:
"""生成考勤异常处理条数汇总消息(与汇总日志格式一致)。"""
return (
"【考勤异常处理汇总】\n"
f"查询范围:{begin} ~ {end}\n"
f"考勤异常总数:{total_count} 条\n"
f"直接处理(轻微,自动确认):{mild_count} 条,确认成功 {confirmed_count} 条\n"
f"非职能人员(不推送):{non_func_count} 条\n"
f"待通知(职能人员):{func_count} 条,合并为 {push_count} 条推送"
)
def download_attendance_abnormal(save_dir: str = None, month: str = None) -> str:
@@ -318,9 +368,22 @@ def handle_attendance_abnormal(save_dir: str = None, month: str = None) -> str:
path = export_excel(data, save_dir, month_str)
push_list = [it for it in data if classify_abnormal(it)]
handled_count = len(data) - len(push_list)
logger.info("考勤异常共 %d 条:需推送 %d 条,轻微异常(迟到/早退<30分钟,直接处理) %d 条",
len(data), len(push_list), handled_count)
handled_list = [it for it in data if not classify_abnormal(it)]
logger.info("考勤异常共 %d 条:需推送 %d 条,轻微异常(迟到/早退<30分钟,自动确认) %d 条",
len(data), len(push_list), len(handled_list))
# 自动确认轻微异常(迟到/早退<30分钟,不分职能序列)——总开关 AUTO_CONFIRM_ENABLED 控制
# 统计不受开关影响:始终按数据计算实际条数(开关只决定是否真正调用确认接口)
if month:
confirm_begin, confirm_end = f"{month}-01", f"{month}-31"
else:
confirm_begin, confirm_end = current_month_range()
mild_count = len([it for it in handled_list if it.get("abnormal_id")])
if AUTO_CONFIRM_ENABLED:
confirmed_count = auto_confirm_mild(handled_list, session=session)
else:
confirmed_count = 0
logger.info("自动确认未启用(AUTO_CONFIRM_ENABLED!=true),仅统计待自动确认 %d 条,未执行确认", mild_count)
# 只推送「职能」序列(first_type_name 含「职能」),并按 姓名-工号 合并推送
func_list = [it for it in push_list if is_func_staff(it)]
@@ -330,12 +393,15 @@ def handle_attendance_abnormal(save_dir: str = None, month: str = None) -> str:
else:
push_count = 0
logger.info("无需推送的异常,跳过钉钉通知")
logger.info("处理条数汇总:%s",
build_group_summary(len(data), mild_count, confirmed_count, skipped_non_func,
len(func_list), push_count, confirm_begin, confirm_end))
# ---- 最终统计汇总 ----
logger.info("=" * 60)
logger.info("【考勤异常处理汇总】")
logger.info(" 考勤异常总数:%d 条", len(data))
logger.info(" 直接处理(轻微,无需通知):%d 条", handled_count)
logger.info(" 直接处理(轻微,自动确认):%d 条,确认成功 %d 条", mild_count, confirmed_count)
logger.info(" 非职能人员(first_type_name 不含「职能」,不推送):%d 条", skipped_non_func)
logger.info(" 待通知(职能人员):%d 条,合并为 %d 条推送", len(func_list), push_count)
logger.info(" 导出文件:%s", path)