mysql_dao.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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._eval_order_name = "tads_brandcul_consumer_order_check"
  19. self._mock_order_tablename = "yunfu_mock_data"
  20. self._shopping_tablename = "tads_brandcul_cust_info_lbs_f"
  21. # self._shopping_tablename = "yunfu_shopping_mock_data"
  22. self._initialized = True
  23. def load_product_data(self, city_uuid):
  24. """从数据库中读取商品信息"""
  25. query = f"SELECT * FROM {self._product_tablename} WHERE city_uuid = :city_uuid"
  26. params = {"city_uuid": city_uuid}
  27. data = self.db_helper.load_data_with_page(query, params)
  28. return data
  29. def load_cust_data(self, city_uuid):
  30. """从数据库中读取商户信息"""
  31. query = f"SELECT * FROM {self._cust_tablename} WHERE BA_CITY_ORG_CODE = :city_uuid"
  32. params = {"city_uuid": city_uuid}
  33. data = self.db_helper.load_data_with_page(query, params)
  34. return data
  35. def load_order_data(self, city_uuid):
  36. """从数据库中读取订单信息"""
  37. query = f"SELECT * FROM {self._order_tablename} WHERE city_uuid = :city_uuid"
  38. params = {"city_uuid": city_uuid}
  39. data = self.db_helper.load_data_with_page(query, params)
  40. data.drop('stat_month', axis=1, inplace=True)
  41. data.drop('city_uuid', axis=1, inplace=True)
  42. return data
  43. def load_eval_order_data(self, city_uuid):
  44. """从数据库中读取订单信息"""
  45. query = f"SELECT * FROM {self._eval_order_name} WHERE city_uuid = :city_uuid"
  46. params = {"city_uuid": city_uuid}
  47. data = self.db_helper.load_data_with_page(query, params)
  48. data.drop('stat_month', axis=1, inplace=True)
  49. data.drop('city_uuid', axis=1, inplace=True)
  50. return data
  51. def load_mock_order_data(self):
  52. """从数据库中读取mock的订单信息"""
  53. query = f"SELECT * FROM {self._mock_order_tablename}"
  54. data = self.db_helper.load_data_with_page(query, {})
  55. return data
  56. def load_shopping_data(self, city_uuid):
  57. """从数据库中读取商圈数据"""
  58. query = f"SELECT * FROM {self._shopping_tablename} WHERE city_uuid = :city_uuid"
  59. params = {"city_uuid": city_uuid}
  60. data = self.db_helper.load_data_with_page(query, params)
  61. return data
  62. def get_cust_list(self, city_uuid):
  63. """获取商户列表"""
  64. data = self.load_cust_data(city_uuid)
  65. cust_list = data["BB_RETAIL_CUSTOMER_CODE"].to_list()
  66. if len(cust_list) == 0:
  67. return []
  68. return cust_list
  69. def get_product_by_id(self, city_uuid, product_id):
  70. """根据city_uuid 和 product_id 从表中获取拼柜信息"""
  71. query = f"""
  72. SELECT *
  73. FROM {self._product_tablename}
  74. WHERE city_uuid = :city_uuid
  75. AND product_code = :product_id
  76. """
  77. params = {"city_uuid": city_uuid, "product_id": product_id}
  78. data = self.db_helper.load_data_with_page(query, params)
  79. return data
  80. def get_cust_by_ids(self, city_uuid, cust_id_list):
  81. """根据零售户列表查询其信息"""
  82. if not cust_id_list:
  83. return None
  84. cust_id_str = ",".join([f"'{cust_id}'" for cust_id in cust_id_list])
  85. query = f"""
  86. SELECT *
  87. FROM {self._cust_tablename}
  88. WHERE BA_CITY_ORG_CODE = :city_uuid
  89. AND BB_RETAIL_CUSTOMER_CODE IN ({cust_id_str})
  90. """
  91. params = {"city_uuid": city_uuid}
  92. data = self.db_helper.load_data_with_page(query, params)
  93. return data
  94. def get_shop_by_ids(self, city_uuid, cust_id_list):
  95. """根据零售户列表查询其信息"""
  96. if not cust_id_list:
  97. return None
  98. cust_id_str = ",".join([f"'{cust_id}'" for cust_id in cust_id_list])
  99. query = f"""
  100. SELECT *
  101. FROM {self._shopping_tablename}
  102. WHERE city_uuid = :city_uuid
  103. AND cust_code IN ({cust_id_str})
  104. """
  105. params = {"city_uuid": city_uuid}
  106. data = self.db_helper.load_data_with_page(query, params)
  107. return data
  108. def get_product_by_ids(self, city_uuid, product_id_list):
  109. """根据product_code列表查询其信息"""
  110. if not product_id_list:
  111. return None
  112. product_id_str = ",".join([f"'{product_id}'" for product_id in product_id_list])
  113. query = f"""
  114. SELECT *
  115. FROM {self._product_tablename}
  116. WHERE city_uuid = :city_uuid
  117. AND product_code IN ({product_id_str})
  118. """
  119. params = {"city_uuid": city_uuid}
  120. data = self.db_helper.load_data_with_page(query, params)
  121. return data
  122. def get_order_by_product_ids(self, city_uuid, product_ids):
  123. """获取指定香烟列表的所有售卖记录"""
  124. if not product_ids:
  125. return None
  126. product_ids_str = ",".join([f"'{product_code}'" for product_code in product_ids])
  127. query = f"""
  128. SELECT *
  129. FROM {self._order_tablename}
  130. WHERE city_uuid = :city_uuid
  131. AND product_code IN ({product_ids_str})
  132. """
  133. params = {"city_uuid": city_uuid}
  134. data = self.db_helper.load_data_with_page(query, params)
  135. return data
  136. def get_order_by_product(self, city_uuid, product_id):
  137. query = f"""
  138. SELECT *
  139. FROM {self._order_tablename}
  140. WHERE city_uuid = :city_uuid
  141. AND product_code = :product_id
  142. """
  143. params = {"city_uuid": city_uuid, "product_id": product_id}
  144. data = self.db_helper.load_data_with_page(query, params)
  145. return data
  146. def get_eval_order_by_product(self, city_uuid, product_id):
  147. query = f"""
  148. SELECT *
  149. FROM {self._eval_order_name}
  150. WHERE city_uuid = :city_uuid
  151. AND product_code = :product_id
  152. """
  153. params = {"city_uuid": city_uuid, "product_id": product_id}
  154. data = self.db_helper.load_data_with_page(query, params)
  155. return data
  156. def get_order_by_cust(self, city_uuid, cust_id):
  157. query = f"""
  158. SELECT *
  159. FROM {self._order_tablename}
  160. WHERE city_uuid = :city_uuid
  161. AND cust_code = :cust_id
  162. """
  163. params = {"city_uuid": city_uuid, "cust_id": cust_id}
  164. data = self.db_helper.load_data_with_page(query, params)
  165. return data
  166. def get_order_by_cust_and_product(self, city_uuid, cust_id, product_id):
  167. query = f"""
  168. SELECT *
  169. FROM {self._order_tablename}
  170. WHERE city_uuid = :city_uuid
  171. AND cust_code = :cust_id
  172. AND product_code =:product_id
  173. """
  174. params = {"city_uuid": city_uuid, "cust_id": cust_id, "product_id": product_id}
  175. data = self.db_helper.load_data_with_page(query, params)
  176. return data
  177. def get_product_from_order(self, city_uuid):
  178. query = f"SELECT DISTINCT product_code FROM {self._order_tablename} WHERE city_uuid = :city_uuid"
  179. params = {"city_uuid": city_uuid}
  180. data = pd.DataFrame(self.db_helper.fetch_all(text(query), params))
  181. return data
  182. def data_preprocess(self, data: pd.DataFrame):
  183. data.drop(["cust_uuid", "longitude", "latitude", "range_radius"], axis=1, inplace=True)
  184. remaining_cols = data.columns.drop(["city_uuid", "cust_code"])
  185. col_with_missing = remaining_cols[data[remaining_cols].isnull().any()].tolist() # 判断有缺失的字段
  186. col_all_missing = remaining_cols[data[remaining_cols].isnull().all()].to_list() # 全部缺失的字段
  187. col_partial_missing = list(set(col_with_missing) - set(col_all_missing)) # 部分缺失的字段
  188. for col in col_partial_missing:
  189. data[col] = data[col].fillna(data[col].mean())
  190. for col in col_all_missing:
  191. data[col] = data[col].fillna(0).infer_objects(copy=False)
  192. if __name__ == "__main__":
  193. dao = MySqlDao()
  194. city_uuid = "00000000000000000000000011445301"
  195. order_data = dao.load_order_data(city_uuid)
  196. order_data["sale_qty"] = order_data["sale_qty"].fillna(0)
  197. order_data = order_data.infer_objects(copy=False)
  198. # 将销售量进行分组求和
  199. order_data = order_data.groupby(["cust_code", "product_code"], as_index=False)["sale_qty"].sum()
  200. cust_data = dao.load_cust_data(city_uuid)
  201. cust_data = cust_data["BB_RETAIL_CUSTOMER_NAME"]
  202. sale_data = order_data.merge(cust_data, left_on='cust_code', right_on='BB_RETAIL_CUSTOMER_CODE', how="inner")
  203. sale_data.to_csv("./data/sale.csv", index=False)