1. 重构会话管理模块,新增app.common.app_paths统一管理缓存路径,所有登录态与缓存统一迁移至%LOCALAPPDATA%\37duAutomation\.sessions 2. 更新所有涉及文件存储路径的代码,包括考勤、工资卡、社保、审批等模块 3. 新增银行字典缓存功能,统一管理银行编码映射 4. 添加雇主险业务模块,实现花名册筛选复制功能 5. 更新.gitignore,将所有.sessions目录纳入忽略列表 6. 删除影刀脚本旧的本地缓存文件 7. 优化加班审批校验逻辑,新增提前到岗型加班校验规则 8. 完善工资卡核对功能,补充开卡银行信息展示
312 lines
12 KiB
Python
312 lines
12 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
@Module: hrbp.ehr.wage_card
|
||
@FileName: wage_card_check.py
|
||
@Author: 小布丁
|
||
@CreateTime: 2026-08-08
|
||
@Description: 工资卡汇总表核对:身份证一致性、姓名一致性、空值标记、银行卡号校验。
|
||
读取工资卡汇总表 Excel,添加核对结果列后保存。
|
||
@Version: 1.0.0
|
||
"""
|
||
|
||
import os
|
||
import re
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
# ---- 单文件运行支持 ----
|
||
def _find_project_root() -> str:
|
||
for parent in Path(__file__).resolve().parents:
|
||
if (parent / "app").is_dir() and (parent / "app" / "__init__.py").is_file():
|
||
return str(parent)
|
||
return str(Path(__file__).resolve().parents[5])
|
||
|
||
|
||
_root = _find_project_root()
|
||
if _root not in sys.path:
|
||
sys.path.insert(0, _root)
|
||
|
||
from app.utils.logger import get_logger, setup_logging # noqa: E402
|
||
from app.service.hrbp.ehr.common.hr_api import get_bank_dict, bank_type_to_name # noqa: E402
|
||
|
||
try:
|
||
import pandas as pd
|
||
except ImportError:
|
||
pd = None
|
||
|
||
logger = get_logger(__name__)
|
||
|
||
# ===================== 银行卡校验规则 =====================
|
||
# 固定通用规则,无需随表格修改
|
||
# 16 位银联卡 BIN(按银行归类,经实际工资卡数据比对核验)
|
||
BIN_16 = {
|
||
# 招商银行
|
||
"621483", "621485", "621486", "622588", "622609",
|
||
# 民生银行
|
||
"621691", "622615", "622617", "622619", "622620", "622622",
|
||
# 民生银行信用卡(16 位贷记卡)
|
||
"622601",
|
||
# 重庆农村商业银行
|
||
"621258", "621465", "621528", "622867",
|
||
# 浦东发展银行
|
||
"621792", "621793", "622521",
|
||
# 其他银行
|
||
"621499", # 建设银行理财金卡
|
||
"621755", # 重庆三峡银行
|
||
"623020", # 华夏银行
|
||
}
|
||
# 18 位银联储蓄卡 BIN
|
||
BIN_18 = {"622908"} # 兴业银行
|
||
# 非 62 开头但合法的老版储蓄卡 BIN(历史卡仍在使用,标准均为 19 位)
|
||
NON62_BIN = {
|
||
"436742", # 建设银行龙卡储蓄卡
|
||
"601382", # 中国银行长城电子借记卡
|
||
"955882", # 工商银行牡丹灵通卡
|
||
}
|
||
# 正则:匹配全数字
|
||
NUM_REG = re.compile(r"^\d+$")
|
||
|
||
|
||
def check_bank_card(card_raw) -> dict:
|
||
"""校验银行卡号,返回 {card, status, reason}。"""
|
||
# 处理 pandas 从 Excel 读取时将卡号转为 float 的问题
|
||
if isinstance(card_raw, float):
|
||
if card_raw != card_raw: # NaN
|
||
card = ""
|
||
else:
|
||
card = str(int(card_raw))
|
||
elif isinstance(card_raw, int):
|
||
card = str(card_raw)
|
||
else:
|
||
card = str(card_raw).strip().replace(" ", "")
|
||
|
||
if not card:
|
||
return {"card": card, "status": "无效", "reason": "空白,无卡号"}
|
||
|
||
if not NUM_REG.match(card):
|
||
return {"card": card, "status": "无效", "reason": "非纯数字,属于文字备注/脏数据"}
|
||
|
||
bin6 = card[:6]
|
||
if not (card.startswith("62") or bin6 in NON62_BIN):
|
||
return {"card": card, "status": "无效", "reason": "非62开头,非标准银联储蓄卡"}
|
||
|
||
card_len = len(card)
|
||
if card_len not in (16, 18, 19):
|
||
return {"card": card, "status": "错误", "reason": f"位数异常{card_len}位,仅支持16/18/19位银行卡"}
|
||
|
||
if bin6 in BIN_16:
|
||
if card_len == 16:
|
||
return {"card": card, "status": "正常", "reason": "合规16位银联卡"}
|
||
else:
|
||
return {"card": card, "status": "错误", "reason": f"BIN{bin6}标准16位,录入位数错误"}
|
||
|
||
if bin6 in BIN_18:
|
||
if card_len == 18:
|
||
return {"card": card, "status": "正常", "reason": "合规18位兴业储蓄卡"}
|
||
else:
|
||
return {"card": card, "status": "错误", "reason": f"BIN{bin6}标准18位,录入位数错误"}
|
||
|
||
if card_len == 19:
|
||
return {"card": card, "status": "正常", "reason": "合规19位储蓄卡"}
|
||
else:
|
||
return {"card": card, "status": "错误", "reason": f"BIN{bin6}标准19位,录入位数错误,需人工核对"}
|
||
|
||
|
||
# ===================== 核对分类 =====================
|
||
def classify_card_issue(idcard, card_idcard, staff_name, card_name, bank_account):
|
||
"""按 4 类异常对单条工资卡记录分类。
|
||
|
||
返回 (issues, basis, id_inconsistent, bank_info)
|
||
issues: 4 类异常标签(写入核对结果)
|
||
basis : 详细校验依据(写入校验依据)
|
||
1. 开户人信息或银行卡空白
|
||
2. 员工身份证与开户人身份证不一致
|
||
3. 员工姓名与开户人姓名不一致(仅身份证一致时比对)
|
||
4. 银行卡信息异常(银行卡非空但格式/位数错误)
|
||
"""
|
||
issues = []
|
||
basis = []
|
||
bank_info = None
|
||
|
||
# ---- 1. 开户人信息或银行卡空白 ----
|
||
if _is_empty(card_idcard):
|
||
issues.append("开户人身份证空白")
|
||
basis.append("开户人身份证空白")
|
||
if _is_empty(card_name):
|
||
issues.append("开户人姓名空白")
|
||
basis.append("开户人姓名空白")
|
||
if _is_empty(bank_account):
|
||
issues.append("银行卡空白")
|
||
basis.append("银行卡空白")
|
||
|
||
# ---- 2. 身份证不一致(开户人身份证非空且与员工不一致) ----
|
||
id_inconsistent = (not _is_empty(card_idcard)) and idcard != card_idcard
|
||
if id_inconsistent:
|
||
issues.append("员工身份证与开户人身份证不一致")
|
||
basis.append(f"身份证不一致(员工:{idcard} vs 开户人:{card_idcard})")
|
||
|
||
# ---- 3. 姓名不一致(仅身份证一致时比对) ----
|
||
if (not id_inconsistent and not _is_empty(card_idcard)
|
||
and not _is_empty(card_name) and staff_name != card_name):
|
||
issues.append("员工姓名与开户人姓名不一致")
|
||
basis.append(f"姓名不一致(员工:{staff_name} vs 开户人:{card_name})")
|
||
|
||
# ---- 4. 银行卡信息异常(银行卡非空但格式/位数错误) ----
|
||
if not _is_empty(bank_account):
|
||
bank_info = check_bank_card(bank_account)
|
||
if bank_info["status"] != "正常":
|
||
issues.append("银行卡信息异常")
|
||
basis.append(f"银行卡信息异常({bank_info['reason']})")
|
||
|
||
return issues, basis, id_inconsistent, bank_info
|
||
|
||
|
||
# ===================== 列名常量 =====================
|
||
COL_IDCARD = "身份证号"
|
||
COL_CARD_IDCARD = "开户人身份证"
|
||
COL_STAFF_NAME = "员工姓名"
|
||
COL_CARD_NAME = "开户人姓名"
|
||
COL_BANK_ACCOUNT = "银行卡号"
|
||
COL_BANK_NAME = "开卡银行"
|
||
|
||
COL_BANK_STATUS = "银行卡状态"
|
||
COL_BANK_REASON = "银行卡说明"
|
||
COL_CHECK_RESULT = "核对结果"
|
||
COL_CHECK_BASIS = "校验依据"
|
||
|
||
|
||
def _is_empty(val) -> bool:
|
||
"""判断值是否为空(None、空字符串、NaN)。"""
|
||
if val is None:
|
||
return True
|
||
if isinstance(val, float) and val != val: # NaN
|
||
return True
|
||
s = str(val).strip()
|
||
return s == "" or s.lower() == "nan"
|
||
|
||
|
||
def _normalize_id(val) -> str:
|
||
"""将身份证号/银行卡号统一转为字符串,处理 pandas 读取 Excel 时的 float 转换问题。"""
|
||
if val is None:
|
||
return ""
|
||
if isinstance(val, float):
|
||
if val != val: # NaN
|
||
return ""
|
||
return str(int(val))
|
||
if isinstance(val, int):
|
||
return str(val)
|
||
return str(val).strip().replace(" ", "")
|
||
|
||
|
||
def check_wage_card(input_path: str, output_path: str = None) -> str:
|
||
"""读取工资卡汇总表,执行核对并保存结果。
|
||
|
||
输出文件:
|
||
- {汇总表}_核对结果.xlsx:全量核对结果
|
||
- 无银行卡表.xlsx:未提供银行卡号(银行卡空白)的人员
|
||
- 工资卡异常名单.xlsx:除无银行卡外的其他异常人员(含身份证不一致)
|
||
|
||
Args:
|
||
input_path: 工资卡汇总表 Excel 路径
|
||
output_path: 核对结果保存路径,默认在输入文件同目录加 _核对结果
|
||
|
||
Returns:
|
||
核对结果文件路径
|
||
"""
|
||
if pd is None:
|
||
raise RuntimeError("缺少 pandas 依赖,无法处理 Excel")
|
||
|
||
df = pd.read_excel(input_path)
|
||
logger.info("读取工资卡汇总表:%s(%d 行)", input_path, len(df))
|
||
|
||
# 汇总表未带开卡银行列时,按银行编码(BANK_TYPE)补充(aa.md 银行字典接口)
|
||
if COL_BANK_NAME not in df.columns and "银行编码" in df.columns:
|
||
_, bank_map = get_bank_dict()
|
||
df[COL_BANK_NAME] = df["银行编码"].apply(lambda c: bank_type_to_name(c, bank_map))
|
||
|
||
no_bank_rows = []
|
||
|
||
for idx, row in df.iterrows():
|
||
idcard = _normalize_id(row.get(COL_IDCARD, ""))
|
||
card_idcard = _normalize_id(row.get(COL_CARD_IDCARD, ""))
|
||
staff_name = str(row.get(COL_STAFF_NAME, "")).strip()
|
||
card_name = str(row.get(COL_CARD_NAME, "")).strip()
|
||
bank_account = row.get(COL_BANK_ACCOUNT, "")
|
||
|
||
issues, basis, _, bk = classify_card_issue(
|
||
idcard, card_idcard, staff_name, card_name, bank_account,
|
||
)
|
||
if _is_empty(bank_account):
|
||
no_bank_rows.append(idx)
|
||
|
||
df.at[idx, COL_CHECK_RESULT] = "、".join(issues) if issues else "通过"
|
||
df.at[idx, COL_CHECK_BASIS] = ";".join(basis) if basis else ""
|
||
df.at[idx, COL_BANK_STATUS] = bk["status"] if bk else ""
|
||
df.at[idx, COL_BANK_REASON] = bk["reason"] if bk else ""
|
||
|
||
# 统计
|
||
result_counts = df[df[COL_CHECK_RESULT] != "通过"][COL_CHECK_RESULT].value_counts().to_dict()
|
||
bank_issues = df[df[COL_BANK_STATUS] != "正常"][COL_BANK_STATUS].value_counts().to_dict()
|
||
passed = len(df[df[COL_CHECK_RESULT] == "通过"])
|
||
|
||
logger.info("核对完成:通过 %d / 总计 %d", passed, len(df))
|
||
if result_counts:
|
||
logger.info("核对问题分布:%s", result_counts)
|
||
if bank_issues:
|
||
logger.info("银行卡问题:%s", bank_issues)
|
||
|
||
# 保存
|
||
if output_path is None:
|
||
base, ext = os.path.splitext(input_path)
|
||
output_path = f"{base}_核对结果{ext}"
|
||
|
||
# 将核对结果列移到最前面
|
||
check_cols = [COL_CHECK_RESULT, COL_CHECK_BASIS, COL_BANK_STATUS, COL_BANK_REASON]
|
||
other_cols = [c for c in df.columns if c not in check_cols]
|
||
df = df[check_cols + other_cols]
|
||
|
||
df.to_excel(output_path, index=False)
|
||
logger.info("核对结果已保存:%s", output_path)
|
||
|
||
# 无银行卡人员单独存表(独立文件)
|
||
if no_bank_rows:
|
||
base, ext = os.path.splitext(output_path)
|
||
no_bank_path = os.path.join(os.path.dirname(base), f"无银行卡表{ext}")
|
||
df.loc[no_bank_rows].to_excel(no_bank_path, index=False)
|
||
logger.info("无银行卡表已保存:%s(%d 条)", no_bank_path, len(no_bank_rows))
|
||
else:
|
||
logger.info("无银行卡空白记录,跳过无银行卡表生成")
|
||
|
||
# ---- 生成异常名单(含身份证不一致,排除已单独成表的无银行卡记录) ----
|
||
mask = (df[COL_CHECK_RESULT] != "通过") & (~df.index.isin(no_bank_rows))
|
||
abnormal = df[mask]
|
||
if len(abnormal) > 0:
|
||
base, ext = os.path.splitext(output_path)
|
||
abnormal_path = f"{os.path.dirname(base)}{os.sep}工资卡异常名单{ext}"
|
||
abnormal.to_excel(abnormal_path, index=False)
|
||
logger.info("异常名单已保存:%s(%d 条)", abnormal_path, len(abnormal))
|
||
else:
|
||
abnormal_path = None
|
||
logger.info("无异常记录,跳过异常名单生成")
|
||
|
||
return output_path
|
||
|
||
|
||
def main() -> None:
|
||
"""命令行入口:python wage_card_check.py [输入文件] [输出文件]"""
|
||
setup_logging()
|
||
input_path = sys.argv[1] if len(sys.argv) > 1 else os.path.join(
|
||
r"D:\资料\人资\银行卡检查", "工资卡汇总表.xlsx"
|
||
)
|
||
output_path = sys.argv[2] if len(sys.argv) > 2 else None
|
||
try:
|
||
out = check_wage_card(input_path, output_path)
|
||
logger.info("===== 工资卡核对完成:%s =====", out)
|
||
except Exception as e:
|
||
logger.error("工资卡核对失败:%s", e)
|
||
raise
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|