feat(xq0007): 新增 OA 社保增减名单梳理插件(v1.0.1)
从 OA 导出员工参保/停保申请名单(申请日期筛选、去废弃、同工号取最新),清洗后按 15 个/批与 HR 参保记录逐工号核对,输出标红核对表;支持单文件直传/多文件打包 zip、按份数留档、钉钉摘要推送。
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""名单清洗去重 + 参保核对结果落盘(影刀版 _read_and_clean / _write_annotated 移植)。
|
||||
|
||||
清洗口径(与影刀版一致):
|
||||
- 工号为空的行丢弃;
|
||||
- 文档状态 = 废弃 的行丢弃;
|
||||
- 同一工号保留「申请日期」最新的一条,输出顺序按首次出现顺序。
|
||||
核对口径:
|
||||
- 参保:系统已有该工号参保记录 → 异常(说明已参过保,需人工确认);
|
||||
- 停保:系统已无该工号参保记录 → 异常(说明已停保,无需再办);
|
||||
- 该批接口查询失败 → 标注「查询失败,需人工复核」,不当作无记录。
|
||||
"""
|
||||
import re
|
||||
from datetime import datetime
|
||||
|
||||
import xlrd
|
||||
from openpyxl import Workbook
|
||||
from openpyxl.styles import Alignment, Font, PatternFill
|
||||
from openpyxl.utils import get_column_letter
|
||||
|
||||
from .. import config
|
||||
|
||||
_HEAD_FILL = PatternFill(start_color="DDEBF7", end_color="DDEBF7", fill_type="solid")
|
||||
_RED_FILL = PatternFill(start_color="FFC7CE", end_color="FFC7CE", fill_type="solid")
|
||||
_RED_FONT = Font(color="9C0006", bold=True)
|
||||
_BOLD_FONT = Font(bold=True)
|
||||
|
||||
# 名称列候选(用于异常行文本,便于人工定位)
|
||||
_NAME_COLS = {config.KIND_PARTICIPATE: ("参保人",), config.KIND_SUSPEND: ("停保人姓名",)}
|
||||
|
||||
|
||||
# ==================== 清洗去重 ====================
|
||||
def _normalize_date_key(value):
|
||||
"""申请日期归一为可比较数值(Excel 序列号 / YYYY-MM-DD 文本都支持)。"""
|
||||
if value is None:
|
||||
return 0.0
|
||||
if isinstance(value, (int, float)):
|
||||
return float(value)
|
||||
if isinstance(value, str):
|
||||
m = re.match(r"(\d{4})[-/年](\d{1,2})[-/月](\d{1,2})", value.strip())
|
||||
if m:
|
||||
try:
|
||||
y, mo, d = int(m.group(1)), int(m.group(2)), int(m.group(3))
|
||||
return float((datetime(y, mo, d) - datetime(1899, 12, 30)).days)
|
||||
except ValueError:
|
||||
return 0.0
|
||||
return 0.0
|
||||
|
||||
|
||||
def _date_display(value, wb):
|
||||
"""xlrd 日期序列号转可读文本(整数→%Y-%m-%d,带时间→%Y-%m-%d %H:%M:%S)。"""
|
||||
if isinstance(value, (int, float)):
|
||||
try:
|
||||
dt = xlrd.xldate_as_datetime(float(value), wb.datemode)
|
||||
except Exception:
|
||||
return value
|
||||
if float(value).is_integer():
|
||||
return dt.strftime("%Y-%m-%d")
|
||||
return dt.strftime("%Y-%m-%d %H:%M:%S")
|
||||
return value
|
||||
|
||||
|
||||
def read_and_clean(xls_path):
|
||||
"""读取 OA 导出的 xls,返回 (表头, 清洗去重后的行列表, 工号列下标)。"""
|
||||
wb = xlrd.open_workbook(xls_path)
|
||||
sh = wb.sheet_by_index(0)
|
||||
headers = [str(sh.cell_value(0, c)).strip() for c in range(sh.ncols)]
|
||||
|
||||
def find_col(keyword):
|
||||
for i, h in enumerate(headers):
|
||||
if keyword in h:
|
||||
return i
|
||||
return None
|
||||
|
||||
emp_col = find_col("工号")
|
||||
date_col = find_col("申请日期")
|
||||
status_col = find_col("文档状态")
|
||||
if emp_col is None:
|
||||
raise ValueError(f"导出表未找到『工号』列,表头:{headers}")
|
||||
|
||||
rows = []
|
||||
for r in range(1, sh.nrows):
|
||||
row = []
|
||||
for c in range(sh.ncols):
|
||||
v = sh.cell_value(r, c)
|
||||
if sh.cell_type(r, c) == xlrd.XL_CELL_DATE:
|
||||
v = _date_display(v, wb)
|
||||
row.append(v)
|
||||
emp = str(row[emp_col]).strip()
|
||||
if not emp:
|
||||
continue
|
||||
if status_col is not None and str(row[status_col]).strip() == "废弃":
|
||||
continue
|
||||
row[emp_col] = emp
|
||||
rows.append(row)
|
||||
|
||||
# 同一工号只保留申请日期最新的一条
|
||||
latest = {}
|
||||
for row in rows:
|
||||
emp = row[emp_col]
|
||||
key = _normalize_date_key(row[date_col]) if date_col is not None else 0.0
|
||||
if emp not in latest or key > latest[emp][0]:
|
||||
latest[emp] = (key, row)
|
||||
|
||||
ordered, seen = [], set()
|
||||
for row in rows:
|
||||
emp = row[emp_col]
|
||||
if emp not in seen:
|
||||
seen.add(emp)
|
||||
ordered.append(latest[emp][1])
|
||||
|
||||
config.logger.info("清洗完成:去掉废弃后 %d 条,去重后 %d 条", len(rows), len(ordered))
|
||||
return headers, ordered, emp_col
|
||||
|
||||
|
||||
# ==================== 核对结论 + 落盘 ====================
|
||||
def build_result_value(kind, has_record, failed):
|
||||
is_participate = kind == config.KIND_PARTICIPATE
|
||||
if failed:
|
||||
return "异常:参保记录查询失败,需人工复核"
|
||||
if is_participate:
|
||||
return "异常:已参保(系统有该工号记录)" if has_record else "正常:系统无该工号记录"
|
||||
return "异常:已停保(系统无该工号记录)" if not has_record else "正常:系统有该工号记录"
|
||||
|
||||
|
||||
def write_annotated(headers, rows, emp_col, kind, out_path, found, failed):
|
||||
"""追加『社保记录查询结果』列并写出 xlsx(异常行标红、表头冻结筛选)。
|
||||
|
||||
Returns:
|
||||
(输出文件绝对路径, 异常行文本列表, 异常行数)
|
||||
"""
|
||||
label = config.KIND_LABELS.get(kind, "参保")
|
||||
new_rows = []
|
||||
for row in rows:
|
||||
code = str(row[emp_col]).strip()
|
||||
val = build_result_value(kind, code in found, code in failed)
|
||||
new_rows.append(list(row) + [val])
|
||||
|
||||
out_wb = Workbook()
|
||||
ws = out_wb.active
|
||||
ws.title = "核对明细"
|
||||
ws.append(list(headers) + [config.SI_RESULT_COL])
|
||||
for c in range(1, len(headers) + 2):
|
||||
cell = ws.cell(1, c)
|
||||
cell.fill = _HEAD_FILL
|
||||
cell.font = _BOLD_FONT
|
||||
cell.alignment = Alignment(horizontal="center")
|
||||
|
||||
for idx, row in enumerate(new_rows, start=2):
|
||||
ws.append(row)
|
||||
if str(row[-1]).startswith("异常"):
|
||||
for c in range(1, len(row) + 1):
|
||||
cell = ws.cell(idx, c)
|
||||
cell.fill = _RED_FILL
|
||||
if c == len(row):
|
||||
cell.font = _RED_FONT
|
||||
|
||||
ws.freeze_panes = "A2"
|
||||
ws.auto_filter.ref = ws.dimensions
|
||||
for c in range(1, len(headers) + 2):
|
||||
ws.column_dimensions[get_column_letter(c)].width = 20
|
||||
out_wb.save(out_path)
|
||||
|
||||
# 异常行文本(「工号 XXX 姓名 —— 结论」),供汇总/推送复用
|
||||
name_col = next((i for i, h in enumerate(headers)
|
||||
if h in _NAME_COLS.get(kind, ())), None)
|
||||
abnormal = []
|
||||
for row in new_rows:
|
||||
val = str(row[-1])
|
||||
if not val.startswith("异常"):
|
||||
continue
|
||||
emp = str(row[emp_col]).strip()
|
||||
name = str(row[name_col]).strip() if name_col is not None else ""
|
||||
abnormal.append(f" · 工号{emp} {name} —— {val}".rstrip())
|
||||
|
||||
config.logger.info("核对完成(%s):共 %d 条,异常 %d 条,已导出 %s",
|
||||
label, len(new_rows), len(abnormal), out_path)
|
||||
return out_path, abnormal, len(abnormal)
|
||||
Reference in New Issue
Block a user