"""把评估结果写进两份固定模板:频段清退评估、低效拆除评估。 模板版式不改,只按数据扩缩示例行。各块按表头文字定位,不依赖固定行号。 判断列和评估结果不按数据算,和原来的模板工具一样写固定值。 """ from __future__ import annotations import io from dataclasses import dataclass, field from datetime import date from pathlib import Path from openpyxl import load_workbook from openpyxl.utils import get_column_letter from app.evaluate.demand import base_id from app.evaluate.xlsx import ( MISSING, ZEBRA, apply_style, block_end, clear_row, find_row, fill_rows, fit_block, header_map, merge_down, put, row_style, text, unmerge_within, ) TEMPLATES = Path(__file__).with_name("templates") NEIGHBOR_LAST_COL = 24 SECTOR_LAST_COL = 25 BEFORE_KEYS = ("traffic_gb", "rrc_max", "high_load", "mr_weak_pct", "access_rate", "drop_rate") # 判断列的固定值,逐格照原来的模板工具:周边站小区行 P–R 填「/」、S–V 填 Y,汇总行 P–V 全填 Y; # 扇区小区行五个判断列填「/」,扇区汇总行填 N、Y、Y、Y、Y;评估结果都填「通过」。 NEIGHBOR_ROW_FLAGS = {16: "/", 17: "/", 18: "/", 19: "Y", 20: "Y", 21: "Y", 22: "Y"} NEIGHBOR_SUMMARY_FLAGS = dict.fromkeys(range(16, 23), "Y") SECTOR_ROW_FLAGS = dict.fromkeys(range(19, 24), "/") SECTOR_SUMMARY_FLAGS = {19: "N", 20: "Y", 21: "Y", 22: "Y", 23: "Y"} PASSED = "通过" @dataclass class Item1: county: str logical_site: str cell_name: str cgi: str retire_reason: str applicant: str need_date: date | None locatable: bool = True @dataclass class NeighborGroup: site: str cells: list[dict] = field(default_factory=list) def write_pinduan(item1: list[Item1], groups: list[NeighborGroup]) -> bytes: workbook = load_workbook(TEMPLATES / "pinduan.xlsx") sheet = workbook.worksheets[0] _write_item1(sheet, item1) _write_neighbors(sheet, _neighbor_leaf(sheet), groups) return _save(workbook) def write_dixiao(item1: list[Item1], section1: list[dict], sectors: list[dict], groups: list[NeighborGroup]) -> bytes: workbook = load_workbook(TEMPLATES / "dixiao.xlsx") sheet = workbook["低效评估模板"] if "低效评估模板" in workbook.sheetnames else workbook.worksheets[0] _write_item1(sheet, item1) _write_section1(sheet, section1) _write_sectors(sheet, sectors) _write_neighbors(sheet, _neighbor_leaf(sheet), groups) return _save(workbook) def _save(workbook) -> bytes: workbook.calculation.fullCalcOnLoad = True buffer = io.BytesIO() workbook.save(buffer) return buffer.getvalue() def _write_item1(ws, items: list[Item1]) -> None: """退网站点审核信息:清单逐行;整个逻辑站点都定位不到的行标红。""" head = find_row(ws, lambda row: text(ws, row, 1) == "区县" and text(ws, row, 2) == "逻辑站点名称") if head is None: raise ValueError("模板里没有找到退网站点审核信息的表头") columns = header_map(ws, head) last_col = max(columns.values()) first = head + 1 style = row_style(ws, first, last_col) fit_block(ws, first, block_end(ws, first, last_col), len(items)) for index, item in enumerate(items): row = first + index apply_style(ws, row, style) clear_row(ws, row, last_col) values = { "区县": item.county, "逻辑站点名称": item.logical_site, "小区名称": item.cell_name, "基站ID": base_id(item.cgi), "CGI": item.cgi, "退网原因": item.retire_reason, "是否VIP小区": "否", "申请人": item.applicant, "地市优化、维护组长审核结果": "通过", "计划退网时间": item.need_date, } for label, value in values.items(): col = columns.get(label) if col: put(ws, row, col, value, "yyyy-mm-dd" if label == "计划退网时间" else None) if not item.locatable: fill_rows(ws, row, row, last_col, MISSING) def _neighbor_leaf(ws) -> int: leaf = find_row(ws, lambda row: text(ws, row, 2) == "日均流量(GB)" and text(ws, row, 3) == "日均有效RRC连接最大数") if leaf is None: raise ValueError("模板里没有找到(5G、LTE)网管统计指标评估的表头") return leaf def _write_neighbors(ws, leaf: int, groups: list[NeighborGroup]) -> None: """(5G、LTE)网管统计指标评估:每个逻辑站点一组,周边小区逐行,组尾汇总;评估结果、备注按组合并。""" summary = find_row(ws, lambda row: text(ws, row, 1) == "汇总", start=leaf + 1) if summary is None: raise ValueError("模板里没有找到(5G、LTE)汇总行") first = leaf + 1 cell_style = row_style(ws, first, NEIGHBOR_LAST_COL) sum_style = row_style(ws, summary, NEIGHBOR_LAST_COL) unmerge_within(ws, first, summary) needed = sum(len(group.cells) + 1 for group in groups if group.cells) fit_block(ws, first, summary, needed or 2) if not needed: apply_style(ws, first, cell_style) clear_row(ws, first, NEIGHBOR_LAST_COL) put(ws, first, 1, "没有可评估的周边站") apply_style(ws, first + 1, sum_style) clear_row(ws, first + 1, NEIGHBOR_LAST_COL) put(ws, first + 1, 1, "汇总") return row = first shown = 0 for group in groups: if not group.cells: continue group_first = row for cell in group.cells: apply_style(ws, row, cell_style) clear_row(ws, row, NEIGHBOR_LAST_COL) put(ws, row, 1, cell["cell"]) for offset, key in enumerate(BEFORE_KEYS): put(ws, row, 2 + offset, (cell.get("before") or {}).get(key)) put(ws, row, 9 + offset, (cell.get("after") or {}).get(key)) put(ws, row, 8, 0) put(ws, row, 15, 0 if cell.get("after") else None) for col, flag in NEIGHBOR_ROW_FLAGS.items(): put(ws, row, col, flag) row += 1 _neighbor_summary(ws, row, group_first, row - 1, sum_style) border = ws.cell(row=group_first, column=22).border merge_down(ws, 23, group_first, row, PASSED, border_from=border) merge_down(ws, 24, group_first, row, group.site, wrap=True, border_from=border) if shown % 2 == 1: fill_rows(ws, group_first, row, NEIGHBOR_LAST_COL, ZEBRA) shown += 1 row += 1 def _neighbor_summary(ws, row: int, first: int, last: int, style) -> None: apply_style(ws, row, style) clear_row(ws, row, NEIGHBOR_LAST_COL) put(ws, row, 1, "汇总") for col in (2, 3, 5, 6, 7, 9, 10, 12, 13, 14): letter = get_column_letter(col) put(ws, row, col, f"=IFERROR(AVERAGE({letter}{first}:{letter}{last}),\"\")") for col in (4, 11): letter = get_column_letter(col) put(ws, row, col, f'=IF(COUNT({letter}{first}:{letter}{last})=0,"",SUM({letter}{first}:{letter}{last}))') put(ws, row, 8, 0) put(ws, row, 15, f'=IF(COUNT(I{first}:I{last})=0,"",0)') for col, flag in NEIGHBOR_SUMMARY_FLAGS.items(): put(ws, row, col, flag) def _write_section1(ws, rows: list[dict]) -> None: """低效拆除第 1 节:待拆小区自身的容量、覆盖和问题点情况,按表头填。""" title = find_row(ws, lambda row: text(ws, row, 1).startswith("1、评估本站退服")) if title is None: return leaf = find_row(ws, lambda row: text(ws, row, 1) == "逻辑站点名称", start=title + 1, stop=title + 6) if leaf is None: return columns = header_map(ws, leaf) for label, col in header_map(ws, leaf - 1).items(): columns.setdefault(label, col) last_col = max(columns.values()) first = leaf + 1 style = row_style(ws, first, last_col) fit_block(ws, first, block_end(ws, first, last_col), len(rows)) for index, values in enumerate(rows or [{}]): row = first + index apply_style(ws, row, style) clear_row(ws, row, last_col) for label, value in values.items(): col = columns.get(label) if col: put(ws, row, col, value) def _write_sectors(ws, sectors: list[dict]) -> None: """低效拆除第 2 节:同扇区去掉待拆小区后的基础覆盖层,停闭前后指标与扇区汇总评估。""" title = find_row(ws, lambda row: text(ws, row, 1).startswith("2、评估拆除站点是否满足低效原则")) if title is None: return leaf = find_row(ws, lambda row: text(ws, row, 1) == "扇区名称" and text(ws, row, 2) == "小区名称" and text(ws, row, 3) == "CGI", start=title + 1) if leaf is None: return summary = find_row(ws, lambda row: text(ws, row, 2) == "扇区汇总评估", start=leaf + 1) if summary is None: return first = leaf + 1 cell_style = row_style(ws, first, SECTOR_LAST_COL) sum_style = row_style(ws, summary, SECTOR_LAST_COL) unmerge_within(ws, first, summary) needed = sum(len(sector["cells"]) + 1 for sector in sectors) fit_block(ws, first, summary, needed or 2) if not needed: apply_style(ws, first, cell_style) clear_row(ws, first, SECTOR_LAST_COL) put(ws, first, 1, "没有可评估的扇区") apply_style(ws, first + 1, sum_style) clear_row(ws, first + 1, SECTOR_LAST_COL) put(ws, first + 1, 2, "扇区汇总评估") return row = first for index, sector in enumerate(sectors): group_first = row for cell in sector["cells"]: apply_style(ws, row, cell_style) clear_row(ws, row, SECTOR_LAST_COL) equiv = cell["equiv"] if cell["equiv"] is not None else "/" values = ( None, cell["cell"], cell["cgi"], cell["band"], sector["total"], sector["base"], cell["traffic_b"], cell["util_b"], equiv, sector["single_b"], sector["util_b"], cell["cover_b"], cell["traffic_a"], cell["util_a"], equiv, sector["single_a"], sector["util_a"], cell["cover_a"], ) for col, value in enumerate(values, start=1): if col > 1: put(ws, row, col, value) for col, flag in SECTOR_ROW_FLAGS.items(): put(ws, row, col, flag) row += 1 apply_style(ws, row, sum_style) clear_row(ws, row, SECTOR_LAST_COL) equiv_sum = sector["equiv"] if sector["equiv"] is not None else "/" totals = ( None, "扇区汇总评估", "/", 0, sector["total"], sector["base"], sector["traffic_b"], "/", equiv_sum, sector["single_b"], sector["util_b"], sector["cover_b"], sector["traffic_a"], "/", equiv_sum, sector["single_a"], sector["util_a"], sector["cover_a"], ) for col, value in enumerate(totals, start=1): if col > 1: put(ws, row, col, value) for col, flag in SECTOR_SUMMARY_FLAGS.items(): put(ws, row, col, flag) border = ws.cell(row=group_first, column=12).border merge_down(ws, 1, group_first, row, sector["sector"], wrap=True, border_from=border) merge_down(ws, 24, group_first, row, PASSED, border_from=border) merge_down(ws, 25, group_first, row, sector["site"], wrap=True, border_from=border) if index % 2 == 1: fill_rows(ws, group_first, row, SECTOR_LAST_COL, ZEBRA) row += 1