# -*- coding: utf-8 -*- """目录预检:校验 base_dir(统一目录)是否满足插件各阶段需求。 用户设置资料根目录后,可先运行「目录预检」阶段确认目录结构齐备(真实布局): - 根目录:源名单《*社保变更及增减名单*.xlsx》、员工花名册.xlsx - 台账目录(默认根目录,可单独设置):社保户头管理台账*.xlsx(含缴费比例 sheet) - 社保增减名单制作/模板/:增员模版.xls、减员模版.xls - 告知书下发/模板/:三类告知书模板 (名单制作输出/、回盘打印/、告知书下发/输出/ 由各阶段运行时自动创建,不预检) 输出逐项 {名称, 是否就绪, 说明},未就绪项汇总列出,供用户补齐后重跑。 """ import os import re from datetime import datetime from .. import config def _current_month_file(dir_path, pattern): """目录内匹配 pattern 且文件名月份=当月的文件(告知书数据源要求当月)。""" if not os.path.isdir(dir_path): return None, "目录不存在" found = [n for n in os.listdir(dir_path) if n.lower().endswith(".xlsx") and not n.startswith("~$") and re.match(pattern.replace("*", ".*"), n)] if not found: return None, f"未找到匹配 {pattern} 的 xlsx" now = datetime.now() for name in found: m = re.search(r"(20\d{2})年(\d{1,2})月", name) or re.search(r"(20\d{2})(\d{2})", name) if m and (int(m.group(1)), int(m.group(2))) == (now.year, now.month): return os.path.join(dir_path, name), "当月文件" return None, f"均为历史月份:{sorted(found)[:5]}" def _check_sheets(excel_path, required): """校验 xlsx 是否包含所有必需 sheet(关键字匹配)。返回 (ok, 缺失列表)。""" try: import pandas as pd xl = pd.ExcelFile(excel_path) names = [n.strip() for n in xl.sheet_names] missing = [kw for kw in required if not any(kw in n for n in names)] return (not missing), missing except Exception as e: return False, [f"读取失败:{e}"] def check_directory(): """执行目录预检。返回 dict {ok, missing, checks}。 checks: [{name, ok, detail}] 逐项清单(缺失/异常项 ok=False)。 """ base = config.get_base_dir() ledger = config.get_ledger_dir() checks = [] def add(name, ok, detail): checks.append({"name": name, "ok": bool(ok), "detail": detail}) # ---- 0. 目录本身 ---- base_ok = os.path.isdir(base) add("资料根目录", base_ok, base if base_ok else f"目录不存在:{base}") # ---- 1. 名单制作 / 系统办理:源名单 ---- src_file = None candidates = [] if base_ok: candidates = [p for p in os.listdir(base) if p.lower().endswith(".xlsx") and not p.startswith("~$") and config.SOURCE_KEYWORD in p] add(f"源名单(*{config.SOURCE_KEYWORD}*.xlsx)", bool(candidates), "、".join(sorted(candidates)[:3]) or "未找到,请放入当月《社保变更及增减名单》") if candidates: src_file = os.path.join(base, max( candidates, key=lambda n: os.path.getmtime(os.path.join(base, n)))) # ---- 2. 告知书数据源:当月文件 ---- if base_ok: cur, note = _current_month_file(base, f"*{config.SOURCE_KEYWORD}*") else: cur, note = None, "根目录不存在" add("源名单(当月文件,告知书用)", cur is not None, note) # ---- 3. 源名单关键 sheet ---- if src_file: ok, missing = _check_sheets(src_file, ["参保名单", "停保名单"]) add("源名单关键 sheet(参保名单/停保名单)", ok, "缺失:" + "、".join(missing) if missing else "齐备") else: add("源名单关键 sheet(参保名单/停保名单)", False, "源名单缺失,跳过校验") # ---- 4. 名单制作:模板 + 输出目录 ---- tpl_dir = config.roster_template_dir() add_exact = os.path.join(tpl_dir, config.TEMPLATE_ADD) add_hit = (os.path.isfile(add_exact) or any("增员模版" in f for f in os.listdir(tpl_dir)) if os.path.isdir(tpl_dir) else False) add("增员模版(社保增减名单制作/模板/增员模版.xls)", add_hit, f"{tpl_dir}\\{config.TEMPLATE_ADD}" if base_ok else "根目录不存在") stop_exact = os.path.join(tpl_dir, config.TEMPLATE_STOP) stop_hit = (os.path.isfile(stop_exact) or any("减员模版" in f for f in os.listdir(tpl_dir)) if os.path.isdir(tpl_dir) else False) add("减员模版(社保增减名单制作/模板/减员模版.xls)", stop_hit, f"{tpl_dir}\\{config.TEMPLATE_STOP}" if base_ok else "根目录不存在") # ---- 5. 台账(含缴费比例 sheet) ---- ledger_ok = os.path.isdir(ledger) ledger_file = None if ledger_ok: cand = [p for p in os.listdir(ledger) if p.lower().endswith(".xlsx") and "社保户头管理台账" in p and not p.startswith("~$")] if cand: ledger_file = os.path.join(ledger, max(cand, key=lambda n: os.path.getmtime(os.path.join(ledger, n)))) add("社保户头管理台账", ledger_file is not None, os.path.basename(ledger_file) if ledger_file else f"未找到(目录:{ledger})") if ledger_file: ok, missing = _check_sheets(ledger_file, ["缴费比例"]) add("台账「缴费比例」sheet", ok, "缺失:" + "、".join(missing) if missing else "齐备") else: add("台账「缴费比例」sheet", False, "台账缺失,跳过校验") # ---- 6. 告知书:花名册 + 三类模板 ---- roster = os.path.join(base, "员工花名册.xlsx") add("员工花名册(告知书二级部门/接收人)", base_ok and os.path.isfile(roster), roster if base_ok else "根目录不存在") tpl_dir_notice = config.notice_template_dir() tpl_dir_ok = os.path.isdir(tpl_dir_notice) for key, tpl in config.NOTICE_TEMPLATES.items(): p = os.path.join(tpl_dir_notice, tpl["file"]) add(f"告知书模板[{key}](告知书下发/模板/{tpl['file']})", tpl_dir_ok and os.path.isfile(p), p if tpl_dir_ok else f"模板目录不存在:{tpl_dir_notice}") missing = [c for c in checks if not c["ok"]] return {"ok": not missing, "missing": len(missing), "checks": checks}