Selaa lähdekoodia

添加读取商户信息和卷烟信息数据功能

Sherlock 1 vuosi sitten
vanhempi
commit
2e130cf424
3 muutettua tiedostoa jossa 39 lisäystä ja 9 poistoa
  1. 4 2
      dao/__init__.py
  2. 31 3
      dao/dao.py
  3. 4 4
      dao/mysql_client.py

+ 4 - 2
dao/__init__.py

@@ -1,9 +1,11 @@
 #!/usr/bin/env python3
 # -*- coding:utf-8 -*-
 from dao.mysql_client import Mysql
-from dao.dao import load_order_data_from_mysql
+from dao.dao import load_order_data_from_mysql, load_cust_data_from_mysql, load_product_data_from_mysql
 
 __all__ = [
     "Mysql",
-    "load_order_data_from_mysql"
+    "load_order_data_from_mysql",
+    "load_cust_data_from_mysql",
+    "load_product_data_from_mysql"
 ]

+ 31 - 3
dao/dao.py

@@ -1,12 +1,12 @@
 from dao import Mysql
 
 def load_order_data_from_mysql(city_uuid):
-    """从数据库中读取数据"""
+    """从数据库中读取订单数据"""
     client = Mysql()
     tablename = "tads_brandcul_cust_order"
     query_text = "*"
     
-    df = client.load_data(tablename, query_text, city_uuid)
+    df = client.load_data(tablename, query_text, "city_uuid", city_uuid)
     if len(df) == 0:
         return None
     
@@ -17,4 +17,32 @@ def load_order_data_from_mysql(city_uuid):
      # 去除重复值和填补缺失值
     df.drop_duplicates(inplace=True)
     df.fillna(0, inplace=True)
-    return df
+    return df
+
+def load_cust_data_from_mysql(city_uuid):
+    """从数据库中读取商户信息数据"""
+    client = Mysql()
+    tablename = "tads_brandcul_cust_info"
+    query_text = "*"
+    
+    df = client.load_data(tablename, query_text, "BA_CITY_ORG_CODE", city_uuid)
+    if len(df) == 0:
+        return None
+    
+    return df
+
+def load_product_data_from_mysql(city_uuid):
+    """从数据库中读取商品信息"""
+    client = Mysql()
+    tablename = "tads_brandcul_product_info"
+    query_text = "*"
+    
+    df = client.load_data(tablename, query_text, "city_uuid", city_uuid)
+    if len(df) == 0:
+        return None
+    
+    return df
+
+if __name__ == '__main__':
+    data = load_product_data_from_mysql("00000000000000000000000011445301")
+    print(data)

+ 4 - 4
dao/mysql_client.py

@@ -39,10 +39,10 @@ class Mysql(object):
         """创建返回一个新的数据库session"""
         return self._DBSession()
     
-    def fetch_data_with_pagination(self, tablename, query_text, city_uuid, page=1, page_size=1000):
+    def fetch_data_with_pagination(self, tablename, query_text, field_name, city_uuid, page=1, page_size=1000):
         """分页查询数据,并根据 city_uuid 进行过滤"""
         offset = (page - 1) * page_size  # 计算偏移量
-        query = text(f"SELECT {query_text} FROM {tablename} WHERE city_uuid = :city_uuid LIMIT :limit OFFSET :offset")
+        query = text(f"SELECT {query_text} FROM {tablename} WHERE {field_name} = :city_uuid LIMIT :limit OFFSET :offset")
     
         with self.create_session() as session:
             results = session.execute(query, {"city_uuid": city_uuid, "limit": page_size, "offset": offset}).fetchall()
@@ -50,13 +50,13 @@ class Mysql(object):
     
         return df
     
-    def load_data(self, tablename, query_text, city_uuid, page=1, page_size=1000):
+    def load_data(self, tablename, query_text, field_name, city_uuid, page=1, page_size=1000):
         # 创建一个空的DataFrame用于存储所有数据
         total_df = pd.DataFrame()
     
         try:
             while True:
-                df = self.fetch_data_with_pagination(tablename, query_text, city_uuid, page, page_size)
+                df = self.fetch_data_with_pagination(tablename, query_text, field_name, city_uuid, page, page_size)
                 if df.empty:
                     break