mysql_dao.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. from database import MySqlDatabaseHelper
  2. from sqlalchemy import text
  3. import pandas as pd
  4. class MySqlDao:
  5. _instance = None
  6. def __new__(cls):
  7. if not cls._instance:
  8. cls._instance = super(MySqlDao, cls).__new__(cls)
  9. cls._instance._initialized = False
  10. return cls._instance
  11. def __init__(self):
  12. if self._initialized:
  13. return
  14. self.db_helper = MySqlDatabaseHelper()
  15. self._product_tablename = "tads_brandcul_product_info_f"
  16. self._cust_tablename = "tads_brandcul_cust_info_f"
  17. self._order_tablename = "tads_brandcul_consumer_order"
  18. # self._order_tablename = "tads_brandcul_consumer_order"
  19. # self._eval_order_name = "tads_brandcul_consumer_order_check"
  20. self._mock_order_tablename = "yunfu_mock_data"
  21. self._shopping_tablename = "tads_brandcul_cust_info_lbs_f"
  22. # self._shopping_tablename = "yunfu_shopping_mock_data"
  23. self._initialized = True
  24. def load_product_data(self, city_uuid):
  25. """从数据库中读取商品信息"""
  26. query = f"SELECT * FROM {self._product_tablename} WHERE city_uuid = :city_uuid"
  27. params = {"city_uuid": city_uuid}
  28. data = self.db_helper.load_data_with_page(query, params)
  29. return data
  30. def load_cust_data(self, city_uuid):
  31. """从数据库中读取商户信息"""
  32. query = f"SELECT * FROM {self._cust_tablename} WHERE BA_CITY_ORG_CODE = :city_uuid"
  33. params = {"city_uuid": city_uuid}
  34. data = self.db_helper.load_data_with_page(query, params)
  35. return data
  36. def load_order_data(self, city_uuid):
  37. """从数据库中读取订单信息"""
  38. query = f"SELECT * FROM {self._order_tablename} WHERE city_uuid = :city_uuid"
  39. params = {"city_uuid": city_uuid}
  40. data = self.db_helper.load_data_with_page(query, params)
  41. # data.drop('stat_month', axis=1, inplace=True)
  42. data.drop('city_uuid', axis=1, inplace=True)
  43. return data
  44. def load_mock_order_data(self):
  45. """从数据库中读取mock的订单信息"""
  46. query = f"SELECT * FROM {self._mock_order_tablename}"
  47. data = self.db_helper.load_data_with_page(query, {})
  48. return data
  49. def load_shopping_data(self, city_uuid):
  50. """从数据库中读取商圈数据"""
  51. query = f"SELECT * FROM {self._shopping_tablename} WHERE city_uuid = :city_uuid"
  52. params = {"city_uuid": city_uuid}
  53. data = self.db_helper.load_data_with_page(query, params)
  54. return data
  55. def get_cust_list(self, city_uuid):
  56. """获取商户列表"""
  57. data = self.load_cust_data(city_uuid)
  58. cust_list = data["BB_RETAIL_CUSTOMER_CODE"].to_list()
  59. if len(cust_list) == 0:
  60. return []
  61. return cust_list
  62. def get_product_by_id(self, city_uuid, product_id):
  63. """根据city_uuid 和 product_id 从表中获取拼柜信息"""
  64. query = f"""
  65. SELECT *
  66. FROM {self._product_tablename}
  67. WHERE city_uuid = :city_uuid
  68. AND product_code = :product_id
  69. """
  70. params = {"city_uuid": city_uuid, "product_id": product_id}
  71. data = self.db_helper.load_data_with_page(query, params)
  72. return data
  73. def get_cust_by_ids(self, city_uuid, cust_id_list):
  74. """根据零售户列表查询其信息"""
  75. if not cust_id_list:
  76. return None
  77. cust_id_str = ",".join([f"'{cust_id}'" for cust_id in cust_id_list])
  78. query = f"""
  79. SELECT *
  80. FROM {self._cust_tablename}
  81. WHERE BA_CITY_ORG_CODE = :city_uuid
  82. AND BB_RETAIL_CUSTOMER_CODE IN ({cust_id_str})
  83. """
  84. params = {"city_uuid": city_uuid}
  85. data = self.db_helper.load_data_with_page(query, params)
  86. return data
  87. def get_shop_by_ids(self, city_uuid, cust_id_list):
  88. """根据零售户列表查询其信息"""
  89. if not cust_id_list:
  90. return None
  91. cust_id_str = ",".join([f"'{cust_id}'" for cust_id in cust_id_list])
  92. query = f"""
  93. SELECT *
  94. FROM {self._shopping_tablename}
  95. WHERE city_uuid = :city_uuid
  96. AND cust_code IN ({cust_id_str})
  97. """
  98. params = {"city_uuid": city_uuid}
  99. data = self.db_helper.load_data_with_page(query, params)
  100. return data
  101. def get_order_by_product(self, city_uuid, product_id):
  102. query = f"""
  103. SELECT *
  104. FROM {self._order_tablename}
  105. WHERE city_uuid = :city_uuid
  106. AND product_code = :product_id
  107. """
  108. params = {"city_uuid": city_uuid, "product_id": product_id}
  109. data = self.db_helper.load_data_with_page(query, params)
  110. return data
  111. def get_order_by_cust(self, city_uuid, cust_id):
  112. query = f"""
  113. SELECT *
  114. FROM {self._order_tablename}
  115. WHERE city_uuid = :city_uuid
  116. AND cust_code = :cust_id
  117. """
  118. params = {"city_uuid": city_uuid, "cust_id": cust_id}
  119. data = self.db_helper.load_data_with_page(query, params)
  120. return data
  121. def data_preprocess(self, data: pd.DataFrame):
  122. data.drop(["cust_uuid", "longitude", "latitude", "range_radius"], axis=1, inplace=True)
  123. remaining_cols = data.columns.drop(["city_uuid", "cust_code"])
  124. col_with_missing = remaining_cols[data[remaining_cols].isnull().any()].tolist() # 判断有缺失的字段
  125. col_all_missing = remaining_cols[data[remaining_cols].isnull().all()].to_list() # 全部缺失的字段
  126. col_partial_missing = list(set(col_with_missing) - set(col_all_missing)) # 部分缺失的字段
  127. for col in col_partial_missing:
  128. data[col] = data[col].fillna(data[col].mean())
  129. for col in col_all_missing:
  130. data[col] = data[col].fillna(0).infer_objects(copy=False)
  131. if __name__ == "__main__":
  132. dao = MySqlDao()
  133. city_uuid = "00000000000000000000000011445301"
  134. order_data = dao.load_order_data(city_uuid)
  135. order_data["sale_qty"] = order_data["sale_qty"].fillna(0)
  136. order_data = order_data.infer_objects(copy=False)
  137. # 将销售量进行分组求和
  138. order_data = order_data.groupby(["cust_code", "product_code"], as_index=False)["sale_qty"].sum()
  139. cust_data = dao.load_cust_data(city_uuid)
  140. cust_data = cust_data["BB_RETAIL_CUSTOMER_NAME"]
  141. sale_data = order_data.merge(cust_data, left_on='cust_code', right_on='BB_RETAIL_CUSTOMER_CODE', how="inner")
  142. sale_data.to_csv("./data/sale.csv", index=False)