|
|
@@ -13,6 +13,8 @@ from utils.file import read_file
|
|
|
from utils.logu import get_logger
|
|
|
from openpyxl import load_workbook,Workbook
|
|
|
from .base_writer import ExcelWriterBase
|
|
|
+from src.models.product_model import Product,CompetitorAnalysis
|
|
|
+
|
|
|
|
|
|
logger = get_logger('excel')
|
|
|
|
|
|
@@ -82,24 +84,37 @@ class CompetitiveAnalysisWriter(ExcelWriterBase):
|
|
|
self.ws = self.wb.create_sheet(self.sheet_name, index=self.sheet_index)
|
|
|
logger.info(f"新建工作表: {self.sheet_name}")
|
|
|
|
|
|
- def add_data(self, data: List[Dict[str, Any]]):
|
|
|
- for product_data in data:
|
|
|
- logger.info(f"{product_data['asin']}, 处理中...")
|
|
|
- self.add_product(product_data['extra_result_data'], product_data['asin'])
|
|
|
+ def add_data(self, competitor_analysis: Dict[str, CompetitorAnalysis]):
|
|
|
+
|
|
|
+ for asin, analysis in competitor_analysis.items():
|
|
|
+ logger.info(f"正在处理竞品 {asin}...")
|
|
|
+ self.add_product(analysis)
|
|
|
|
|
|
self.apply_formatting()
|
|
|
|
|
|
- def add_product(self, data: dict, asin: str):
|
|
|
- """添加产品数据"""
|
|
|
+ def add_product(self, analysis: CompetitorAnalysis) -> None:
|
|
|
+ """添加竞品分析数据
|
|
|
+ Args:
|
|
|
+ analysis (CompetitorAnalysis): 竞品分析数据对象,必须包含extra_result和asin字段
|
|
|
+ """
|
|
|
try:
|
|
|
- # 加载并处理数据
|
|
|
- processor = ProductDataProcessor(data, asin)
|
|
|
+ # 参数校验
|
|
|
+ if not analysis.extra_result:
|
|
|
+ raise ValueError(f"竞品分析数据缺失: {analysis.asin}")
|
|
|
+ if not analysis.asin:
|
|
|
+ raise ValueError("竞品ASIN不能为空")
|
|
|
+
|
|
|
+ # 初始化数据处理
|
|
|
+ processor = ProductDataProcessor(
|
|
|
+ json_data=analysis.extra_result,
|
|
|
+ asin=analysis.asin
|
|
|
+ )
|
|
|
|
|
|
# 记录产品起始列
|
|
|
self.product_cols.append(self.current_col)
|
|
|
|
|
|
- # 写入主数据表
|
|
|
- self._write_main_table(processor, asin)
|
|
|
+ # 写入主数据表(使用竞品分析中的ASIN)
|
|
|
+ self._write_main_table(processor, analysis.asin)
|
|
|
|
|
|
# 写入附加信息
|
|
|
self._write_additional_info(processor)
|
|
|
@@ -110,8 +125,8 @@ class CompetitiveAnalysisWriter(ExcelWriterBase):
|
|
|
# 移动到下一组列
|
|
|
self.current_col += self.COLUMN_SPACING
|
|
|
|
|
|
- except (json.JSONDecodeError, ValueError) as e:
|
|
|
- logger.error(f'Error processing {data}: {e}')
|
|
|
+ except (ValueError, KeyError) as e:
|
|
|
+ logger.error(f'处理竞品 {analysis.asin} 数据失败: {e}')
|
|
|
|
|
|
|
|
|
def _write_main_table(self, processor: ProductDataProcessor, asin: str):
|