# -*- coding: utf-8 -*- """ @Module: hrbp.ehr.social_import.generate_add @FileName: generate_add.py @Author: 小布丁 @CreateTime: 2026-09-05 @Description: XQ0008 重庆社保增员导入名单制作 —— app 分模块版(生成逻辑) 读取《XX月社保变更及增减名单》-【参保名单】sheet(表头第3行),筛选【区域=重庆】且 【参保类型=指定类型】;按【参保单位】分组,复制《增员模版.xls》(xlutils 保留下拉), 写入 B1(社保号)/D1(参保单位全称) 及 B4 起数据,逐单位另存为 《XX增员导入名单.xls》至 BASE_DIR/新参保-增员 或 转移参保-增员。 填写标准:首次参加=本月1号、个人身份=24.工人、工伤=当日、其他参保时间=本月1号; 月基本工资额取源表「工资申报」列。 日期规则:1号→转移参保;11号→新参保(main 自动判断,可显式传入类型)。 @Version: 1.0.0 使用方式(函数传参调用,可被其他流程 import): from app.service.hrbp.ehr.social_import.generate_add import process_add_members process_add_members('新参保') # 显式指定 process_add_members('转移参保') """ import os import sys from datetime import datetime from pathlib import Path # ---- 单文件运行支持:把项目根目录加入 sys.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.social_import import config # noqa: E402 logger = get_logger(__name__) def process_add_members(operation_type): """ 生成增员导入名单 operation_type: '新参保' 或 '转移参保' """ if operation_type not in config.OUTPUT_DIR_ADD: raise ValueError( f"未知参保类型:{operation_type}(可选:{'/'.join(config.OUTPUT_DIR_ADD)})") # 1. 读取源表【参保名单】sheet(表头在第3行) source_file = config.find_source_file() sheet_name = config.find_source_sheet(source_file, config.SHEET_KEY_ADD) logger.info("源表:%s(sheet: %s)", source_file, sheet_name) df_source = config.pd.read_excel( source_file, sheet_name=sheet_name, header=config.SOURCE_HEADER_ROW - 1, # 身份证/电话按文本读:18 位身份证若被推断为数字会丢精度(与停保脚本一致); # 工资申报等数值列保持默认数值读法,写模板时才是数字而非 "5200.0" 文本 dtype={'身份证号码': str, '电话号码': str}) # 2. 筛选:区域=重庆,参保类型=指定类型 mask = ( (df_source[config.REGION_COL] == config.TARGET_REGION) & (df_source[config.TYPE_COL] == operation_type) ) df_filtered = df_source[mask].copy() if df_filtered.empty: logger.warning("没有找到【%s】的数据(%s区域)", operation_type, config.TARGET_REGION) return {"operation_type": operation_type, "files": []} # 3. 获取日期 today, first_day = config._get_dates() logger.info("今日日期:%s;本月1日:%s;工伤参保时间:%s(当日)", today.strftime('%Y-%m-%d'), first_day.strftime('%Y-%m-%d'), today.strftime('%Y-%m-%d')) # 4. 按参保单位分组生成 template_file = config.find_template_file(stop=False) output_dir = config.get_output_dir(config.OUTPUT_DIR_ADD[operation_type]) logger.info("输出目录:%s", output_dir) cm = config.COLUMN_MAP_ADD files = [] for store_name, group in df_filtered.groupby(cm['单位名称']): logger.info("正在处理:%s,共 %d 人", store_name, len(group)) first_row = group.iloc[0] unit_no = first_row[cm['社保号']] # B1 unit_full_name = first_row[cm['参保单位全称']] # D1 # 5. 逐行生成数据(键 = 模板列名) records = [] for _, row in group.iterrows(): rec = { '证件号码': config._to_text(row[cm['证件号码']]), '姓名': row[cm['姓名']], '民族': config.translate_nation(row[cm['民族']]), '首次参加工作日期': first_day, '月基本工资额': config._clean_value(row[cm['月基本工资额']]), '工种': config.DEFAULT_VALUES_ADD['工种'], '个人身份': config.DEFAULT_VALUES_ADD['个人身份'], '户口性质': config.translate_residence(row[cm['户口性质']]), '联系电话': config._to_text(row[cm['联系电话']]), '文化程度': config.translate_education(row[cm['文化程度']]), '养老保险参保时间': first_day, '失业保险参保时间': first_day, '医疗保险参保时间': first_day, '工伤保险参保时间': today, '生育保险参保时间': first_day, '证件类型': config.DEFAULT_VALUES_ADD['证件类型'], '国籍': config.DEFAULT_VALUES_ADD['国籍'], '备注': config.DEFAULT_VALUES_ADD['备注'], '专职技能': config.DEFAULT_VALUES_ADD['专职技能'], '技能等级': config.DEFAULT_VALUES_ADD['技能等级'], } records.append(rec) # 6. 写入模板并另存 output_path = os.path.join(output_dir, f"{config.safe_name(store_name)}增员导入名单.xls") config.write_template(template_file, output_path, unit_no, unit_full_name, records, config.TPL_COLS_ADD, config.DATE_COLS_ADD) files.append(output_path) logger.info("所有【%s】导入名单生成完成,共 %d 个文件", operation_type, len(files)) return {"operation_type": operation_type, "files": files} def main(operation_type=None): """入口:默认按日期规则(1号→转移参保,11号→新参保),可显式传入类型强制生成。""" if operation_type: return process_add_members(operation_type) day_of_month = datetime.now().day if day_of_month == 1: return process_add_members('转移参保') if day_of_month == 11: return process_add_members('新参保') logger.info("今日是 %s 号,不在规定日期(1号或11号),不执行增员任务。", day_of_month) return {"operation_type": None, "files": []} if __name__ == "__main__": setup_logging() main()