Commit aec18105 by 江名洋

feat(analysis): Enhance factory data upload and processing

- Introduced new functions for formatting factory sheet names and determining ecology periods.
- Updated the OEM factory data loading function to return structured data with metadata.
- Improved the analysis upload process to support separate uploads for ecology and nature factory data.
- Enhanced the front-end to allow for multi-sheet uploads and improved user interface for sheet previews.
- Refactored date parsing logic to accommodate new date formats.
1 parent b45c55be
...@@ -407,13 +407,27 @@ class AnalysisService: ...@@ -407,13 +407,27 @@ class AnalysisService:
start = f"{current_year}-{m1:02d}-{d1:02d}" start = f"{current_year}-{m1:02d}-{d1:02d}"
end = f"{current_year}-{m2:02d}-{d2:02d}" end = f"{current_year}-{m2:02d}-{d2:02d}"
return start, end return start, end
# 格式2: MDD-MDD (如 626-722,月份1位+日期2位) # 格式2: MDD-MDD / MMDD-MMDD (如 626-722、0701-0723)
m = re.match(r'(\d{1,2})(\d{1,2})\s*[-~]\s*(\d{1,2})(\d{1,2})', s) m = re.match(r'(\d{3,4})\s*[-~]\s*(\d{3,4})', s)
if m: if m:
m1, d1, m2, d2 = int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4)) def _parse_mdd_token(token: str):
start = f"{current_year}-{m1:02d}-{d1:02d}" token = token.strip()
end = f"{current_year}-{m2:02d}-{d2:02d}" if len(token) == 3:
return start, end return int(token[0]), int(token[1:3])
if len(token) == 4:
month = int(token[:2])
day = int(token[2:4])
if 1 <= month <= 12:
return month, day
return int(token[0]), int(token[1:4])
return None, None
m1, d1 = _parse_mdd_token(m.group(1))
m2, d2 = _parse_mdd_token(m.group(2))
if m1 and d1 and m2 and d2:
start = f"{current_year}-{m1:02d}-{d1:02d}"
end = f"{current_year}-{m2:02d}-{d2:02d}"
return start, end
logger.warning(f"Unable to parse date range: {date_range_str}, using yesterday") logger.warning(f"Unable to parse date range: {date_range_str}, using yesterday")
yesterday = (date.today() - timedelta(days=1)).strftime('%Y-%m-%d') yesterday = (date.today() - timedelta(days=1)).strftime('%Y-%m-%d')
return yesterday, yesterday return yesterday, yesterday
...@@ -966,10 +980,8 @@ class AnalysisService: ...@@ -966,10 +980,8 @@ class AnalysisService:
nat_sheets[sn] = df nat_sheets[sn] = df
if not nature_start: if not nature_start:
nature_start, nature_end = AnalysisService._parse_date_range(sn) nature_start, nature_end = AnalysisService._parse_date_range(sn)
if eco_sheets: ecology_factory = eco_sheets
ecology_factory = eco_sheets nature_factory = nat_sheets
if nat_sheets:
nature_factory = nat_sheets
logger.info(f"Auto-split factory sheets: ecology={list(eco_sheets.keys())}, " logger.info(f"Auto-split factory sheets: ecology={list(eco_sheets.keys())}, "
f"nature={list(nat_sheets.keys())}") f"nature={list(nat_sheets.keys())}")
...@@ -1032,48 +1044,54 @@ class AnalysisService: ...@@ -1032,48 +1044,54 @@ class AnalysisService:
cell.fill = SEPARATOR_FILL cell.fill = SEPARATOR_FILL
cell.border = THIN_BORDER cell.border = THIN_BORDER
cell.alignment = Alignment(horizontal="center", vertical="center") cell.alignment = Alignment(horizontal="center", vertical="center")
# 标签文字放在中间列(约29列),视觉上居中
ws.cell(row=row, column=29).value = label_text ws.cell(row=row, column=29).value = label_text
ws.cell(row=row, column=27).font = SEPARATOR_FONT ws.cell(row=row, column=27).font = SEPARATOR_FONT
ws.row_dimensions[row].height = 24 ws.row_dimensions[row].height = 24
next_row = 4 def _write_section(ws, next_row, label_text, store_subset, factory_sheets,
start_date, end_date, track_first_data=False):
"""标签行 + 数据行(每种数据上方仅一行蓝色标签)"""
nonlocal first_data_row
_write_separator(ws, next_row, label_text)
next_row += 1
if track_first_data and first_data_row is None:
first_data_row = next_row
next_row = AnalysisService._process_one_sheet(
ws, store_subset, factory_sheets,
start_date, end_date, formula_map, skip_external_cols, start_row=next_row)
return next_row
next_row = 5
first_data_row = None first_data_row = None
# 生态数据标签行 eco_label_text = (
eco_label_text = f"▲ 生态数据({ecology_start} ~ {ecology_end})" if ecology_start and ecology_end else "▲ 生态数据" f"▲ 生态月数据({ecology_start} ~ {ecology_end})"
if has_ecology: if ecology_start and ecology_end else "▲ 生态月数据"
_write_separator(ws, next_row, eco_label_text) )
next_row += 1 nat_label_text = (
f"▼ 自然月数据({nature_start} ~ {nature_end})"
if nature_start and nature_end else "▼ 自然月数据"
)
# 处理生态数据(上部) # 生态月数据(上部)
if has_ecology: if has_ecology:
if first_data_row is None: next_row = _write_section(
first_data_row = next_row ws, next_row, eco_label_text,
next_row = AnalysisService._process_one_sheet( ecology_stores, ecology_factory,
ws, ecology_stores, ecology_factory, ecology_start, ecology_end, track_first_data=True,
ecology_start, ecology_end, formula_map, skip_external_cols, start_row=next_row) )
else: else:
reason = "no stores" if len(ecology_stores) == 0 else "no factory data" reason = "no stores" if len(ecology_stores) == 0 else "no factory data"
logger.info(f"Skipping ecology section ({reason})") logger.info(f"Skipping ecology section ({reason})")
# 自然月数据标签行(中间隔离行) # 自然月数据(下部,与生态月拼在同一表)
nat_label_text = f"▼ 自然月数据({nature_start} ~ {nature_end})" if nature_start and nature_end else "▼ 自然月数据"
if has_ecology and has_nature:
_write_separator(ws, next_row, nat_label_text)
next_row += 1
# 处理自然月数据(下部)
if has_nature: if has_nature:
# 如果仅自然月(无生态数据),在顶部也加标签 next_row = _write_section(
if not has_ecology: ws, next_row, nat_label_text,
_write_separator(ws, next_row, nat_label_text) nature_stores, nature_factory,
next_row += 1 nature_start, nature_end,
if first_data_row is None: track_first_data=(first_data_row is None),
first_data_row = next_row )
next_row = AnalysisService._process_one_sheet(
ws, nature_stores, nature_factory,
nature_start, nature_end, formula_map, skip_external_cols, start_row=next_row)
else: else:
reason = "no stores" if len(nature_stores) == 0 else "no factory data" reason = "no stores" if len(nature_stores) == 0 else "no factory data"
logger.info(f"Skipping nature section ({reason})") logger.info(f"Skipping nature section ({reason})")
...@@ -1086,6 +1104,9 @@ class AnalysisService: ...@@ -1086,6 +1104,9 @@ class AnalysisService:
last_col_letter = get_column_letter(len(OUTPUT_HEADERS)) last_col_letter = get_column_letter(len(OUTPUT_HEADERS))
ws.auto_filter.ref = f"A3:{last_col_letter}{next_row - 1}" ws.auto_filter.ref = f"A3:{last_col_letter}{next_row - 1}"
# 冻结窗格:D5(固定前3行表头 + A~C列)
ws.freeze_panes = "D5"
wb.save(output_path) wb.save(output_path)
logger.info(f"Report generated: {output_path}") logger.info(f"Report generated: {output_path}")
return output_path return output_path
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!