1. 更新影刀HR登录的测试账号密码 2. 新增common_query加密工具类,实现HR平台查询参数加密逻辑 3. 新增组织维护API接口实现与导出工具 4. 更新HR配置文件,添加组织维护接口常量与参数 5. 更新API导出模块,新增组织数据接口导出支持
173 lines
6.1 KiB
Python
173 lines
6.1 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
@Module: hrbp.ehr.common.hr_api
|
||
@FileName: org.py
|
||
@Description: 组织维护(组织设置)台账查询接口
|
||
@Version: 1.0.0
|
||
"""
|
||
|
||
import time
|
||
from datetime import datetime, timedelta
|
||
from typing import Any, Dict, List
|
||
|
||
from app.utils.logger import get_logger
|
||
from app.service.hrbp.ehr.common.config import (
|
||
ORG_QUERY_URL,
|
||
ORG_QUERY_BASE_PARAMS,
|
||
PAGE_MAX_SIZE,
|
||
)
|
||
from app.service.hrbp.common.hr_session import HRSessionExpiredError, hr_request_json
|
||
|
||
logger = get_logger(__name__)
|
||
|
||
# 接口字段 → 页面列名映射(「序号」接口不返回,转换时按行号生成;
|
||
# 分管部门对应 pt_structure2.ORG_ALIAS,当前数据未使用但列位保留)
|
||
ORG_FIELD_MAP = [
|
||
("pt_organzation.ORG_NAME", "组织名称"),
|
||
("pt_organzation.ORG_CODE", "组织编号"),
|
||
("pt_organzation.ORG_TYPE_CODE", "组织类型"),
|
||
("pt_structure1.ORG_ALIAS", "上级组织"),
|
||
("org_principal_staff_name_select.org_principal_staff_name", "组织负责人"),
|
||
("pt_structure2.ORG_ALIAS", "分管部门"),
|
||
("branch_leader_name_select.branch_leader_name", "分管领导"),
|
||
("pt_organzation.BEGIN_DATE", "成立日期"),
|
||
("pt_orgnization_extend.FOUND_DOC_NUM", "成立文号"),
|
||
("pt_organzation.CANT_CODE", "所属地区"),
|
||
("pt_organzation.office_address", "办公地址"),
|
||
("pt_organzation.remarks", "备注"),
|
||
("pt_orgnization_extend.FOUND_REASON", "成立原因"),
|
||
("pt_structure.ORG_FULL_PATH", "组织全路径"),
|
||
("pt_organzation.create_time", "创建时间"),
|
||
("pt_organzation.IN_USE", "使用状态"),
|
||
("pt_organzation_ext.ext_payday", "发薪日"),
|
||
]
|
||
|
||
# 使用状态编码 → 文字
|
||
IN_USE_MAP = {"1": "在用", "0": "停用"}
|
||
|
||
|
||
def to_org_rows(datalist: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||
"""将接口原始记录转换为页面列名顺序的字典列表(18 列,含序号)。
|
||
|
||
Args:
|
||
datalist: fetch_all_orgs / fetch_org_page 返回的 datas 原始记录列表
|
||
|
||
Returns:
|
||
按页面列顺序组织好的字典列表(序号/组织名称/.../发薪日)
|
||
"""
|
||
rows: List[Dict[str, Any]] = []
|
||
for i, r in enumerate(datalist, 1):
|
||
row: Dict[str, Any] = {"序号": i}
|
||
for key, name in ORG_FIELD_MAP:
|
||
value = r.get(key)
|
||
if name == "使用状态":
|
||
value = IN_USE_MAP.get(str(value), value)
|
||
elif name == "成立日期" and value:
|
||
value = str(value).split(" ")[0] # 去掉 00:00:00 时分秒
|
||
row[name] = value
|
||
rows.append(row)
|
||
return rows
|
||
|
||
|
||
def get_date_params() -> Dict[str, str]:
|
||
"""生成组织维护查询所需的动态日期参数(当月 / 上月 / 当年首月)。
|
||
|
||
Returns:
|
||
含 currentDate / currentMonth / currentMonthBar / lastMonth /
|
||
lastMonFirstDay / lastMonLastDay / curYearFirstMon / curYearFirstMonBar 的字典
|
||
"""
|
||
now = datetime.now()
|
||
year, month, day = now.year, now.month, now.day
|
||
|
||
# 上月(本月 1 号减一天即上月最后一天)
|
||
last_month_date = datetime(year, month, 1) - timedelta(days=1)
|
||
|
||
return {
|
||
"currentDate": f"{year}-{month:02d}-{day:02d}",
|
||
"currentMonth": f"{year}{month:02d}",
|
||
"currentMonthBar": f"{year}-{month:02d}",
|
||
"lastMonth": f"{last_month_date.year}{last_month_date.month:02d}",
|
||
"lastMonFirstDay": f"{last_month_date.year}-{last_month_date.month:02d}-01",
|
||
"lastMonLastDay": last_month_date.strftime("%Y-%m-%d"),
|
||
"curYearFirstMon": f"{year}01",
|
||
"curYearFirstMonBar": f"{year}-01",
|
||
}
|
||
|
||
|
||
def fetch_org_page(
|
||
session,
|
||
page_no: int = 1,
|
||
page_size: int = PAGE_MAX_SIZE,
|
||
) -> Dict[str, Any]:
|
||
"""
|
||
请求单页组织维护数据(aa.md 提供的接口入参)。
|
||
|
||
Args:
|
||
session: 已登录的 requests.Session 对象
|
||
page_no: 页码(从 1 开始)
|
||
page_size: 每页条数(最大 500)
|
||
|
||
Returns:
|
||
接口返回的 JSON 字典(datas 为组织明细数组,count 总条数,pageCount 总页数)
|
||
|
||
Raises:
|
||
RuntimeError: 请求失败或响应异常
|
||
HRSessionExpiredError: 登录态过期(接口返回非 JSON)
|
||
"""
|
||
params = ORG_QUERY_BASE_PARAMS.copy()
|
||
params.update(get_date_params())
|
||
params.update({
|
||
"pageNo": str(page_no),
|
||
"pageSize": str(page_size),
|
||
"start": str((page_no - 1) * page_size),
|
||
"_t": str(int(time.time() * 1000)),
|
||
})
|
||
return hr_request_json(session, "POST", ORG_QUERY_URL, params, "组织维护")
|
||
|
||
|
||
def fetch_all_orgs(session) -> List[Dict[str, Any]]:
|
||
"""
|
||
分页抓取全部组织维护数据(在职组织,in_use=1)。
|
||
|
||
Args:
|
||
session: 已登录的 requests.Session 对象
|
||
|
||
Returns:
|
||
组织明细列表(接口 datas 字段内容,每条含 ORG_ID / ORG_NAME / ORG_CODE /
|
||
ORG_FULL_PATH / STRU_LEVEL / STRU_ORDER 等字段)
|
||
|
||
Raises:
|
||
HRSessionExpiredError: 登录态过期且重新登录后仍失败
|
||
"""
|
||
all_rows: List[Dict[str, Any]] = []
|
||
page_no = 1
|
||
while True:
|
||
try:
|
||
data = fetch_org_page(session, page_no, PAGE_MAX_SIZE)
|
||
except HRSessionExpiredError:
|
||
logger.warning("组织维护 HR 登录态过期,重新登录后续抓")
|
||
from app.service.hrbp.common.hr_session import refresh_hr_session
|
||
session = refresh_hr_session()
|
||
data = fetch_org_page(session, page_no, PAGE_MAX_SIZE)
|
||
|
||
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 = data.get("count", 0)
|
||
total_page = data.get("pageCount", 0)
|
||
logger.info("组织维护已抓取 %d/%d 页,累计 %d/%d 条",
|
||
page_no, total_page, len(all_rows), total)
|
||
if page_no >= total_page:
|
||
break
|
||
page_no += 1
|
||
time.sleep(0.2) # 轻微限速,避免触发风控
|
||
return all_rows
|