1. 新增hrbp.ehr业务模块目录结构与基础配置 2. 新增工资卡数据抓取、核对功能,支持在职/离职员工数据导出 3. 新增员工花名册自定义导出自动化流程 4. 添加pandas依赖到requirements.txt 5. 清理调试用的验证码图片文件
240 lines
8.4 KiB
Python
240 lines
8.4 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
|
||
|
||
try:
|
||
import pandas as pd
|
||
except ImportError:
|
||
pd = None
|
||
|
||
logger = get_logger(__name__)
|
||
|
||
# ===================== 银行卡校验规则 =====================
|
||
# 固定通用规则,无需随表格修改
|
||
# 必须16位的BIN
|
||
BIN_16 = {"621483", "622622", "621691", "622619", "622620"}
|
||
# 必须18位的BIN
|
||
BIN_18 = {"622908"}
|
||
# 正则:匹配全数字
|
||
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": "非纯数字,属于文字备注/脏数据"}
|
||
|
||
if not card.startswith("62"):
|
||
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位银行卡"}
|
||
|
||
bin6 = card[:6]
|
||
|
||
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位,录入位数错误,需人工核对"}
|
||
|
||
|
||
# ===================== 列名常量 =====================
|
||
COL_IDCARD = "身份证号"
|
||
COL_CARD_IDCARD = "开户人身份证"
|
||
COL_STAFF_NAME = "员工姓名"
|
||
COL_CARD_NAME = "开户人姓名"
|
||
COL_BANK_ACCOUNT = "银行卡号"
|
||
|
||
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:
|
||
"""读取工资卡汇总表,执行核对并保存结果。
|
||
|
||
核对逻辑:
|
||
1. 开户人身份证为空 -> 标记
|
||
2. 身份证号 vs 开户人身份证 不一致 -> 标记
|
||
3. (身份证一致时)开户人姓名为空 -> 标记
|
||
4. (身份证一致时)员工姓名 vs 开户人姓名 不一致 -> 标记
|
||
5. 银行卡号校验(check_bank_card)
|
||
|
||
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))
|
||
|
||
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 = [] # 校验依据(详细对比信息)
|
||
|
||
# ---- 身份证核对 ----
|
||
if _is_empty(card_idcard):
|
||
issues.append("开户人身份证为空")
|
||
elif idcard != card_idcard:
|
||
issues.append("身份证不一致")
|
||
basis.append(f"身份证不一致(员工:{idcard} vs 开户人:{card_idcard})")
|
||
else:
|
||
# ---- 姓名核对(仅身份证一致时) ----
|
||
if _is_empty(card_name):
|
||
issues.append("开户人姓名为空")
|
||
elif staff_name != card_name:
|
||
issues.append("姓名不一致")
|
||
basis.append(f"姓名不一致(员工:{staff_name} vs 开户人:{card_name})")
|
||
|
||
# ---- 银行卡校验 ----
|
||
bk = check_bank_card(bank_account)
|
||
|
||
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"]
|
||
df.at[idx, COL_BANK_REASON] = bk["reason"]
|
||
|
||
# 统计
|
||
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)
|
||
|
||
# ---- 生成异常名单(仅含核对不通过的行) ----
|
||
abnormal = df[df[COL_CHECK_RESULT] != "通过"]
|
||
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()
|