# -*- coding: utf-8 -*- """XQ0084 输出层:核对结果工作簿。""" from __future__ import annotations import os from openpyxl import Workbook from openpyxl.styles import Alignment, Font, PatternFill from openpyxl.utils import get_column_letter from .. import config COLUMNS = [ ("emp_no", "工号", 11), ("holder", "持卡人姓名", 14), ("reason", "变更原因", 24), ("table_bank", "开户银行(表格)", 18), ("table_branch", "开户支行(表格)", 20), ("table_card", "银行卡号(表格)", 23), ("table_id", "身份证号(表格)", 23), ("ocr_id", "身份证号(识别)", 23), ("ocr_name", "姓名(识别)", 11), ("ocr_card", "银行卡号(识别)", 23), ("ocr_bank", "开户银行(识别)", 18), ("id_status", "身份证核对", 11), ("name_status", "姓名核对", 11), ("card_status", "卡号核对", 11), ("bank_status", "开户银行核对", 13), ("self_check", "表格自检", 26), ("triggers", "触发原因", 22), ("hr_status", "HR导入", 12), ("hr_note", "HR导入说明", 36), ("conclusion", "核对结论", 22), ("notes", "说明", 60), ("image_count", "影像数", 8), ("image_dir", "影像目录", 50), ] _HEAD_FILL = PatternFill("solid", fgColor="1F4E78") _HEAD_FONT = Font(color="FFFFFF", bold=True) _MANUAL_FILL = PatternFill("solid", fgColor="FFF2CC") _PASS_FILL = PatternFill("solid", fgColor="E2EFDA") _CASH_FILL = PatternFill("solid", fgColor="DDEBF7") def _write_sheet(ws, rows): ws.append([title for _, title, _ in COLUMNS]) for col, (_, title, width) in enumerate(COLUMNS, start=1): cell = ws.cell(row=1, column=col) cell.fill = _HEAD_FILL cell.font = _HEAD_FONT cell.alignment = Alignment(horizontal="center", vertical="center") ws.column_dimensions[get_column_letter(col)].width = width ws.row_dimensions[1].height = 24 ws.freeze_panes = "A2" for record in rows: ws.append([record.get(key, "") for key, _, _ in COLUMNS]) r = ws.max_row for col in range(1, len(COLUMNS) + 1): ws.cell(row=r, column=col).alignment = Alignment(vertical="center", wrap_text=True) conclusion = record.get("conclusion", "") fill = (_MANUAL_FILL if conclusion == "需人工核实" else _CASH_FILL if conclusion.startswith("现金发放") else _PASS_FILL) ws.cell(row=r, column=[k for k, _, _ in COLUMNS].index("conclusion") + 1).fill = fill ws.auto_filter.ref = f"A1:{get_column_letter(len(COLUMNS))}{max(ws.max_row, 1)}" def write_workbook(rows, path): """写出「核对结果 / 异常名单」两个 sheet。""" os.makedirs(os.path.dirname(path), exist_ok=True) wb = Workbook() ws = wb.active ws.title = config.SHEET_FULL _write_sheet(ws, rows) abnormal = [r for r in rows if r.get("conclusion") == "需人工核实"] ws2 = wb.create_sheet(config.SHEET_ABNORMAL) _write_sheet(ws2, abnormal) wb.save(path) return path