fix(OA请假): 修复OA请假选择代理人乱码问题

This commit is contained in:
runny committed 2026-07-13 11:00:09 +08:00
1 parent 6d39e474f5
commit db067085e4
1 file changed
+54 -13
+54 -13
View File
@@ -1,8 +1,49 @@
"""
backend/app/services/oa_sqlserver.py
用途:封装 OA SQL Server 的员工检索与组织信息查询能力,供请假代理人/申请人选择等场景复用。
依赖:pyodbc、OA ecology 数据库视图 userInfo_view / HrmDepartment / HrmSubCompany / HrmJobTitles。
逻辑:通过只读 SQL 查询返回 OA 用户与组织字段;对于 OA 中夹带的多语言包装串,统一在服务层清洗成可直接展示的中文文本。
"""
import os
import re
import pyodbc
from flask import current_app
_OA_MULTILINGUAL_WRAPPER_PATTERN = re.compile(
r"~`~`7\s*(.*?)\s*~`8\s*(.*?)\s*~`9\s*(.*?)\s*~`~"
)
class OASQLServerService:
@staticmethod
def _normalize_oa_text(value) -> str:
"""
清洗 OA 多语言包装字段。
AI 触发时机:当 OA 视图返回的姓名、部门、分部、职务等字段中夹带
`~`~`7 中文 ~`8 English ~`9 繁中 ~`~` 这类原始包装串时,需统一提取
可直接给用户展示的文本,避免在代理人选择弹窗中看到乱码样式的标记串。
Args:
value: 来自 OA SQL Server 的原始字段值。
Returns:
优先返回简体中文,其次英文、繁中;若未命中包装规则,则返回原始去空白文本。
"""
text = str(value or "").strip()
if not text:
return ""
matched = _OA_MULTILINGUAL_WRAPPER_PATTERN.search(text)
if not matched:
return text
simplified, english, traditional = (
segment.strip().strip("`").strip()
for segment in matched.groups()
)
return simplified or english or traditional or text
@staticmethod
def _pick_driver():
drivers = pyodbc.drivers()
@@ -96,13 +137,13 @@ class OASQLServerService:
items.append({
"uid": str(row.id) if row.id is not None else "",
"employeeId": str(row.workcode) if row.workcode is not None else "",
"name": str(row.lastname) if row.lastname is not None else "",
"jobtitle": str(row.jobtitle) if row.jobtitle is not None else "",
"name": cls._normalize_oa_text(row.lastname),
"jobtitle": cls._normalize_oa_text(row.jobtitle),
"departmentid": str(row.departmentid) if row.departmentid is not None else "",
"subcompanyid1": str(row.subcompanyid1) if row.subcompanyid1 is not None else "",
"departmentName": str(row.departmentname).strip() if row.departmentname else "",
"subcompanyName": str(row.subcompanyname).strip() if row.subcompanyname else "",
"jobtitleName": str(row.jobtitlename).strip() if row.jobtitlename else "",
"departmentName": cls._normalize_oa_text(row.departmentname),
"subcompanyName": cls._normalize_oa_text(row.subcompanyname),
"jobtitleName": cls._normalize_oa_text(row.jobtitlename),
})
return {
@@ -145,13 +186,13 @@ class OASQLServerService:
return {
"uid": str(row.id) if row.id is not None else "",
"employeeId": str(row.workcode) if row.workcode is not None else "",
"name": str(row.lastname) if row.lastname is not None else "",
"jobtitle": str(row.jobtitle) if row.jobtitle is not None else "",
"name": cls._normalize_oa_text(row.lastname),
"jobtitle": cls._normalize_oa_text(row.jobtitle),
"departmentid": str(row.departmentid) if row.departmentid is not None else "",
"subcompanyid1": str(row.subcompanyid1) if row.subcompanyid1 is not None else "",
"departmentName": str(row.departmentname).strip() if row.departmentname else "",
"subcompanyName": str(row.subcompanyname).strip() if row.subcompanyname else "",
"jobtitleName": str(row.jobtitlename).strip() if row.jobtitlename else "",
"departmentName": cls._normalize_oa_text(row.departmentname),
"subcompanyName": cls._normalize_oa_text(row.subcompanyname),
"jobtitleName": cls._normalize_oa_text(row.jobtitlename),
}
finally:
try:
@@ -171,7 +212,7 @@ class OASQLServerService:
[department_id.strip()],
)
row = cursor.fetchone()
return str(row.departmentname).strip() if row and row.departmentname else ""
return cls._normalize_oa_text(row.departmentname) if row and row.departmentname else ""
finally:
try:
cursor.close()
@@ -190,7 +231,7 @@ class OASQLServerService:
[subcompany_id.strip()],
)
row = cursor.fetchone()
return str(row.subcompanyname).strip() if row and row.subcompanyname else ""
return cls._normalize_oa_text(row.subcompanyname) if row and row.subcompanyname else ""
finally:
try:
cursor.close()
@@ -209,7 +250,7 @@ class OASQLServerService:
[jobtitle_id.strip()],
)
row = cursor.fetchone()
return str(row.jobtitlename).strip() if row and row.jobtitlename else ""
return cls._normalize_oa_text(row.jobtitlename) if row and row.jobtitlename else ""
finally:
try:
cursor.close()