Commit 27ef3034 by 江名洋

fix(analysis): 兜底获取门店信息并修复兜底指标解析

1 parent e0239b6d
Showing 43 changed files with 26 additions and 31 deletions
...@@ -535,12 +535,12 @@ class AnalysisService: ...@@ -535,12 +535,12 @@ class AnalysisService:
sql = f""" sql = f"""
SELECT SELECT
merchant_name, merchant_name,
province_name, province,
operation_mode, operation_mode,
area_manager, area_manager,
flight_supervisor, flight_supervisor,
online_ops online_ops
FROM ods_store_info FROM ods_store_config
WHERE merchant_name IN ({placeholders}) WHERE merchant_name IN ({placeholders})
AND deleted_flag = 'N' AND deleted_flag = 'N'
""" """
...@@ -561,7 +561,7 @@ class AnalysisService: ...@@ -561,7 +561,7 @@ class AnalysisService:
if not name: if not name:
continue continue
result[name] = { result[name] = {
"province_name": row.get('province_name') or "", "province_name": row.get('province') or "",
"direct_sale": row.get('operation_mode') or "非直营", "direct_sale": row.get('operation_mode') or "非直营",
"area_supervision": row.get('area_manager') or "", "area_supervision": row.get('area_manager') or "",
"flight_supervision": row.get('flight_supervisor') or "", "flight_supervision": row.get('flight_supervisor') or "",
...@@ -740,8 +740,12 @@ class AnalysisService: ...@@ -740,8 +740,12 @@ class AnalysisService:
return start_row return start_row
db_perf = AnalysisService._query_store_performance(store_names, start_date, end_date) db_perf = AnalysisService._query_store_performance(store_names, start_date, end_date)
logger.info(f"DB fetched {len(db_perf)} stores ({start_date} ~ {end_date})") logger.info(f"DB fetched {len(db_perf)} stores ({start_date} ~ {end_date})")
# 兜底:从 ods_store_info 获取直营/非直营等门店基础信息
store_info = AnalysisService._query_store_info(store_names)
logger.info(f"Store info fetched {len(store_info)} stores")
return AnalysisService._write_data_rows(ws, store_subset, factory_sheets, db_perf, return AnalysisService._write_data_rows(ws, store_subset, factory_sheets, db_perf,
start_date, end_date, formula_map, skip_external_cols, start_row) start_date, end_date, formula_map, skip_external_cols, start_row,
store_info=store_info)
@staticmethod @staticmethod
def build_report(store_df: pd.DataFrame, def build_report(store_df: pd.DataFrame,
...@@ -930,7 +934,8 @@ class AnalysisService: ...@@ -930,7 +934,8 @@ class AnalysisService:
@staticmethod @staticmethod
def _write_data_rows(ws, store_df, factory_sheets, db_perf: dict = None, def _write_data_rows(ws, store_df, factory_sheets, db_perf: dict = None,
start_date: str = None, end_date: str = None, start_date: str = None, end_date: str = None,
formula_map: dict = None, skip_external_cols: set = None, start_row=4): formula_map: dict = None, skip_external_cols: set = None, start_row=4,
store_info: dict = None):
"""写入数据行到模板,返回下一个可用行号 """写入数据行到模板,返回下一个可用行号
Args: Args:
db_perf: 从数据库查询的门店产值数据 db_perf: 从数据库查询的门店产值数据
...@@ -1027,12 +1032,13 @@ class AnalysisService: ...@@ -1027,12 +1032,13 @@ class AnalysisService:
province_db = store_perf.get("province_name", "") province_db = store_perf.get("province_name", "")
province_factory = store_row.get(province_col if province_col else "区域(省)", "") province_factory = store_row.get(province_col if province_col else "区域(省)", "")
_style_cell(row_num, 6, province_db if province_db else province_factory) _style_cell(row_num, 6, province_db if province_db else province_factory)
# 直营与否:从数据库查询(直营/非直营),operation_mode 来自 ods_store_config # 直营与否:优先从 db_perf 取,兜底用 store_info(ods_store_info 表)
_style_cell(row_num, 7, store_perf.get("operation_mode", "")) si = (store_info or {}).get(store_name_str, {})
# 区域运营 / 飞行督导 / 线上运营:从数据库提取 _style_cell(row_num, 7, store_perf.get("operation_mode", "") or si.get("direct_sale", ""))
_style_cell(row_num, 8, store_perf.get("area_supervision", "")) # 区域运营 / 飞行督导 / 线上运营:从数据库提取,兜底用 store_info
_style_cell(row_num, 9, store_perf.get("flight_supervision", "")) _style_cell(row_num, 8, store_perf.get("area_supervision", "") or si.get("area_supervision", ""))
_style_cell(row_num, 10, store_perf.get("online_supervision", "")) _style_cell(row_num, 9, store_perf.get("flight_supervision", "") or si.get("flight_supervision", ""))
_style_cell(row_num, 10, store_perf.get("online_supervision", "") or si.get("online_supervision", ""))
# ========== 主机厂数据 (K-O) - 来自上传的Excel ========== # ========== 主机厂数据 (K-O) - 来自上传的Excel ==========
# 提前获取产值表交付数2,用于主机厂交付数为空时的兜底 # 提前获取产值表交付数2,用于主机厂交付数为空时的兜底
......
...@@ -132,7 +132,7 @@ def parse_wenjie_store_guarantee(file_path: str) -> List[Dict]: ...@@ -132,7 +132,7 @@ def parse_wenjie_store_guarantee(file_path: str) -> List[Dict]:
for _, r in df.iterrows(): for _, r in df.iterrows():
rows.append({ rows.append({
"store_name": str(r["门店名"]).strip(), "store_name": str(r["门店名"]).strip(),
"guarantee_target": _normalize_target(str(r.get("兜底指标", ""))), "guarantee_target": str(r.get("兜底指标", "")) if pd.notna(r.get("兜底指标")) else None,
"guarantee_type": str(r.get("兜底类型", "")) if pd.notna(r.get("兜底类型")) else None, "guarantee_type": str(r.get("兜底类型", "")) if pd.notna(r.get("兜底类型")) else None,
"special_case_1": str(r.get("特殊情况1", "")) if pd.notna(r.get("特殊情况1")) else None, "special_case_1": str(r.get("特殊情况1", "")) if pd.notna(r.get("特殊情况1")) else None,
"special_case_2": str(r.get("特殊情况2", "")) if pd.notna(r.get("特殊情况2")) else None, "special_case_2": str(r.get("特殊情况2", "")) if pd.notna(r.get("特殊情况2")) else None,
...@@ -212,7 +212,7 @@ def parse_harmony_store_guarantee(file_path: str) -> List[Dict]: ...@@ -212,7 +212,7 @@ def parse_harmony_store_guarantee(file_path: str) -> List[Dict]:
"area_supervision": str(r.get("区域运营", "")) if pd.notna(r.get("区域运营")) else None, "area_supervision": str(r.get("区域运营", "")) if pd.notna(r.get("区域运营")) else None,
"flight_supervision": str(r.get("飞行督导", "")) if pd.notna(r.get("飞行督导")) else None, "flight_supervision": str(r.get("飞行督导", "")) if pd.notna(r.get("飞行督导")) else None,
"online_supervision": str(r.get("线上运营", "")) if pd.notna(r.get("线上运营")) else None, "online_supervision": str(r.get("线上运营", "")) if pd.notna(r.get("线上运营")) else None,
"guarantee_target": _normalize_target(str(r.get("兜底指标", ""))), "guarantee_target": str(r.get("兜底指标", "")) if pd.notna(r.get("兜底指标")) else None,
"guarantee_type": str(r.get("兜底类型", "")) if pd.notna(r.get("兜底类型")) else None, "guarantee_type": str(r.get("兜底类型", "")) if pd.notna(r.get("兜底类型")) else None,
"special_case_2": str(r.get("特殊情况", "")) if pd.notna(r.get("特殊情况")) else None, "special_case_2": str(r.get("特殊情况", "")) if pd.notna(r.get("特殊情况")) else None,
"ew_conversion_rate": _parse_rate(r.get("需达标转化率_整车延保")), "ew_conversion_rate": _parse_rate(r.get("需达标转化率_整车延保")),
...@@ -352,22 +352,6 @@ def parse_harmony_oem_delivery(file_path: str) -> List[Dict]: ...@@ -352,22 +352,6 @@ def parse_harmony_oem_delivery(file_path: str) -> List[Dict]:
# 辅助函数 # 辅助函数
# ============================================================================ # ============================================================================
def _normalize_target(val: str) -> Optional[str]:
"""归一化兜底指标"""
if val is None:
return None
if isinstance(val, float) and np.isnan(val):
return None
val = str(val).strip()
if not val:
return None
if "触" in val or "touch" in val.lower():
return "touch_count"
if "交付" in val or "delivery" in val.lower():
return "delivery_count"
return val[:64]
def _parse_rate(val) -> Optional[float]: def _parse_rate(val) -> Optional[float]:
"""解析达标转化率,去除 % 符号""" """解析达标转化率,去除 % 符号"""
if val is None: if val is None:
......
...@@ -16,7 +16,7 @@ from openpyxl import load_workbook ...@@ -16,7 +16,7 @@ from openpyxl import load_workbook
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
from openpyxl.utils import get_column_letter from openpyxl.utils import get_column_letter
SRC = r'C:\Users\jiata\Desktop\问界生态系数生态(生态6.26-7.6&7.1-7.6).xlsx' SRC = r'C:\Users\jiata\Desktop\问界生态系数生态(生态6.26-7.16&7.1-7.16).xlsx'
# 自动递增三位序号: 门店数据-问界-模板001.xlsx, 002.xlsx, ... # 自动递增三位序号: 门店数据-问界-模板001.xlsx, 002.xlsx, ...
import os, glob import os, glob
...@@ -31,7 +31,12 @@ OUT = f'app/templates/xlsx/门店数据-问界-模板{max_no + 1:03d}.xlsx' ...@@ -31,7 +31,12 @@ OUT = f'app/templates/xlsx/门店数据-问界-模板{max_no + 1:03d}.xlsx'
# ── 1. 读取主表门店信息 ── # ── 1. 读取主表门店信息 ──
wb = load_workbook(SRC, data_only=True) wb = load_workbook(SRC, data_only=True)
main_ws = wb['问界生态系数(6.26-7.5)&(7.1-7.5)'] # 自动匹配主表sheet(名称以"问界生态系数"开头,排除"阶梯赔付明细子表"等辅助sheet)
main_sheet_names = [s for s in wb.sheetnames if s.startswith('问界生态系数') and '阶梯' not in s]
if not main_sheet_names:
raise SystemExit(f"未找到主表sheet,可用sheet: {wb.sheetnames}")
main_ws = wb[main_sheet_names[0]]
print(f'主表sheet: {main_sheet_names[0]}')
stores = [] # [(store_name, baodi, baodi_type, special1, special2), ...] stores = [] # [(store_name, baodi, baodi_type, special1, special2), ...]
for r in range(5, main_ws.max_row + 1): for r in range(5, main_ws.max_row + 1):
sn = main_ws.cell(row=r, column=1).value sn = main_ws.cell(row=r, column=1).value
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!