- libs: 新增 hr_sdk / hotel_report / dingtalk,更新 desktopauto - controlplane-next: SDK 管理模块(controller/entity/migrations)与前端视图 - electron-shell: 插件同步、注册表、runner 调整 - plugins: XQ0035/XQ0041/XQ0042/XQ0045_XQ0046/XQ0048/XQ0076/XQ0078 等版本更新 - tests/tools: 新增 SDK 兼容性检查与打包工具(sdk_pack.py、sdk_compat_check.py)
118 lines
4.8 KiB
Python
118 lines
4.8 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""考勤接口:逐日考勤 / 请假记录 / 打卡明细。
|
||
|
||
用法::
|
||
|
||
from hr_sdk import HrClient, attendance
|
||
|
||
client = HrClient(ctx.get_session("hr"))
|
||
days = attendance.daily(client, staff_id, "2026-10-01", "2026-10-31")
|
||
leaves = attendance.leaves(client, staff_id, "2026-10-01", "2026-10-31")
|
||
punches = attendance.punches(client, staff_id, "2026-10-01", "2026-10-31")
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import time
|
||
from typing import Dict, List
|
||
|
||
from .config import MAX_PAGES, REDSEA
|
||
from .errors import ApiError, SessionExpiredError
|
||
|
||
ATTENDANCE_DAILY_URL = f"{REDSEA}/redmagicapi/rf_s_kq_count_leave_yc_day_all/redApiExec.mc"
|
||
LEAVE_RECORD_URL = f"{REDSEA}/redmagicapi/rf_s_kq_leave_record_SelectByStaffID/redApiExec.mc"
|
||
PUNCH_RECORD_URL = f"{REDSEA}/redmagicapi/rf_s_kq_data_queryByStaffId/redApiExec.mc"
|
||
|
||
DEFAULT_PAGE_SIZE = 100
|
||
#: 逐日考勤按考勤状态各查一次:0=休息日 1=正常班 9=未打卡 11=异常
|
||
DAILY_STATUSES = ("0", "1", "9", "11")
|
||
|
||
|
||
def _check_success(data: dict, label: str) -> dict:
|
||
"""``redApiExec`` 用 ``success/code`` 信封(与 ``state`` 不同)。"""
|
||
if data.get("success") is True and str(data.get("code")) == "200":
|
||
return data
|
||
message = str(data.get("message") or data.get("msg") or data)
|
||
if any(hint in message for hint in ("会话", "登录")):
|
||
raise SessionExpiredError(f"{label}登录态失效:{message}")
|
||
raise ApiError(f"{label}返回失败:{message}", code=str(data.get("code") or ""),
|
||
payload=data)
|
||
|
||
|
||
def _page_records(client, url: str, payload: dict, label: str, *,
|
||
page_size: int = DEFAULT_PAGE_SIZE, max_pages: int = MAX_PAGES,
|
||
sleep: float = 0.2) -> List[dict]:
|
||
"""``data{list,total,pageCount}`` 形态的自动分页。"""
|
||
records: List[dict] = []
|
||
page_count = 1
|
||
for page_no in range(1, max_pages + 1):
|
||
body = dict(payload)
|
||
body.update({"start": str((page_no - 1) * page_size), "pageNo": str(page_no),
|
||
"pageSize": str(page_size)})
|
||
data = _check_success(client.request_json("POST", url, data=body, label=label),
|
||
label)
|
||
info = data.get("data") if isinstance(data.get("data"), dict) else {}
|
||
page_rows = info.get("list") or []
|
||
records.extend(page_rows)
|
||
if page_no == 1:
|
||
page_count = int(info.get("pageCount") or 1)
|
||
client.log(f"[INFO] {label}共 {info.get('total')} 条 / {page_count} 页")
|
||
if page_no >= page_count or not page_rows:
|
||
break
|
||
if sleep:
|
||
time.sleep(sleep)
|
||
return records
|
||
|
||
|
||
# ==================== 逐日考勤 ====================
|
||
|
||
def daily(client, staff_id: str, start: str, end: str) -> List[dict]:
|
||
"""按员工 + 日期范围查每日考勤(按 ``work_day`` 合并四种状态)。"""
|
||
by_day: Dict[str, dict] = {}
|
||
for status in DAILY_STATUSES:
|
||
params = {"staff_id": staff_id, "begin_date": start, "end_date": end,
|
||
"kq_status_total": status}
|
||
data = client.request_json("POST", ATTENDANCE_DAILY_URL, data=params,
|
||
label="考勤逐日")
|
||
_check_success(data, "考勤逐日")
|
||
for row in data.get("data") or []:
|
||
by_day.setdefault(str(row.get("work_day", "")), row)
|
||
records = [by_day[k] for k in sorted(by_day)]
|
||
client.log(f"[INFO] 逐日考勤合并 {len(records)} 天({staff_id} {start}~{end})")
|
||
return records
|
||
|
||
|
||
# ==================== 请假 / 打卡 ====================
|
||
|
||
def leaves(client, staff_id: str, start: str, end: str, *,
|
||
sub_type: str = "-1", page_size: int = DEFAULT_PAGE_SIZE) -> List[dict]:
|
||
"""请假记录(自动分页)。"""
|
||
payload = {"sub_type": sub_type, "staff_id": staff_id,
|
||
"begin_date": start, "end_date": end,
|
||
"sortType": "", "sortName": ""}
|
||
return _page_records(client, LEAVE_RECORD_URL, payload, "请假记录",
|
||
page_size=page_size)
|
||
|
||
|
||
def punches(client, staff_id: str, start: str, end: str, *,
|
||
sub_type: str = "-1", page_size: int = DEFAULT_PAGE_SIZE) -> List[dict]:
|
||
"""打卡明细(自动分页;``kq_time`` 真实打卡时间 / ``bc_date`` 考勤归属日)。"""
|
||
payload = {"staff_id": staff_id, "rootId": "", "countId": "",
|
||
"bc_date": start, "bc_date_end": end,
|
||
"begin": start, "end": end, "sub_type": sub_type,
|
||
"sortType": "", "sortName": ""}
|
||
return _page_records(client, PUNCH_RECORD_URL, payload, "打卡明细",
|
||
page_size=page_size)
|
||
|
||
|
||
__all__ = [
|
||
"ATTENDANCE_DAILY_URL",
|
||
"DAILY_STATUSES",
|
||
"DEFAULT_PAGE_SIZE",
|
||
"LEAVE_RECORD_URL",
|
||
"PUNCH_RECORD_URL",
|
||
"daily",
|
||
"leaves",
|
||
"punches",
|
||
]
|