Files
37duAutomation/app/service/hrbp/ehr/social_import/generate_stop.py
T
puddy d83ee84199 feat(hrbp/ehr): 实现重庆社保增减员导入名单制作功能
新增XQ0008重庆社保增减员导入名单制作全流程:
1. 新增app服务模块app/service/hrbp/ehr/social_import,包含核心生成逻辑、配置文件
2. 新增影刀脚本版本,支持增员/停保名单自动化生成
3. 完善配套文档:需求文档、流程文档、开发文档与评分文档
4. 修复钉钉机器人推送模块默认署名与关键词
5. 优化考勤异常脚本,改为自动查询上月完整数据
2026-09-05 11:48:00 +08:00

124 lines
5.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 -*-
"""
@Module: hrbp.ehr.social_import.generate_stop
@FileName: generate_stop.py
@Author: 小布丁
@CreateTime: 2026-09-05
@Description: XQ0008 重庆社保停保导入名单制作 —— app 分模块版(生成逻辑)
读取《XX月社保变更及增减名单》-【停保名单】sheet(表头第3行),筛选【区域=重庆】;
按【参保单位】分组,复制《减员模版.xls》(xlutils 保留下拉 二级代码),
写入 B1(单位社保号)/D1(参保单位) 及 B4 起数据,逐单位另存为
《XX减员导入名单.xls》至 BASE_DIR/停保-减员。
填写标准:减少时间=当天、减少原因=在职人员主动解除劳动合同、五个减少标志=1 减少、
失业原因=3 劳动者自愿辞职。身份证/社保编号按文本读取防丢精度。
(固定 28 号执行,main 自动判断,可显式调用)
@Version: 1.0.0
使用方式(函数传参调用,可被其他流程 import):
from app.service.hrbp.ehr.social_import.generate_stop import process_stop_members
process_stop_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_stop_members():
"""
生成停保导入名单
规则:区域=重庆;按参保单位分组;减少时间=当天;减少原因=在职人员主动解除劳动合同;
五个险种减少标志=1 减少;失业原因=3 劳动者自愿辞职
"""
# 1. 读取源表【停保名单】sheet(表头在第3行,全列按文本读,保住身份证/社保编号完整值)
source_file = config.find_source_file()
sheet_name = config.find_source_sheet(source_file, config.SHEET_KEY_STOP)
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, dtype=str)
# 2. 筛选:区域=重庆
df_filtered = df_source[df_source[config.REGION_COL] == config.TARGET_REGION].copy()
if df_filtered.empty:
logger.warning("没有找到【%s】区域的停保数据", config.TARGET_REGION)
return {"files": []}
logger.info("共找到 %d 条 %s 区域停保记录", len(df_filtered), config.TARGET_REGION)
# 3. 获取当天日期(减少时间 = 当天)
today, _ = config._get_dates()
logger.info("制作日期:%s;减少时间:%s(当日)", today.strftime('%Y-%m-%d'),
today.strftime('%Y-%m-%d'))
# 4. 按参保单位分组生成
template_file = config.find_template_file(stop=True)
output_dir = config.get_output_dir(config.OUTPUT_DIR_STOP)
logger.info("输出目录:%s", output_dir)
cm = config.COLUMN_MAP_STOP
files = []
for group_key, group in df_filtered.groupby(cm['单位名称']):
logger.info("正在处理:单位【%s】,共 %d 人", group_key, len(group))
first_row = group.iloc[0]
unit_no = first_row[cm['单位社保号']] # 单位社保号 → B1
unit_name = first_row[cm['单位名称']] # 参保单位 → D1
# 5. 逐行生成数据(键 = 模板列名)
records = []
for _, row in group.iterrows():
rec = {
'个人编号': config._clean_value(row[cm['个人编号']]),
'证件号码': config._clean_value(row[cm['证件号码']]),
'姓名': config._clean_value(row[cm['姓名']]),
'减少时间': today,
'减少原因': config.DEFAULT_VALUES_STOP['减少原因'],
'养老保险减少标志': config.DEFAULT_VALUES_STOP['养老保险减少标志'],
'失业保险减少标志': config.DEFAULT_VALUES_STOP['失业保险减少标志'],
'医疗保险减少标志': config.DEFAULT_VALUES_STOP['医疗保险减少标志'],
'工伤保险减少标志': config.DEFAULT_VALUES_STOP['工伤保险减少标志'],
'生育保险减少标志': config.DEFAULT_VALUES_STOP['生育保险减少标志'],
'失业原因': config.DEFAULT_VALUES_STOP['失业原因'],
}
records.append(rec)
# 6. 写入模板并另存
output_path = os.path.join(output_dir, f"{config.safe_name(group_key)}减员导入名单.xls")
config.write_template(template_file, output_path, unit_no, unit_name,
records, config.TPL_COLS_STOP, config.DATE_COLS_STOP)
files.append(output_path)
logger.info("所有停保导入名单生成完成,共 %d 个单位 / %d 个文件",
len(df_filtered[cm['单位名称']].unique()), len(files))
return {"files": files}
def main():
"""入口:固定 28 号执行。"""
day_of_month = datetime.now().day
if day_of_month == 28:
return process_stop_members()
logger.info("今日是 %s 号,不在规定日期(28号),不执行停保任务。", day_of_month)
return {"files": []}
if __name__ == "__main__":
setup_logging()
main()