refactor(hrbp/yingdao): 统一社保核对逻辑,优化异常处理与会话复用
1. 重构_si_check_batch和_si_preflight,统一异常处理策略:查询失败不再内部吞错而是上抛,由调用方处理重登或标注异常 2. 为_write_annotated新增session入参,支持调用方传入预构建会话避免重复预检 3. 新增失败工号集合统计,将查询失败的工号统一标注为需人工复核状态 4. 提前在导出入口做登录态预检,避免导出完成后才发现会话失效 5. 同步更新两个业务模块的代码逻辑,保持行为一致
This commit is contained in:
@@ -499,28 +499,29 @@ def _si_found_codes(response_json):
|
||||
|
||||
|
||||
def _si_check_batch(session, codes_batch) -> set:
|
||||
"""返回这一批中『能查到参保记录』的工号集合;请求失败返回空集合并告警。"""
|
||||
try:
|
||||
return _si_found_codes(_si_query_batch(session, codes_batch))
|
||||
except HRSessionExpiredError:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.warning("参保记录批量查询失败 %s:%s", codes_batch, e)
|
||||
return set()
|
||||
"""返回这一批中『能查到参保记录』的工号集合;查询失败上抛,由调用方决定重登或标注异常。"""
|
||||
return _si_found_codes(_si_query_batch(session, codes_batch))
|
||||
|
||||
|
||||
def _si_preflight(session):
|
||||
"""正式核对前先探测一次接口,确认 HR 登录态有效;过期则重新登录并重建会话。"""
|
||||
"""正式核对前先探测一次接口,确认 HR 登录态有效;过期/异常则重新登录并重建会话。"""
|
||||
try:
|
||||
_si_query_batch(session, ["0"])
|
||||
except HRSessionExpiredError:
|
||||
logger.warning("预检发现 HR 登录态已过期,正在重新登录")
|
||||
except HRSessionExpiredError as e:
|
||||
logger.warning("预检发现 HR 登录态已过期,正在重新登录: %s", e)
|
||||
return refresh_hr_session()
|
||||
except Exception as e:
|
||||
# 网络超时也可能由 cookies 过期(重定向到登录页后加载超时)引发,先重登重建会话再试
|
||||
logger.warning("预检查询异常,尝试重新登录后重试: %s", e)
|
||||
return refresh_hr_session()
|
||||
return session
|
||||
|
||||
|
||||
def _write_annotated(headers, rows, emp_col, mode, base_name, output_dir=".") -> str:
|
||||
"""逐批查 HR 参保记录,追加『社保记录查询结果』列,写出最终 .xlsx。"""
|
||||
def _write_annotated(headers, rows, emp_col, mode, base_name, output_dir=".", session=None) -> str:
|
||||
"""逐批查 HR 参保记录,追加『社保记录查询结果』列,写出最终 .xlsx。
|
||||
|
||||
session 由调用方预检后传入(复用,避免重复探测);为 None 时内部自行预检。
|
||||
"""
|
||||
if _XlsxWorkbook is None:
|
||||
raise RuntimeError("缺少 openpyxl 依赖,无法导出 .xlsx")
|
||||
|
||||
@@ -532,8 +533,10 @@ def _write_annotated(headers, rows, emp_col, mode, base_name, output_dir=".") ->
|
||||
seen.add(code)
|
||||
unique_codes.append(code)
|
||||
|
||||
session = _si_preflight(build_hr_session(auto_login=False))
|
||||
if session is None:
|
||||
session = _si_preflight(build_hr_session(auto_login=False))
|
||||
found = set()
|
||||
failed_codes = set()
|
||||
for i in range(0, len(unique_codes), SI_BATCH_SIZE):
|
||||
batch = unique_codes[i:i + SI_BATCH_SIZE]
|
||||
try:
|
||||
@@ -542,7 +545,15 @@ def _write_annotated(headers, rows, emp_col, mode, base_name, output_dir=".") ->
|
||||
logger.warning("HR 登录态过期,正在重新登录并续跑(第 %d/%d 批)",
|
||||
min(i + SI_BATCH_SIZE, len(unique_codes)), len(unique_codes))
|
||||
session = refresh_hr_session()
|
||||
found |= _si_check_batch(session, batch)
|
||||
try:
|
||||
found |= _si_check_batch(session, batch)
|
||||
except Exception as e:
|
||||
logger.warning("重登后该批仍查询失败,标注为查询异常: %s", e)
|
||||
failed_codes |= set(batch)
|
||||
except Exception as e:
|
||||
# 网络超时/其它异常:宁可标注查询异常,也不当作『无记录』避免误判
|
||||
logger.warning("参保记录查询失败(该批标注查询异常,需人工复核): %s", e)
|
||||
failed_codes |= set(batch)
|
||||
logger.info("参保记录核对 %d/%d 个工号", min(i + SI_BATCH_SIZE, len(unique_codes)), len(unique_codes))
|
||||
time.sleep(0.3)
|
||||
|
||||
@@ -550,11 +561,14 @@ def _write_annotated(headers, rows, emp_col, mode, base_name, output_dir=".") ->
|
||||
new_rows = []
|
||||
for row in rows:
|
||||
code = str(row[emp_col]).strip()
|
||||
has_record = code in found
|
||||
if is_participate:
|
||||
val = "异常:已参保(系统有该工号记录)" if has_record else "正常:系统无该工号记录"
|
||||
if code in failed_codes:
|
||||
val = "异常:参保记录查询失败,需人工复核"
|
||||
else:
|
||||
val = "异常:已停保(系统无该工号记录)" if not has_record else "正常:系统有该工号记录"
|
||||
has_record = code in found
|
||||
if is_participate:
|
||||
val = "异常:已参保(系统有该工号记录)" if has_record else "正常:系统无该工号记录"
|
||||
else:
|
||||
val = "异常:已停保(系统无该工号记录)" if not has_record else "正常:系统有该工号记录"
|
||||
new_rows.append(row + [val])
|
||||
|
||||
out_path = os.path.abspath(os.path.join(
|
||||
@@ -625,6 +639,9 @@ def export_employee_change_list(
|
||||
|
||||
base_params = {**DEFAULT_FIXED_PARAMS, **filter_params}
|
||||
|
||||
# 操作前预检 HR 登录态(核对必用),失效则自动重登,避免 OA 导出完成后才发现 HR 不可用
|
||||
hr_session = _si_preflight(build_hr_session(auto_login=False))
|
||||
|
||||
# 登录态失效时自动删除 cookie、重新登录并重跑一遍(最多 2 轮)
|
||||
for login_attempt in range(2):
|
||||
browser, session = get_session()
|
||||
@@ -664,7 +681,8 @@ def export_employee_change_list(
|
||||
mode = "suspension" if menu_name == MENU_NAME_SUSPEND else "participation"
|
||||
final_path = _write_annotated(
|
||||
headers, rows, emp_col, mode,
|
||||
os.path.splitext(os.path.basename(output_path))[0], output_dir)
|
||||
os.path.splitext(os.path.basename(output_path))[0], output_dir,
|
||||
session=hr_session)
|
||||
|
||||
# 8. 清理中间原始导出文件,只保留最终标注表
|
||||
try:
|
||||
|
||||
@@ -834,27 +834,31 @@ def _si_found_codes(response_json):
|
||||
|
||||
|
||||
def _si_check_batch(session, codes_batch):
|
||||
try:
|
||||
return _si_found_codes(_si_query_batch(session, codes_batch))
|
||||
except HRSessionExpiredError:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.warning("参保记录批量查询失败 %s:%s", codes_batch, e)
|
||||
return set()
|
||||
"""返回这一批中『能查到参保记录』的工号集合;查询失败上抛,由调用方决定重登或标注异常。"""
|
||||
return _si_found_codes(_si_query_batch(session, codes_batch))
|
||||
|
||||
|
||||
def _si_preflight(session):
|
||||
"""正式核对前先探测一次接口,确认 HR 登录态有效;过期则重新登录并重建 session。"""
|
||||
"""正式核对前先探测一次接口,确认 HR 登录态有效;过期/异常则重新登录并重建会话。"""
|
||||
try:
|
||||
_si_query_batch(session, ["0"])
|
||||
except HRSessionExpiredError:
|
||||
logger.warning("预检发现 HR 登录态已过期,正在重新登录")
|
||||
except HRSessionExpiredError as e:
|
||||
logger.warning("预检发现 HR 登录态已过期,正在重新登录: %s", e)
|
||||
hr_login()
|
||||
return _build_hr_session()
|
||||
except Exception as e:
|
||||
# 网络超时也可能由 cookies 过期(重定向到登录页后加载超时)引发,先重登重建会话再试
|
||||
logger.warning("预检查询异常,尝试重新登录后重试: %s", e)
|
||||
hr_login()
|
||||
return _build_hr_session()
|
||||
return session
|
||||
|
||||
|
||||
def _write_annotated(headers, rows, emp_col, mode, base_name, output_dir="."):
|
||||
def _write_annotated(headers, rows, emp_col, mode, base_name, output_dir=".", session=None):
|
||||
"""逐批查 HR 参保记录,追加『社保记录查询结果』列,写出最终 .xlsx。
|
||||
|
||||
session 由调用方预检后传入(复用,避免重复探测);为 None 时内部自行预检。
|
||||
"""
|
||||
unique_codes, seen = [], set()
|
||||
for row in rows:
|
||||
code = str(row[emp_col]).strip()
|
||||
@@ -862,8 +866,10 @@ def _write_annotated(headers, rows, emp_col, mode, base_name, output_dir="."):
|
||||
seen.add(code)
|
||||
unique_codes.append(code)
|
||||
|
||||
session = _si_preflight(get_hr_session())
|
||||
if session is None:
|
||||
session = _si_preflight(get_hr_session())
|
||||
found = set()
|
||||
failed_codes = set()
|
||||
for i in range(0, len(unique_codes), SI_BATCH_SIZE):
|
||||
batch = unique_codes[i:i + SI_BATCH_SIZE]
|
||||
try:
|
||||
@@ -873,7 +879,15 @@ def _write_annotated(headers, rows, emp_col, mode, base_name, output_dir="."):
|
||||
min(i + SI_BATCH_SIZE, len(unique_codes)), len(unique_codes))
|
||||
hr_login()
|
||||
session = _build_hr_session()
|
||||
found |= _si_check_batch(session, batch)
|
||||
try:
|
||||
found |= _si_check_batch(session, batch)
|
||||
except Exception as e:
|
||||
logger.warning("重登后该批仍查询失败,标注为查询异常: %s", e)
|
||||
failed_codes |= set(batch)
|
||||
except Exception as e:
|
||||
# 网络超时/其它异常:宁可标注查询异常,也不当作『无记录』避免误判
|
||||
logger.warning("参保记录查询失败(该批标注查询异常,需人工复核): %s", e)
|
||||
failed_codes |= set(batch)
|
||||
logger.info("参保记录核对 %d/%d 个工号", min(i + SI_BATCH_SIZE, len(unique_codes)), len(unique_codes))
|
||||
time.sleep(0.3)
|
||||
|
||||
@@ -881,11 +895,14 @@ def _write_annotated(headers, rows, emp_col, mode, base_name, output_dir="."):
|
||||
new_rows = []
|
||||
for row in rows:
|
||||
code = str(row[emp_col]).strip()
|
||||
has_record = code in found
|
||||
if is_participate:
|
||||
val = "异常:已参保(系统有该工号记录)" if has_record else "正常:系统无该工号记录"
|
||||
if code in failed_codes:
|
||||
val = "异常:参保记录查询失败,需人工复核"
|
||||
else:
|
||||
val = "异常:已停保(系统无该工号记录)" if not has_record else "正常:系统有该工号记录"
|
||||
has_record = code in found
|
||||
if is_participate:
|
||||
val = "异常:已参保(系统有该工号记录)" if has_record else "正常:系统无该工号记录"
|
||||
else:
|
||||
val = "异常:已停保(系统无该工号记录)" if not has_record else "正常:系统有该工号记录"
|
||||
new_rows.append(row + [val])
|
||||
|
||||
out_path = os.path.abspath(os.path.join(output_dir, f"{base_name}_参保核对.xlsx"))
|
||||
@@ -931,6 +948,9 @@ def export_employee_change_list(menu_name, filter_idx, columns, output_dir, quer
|
||||
logger.info("社保增减名单核对流程开始,菜单:%s", menu_name)
|
||||
base_params = {**DEFAULT_FIXED_PARAMS, **_build_filter_params(filter_idx, query_date)}
|
||||
|
||||
# 操作前预检 HR 登录态(核对必用),失效则自动重登,避免 OA 导出完成后才发现 HR 不可用
|
||||
hr_session = _si_preflight(get_hr_session())
|
||||
|
||||
# 登录态失效时自动删除 cookie、重新登录并重跑一遍(最多 2 轮)
|
||||
for login_attempt in range(2):
|
||||
browser, session = get_oa_session()
|
||||
@@ -955,7 +975,8 @@ def export_employee_change_list(menu_name, filter_idx, columns, output_dir, quer
|
||||
mode = "suspension" if menu_name == MENU_NAME_SUSPEND else "participation"
|
||||
final_path = _write_annotated(
|
||||
headers, rows, emp_col, mode,
|
||||
os.path.splitext(os.path.basename(output_path))[0], output_dir)
|
||||
os.path.splitext(os.path.basename(output_path))[0], output_dir,
|
||||
session=hr_session)
|
||||
try:
|
||||
os.remove(output_path)
|
||||
logger.info("已清理中间原始导出文件:%s", output_path)
|
||||
|
||||
Reference in New Issue
Block a user