# -*- coding: utf-8 -*- """ @Module: hrbp.ehr.common.hr_api @FileName: attendance.py @Description: 当前用户信息 + 考勤异常查询/确认接口 @Version: 1.0.0 """ import time from typing import Any, Dict, List from app.utils.logger import get_logger from app.service.hrbp.ehr.common.config import ( GET_CUR_USER_INFO_URL, ATTENDANCE_ABNORMAL_URL, ATTENDANCE_ADJUST_URL, PAGE_MAX_SIZE, ) from app.service.hrbp.common.hr_session import HRSessionExpiredError, hr_request_json logger = get_logger(__name__) def fetch_cur_user_info(session) -> Dict[str, Any]: """ 调用 getCurUserInfo 校验 cookies 有效性,并返回当前登录用户信息。 该接口同时返回查询考勤异常所需的 userId 与 rootId(treecode): - 正常返回 JSON(含 userId / rootId),说明 cookies 有效; - 返回登录页等非 JSON 内容,说明登录态过期。 Returns: 接口返回的 JSON 字典(含 userId / rootId 等字段) Raises: RuntimeError: 请求失败或响应异常 HRSessionExpiredError: 登录态过期(接口返回非 JSON) """ return hr_request_json(session, "POST", GET_CUR_USER_INFO_URL, {}, "获取当前用户信息") def adjust_abnormal( session, abnormal_ids, type_code: int = 1, ) -> Dict[str, Any]: """ 确认/调整考勤异常单据。 Args: session: 已登录的 requests.Session 对象 abnormal_ids: 异常单据唯一编号 abnormal_id,可为单个 id 或 id 列表 type_code: 处理类型,1=自动确认异常(按接口约定) Returns: 接口返回的 JSON 字典(state=1 表示处理成功) Raises: RuntimeError: 请求失败或响应异常 HRSessionExpiredError: 登录态过期(接口返回非 JSON) """ if isinstance(abnormal_ids, (list, tuple)): ids = ",".join(str(i) for i in abnormal_ids) else: ids = str(abnormal_ids) params = {"abnormalIds": ids, "type": type_code} return hr_request_json(session, "POST", ATTENDANCE_ADJUST_URL, params, "考勤异常确认") def fetch_attendance_abnormal_page( session, user_id: str, treecode: str, begin_date: str, end_date: str, page_no: int = 1, page_size: int = PAGE_MAX_SIZE, is_adjusted: str = "0", abnormal_type: str = "", ) -> Dict[str, Any]: """ 请求单页考勤异常数据(aa.md 提供的接口入参)。 Args: session: 已登录的 requests.Session 对象 user_id / treecode: 由 getCurUserInfo 返回的 userId / rootId begin_date / end_date: 查询日期范围 "YYYY-MM-DD"(当月) page_no: 页码(从 1 开始) page_size: 每页条数 is_adjusted: 调整状态过滤,0=未调整、1=补签调整、2=确认异常、3=正常调整、空=所有状态 abnormal_type: 异常类型过滤,2=旷工类(旷工/上班旷工/下班旷工),空=全部 Returns: 接口返回的 JSON 字典(datas 为明细数组,pageCount 为总页数) Raises: RuntimeError: 请求失败或响应异常 HRSessionExpiredError: 登录态过期(接口返回非 JSON) """ params = { "treecode": treecode, "user_id": user_id, "commonQuery": "", "commonQueryStr": "", "sqlId": "kq_count_abnormal_Select", "redapiId": "rf_p_kq_count_abnormal_Select", "treeCodeList": "", "abnormal_type": abnormal_type, "base_code": "", "join_date": "", "join_date_end": end_date, "person_status": "", "staff_id": "", "post_no": "", "post_type": "1", "period_order": "", "identity_test": "", "identity": "", "sex": "", "age": "", "age_end": "", "start_operat_time": "", "end_operat_time": "", "operator_username": "", "operator_userid": "", "is_adjusted": is_adjusted, "begin_date": begin_date, "end_date": end_date, "STAFF_NAME": "", "start": str((page_no - 1) * page_size), "sortType": "desc", "sortName": "work_day", "pageNo": page_no, "pageSize": page_size, } return hr_request_json(session, "POST", ATTENDANCE_ABNORMAL_URL, params, "考勤异常") def fetch_all_attendance_abnormal( session, user_id: str, treecode: str, begin_date: str, end_date: str, is_adjusted: str = "0", abnormal_type: str = "", ) -> List[Dict[str, Any]]: """ 分页抓取指定日期范围内的全部考勤异常数据。 Args: session: 已登录的 requests.Session 对象 user_id / treecode: 由 getCurUserInfo 返回的 userId / rootId begin_date / end_date: 查询日期范围 "YYYY-MM-DD" is_adjusted: 调整状态过滤,0=未调整、1=补签调整、2=确认异常、3=正常调整、空=所有状态 abnormal_type: 异常类型过滤,2=旷工类(旷工/上班旷工/下班旷工),空=全部 Returns: 异常明细列表(接口 datas 字段内容) Raises: HRSessionExpiredError: 登录态过期且重新登录后仍失败 """ all_rows: List[Dict[str, Any]] = [] page_no = 1 while True: try: data = fetch_attendance_abnormal_page( session, user_id, treecode, begin_date, end_date, page_no, PAGE_MAX_SIZE, is_adjusted, abnormal_type ) except HRSessionExpiredError: logger.warning("考勤异常 HR 登录态过期,重新登录后续抓") from app.service.hrbp.common.hr_session import refresh_hr_session session = refresh_hr_session() # 重新登录后需重新获取 user_id / treecode info = fetch_cur_user_info(session) user_id = info.get("userId", "") treecode = info.get("rootId", "") data = fetch_attendance_abnormal_page( session, user_id, treecode, begin_date, end_date, page_no, PAGE_MAX_SIZE, is_adjusted, abnormal_type ) if data.get("state") != "1": logger.error("考勤异常第%d页请求失败:%s", page_no, data.get("meg")) break page_list = data.get("datas", []) or [] if not page_list: logger.info("考勤异常无更多数据") break all_rows.extend(page_list) total_page = data.get("pageCount", 0) logger.info("考勤异常已抓取 %d/%d 页,累计 %d 条", page_no, total_page, len(all_rows)) if page_no >= total_page: break page_no += 1 time.sleep(0.2) # 轻微限速,避免触发风控 return all_rows