138 lines
5.7 KiB
Python
138 lines
5.7 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""XQ0010 EHR系统月考勤异常处理 插件 - 数据层(门面:统一走 hr_sdk)。
|
||
|
||
插件不再内置 HR HTTP 调用:接口 URL、分页、`state` 信封、登录态失效识别
|
||
全部由执行机下发的 `hr_sdk`(`libs/hr_sdk`)提供,本文件只做三件事:
|
||
1. 懒加载 SDK(SDK 由执行机按 plugin.json.requires.sdks 挂载);
|
||
2. 把 SDK 的日志/异常翻译成插件既有口径(ctx.log、RuntimeError 提示重新登录);
|
||
3. 保留原有函数签名,业务层(report/logic)无需感知 SDK。
|
||
|
||
SDK 覆盖:`attendance.abnormal / overtime / leave_records / punch_records`、
|
||
`shift.person`、`HrClient.current_user`。
|
||
"""
|
||
from ..config import logger
|
||
|
||
|
||
def _sdk_log(msg):
|
||
"""SDK 日志回调 → config.logger(按消息前缀映射级别)。"""
|
||
text = str(msg)
|
||
if "[WARN]" in text:
|
||
logger.warning("%s", text)
|
||
elif "[ERROR]" in text:
|
||
logger.error("%s", text)
|
||
else:
|
||
logger.info("%s", text)
|
||
|
||
|
||
def _client(session):
|
||
"""构造借用会话的 HrClient(懒加载:SDK 由执行机挂载,开发态缺包时给出明确提示)。"""
|
||
try:
|
||
from hr_sdk import HrClient
|
||
except ImportError as exc: # pragma: no cover - 仅运行环境缺 SDK 时触发
|
||
raise RuntimeError(
|
||
"缺少 hr_sdk(HR 接口 SDK):请确认执行机已同步安装本插件声明的 SDK 依赖") from exc
|
||
return HrClient(session, log=_sdk_log)
|
||
|
||
|
||
def _run(label, fn, *args, **kwargs):
|
||
"""执行 SDK 调用并翻译异常(登录态失效提示重新登录,业务错误抛 RuntimeError)。"""
|
||
from hr_sdk.errors import HrError, SessionExpiredError
|
||
|
||
try:
|
||
return fn(*args, **kwargs)
|
||
except SessionExpiredError as exc:
|
||
raise RuntimeError(
|
||
f"{label}:HR 登录态已失效,无法自动补登。请在插件界面重新完成 HR 登录后重试。") from exc
|
||
except HrError as exc:
|
||
raise RuntimeError(f"{label}失败:{exc}") from exc
|
||
|
||
|
||
def validate_session(session, user_id):
|
||
"""调用 getCurUserInfo 校验会话有效性,返回 (session, user_id, treecode)。
|
||
|
||
登录态失效时直接抛错提示重新登录(插件不做自动补登)。
|
||
"""
|
||
from hr_sdk.errors import HrError, SessionExpiredError
|
||
|
||
client = _client(session)
|
||
try:
|
||
info = client.current_user(refresh=True)
|
||
except SessionExpiredError as exc:
|
||
raise RuntimeError(
|
||
"会话校验:HR 登录态已失效,无法自动补登。请在插件界面重新完成 HR 登录后重试。"
|
||
) from exc
|
||
except HrError as exc:
|
||
raise RuntimeError(f"会话校验失败:{exc}") from exc
|
||
user_id = str(user_id or info.get("userId", ""))
|
||
treecode = str(info.get("rootId", "") or "")
|
||
if not user_id or not treecode:
|
||
raise RuntimeError(f"员工基础信息接口未返回 userId/rootId:{info}")
|
||
logger.info("HR 会话校验通过,user_id=%s,treecode=%s", user_id, treecode)
|
||
return session, user_id, treecode
|
||
|
||
|
||
# ==================== 考勤异常 ====================
|
||
def fetch_all_attendance_abnormal(session, user_id, treecode, begin_date, end_date,
|
||
is_adjusted=""):
|
||
"""分页拉取全部考勤异常(`is_adjusted`:0=未调整、2=确认异常、空=全部)。"""
|
||
from hr_sdk import attendance
|
||
|
||
return _run("考勤异常", attendance.abnormal, _client(session), begin_date, end_date,
|
||
is_adjusted=is_adjusted, treecode=treecode, user_id=user_id)
|
||
|
||
|
||
# ==================== 请假 ====================
|
||
def fetch_all_leave(session, user_id, base_code, start_time, end_time,
|
||
treecode="cq37du"):
|
||
"""分页抓取指定工号、日期范围内的全部请假记录(空工号=全量)。"""
|
||
from hr_sdk import attendance
|
||
|
||
return _run("请假查询", attendance.leave_records, _client(session), start_time, end_time,
|
||
base_code=base_code, treecode=treecode, user_id=user_id)
|
||
|
||
|
||
# ==================== 加班 ====================
|
||
def fetch_all_overtime(session, user_id, base_code, start_time, end_time,
|
||
work_overtime_type="", treecode="cq37du"):
|
||
"""分页抓取指定工号、日期范围内的全部加班记录(空工号=全量)。"""
|
||
from hr_sdk import attendance
|
||
|
||
return _run("加班查询", attendance.overtime, _client(session), start_time, end_time,
|
||
work_type=work_overtime_type, base_code=base_code,
|
||
treecode=treecode, user_id=user_id)
|
||
|
||
|
||
# ==================== 打卡 ====================
|
||
def fetch_all_kq_data(session, user_id, base_code, start_time, end_time,
|
||
treecode="cq37du"):
|
||
"""分页抓取指定工号、日期范围内的全部打卡记录。"""
|
||
from hr_sdk import attendance
|
||
|
||
return _run("打卡查询", attendance.punch_records, _client(session), base_code,
|
||
start_time, end_time, treecode=treecode, user_id=user_id)
|
||
|
||
|
||
# ==================== 排班 ====================
|
||
def fetch_all_shift(session, user_id, base_code, begin_date, end_date,
|
||
treecode="cq37du"):
|
||
"""分页抓取指定工号、日期范围内的全部排班数据(动态列 `{date}_bc_time`)。"""
|
||
from hr_sdk import shift
|
||
|
||
return _run("排班", shift.person, _client(session), user_id, base_code,
|
||
begin_date, end_date, treecode=treecode)
|
||
|
||
|
||
# ==================== 会话构建(插件侧仅转发,不自行读 cookies) ====================
|
||
def build_hr_session():
|
||
"""取底座注入的 HR session(等价于影刀版 build_hr_session)。"""
|
||
from ..config import require_session
|
||
|
||
return require_session()
|
||
|
||
|
||
def get_user_id():
|
||
"""取底座注入的 HR userId。"""
|
||
from ..config import require_user_id
|
||
|
||
return require_user_id()
|