Files
37duAutomation/app/service/hrbp/oa/leave/parser.py
T

72 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""
@Module: hrbp.oa.leave.parser
@FileName: parser.py
@Author: 小布丁
@CreateTime: 2026-08-06
@Description: 请假表单解析模块
@Version: 1.1.0
"""
from typing import Any, Dict
from app.utils.logger import get_logger
from app.service.hrbp.common.config import VACATION_TYPE_MAP
logger = get_logger(__name__)
# ---------- 假期类型编码 -> 名称映射 ----------
# 已统一收口到 HR 平台共享层(hrbp/common/config.py),此处直接复用,不再重复定义
# ---------- 请假表单解析 ----------
def _first_non_null(result: Dict[str, Any], *keys: str) -> Any:
"""返回 result 中第一个值非 None 的字段(注意:不能用 or 链,0.0 / 0 是合法值但为 falsy)。"""
for k in keys:
v = result.get(k)
if v is not None:
return v
return None
def _resolve_vacation_type(result: Dict[str, Any]) -> str:
"""从表单数据中提取假期类型名称(支持编码和名称两种输入)"""
raw = _first_non_null(result, "vacation_type", "leave_type", "sub_type")
raw = str(raw).strip() if raw is not None else ""
# 先尝试按编码映射
if raw in VACATION_TYPE_MAP:
return VACATION_TYPE_MAP[raw]
# 否则直接返回原始值(可能已是名称)
return raw
def _parse_leave_form(result: Dict[str, Any]) -> Dict[str, Any]:
"""
解析请假表单数据,提取关键字段。
Args:
result: WorkformEngine 返回的 result 字典(即 doc['formData'])
Returns:
包含请假关键字段的字典(中文键名)
"""
return {
"请假人员ID": result.get("staff_id"),
"工号": result.get("staff_code"),
"请假人员": result.get("staff_name"),
"所属部门": result.get("dept_org_name"),
"假期类型": _resolve_vacation_type(result),
# 日期区间以 begin_date/end_date 为准(start_time/end_time 可能跨天,不可用于取日期)
"开始日期": result.get("begin_date"),
"开始时间": result.get("start_time"),
"结束日期": result.get("end_date"),
"结束时间": result.get("end_time"),
# 注意:leave_days 可能为 0.0(小时假),不能用 or 链(会被 falsy 短路成 None)
"请假天数": _first_non_null(result, "leave_days", "leave_day", "leave_days_new", "ot_day"),
"请假小时": _first_non_null(result, "leave_hours", "leave_hour", "ot_hour"),
"请假原因": result.get("leave_reason"),
"附件": result.get("file"),
}