# -*- coding: utf-8 -*- """ @Module: hrbp.oa.common.oa_api @FileName: attendance.py @Description: 排班 HTML 与考勤打卡明细接口。 @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 PAIBAN_URL, KQ_DATA_URL from app.service.hrbp.oa.common.oa_api.base import oa_request_json logger = get_logger(__name__) def get_attendance_html( session: SessionPage, staff_id: str, year: int, month: int ) -> str: """ 获取指定员工某月的排班 HTML。 Args: session: 已登录的 SessionPage 对象 staff_id: 员工 UUID year: 年份(如 2026) month: 月份(1-12) Returns: 排班表格的 HTML 字符串(从返回 JSON 的 caleHtml 字段提取) Raises: RuntimeError: 请求失败或响应异常 """ url = f"{PAIBAN_URL}?method=geRenPaibanByMonth" data = { "type": "2", "staffId": staff_id, "year": str(year), "month": str(month) } logger.info("获取排班: %s %d-%02d", staff_id, year, month) json_data = oa_request_json(session, "POST", url, data, "排班") if json_data.get("success") != "true": raise RuntimeError(f"排班接口返回失败: {json_data}") html = json_data.get("caleHtml") if not html: raise RuntimeError("排班接口未返回 caleHtml 字段") return html def get_attendance_records( session: SessionPage, staff_id: str, start_date: str, end_date: str ) -> List[Dict[str, Any]]: """ 查询员工某时间段的原始打卡明细。 Args: session: 已登录的 SessionPage 对象 staff_id: 员工 UUID start_date: 查询起始日期 "YYYY-MM-DD" end_date: 查询结束日期 "YYYY-MM-DD" Returns: 打卡记录列表(接口返回的 jsonList,每条含 kq_time / status / time_order 等) - status: "1"=上班打卡, "5"=下班打卡, "6"=重复打卡 - time_order: "1"=上班时段, "2"=下班时段 Raises: RuntimeError: 请求失败或响应异常 """ params = { "staff_id": staff_id, "rootId": "", "countId": "", "bc_date": start_date, "bc_date_end": end_date, "begin": start_date, "end": end_date, } logger.info("获取打卡明细: staff_id=%s, %s 至 %s", staff_id, start_date, end_date) json_data = oa_request_json(session, "GET", KQ_DATA_URL, params, "考勤打卡") records = json_data.get("jsonList", []) logger.info("打卡明细返回 %d 条", len(records)) return records