Explorar o código

修复从销售表中读取卷烟溢出的问题

杨泽宇 hai 3 meses
pai
achega
a88b37012b
Modificáronse 2 ficheiros con 35 adicións e 23 borrados
  1. 4 4
      api_test.py
  2. 31 19
      database/dao/mysql_dao.py

+ 4 - 4
api_test.py

@@ -3,11 +3,11 @@ import json
 
 url = "http://127.0.0.1:7960/brandcultivation/api/v1/recommend"
 payload = {
-    "city_uuid": "00000000000000000000000011440801",
+    "city_uuid": "00000000000000000000000011440901",
     "product_code": "440308",
-    "recall_cust_count": 200,
-    "delivery_count": 200,
-    "cultivacation_id": "10000001",
+    "recall_cust_count": 80,
+    "delivery_count": 80,
+    "cultivacation_id": "10000003",
     "limit_cycle_name": "202603W4(03.21-03.29)"
 }
 headers = {'Content-Type': 'application/json'}

+ 31 - 19
database/dao/mysql_dao.py

@@ -147,32 +147,44 @@ class MySqlDao:
         if not product_id_list:
             return pd.DataFrame()
 
-        query = text(f"""
-        SELECT *
-        FROM {self._product_tablename}
-        WHERE city_uuid = :city_uuid
-        AND product_code IN :ids
-        ORDER BY product_code
-    """).bindparams(bindparam("ids", expanding=True))
-        params = {"city_uuid": city_uuid, "ids": list(product_id_list)}
-        data = pd.DataFrame(self.db_helper.fetch_all(query, params))
+        product_id_list = list(product_id_list)
+        batch_size = 2000
+        batches = [product_id_list[i:i + batch_size] for i in range(0, len(product_id_list), batch_size)]
+        results = []
+        for batch in batches:
+            query = text(f"""
+            SELECT *
+            FROM {self._product_tablename}
+            WHERE city_uuid = :city_uuid
+            AND product_code IN :ids
+            ORDER BY product_code
+        """).bindparams(bindparam("ids", expanding=True))
+            params = {"city_uuid": city_uuid, "ids": batch}
+            results.append(pd.DataFrame(self.db_helper.fetch_all(query, params)))
 
-        return data
+        return pd.concat(results, ignore_index=True) if results else pd.DataFrame()
     
     def get_order_by_product_ids(self, city_uuid, product_ids):
         """获取指定香烟列表的所有售卖记录"""
         if not product_ids:
             return pd.DataFrame()
 
-        query = text(f"""
-        SELECT *
-        FROM {self._order_tablename}
-        WHERE city_uuid = :city_uuid
-        AND product_code IN :ids
-        ORDER BY cust_code, product_code
-    """).bindparams(bindparam("ids", expanding=True))
-        params = {"city_uuid": city_uuid, "ids": list(product_ids)}
-        data = pd.DataFrame(self.db_helper.fetch_all(query, params))
+        product_ids = list(product_ids)
+        batch_size = 2000
+        batches = [product_ids[i:i + batch_size] for i in range(0, len(product_ids), batch_size)]
+        results = []
+        for batch in batches:
+            query = text(f"""
+            SELECT *
+            FROM {self._order_tablename}
+            WHERE city_uuid = :city_uuid
+            AND product_code IN :ids
+            ORDER BY cust_code, product_code
+        """).bindparams(bindparam("ids", expanding=True))
+            params = {"city_uuid": city_uuid, "ids": batch}
+            results.append(pd.DataFrame(self.db_helper.fetch_all(query, params)))
+
+        data = pd.concat(results, ignore_index=True) if results else pd.DataFrame()
 
         cust_list = self.get_cust_list(city_uuid)
         cust_index = cust_list.set_index("cust_code")