Commit b7c2daf2 by 周立明

北汽导出数据填充前先清空模板数据

1 parent 1c888b15
...@@ -1379,9 +1379,7 @@ class BaicSpecialProcessor: ...@@ -1379,9 +1379,7 @@ class BaicSpecialProcessor:
# + 广联报表(产值目标/新车交付数/触客数/交车数核对-产值表/触客数核对-产值表) # + 广联报表(产值目标/新车交付数/触客数/交车数核对-产值表/触客数核对-产值表)
# 12/13 = 截止目前产值(万)/ 达成率(D+);24/25/26 = 评灯标准(按品牌固定取值) # 12/13 = 截止目前产值(万)/ 达成率(D+);24/25/26 = 评灯标准(按品牌固定取值)
# 20/21/22/23 = 周度评灯(截止目前/W1/W2/W3),由代码按综合渗透+评灯标准填灯色+底色 # 20/21/22/23 = 周度评灯(截止目前/W1/W2/W3),由代码按综合渗透+评灯标准填灯色+底色
KEEP = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, # 注:本门店块已显式写入全部 1~32 列(A~AF),无需再按白名单清空(见 _fill_block 末尾说明)。
20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32}
def _fill_block(r_a, r_b, idx, d): def _fill_block(r_a, r_b, idx, d):
# r_a 为两行合并的锚点行:可写入/清空本块所有列(合并区显示锚点值) # r_a 为两行合并的锚点行:可写入/清空本块所有列(合并区显示锚点值)
...@@ -1466,10 +1464,9 @@ class BaicSpecialProcessor: ...@@ -1466,10 +1464,9 @@ class BaicSpecialProcessor:
(int(node_delivery) if node_delivery is not None else None)) (int(node_delivery) if node_delivery is not None else None))
setattr(d, f'w{j}_comprehensive_penetration', pen_node) setattr(d, f'w{j}_comprehensive_penetration', pen_node)
setattr(d, f'w{j}_light_level', light_txt) setattr(d, f'w{j}_light_level', light_txt)
# 其余列(截止目前产值/达成率/触客率/周趋势等)来自 D+ 或系统计算,尚未接入 → 清空 # 说明:_fill_block 已显式写入本门店块的全部 1~32 列(A~AF),
for c in range(1, 33): # 不存在「已写入列之外、仍需清空的 1~32 列」,故原先用于清空白名单外列的循环为 no-op,已移除。
if c not in KEEP: # 32 列之外(AG 起)的内容保持模板原样,不受本函数影响。
_set_cell(ws, r_a, c, None)
# r_b(权益行)仅非合并列可写:业务(I) + Q/R/S # r_b(权益行)仅非合并列可写:业务(I) + Q/R/S
_set_cell(ws, r_b, 9, '权益') _set_cell(ws, r_b, 9, '权益')
_set_cell(ws, r_b, 17, rights_cnt) # Q 权益订单数 _set_cell(ws, r_b, 17, rights_cnt) # Q 权益订单数
...@@ -1638,15 +1635,104 @@ class BaicSpecialProcessor: ...@@ -1638,15 +1635,104 @@ class BaicSpecialProcessor:
_set_cell(ws, data_row, 12, _penetration(ext_total, total_delivery)) # L 整车延保渗透率 _set_cell(ws, data_row, 12, _penetration(ext_total, total_delivery)) # L 整车延保渗透率
_set_cell(ws, data_row, 13, rights_total) # M 原厂权益成交数 = 权益订单合计 _set_cell(ws, data_row, 13, rights_total) # M 原厂权益成交数 = 权益订单合计
_set_cell(ws, data_row, 14, _penetration(rights_total, total_delivery)) # N 原厂权益渗透率 _set_cell(ws, data_row, 14, _penetration(rights_total, total_delivery)) # N 原厂权益渗透率
# O(15)~R(18) 环比:当前管线无上月同口径数据,显式清空(避免残留模板示例值) # O(15)~R(18) 环比(本月 - 上月)在下方「上月真实数据」计算完成后回填(见 _fill_mom 段)
# ---- 上月真实数据:用于填充「上月」汇总段,使其与上月结果表的「本月」汇总对齐 ----
# 旧逻辑「上月段数据行保留模板原样」会让 9 月导出的「8月汇总」显示模板示例占位值,
# 与 8 月结果表真实汇总对不上。现改为:取上一月真实任务(DataProcessTask,baic_special,
# month=上月)的明细/业务/汇总,按与「本月」段完全相同的口径聚合后回填。
# 无上月任务数据 → 仅更新月份标签,保留模板原样(避免用占位值误导)。
prev_month = None
if len(month) >= 7 and month[4] == '-':
try:
_y, _m = int(month[:4]), int(month[5:7])
_pm = _m - 1 if _m > 1 else 12
_py = _y if _pm != 12 else _y - 1
prev_month = f"{_py:04d}-{_pm:02d}"
except (ValueError, IndexError):
prev_month = None
_prev_details = []
_prev_ext = _prev_rights = 0
_prev_delivery = _prev_visit = _prev_current = 0
_prev_visit_rate = _prev_combined = None
_prev_store_cnt = 0
_prev_summary = None
if prev_month:
_prev_task = DataProcessTask.query.filter_by(
data_table_type='baic_special', month=prev_month).first()
if _prev_task:
_prev_details = (BaicSpecialMonthData.query
.filter_by(task_id=_prev_task.id, is_deleted=0)
.order_by(BaicSpecialMonthData.store_id).all())
if _prev_details:
_p_ids = [d.store_id for d in _prev_details]
_p_biz = (BaicSpecialMonthlyBusiness.query
.filter(BaicSpecialMonthlyBusiness.store_id.in_(_p_ids)).all())
_p_biz_map = {}
for _br in _p_biz:
_p_biz_map.setdefault(_br.store_id, {})[_br.business_type] = _br.order_cnt
_prev_ext = sum((_p_biz_map.get(d.store_id, {}).get(BAIC_BIZ_EXT_WARRANTY) or 0)
for d in _prev_details)
_prev_rights = sum((_p_biz_map.get(d.store_id, {}).get(BAIC_BIZ_RIGHTS) or 0)
for d in _prev_details)
_prev_delivery = sum((d.new_delivery_cnt or 0) for d in _prev_details)
_prev_visit = sum((d.visit_cnt or 0) for d in _prev_details)
_prev_current = sum((_dec_to_float(d.output_current_wan) or 0) for d in _prev_details)
_prev_visit_rate = (_prev_visit / _prev_delivery) if _prev_delivery else None
_prev_combined = _prev_ext + _prev_rights
_prev_store_cnt = len(_prev_details)
_prev_summary = (BaicSpecialMonthSummaryMetrics.query
.filter_by(task_id=_prev_task.id, is_deleted=0)
.order_by(BaicSpecialMonthSummaryMetrics.id.desc()).first())
# ---- 品牌汇总·本月段 环比(O~R) = 本月 - 上月 ----
# 与模板示例口径一致(已用模板 7月/6月样例校验:售前触客率0.8667-0.8562=0.0105 等)。
# 仅当存在上月真实任务数据时才填;否则清空,避免残留模板示例值误导。
if cur_month_row is not None:
if _prev_details:
_cur_open_rate = _dec_to_float(summary.after_sale_scene_open_rate) if summary else None
_prev_open_rate = _dec_to_float(_prev_summary.after_sale_scene_open_rate) if _prev_summary else None
_set_cell(ws, data_row, 15, _diff(_cur_open_rate, _prev_open_rate)) # O 售后场景开通率环比
_set_cell(ws, data_row, 16, _diff(_visit_rate_total, _prev_visit_rate)) # P 售前触客率环比
_set_cell(ws, data_row, 17, _diff( # Q 整车延保渗透率环比
_penetration(ext_total, total_delivery),
_penetration(_prev_ext, _prev_delivery)))
_set_cell(ws, data_row, 18, _diff( # R 原厂权益渗透率环比
_penetration(rights_total, total_delivery),
_penetration(_prev_rights, _prev_delivery)))
else:
# 无上月真实任务数据 → 清空,避免残留模板示例值
for c in range(15, 19): for c in range(15, 19):
_set_cell(ws, data_row, c, None) _set_cell(ws, data_row, c, None)
if cur_prev_row is not None: if cur_prev_row is not None:
lbl = ws.cell(cur_prev_row, 1).value lbl = ws.cell(cur_prev_row, 1).value
if lbl and '上月' in str(lbl): if lbl and '上月' in str(lbl):
ws.cell(cur_prev_row, 1).value = re.sub( ws.cell(cur_prev_row, 1).value = re.sub(
r'上月\s*\S*', f'上月 {prev_month_label}', str(lbl)) r'上月\s*\S*', f'上月 {prev_month_label}', str(lbl))
# 上月段数据行保留模板原样(作为固定参考,框架轮不重写) _p_row = cur_prev_row + 2
if _prev_details:
# 上月段数据行:用上月真实任务数据回填(与上月结果表「本月」汇总同口径)
_set_cell(ws, _p_row, 1, '北汽') # 项目
_set_cell(ws, _p_row, 2, '北汽') # 品牌
_set_cell(ws, _p_row, 3, _prev_store_cnt) # 门店数
_set_cell(ws, _p_row, 4, _prev_current) # D 产值(万)
_set_cell(ws, _p_row, 5, _prev_delivery) # E 交付数
_set_cell(ws, _p_row, 6, _prev_visit) # F 售前触客数
_set_cell(ws, _p_row, 7, _prev_visit_rate) # G 售前触客率
if _prev_summary:
_set_cell(ws, _p_row, 8, _prev_summary.after_sale_store_cnt)
_set_cell(ws, _p_row, 9, _dec_to_float(_prev_summary.after_sale_scene_open_rate))
_set_cell(ws, _p_row, 10, _prev_combined) # J 售后订单数
_set_cell(ws, _p_row, 11, _prev_ext) # K 整车延保成交数
_set_cell(ws, _p_row, 12, _penetration(_prev_ext, _prev_delivery)) # L 整车延保渗透率
_set_cell(ws, _p_row, 13, _prev_rights) # M 原厂权益成交数
_set_cell(ws, _p_row, 14, _penetration(_prev_rights, _prev_delivery)) # N 原厂权益渗透率
for _c in range(15, 19): # O~R 环比:清空
_set_cell(ws, _p_row, _c, None)
else:
# 无上月真实任务数据 → 清除模板占位数据,整行(A~R)置空,避免示例值误导
for _c in range(1, 19):
_set_cell(ws, _p_row, _c, None)
# ---- sheet 重命名(月份占位替换)---- # ---- sheet 重命名(月份占位替换)----
try: try:
...@@ -1668,7 +1754,11 @@ class BaicSpecialProcessor: ...@@ -1668,7 +1754,11 @@ class BaicSpecialProcessor:
# OSS 上传(失败不影响本地预览) # OSS 上传(失败不影响本地预览)
url = '' url = ''
try: try:
object_key = f"data-process/{datetime.now().strftime('%Y%m%d')}/baic_special_{month}_{task_id}.xlsx" # 每次重新生成都带唯一时间戳(到微秒),避免同一 task_id 复用同一 OSS key
# 导致覆盖后下游/CDN 仍返回首次上传的文件。
_now = datetime.now()
_ts = _now.strftime('%Y%m%d%H%M%S%f')
object_key = f"data-process/{_now.strftime('%Y%m%d')}/baic_special_{month}_{task_id}_{_ts}.xlsx"
url = upload_file(tmp, object_key) url = upload_file(tmp, object_key)
logger.info(f"[北汽] 结果表已上传 OSS:task {task_id} -> {url}") logger.info(f"[北汽] 结果表已上传 OSS:task {task_id} -> {url}")
except Exception as ue: except Exception as ue:
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!