feat: 退网评估后端:单个或清单提交,按逻辑站点并行评估并写入固定模板

每个逻辑站点一个排队任务,与采集共用并行数量;工参按拆除前后规则取本系统快照,指标暂用模拟数据;全部站点结束后写频段清退、低效拆除两份结果表。去掉原来的通用导出任务。
This commit is contained in:
2026-11-11 03:25:38 +08:00
parent 14d1b4c7bc
commit 85ac243ea1
20 changed files with 2490 additions and 173 deletions
+302
View File
@@ -0,0 +1,302 @@
"""把评估结果写进两份固定模板:频段清退评估、低效拆除评估。
模板版式不改,只按数据扩缩示例行。各块按表头文字定位,不依赖固定行号。判定列沿用模板里的公式,
停闭后还没有数据时判定写「/」,评估结果写「拆除后再评估」。
"""
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")
LATER = "拆除后再评估"
@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 in (16, 17, 18):
put(ws, row, col, "/")
for col, formula in _row_checks(row).items():
put(ws, row, col, formula)
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, _verdict("P", "V", row), 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 _row_checks(row: int) -> dict[int, str]:
after = f"COUNT(I{row}:N{row})=0"
return {
19: f'=IF({after},"/",IF(L{row}>10,"N",IF((L{row}-E{row})>5,"N","Y")))',
20: f'=IF({after},"/",IF(M{row}<97,"N",IF((F{row}-M{row})>1,"N","Y")))',
21: f'=IF({after},"/",IF(N{row}<0.3,"Y","N"))',
22: f'=IF({after},"/",IF(O{row}<0.3,"Y","N"))',
}
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)')
put(ws, row, 16, f'=IF(OR(COUNT(B{row},I{row})<2,N(B{row})=0),"/",IF(((B{row}-I{row})/B{row})<0.1,"Y","N"))')
put(ws, row, 17, f'=IF(OR(COUNT(C{row},J{row})<2,N(C{row})=0),"/",IF(((C{row}-J{row})/C{row})<0.1,"Y","N"))')
put(ws, row, 18, f'=IF(COUNT(D{row},K{row})<2,"/",IF((K{row}-D{row})<2,"Y","N"))')
for col, formula in _row_checks(row).items():
put(ws, row, col, formula)
def _verdict(first_col: str, last_col: str, row: int) -> str:
area = f"{first_col}{row}:{last_col}{row}"
return f'=IF(COUNTIF({area},"N")>0,"不通过",IF(COUNTIF({area},"/")>0,"{LATER}","通过"))'
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)
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)
checks = {
19: f'=IF(N(E{row})>=3,"Y","N")',
20: f'=IF(ISNUMBER(J{row}),IF(J{row}<=20,"Y","N"),"/")',
21: f'=IF(ISNUMBER(K{row}),IF(K{row}<=20,"Y","N"),"/")',
22: f'=IF(ISNUMBER(Q{row}),IF(Q{row}<=40,"Y","N"),"/")',
23: f'=IF(AND(ISNUMBER(L{row}),ISNUMBER(R{row})),IF((L{row}-R{row})<5,"Y","N"),"/")',
}
for col, formula in checks.items():
put(ws, row, col, formula)
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, _verdict("S", "W", row), 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