feat: 新增打卡明细接口支持,优化加班校验逻辑
1. 新增打卡明细接口封装,支持单页查询与自动分页获取员工真实打卡记录 2. 重构加班校验逻辑,使用打卡明细接口替代原考勤逐日接口的打卡字段,解决跨天打卡归属错误问题 3. 移除过时的打卡时间解析与单条打卡修正逻辑,简化代码维护 4. 优化OA登录脚本,新增requests依赖并完善会话校验逻辑 5. 新增钉钉群机器人推送通用模块,支持多场景通知推送
This commit is contained in:
@@ -35,6 +35,12 @@ ATTENDANCE_DAILY_URL = f"{HR_BASE_URL}/RedseaPlatform/redmagicapi/rf_s_kq_count_
|
||||
# 请假记录接口(hr 域名,按员工 + 日期范围查询请假明细)
|
||||
LEAVE_RECORD_URL = f"{HR_BASE_URL}/RedseaPlatform/redmagicapi/rf_s_kq_leave_record_SelectByStaffID/redApiExec.mc"
|
||||
|
||||
# 打卡明细接口(hr 域名,按员工 + 日期范围查询真实打卡明细)
|
||||
# kq_time=真实打卡时间、bc_date=考勤归属日(跨天打卡次日0点~6点归入前一天)、
|
||||
# time_order=上下班类型(1=上班、2=下班)。加班校验用其替代考勤逐日接口的打卡字段,
|
||||
# 避免人工拼接日期导致跨天打卡归属错误。
|
||||
PUNCH_RECORD_URL = f"{HR_BASE_URL}/RedseaPlatform/redmagicapi/rf_s_kq_data_queryByStaffId/redApiExec.mc"
|
||||
|
||||
# 附件下载接口(hr 域名):按 fileId 下载请假单据附件
|
||||
FILE_DOWNLOAD_URL = f"{HR_BASE_URL}/RedseaPlatform/corePlatformOutUser/fjkFileDownload.mob"
|
||||
# 附件默认保存目录
|
||||
|
||||
@@ -31,6 +31,10 @@ from app.service.hrbp.oa.common.oa_api.leave import (
|
||||
fetch_leave_record_page,
|
||||
get_leave_records,
|
||||
)
|
||||
from app.service.hrbp.oa.common.oa_api.punch import (
|
||||
fetch_punch_record_page,
|
||||
get_punch_records,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"oa_request_json",
|
||||
@@ -46,4 +50,6 @@ __all__ = [
|
||||
"get_attendance_daily",
|
||||
"fetch_leave_record_page",
|
||||
"get_leave_records",
|
||||
"fetch_punch_record_page",
|
||||
"get_punch_records",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@Module: hrbp.oa.common.oa_api
|
||||
@FileName: punch.py
|
||||
@Description: 员工打卡明细接口:单页查询与自动分页。
|
||||
kq_time 为真实打卡时间,bc_date 为考勤归属日(跨天打卡次日0点~6点归入前一天)。
|
||||
@Version: 1.0.0
|
||||
"""
|
||||
|
||||
from typing import List, Dict, Any
|
||||
|
||||
from app.utils.logger import get_logger
|
||||
try:
|
||||
from DrissionPage import SessionPage
|
||||
except ImportError:
|
||||
SessionPage = None
|
||||
|
||||
from app.service.hrbp.oa.common.config import PUNCH_RECORD_URL, DEFAULT_PAGE_SIZE
|
||||
from app.service.hrbp.oa.common.oa_api.base import oa_request_json
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
def fetch_punch_record_page(
|
||||
session: SessionPage,
|
||||
staff_id: str,
|
||||
begin_date: str,
|
||||
end_date: str,
|
||||
page_no: int = 1,
|
||||
page_size: int = DEFAULT_PAGE_SIZE,
|
||||
sub_type: str = "-1"
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
请求单页员工打卡明细。
|
||||
|
||||
Args:
|
||||
session: 已登录的 SessionPage 对象
|
||||
staff_id: 员工 UUID
|
||||
begin_date: 查询起始日期 "YYYY-MM-DD"(同时作为 bc_date/bc_date_end/begin/end)
|
||||
end_date: 查询结束日期 "YYYY-MM-DD"
|
||||
page_no: 页码(从1开始)
|
||||
page_size: 每页条数
|
||||
sub_type: 类型编码,默认 "-1" 表示全部
|
||||
|
||||
Returns:
|
||||
接口返回的 data 字段字典,包含 total / pageCount / list
|
||||
|
||||
Raises:
|
||||
RuntimeError: 请求失败或响应异常
|
||||
"""
|
||||
data = {
|
||||
"start": str((page_no - 1) * page_size),
|
||||
"sortType": "",
|
||||
"sortName": "",
|
||||
"pageNo": str(page_no),
|
||||
"pageSize": str(page_size),
|
||||
"staff_id": staff_id,
|
||||
"rootId": "",
|
||||
"countId": "",
|
||||
"bc_date": begin_date,
|
||||
"bc_date_end": end_date,
|
||||
"begin": begin_date,
|
||||
"end": end_date,
|
||||
"sub_type": sub_type,
|
||||
}
|
||||
logger.info("获取打卡明细: staff_id=%s, %s 至 %s, 第%d页", staff_id, begin_date, end_date, page_no)
|
||||
json_data = oa_request_json(session, "POST", PUNCH_RECORD_URL, data, "打卡明细")
|
||||
|
||||
if json_data.get("success") is not True or json_data.get("code") != 200:
|
||||
raise RuntimeError(f"打卡明细接口返回失败: {json_data}")
|
||||
|
||||
page_info = json_data.get("data")
|
||||
if not isinstance(page_info, dict):
|
||||
raise RuntimeError(f"打卡明细接口未返回 data 字段: {json_data}")
|
||||
return page_info
|
||||
|
||||
|
||||
def get_punch_records(
|
||||
session: SessionPage,
|
||||
staff_id: str,
|
||||
begin_date: str,
|
||||
end_date: str,
|
||||
page_size: int = DEFAULT_PAGE_SIZE,
|
||||
sub_type: str = "-1"
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
查询员工某时间段内的全部打卡明细(自动分页)。
|
||||
|
||||
Args:
|
||||
session: 已登录的 SessionPage 对象
|
||||
staff_id: 员工 UUID
|
||||
begin_date: 查询起始日期 "YYYY-MM-DD"
|
||||
end_date: 查询结束日期 "YYYY-MM-DD"
|
||||
page_size: 每页条数
|
||||
sub_type: 类型编码,默认 "-1" 表示全部
|
||||
|
||||
Returns:
|
||||
打卡明细列表,每条包含:
|
||||
- kq_time: 真实打卡时间 "YYYY-MM-DD HH:MM:SS"
|
||||
- bc_date: 考勤归属日 "YYYY-MM-DD"(跨天打卡次日0点~6点归入前一天)
|
||||
- time_order: 上下班类型 "1"=上班 "2"=下班
|
||||
- status: 打卡状态
|
||||
- address: 打卡地点
|
||||
|
||||
Raises:
|
||||
RuntimeError: 请求失败或响应异常
|
||||
"""
|
||||
all_records: List[Dict[str, Any]] = []
|
||||
|
||||
# 请求第一页,获取总条数与总页数
|
||||
page_info = fetch_punch_record_page(
|
||||
session, staff_id, begin_date, end_date,
|
||||
page_no=1, page_size=page_size, sub_type=sub_type
|
||||
)
|
||||
total = int(page_info.get("total", 0))
|
||||
page_total = int(page_info.get("pageCount", 1))
|
||||
records = page_info.get("list", []) or []
|
||||
all_records.extend(records)
|
||||
logger.info("打卡明细总数: %d, 第1页获取 %d 条", total, len(records))
|
||||
|
||||
# 若存在多页,继续翻页
|
||||
if page_total > 1:
|
||||
for p in range(2, page_total + 1):
|
||||
page_info = fetch_punch_record_page(
|
||||
session, staff_id, begin_date, end_date,
|
||||
page_no=p, page_size=page_size, sub_type=sub_type
|
||||
)
|
||||
records = page_info.get("list", []) or []
|
||||
all_records.extend(records)
|
||||
logger.info("第 %d 页获取 %d 条,累计 %d 条", p, len(records), len(all_records))
|
||||
|
||||
return all_records
|
||||
@@ -21,6 +21,7 @@ from app.service.hrbp.oa.work_overtime.parser import parse_overtime_form
|
||||
from app.service.hrbp.oa.common.oa_api import (
|
||||
get_attendance_daily,
|
||||
get_leave_records,
|
||||
get_punch_records,
|
||||
)
|
||||
from app.service.hrbp.oa.common.config import (
|
||||
WORK_START_TIME,
|
||||
@@ -187,80 +188,21 @@ def _extract_shift_times(daily_records: List[Dict[str, Any]], date_str: str) ->
|
||||
return None, None
|
||||
|
||||
|
||||
def _resolve_single_punch(
|
||||
day_summary: Optional[Dict[str, Any]],
|
||||
shift_begin: Optional[str],
|
||||
shift_end: Optional[str],
|
||||
) -> tuple:
|
||||
"""当天仅一条打卡时,结合班次时段判断它是上班还是下班打卡。
|
||||
|
||||
考勤逐日数据中当天只有一条打卡时 last_check_out 固定为 None,
|
||||
会把唯一一条下班打卡误判成"当日无下班打卡"。这里按打卡时间与班次时段的
|
||||
相对位置修正:晚于班次中点(无班次时段时默认以 12:00 为分界)的打卡视为
|
||||
下班打卡,赋给"当日最晚打卡";否则仍视为上班打卡。
|
||||
|
||||
Returns:
|
||||
(当日最早打卡, 当日最晚打卡) 修正后的元组
|
||||
"""
|
||||
if not day_summary:
|
||||
return None, None
|
||||
first = day_summary.get("first_check_in")
|
||||
last = day_summary.get("last_check_out")
|
||||
if last is not None:
|
||||
return first, last
|
||||
if not first:
|
||||
return None, None
|
||||
t = _parse_dt(first)
|
||||
if t is None:
|
||||
return None, None
|
||||
mins = t.hour * 60 + t.minute
|
||||
mid = 12 * 60
|
||||
if shift_begin and shift_end:
|
||||
try:
|
||||
sb = int(shift_begin[:2]) * 60 + int(shift_begin[3:5])
|
||||
se = int(shift_end[:2]) * 60 + int(shift_end[3:5])
|
||||
mid = (sb + se) // 2
|
||||
except (ValueError, IndexError):
|
||||
pass
|
||||
if mins >= mid:
|
||||
# 晚于班次中点:视为下班打卡
|
||||
return None, first
|
||||
return first, None
|
||||
|
||||
|
||||
# 每日考勤记录中携带的上下班打卡字段(sb=上班、xb=下班,可能分多个时段)
|
||||
_PUNCH_TIME_FIELDS = ("sb_dk_time", "xb_dk_time", "sb_dk_time2", "xb_dk_time2", "sb_dk_time3", "xb_dk_time3")
|
||||
|
||||
|
||||
def _collect_day_punches(record: Optional[Dict[str, Any]], work_day: str) -> List[str]:
|
||||
"""收集某日考勤记录中的全部上下班打卡时间。
|
||||
|
||||
打卡字段为 "HH:MM:SS"(无打卡为 null),这里拼上日期返回
|
||||
"YYYY-MM-DD HH:MM:SS" 完整时间并升序排序,供最早/最晚打卡提取与 _parse_dt 解析。
|
||||
"""
|
||||
if not record or not work_day:
|
||||
return []
|
||||
punches = []
|
||||
for field in _PUNCH_TIME_FIELDS:
|
||||
v = record.get(field)
|
||||
if v:
|
||||
s = str(v).strip()
|
||||
if s:
|
||||
punches.append(f"{work_day} {s}")
|
||||
punches.sort()
|
||||
return punches
|
||||
|
||||
|
||||
def _check_punch_records(session: Any, staff_id: str, ot_date: str) -> Dict[str, Any]:
|
||||
"""
|
||||
查询员工在加班日及次日的打卡明细,按日汇总。
|
||||
|
||||
加班常跨天(如 18:00 至次日 00:00),下班打卡可能落在次日,因此查询
|
||||
范围扩展到 ot_date 次日;次日最早打卡用于延时加班的弹性到岗兑换判定。
|
||||
另扩展到前一天:提前到岗型延时加班(开始<=6点)的到岗打卡可能被系统归到前一天。
|
||||
加班常跨天(如 18:00 至次日 00:00),下班打卡可能落在次日。打卡明细接口
|
||||
(rf_s_kq_data_queryByStaffId)返回 kq_time=真实打卡时间 与 bc_date=考勤归属日,
|
||||
跨天打卡(次日0点~6点)由系统归入加班当天,无需人工拼接/修正日期。
|
||||
查询范围扩展到 ot_date 前一天与次日:次日最早打卡用于延时加班的弹性到岗兑换判定,
|
||||
前一天最晚打卡用于提前到岗型延时加班(开始<=6点)到岗打卡被归到前一天的情况。
|
||||
|
||||
数据源统一为考勤逐日接口(rf_s_kq_count_leave_yc_day_all),一次调用即可拿到
|
||||
班次起止时间与各时段上下班打卡,替代原打卡流水接口。
|
||||
打卡按上下班类型(time_order:1=上班、2=下班)分类:
|
||||
- 当日最早打卡/次日最早打卡 = 当组最早的上班打卡;
|
||||
- 当日最晚打卡 = 当组最晚的下班打卡(真实下班时间,含跨天归入的下班打卡);
|
||||
- 前一天最晚打卡 = 前一天最晚一条打卡(不限类型,兼容提前到岗型跨天到岗打卡)。
|
||||
班次起止时间仍由考勤逐日接口提供。
|
||||
"""
|
||||
if not ot_date or not staff_id:
|
||||
return {"当日最早打卡": None, "当日最晚打卡": None, "次日最早打卡": None,
|
||||
@@ -272,31 +214,46 @@ def _check_punch_records(session: Any, staff_id: str, ot_date: str) -> Dict[str,
|
||||
next_day = (start + timedelta(days=1)).strftime("%Y-%m-%d")
|
||||
|
||||
try:
|
||||
daily = get_attendance_daily(session, staff_id, prev_day, next_day)
|
||||
prev_record = next((r for r in daily if str(r.get("work_day", "")) == prev_day), None)
|
||||
day_record = next((r for r in daily if str(r.get("work_day", "")) == ot_date), None)
|
||||
next_record = next((r for r in daily if str(r.get("work_day", "")) == next_day), None)
|
||||
if not day_record and not next_record and not prev_record:
|
||||
punches = get_punch_records(session, staff_id, prev_day, next_day)
|
||||
if not punches:
|
||||
return {"当日最早打卡": None, "当日最晚打卡": None, "次日最早打卡": None,
|
||||
"前一天最晚打卡": None, "班次开始时间": None, "班次结束时间": None,
|
||||
"打卡备注": "当天及次日无打卡记录"}
|
||||
# 按考勤归属日 bc_date 分组;time_order:1=上班打卡、2=下班打卡
|
||||
arrivals: Dict[str, List[str]] = {}
|
||||
checkouts: Dict[str, List[str]] = {}
|
||||
all_by_date: Dict[str, List[str]] = {}
|
||||
for r in punches:
|
||||
bd = str(r.get("bc_date", "")).strip()
|
||||
t = str(r.get("kq_time", "")).strip()
|
||||
if not bd or not t:
|
||||
continue
|
||||
all_by_date.setdefault(bd, []).append(t)
|
||||
if str(r.get("time_order", "")).strip() == "1":
|
||||
arrivals.setdefault(bd, []).append(t)
|
||||
elif str(r.get("time_order", "")).strip() == "2":
|
||||
checkouts.setdefault(bd, []).append(t)
|
||||
# 班次起止时间:取当日实际班次时段(如维护早班 08:00-17:00),
|
||||
# 未获取到时由上层回退默认 09:00/18:00;接口失败不影响打卡数据
|
||||
daily = get_attendance_daily(session, staff_id, prev_day, next_day)
|
||||
shift_begin, shift_end = _extract_shift_times(daily, ot_date)
|
||||
# 当日最早/最晚打卡;单条打卡时结合班次时段识别是上班还是下班打卡
|
||||
day_punches = _collect_day_punches(day_record, ot_date)
|
||||
day_summary = {
|
||||
"first_check_in": day_punches[0] if day_punches else None,
|
||||
# 仅1条打卡时 last_check_out 置空,交由 _resolve_single_punch 结合班次时段修正
|
||||
"last_check_out": day_punches[-1] if len(day_punches) > 1 else None,
|
||||
}
|
||||
day_first, day_last = _resolve_single_punch(day_summary, shift_begin, shift_end)
|
||||
|
||||
def _sorted(date_str: str) -> List[str]:
|
||||
return sorted(all_by_date.get(date_str, []))
|
||||
|
||||
# 当日最早/最晚打卡:优先按上下班类型取,缺该类型时回退取当天全部打卡
|
||||
day_all = _sorted(ot_date)
|
||||
day_arrivals = sorted(arrivals.get(ot_date, []))
|
||||
day_checkouts = sorted(checkouts.get(ot_date, []))
|
||||
day_first = day_arrivals[0] if day_arrivals else (day_all[0] if day_all else None)
|
||||
day_last = day_checkouts[-1] if day_checkouts else (day_all[-1] if day_all else None)
|
||||
# 次日最早打卡(弹性兑换用)
|
||||
next_punches = _collect_day_punches(next_record, next_day)
|
||||
next_first = next_punches[0] if next_punches else None
|
||||
next_arrivals = sorted(arrivals.get(next_day, []))
|
||||
next_all = _sorted(next_day)
|
||||
next_first = next_arrivals[0] if next_arrivals else (next_all[0] if next_all else None)
|
||||
# 前一天最晚打卡(提前到岗型加班用)
|
||||
prev_punches = _collect_day_punches(prev_record, prev_day)
|
||||
prev_last = prev_punches[-1] if prev_punches else None
|
||||
prev_all = _sorted(prev_day)
|
||||
prev_last = prev_all[-1] if prev_all else None
|
||||
return {
|
||||
"当日最早打卡": day_first,
|
||||
"当日最晚打卡": day_last,
|
||||
@@ -527,8 +484,8 @@ def _validate_day_ot(
|
||||
- 排班显示为休息;
|
||||
- 法定假日(仅春节):申请日期必须为春节法定节假日;
|
||||
- 加班时长 > 0;
|
||||
- 当天有上下班打卡(跨天打卡处理:当天无打卡时,若次日首条打卡早于6点,
|
||||
视为归属加班当天的跨天有效打卡,不再因缺打卡驳回);
|
||||
- 当天有上下班打卡(打卡明细接口按考勤归属日 bc_date 分组,跨天打卡次日0点~6点
|
||||
已归入加班当天,当日最早/最晚打卡即含跨天下班打卡);
|
||||
- 打卡时间间隔满足申报天数(0.5天>=4h, 1天>=9h, 1.5天>=13h, 2天>=18h...);
|
||||
职能排班半天(0.5天)例外:上午(开始时间<12:00)>=3.5h、下午>=4.5h,整天仍为9h。
|
||||
"""
|
||||
@@ -558,24 +515,14 @@ def _validate_day_ot(
|
||||
if ot_hour <= 0:
|
||||
reasons.append("加班时长为0或缺失")
|
||||
|
||||
# 当天有上下班打卡,且打卡时间间隔满足申报天数
|
||||
# 跨天打卡处理:当天缺打卡时参考次日首条打卡,次日首条早于6点视为归属加班当天
|
||||
# 当天有上下班打卡,且打卡时间间隔满足申报天数。
|
||||
# 打卡明细接口已按考勤归属日 bc_date 分组,跨天打卡(次日0点~6点)归入加班当天,
|
||||
# 因此当日最早/最晚打卡已包含跨天下班打卡,无需再参考次日打卡补全。
|
||||
first = _parse_dt(punch.get("当日最早打卡"))
|
||||
last = _parse_dt(punch.get("当日最晚打卡"))
|
||||
next_first = _parse_dt(punch.get("次日最早打卡"))
|
||||
interval = None
|
||||
if first is None or last is None:
|
||||
if next_first is not None and next_first.hour < CROSS_DAY_BEFORE_HOUR:
|
||||
# 次日首条早于6点 -> 加班当天跨天有效打卡,补全当天缺失的打卡
|
||||
had_punch = (first is not None) or (last is not None)
|
||||
first = first if first is not None else next_first
|
||||
last = last if last is not None else next_first
|
||||
info["跨天打卡"] = "是"
|
||||
# 当天全无打卡时仅凭跨天记录,视为满足"至少一条打卡"要求,不计算间隔
|
||||
if had_punch:
|
||||
interval = (last - first).total_seconds() / 3600
|
||||
else:
|
||||
reasons.append("当天缺少上班或下班打卡")
|
||||
reasons.append("当天缺少上班或下班打卡")
|
||||
else:
|
||||
interval = (last - first).total_seconds() / 3600
|
||||
|
||||
|
||||
Reference in New Issue
Block a user