Files
37duAutomation/plugins/sb_person_change/source.py
T
puddy 1e533dd1b3 refactor: 完成项目目录重构与代码迁移
本次提交完成了整体项目的目录梳理与代码迁移:
1.  将根级controlplane、webhost等目录的代码迁移至desktop/对应子目录,统一桌面端架构
2.  移动并整理了插件目录结构,将插件统一放入根级plugins/
3.  调整了前端项目的路径与配置,统一前后端开发构建流程
4.  清理了废弃的旧版app/目录与冗余的构建中间文件
5.  更新了.gitignore与README.md,适配新的项目结构
2026-09-14 17:46:09 +08:00

183 lines
8.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""源名单/台账数据层(sb_person_change 插件)。
自 workflows/XQ0014 import_add.py(名单读取、台账取号)与 check_review.py(核查任务)
迁移;名单制作模式的源表读取也在此(与 generate_add/generate_stop 同源)。
print → config.log。
"""
import os
from datetime import datetime
import pandas as pd
from . import config
from . import excelio
def _log(msg):
config.log(msg)
# ==================== 源名单读取(增员办理) ====================
def load_add_units(exclude_ids=frozenset()):
"""读取源名单并筛选,返回 [(参保单位, 人数), ...](按单位分组)。
筛选:区域=重庆 ∩ 参保类型=新参保 ∩ 办理状态为空/NULL
(待审核=已提交社保局、参保失败=已在其它流程处理,均不再办理)。
exclude_ids:需排除的身份证号集合(上次运行已回写办理状态的人员,防重复申报)。
"""
source_file = config._latest_file(config.SOURCE_DIR, config.SOURCE_GLOB)
now = datetime.now()
xl = pd.ExcelFile(source_file)
sheet_name = config._pick_sheet(xl, f"{now:%Y%m}参保名单", config.ADD_SHEET_KEY)
_log(f"源名单:{os.path.basename(source_file)}(sheet: {sheet_name})")
# 增员办理/核查的回写都落在该源名单上
excelio.set_writeback_target(source_file, sheet_name)
df = pd.read_excel(source_file, sheet_name=sheet_name, header=config.SOURCE_HEADER_ROW - 1)
status_text = df[config.STATUS_COL].astype(str).str.strip().str.lower()
mask = (
(df[config.REGION_COL].astype(str).str.strip() == config.REGION)
& (df[config.TYPE_COL].astype(str).str.strip() == config.OPERATION_TYPE)
& (df[config.STATUS_COL].isna() | status_text.isin(['', 'nan', 'none', 'nat']))
)
sub = df[mask].copy()
if exclude_ids and '身份证号码' in sub.columns:
before = len(sub)
ref = [config._norm_id(x) for x in exclude_ids]
sub = sub[~sub['身份证号码'].map(lambda v: config._id_hit(config._norm_id(v), ref))]
dropped = before - len(sub)
if dropped:
_log(f" 已排除 {dropped} 行上次运行已回写的人员(防重复申报)")
if sub.empty:
_log(f"没有筛选到数据({config.REGION} ∩ {config.OPERATION_TYPE} ∩ 办理状态为空)")
return []
grouped = sub.groupby(sub[config.UNIT_COL].astype(str).str.strip()).size()
units = sorted(grouped.items(), key=lambda t: t[0])
_log(f"筛选结果:共 {sum(n for _, n in units)} 人 / {len(units)} 个参保单位")
return units
# ==================== 源名单读取(核查回盘) ====================
def load_check_tasks(exclude_ids=frozenset()):
"""读取源名单,筛选 办理状态=待审核 的行,按参保单位分组。
返回 [{unit, count, cids(set)}]。
exclude_ids:上次运行已回写的人员身份证号(防重复核查)。
"""
source_file = config._latest_file(config.SOURCE_DIR, config.SOURCE_GLOB)
now = datetime.now()
xl = pd.ExcelFile(source_file)
sheet_name = config._pick_sheet(xl, f"{now:%Y%m}参保名单", config.ADD_SHEET_KEY)
_log(f"源名单:{os.path.basename(source_file)}(sheet: {sheet_name})")
excelio.set_writeback_target(source_file, sheet_name)
df = pd.read_excel(source_file, sheet_name=sheet_name, header=config.SOURCE_HEADER_ROW - 1)
status = df[config.STATUS_COL].astype(str).str.strip()
mask = (
(df[config.REGION_COL].astype(str).str.strip() == config.REGION)
& (df[config.TYPE_COL].astype(str).str.strip() == config.OPERATION_TYPE)
& (status == config.CHECK_STATUS)
)
sub = df[mask].copy()
if exclude_ids and '身份证号码' in sub.columns:
before = len(sub)
ref = [config._norm_id(x) for x in exclude_ids]
sub = sub[~sub['身份证号码'].map(lambda v: config._id_hit(config._norm_id(v), ref))]
dropped = before - len(sub)
if dropped:
_log(f" 已排除 {dropped} 行上次运行已回写的人员(防重复核查)")
if sub.empty:
_log(f"没有筛选到数据({config.REGION} ∩ {config.OPERATION_TYPE} ∩ "
f"办理状态={config.CHECK_STATUS})")
return []
tasks = []
for unit, g in sub.groupby(sub[config.UNIT_COL].astype(str).str.strip()):
cids = {config._norm_id(v) for v in g['身份证号码']}
cids.discard('')
tasks.append({"unit": unit, "count": len(cids), "cids": cids})
tasks.sort(key=lambda t: t['unit'])
_log(f"核查范围:共 {sum(t['count'] for t in tasks)} 人 / {len(tasks)} 个参保单位"
f"(办理状态={config.CHECK_STATUS})")
return tasks
# ==================== 源名单读取(名单制作) ====================
def load_add_rows(operation_type):
"""读取【参保名单】sheet,筛选 区域=重庆 ∩ 参保类型=指定类型 ∩ 办理状态为空。
返回 (source_file, df_filtered)。身份证/电话按文本读,防 18 位丢精度。
"""
source_file = config._latest_file(config.SOURCE_DIR, config.SOURCE_GLOB)
xl = pd.ExcelFile(source_file)
sheet_name = config._pick_sheet(xl, "", config.ADD_SHEET_KEY)
_log(f"源表:{os.path.basename(source_file)}(sheet: {sheet_name})")
df_source = pd.read_excel(
source_file, sheet_name=sheet_name, header=config.SOURCE_HEADER_ROW - 1,
dtype={'身份证号码': str, '电话号码': str})
status_text = df_source[config.STATUS_COL].astype(str).str.strip().str.lower()
mask = (
(df_source[config.REGION_COL] == config.REGION)
& (df_source[config.TYPE_COL] == operation_type)
& (df_source[config.STATUS_COL].isna() | status_text.isin(['', 'nan', 'none', 'nat']))
)
df_filtered = df_source[mask].copy()
if df_filtered.empty:
_log(f"没有找到【{operation_type}】的数据({config.REGION}区域,办理状态为空)")
return source_file, df_filtered
def load_stop_rows():
"""读取【停保名单】sheet,筛选 区域=重庆。全列按文本读,保住身份证/社保编号完整值。
返回 (source_file, df_filtered)。
"""
source_file = config._latest_file(config.SOURCE_DIR, config.SOURCE_GLOB)
xl = pd.ExcelFile(source_file)
sheet_name = config._pick_sheet(xl, "", config.STOP_SHEET_KEY)
_log(f"源表:{os.path.basename(source_file)}(sheet: {sheet_name})")
df_source = pd.read_excel(
source_file, sheet_name=sheet_name, header=config.SOURCE_HEADER_ROW - 1, dtype=str)
df_filtered = df_source[df_source[config.REGION_COL] == config.REGION].copy()
if df_filtered.empty:
_log(f"没有找到【{config.REGION}】区域的停保数据")
return source_file, df_filtered
# ==================== 台账取号 ====================
def load_accounts(unit_names):
"""从台账「YYYY年M月缴费比例」sheet 取各单位账号。
返回 {参保单位: {"username":..., "username_src": "信用代码|平台用户名", "password":...}},
取不到的单位不在返回值中(由预检统一报错)。
"""
ledger_file = config._latest_file(config.LEDGER_DIR, config.LEDGER_GLOB)
now = datetime.now()
xl = pd.ExcelFile(ledger_file)
sheet_name = config._pick_sheet(xl, f"{now.year}年{now.month}月缴费比例", "缴费比例",
pattern=r"(20\d{2})年(1[0-2]|[1-9])月缴费比例")
_log(f"台账:{os.path.basename(ledger_file)}(sheet: {sheet_name})")
# 表头为 1-2 行合并结构:跳过前 2 行,B列(户头)非空即数据行
df = pd.read_excel(ledger_file, sheet_name=sheet_name, header=None, skiprows=2)
df = df[df[config.LEDGER_COL["户头"]].notna()].copy()
for c in config.LEDGER_COL.values():
df[c] = df[c].map(config._to_text)
pool = df[(df[config.LEDGER_COL["城市"]] == config.REGION)
& (df[config.LEDGER_COL["在用"]] == "是")]
accounts = {}
for name in unit_names:
hit = pool[pool[config.LEDGER_COL["户头"]] == name]
if hit.empty:
continue # 预检阶段统一报「台账未匹配」
row = hit.iloc[0]
credit, plat_user, plat_pwd = (
row[config.LEDGER_COL["信用代码"]], row[config.LEDGER_COL["平台用户名"]],
row[config.LEDGER_COL["平台密码"]])
# 用户名规则:信用代码优先,空则回退平台用户名
username, src = (credit, "信用代码") if credit else (plat_user, "平台用户名")
accounts[name] = {"username": username, "username_src": src, "password": plat_pwd}
return accounts