瀏覽代碼

增加读取投放销售数据功能,用于生成验证报告

yangzeyu 10 月之前
父節點
當前提交
eb426087dd
共有 5 個文件被更改,包括 75 次插入13 次删除
  1. 2 2
      api_test.py
  2. 28 6
      database/dao/mysql_dao.py
  3. 3 2
      models/rank/data/__init__.py
  4. 17 0
      models/rank/data/config.py
  5. 25 3
      utils/report_utils.py

+ 2 - 2
api_test.py

@@ -5,8 +5,8 @@ url = "http://127.0.0.1:7960/brandcultivation/api/v1/recommend"
 payload = {
     "city_uuid": "00000000000000000000000011445301",
     "product_code": "440298",
-    "recall_cust_count": 500,
-    "delivery_count": 5000
+    "recall_cust_count": 100,
+    "delivery_count": 200
 }
 headers = {'Content-Type': 'application/json'}
 

+ 28 - 6
database/dao/mysql_dao.py

@@ -20,7 +20,7 @@ class MySqlDao:
         self._product_tablename = "tads_brandcul_product_info_f"
         self._cust_tablename = "tads_brandcul_cust_info_f"
         self._order_tablename = "tads_brandcul_consumer_order"
-        self._eval_order_name = "tads_brandcul_consumer_order_check"
+        self._eval_order_name = "tads_brandcul_consumer_order_check_week"
         self._mock_order_tablename = "yunfu_mock_data"
         self._shopping_tablename = "tads_brandcul_cust_info_lbs_f"
         # self._shopping_tablename = "yunfu_shopping_mock_data"
@@ -57,14 +57,17 @@ class MySqlDao:
         data = data.join(cust_index, on="cust_code", how="inner")
         return data
     
-    def load_eval_order_data(self, city_uuid):
+    def load_delivery_order_data(self, city_uuid, start_time, end_time):
         """从数据库中读取订单信息"""
-        query = f"SELECT * FROM {self._eval_order_name} WHERE city_uuid = :city_uuid"
-        params = {"city_uuid": city_uuid}
+        query = f"SELECT * FROM {self._eval_order_name} WHERE city_uuid = :city_uuid AND cycle_begin_date = :start_time AND cycle_end_date = :end_time"
+        params = {
+            "city_uuid": city_uuid,
+            "start_time": start_time,
+            "end_time": end_time
+        }
         
         data = self.db_helper.load_data_with_page(query, params)
-        data.drop('stat_month', axis=1, inplace=True)
-        data.drop('city_uuid', axis=1, inplace=True)
+        
         
         return data
     
@@ -199,6 +202,25 @@ class MySqlDao:
         
         return data
     
+    def get_delivery_data_by_product(self, city_uuid, product_id, start_time, end_time):
+        query = f"""
+            SELECT *
+            FROM {self._eval_order_name}
+            WHERE city_uuid = :city_uuid
+            AND goods_code = :product_id
+            AND cycle_begin_date = :start_time
+            AND cycle_end_date = :end_time
+        """
+        params = {
+            "city_uuid": city_uuid,
+            "product_id": product_id,
+            "start_time": start_time,
+            "end_time": end_time
+        }
+        data = self.db_helper.load_data_with_page(query, params)
+        
+        return data
+    
     def get_order_by_cust(self, city_uuid, cust_id):
         query = f"""
             SELECT *

+ 3 - 2
models/rank/data/__init__.py

@@ -1,4 +1,4 @@
-from models.rank.data.config import CustConfig, ProductConfig, OrderConfig, ShopConfig, ImportanceFeaturesMap
+from models.rank.data.config import CustConfig, ProductConfig, OrderConfig, ShopConfig, ImportanceFeaturesMap, DeliveryConfig
 from models.rank.data.dataloader import DataLoader
 from models.rank.data.utils import one_hot_embedding, sample_data_clear
 __all__ = [
@@ -9,5 +9,6 @@ __all__ = [
     "DataLoader",
     "one_hot_embedding",
     "sample_data_clear",
-    "ImportanceFeaturesMap"
+    "ImportanceFeaturesMap",
+    "DeliveryConfig"
 ]

+ 17 - 0
models/rank/data/config.py

@@ -827,4 +827,21 @@ class ImportanceFeaturesMap:
         "f_workday_age_over_60":               "工作日流动_年龄_61以上",
         "f_workday_sex_woman":                 "工作日流动_性别_女",
         "f_workday_sex_man":                   "工作日流动_性别_男",
+    }
+    
+class DeliveryConfig:
+    FEATURE_COLUMNS = [
+        "customer_code",            # 零售户代码
+        "goods_code",               # 卷烟代码
+        "retail_index_week",        # 周市场零售价格监测指数
+        "turnover_rate_collpoint",  # 采集点销售量动销率(周)
+        "turnover_rate_terminal",   # 零售终端销售量动销率(周)
+        "sale_qty",                 # 周销售量
+    ]
+    
+    CLEANING_RULES = {
+        "retail_index_week":                    {"method": "fillna", "opt": "fill", "value": 0.0000, "type": "num"},
+        "turnover_rate_collpoint":              {"method": "fillna", "opt": "fill", "value": 0.0000, "type": "num"},
+        "turnover_rate_terminal":               {"method": "fillna", "opt": "fill", "value": 0.0000, "type": "num"},
+        "sale_qty":                             {"method": "fillna", "opt": "fill", "value": 0, "type": "num"},
     }

+ 25 - 3
utils/report_utils.py

@@ -1,6 +1,6 @@
 from database.dao.mysql_dao import MySqlDao
 from models import Recommend
-from models.rank.data.config import CustConfig, ImportanceFeaturesMap, ProductConfig, ShopConfig
+from models.rank.data.config import CustConfig, ImportanceFeaturesMap, ProductConfig, ShopConfig, DeliveryConfig
 from models.rank.data.utils import sample_data_clear
 from models.rank import generate_feats_map
 
@@ -114,7 +114,7 @@ class ReportUtils:
         )
         product_similarity_map.to_excel(os.path.join(self._save_dir, "相似卷烟表.xlsx"), index=False)
         
-    def generate_eval_data(self):
+    def generate_eval_data_pre(self):
         if self._product_id == '350139':
             eval_product_id = "350355"
         else:
@@ -127,9 +127,31 @@ class ReportUtils:
         
         report.to_excel(os.path.join(self._save_dir, "效果验证表.xlsx"), index=False)
         
+    def generate_eval_data(self, start_time, end_time):
+        """根据推荐列表生成验证报告"""
+        if self._product_id == '350139':
+            eval_product_id = "350355"
+        else:
+            eval_product_id = self._product_id
+        delivery_data = self._dao.get_delivery_data_by_product(self._city_uuid, eval_product_id, start_time, end_time)
+        delivery_data = delivery_data[DeliveryConfig.FEATURE_COLUMNS]
+        delivery_data = sample_data_clear(delivery_data, DeliveryConfig)
+        
+        delivery_data.to_excel('./data/delivery.xlsx', index=False)
+    
     def generate_all_data(self, recall_count, delivery_count):
         self.generate_feats_ralation_report(recall_count)
         self.generate_product_report()
         self.generate_recommend_report(recall_count, delivery_count)
         self.generate_similarity_product_report()
-        # self.generate_eval_data()
+        # self.generate_eval_data()
+        
+if __name__ == "__main__":
+    city_uuid = "00000000000000000000000011445301"
+    product_id = '440298'
+    start_time = '2025/2/10'
+    end_time = '2025/2/16'
+    report = ReportUtils(city_uuid, product_id)
+    
+    report.generate_eval_data(start_time, end_time)
+