mysql_dao.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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_week"
  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. cust_list = self.get_cust_list(city_uuid)
  43. cust_index = cust_list.set_index("BB_RETAIL_CUSTOMER_CODE")
  44. data = data.join(cust_index, on="cust_code", how="inner")
  45. return data
  46. def load_delivery_order_data(self, city_uuid, start_time, end_time):
  47. """从数据库中读取订单信息"""
  48. 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"
  49. params = {
  50. "city_uuid": city_uuid,
  51. "start_time": start_time,
  52. "end_time": end_time
  53. }
  54. data = self.db_helper.load_data_with_page(query, params)
  55. return data
  56. def load_mock_order_data(self):
  57. """从数据库中读取mock的订单信息"""
  58. query = f"SELECT * FROM {self._mock_order_tablename}"
  59. data = self.db_helper.load_data_with_page(query, {})
  60. return data
  61. def load_shopping_data(self, city_uuid):
  62. """从数据库中读取商圈数据"""
  63. query = f"SELECT * FROM {self._shopping_tablename} WHERE city_uuid = :city_uuid"
  64. params = {"city_uuid": city_uuid}
  65. data = self.db_helper.load_data_with_page(query, params)
  66. return data
  67. def get_product_by_id(self, city_uuid, product_id):
  68. """根据city_uuid 和 product_id 从表中获取拼柜信息"""
  69. query = f"""
  70. SELECT *
  71. FROM {self._product_tablename}
  72. WHERE city_uuid = :city_uuid
  73. AND product_code = :product_id
  74. """
  75. params = {"city_uuid": city_uuid, "product_id": product_id}
  76. data = self.db_helper.load_data_with_page(query, params)
  77. return data
  78. def get_cust_by_ids(self, city_uuid, cust_id_list):
  79. """根据零售户列表查询其信息"""
  80. if not cust_id_list:
  81. return None
  82. cust_id_str = ",".join([f"'{cust_id}'" for cust_id in cust_id_list])
  83. query = f"""
  84. SELECT *
  85. FROM {self._cust_tablename}
  86. WHERE BA_CITY_ORG_CODE = :city_uuid
  87. AND BB_RETAIL_CUSTOMER_CODE IN ({cust_id_str})
  88. """
  89. params = {"city_uuid": city_uuid}
  90. data = self.db_helper.load_data_with_page(query, params)
  91. return data
  92. def get_shop_by_ids(self, city_uuid, cust_id_list):
  93. """根据零售户列表查询其信息"""
  94. if not cust_id_list:
  95. return None
  96. cust_id_str = ",".join([f"'{cust_id}'" for cust_id in cust_id_list])
  97. query = f"""
  98. SELECT *
  99. FROM {self._shopping_tablename}
  100. WHERE city_uuid = :city_uuid
  101. AND cust_code IN ({cust_id_str})
  102. """
  103. params = {"city_uuid": city_uuid}
  104. data = self.db_helper.load_data_with_page(query, params)
  105. return data
  106. def get_product_by_ids(self, city_uuid, product_id_list):
  107. """根据product_code列表查询其信息"""
  108. if not product_id_list:
  109. return None
  110. product_id_str = ",".join([f"'{product_id}'" for product_id in product_id_list])
  111. query = f"""
  112. SELECT *
  113. FROM {self._product_tablename}
  114. WHERE city_uuid = :city_uuid
  115. AND product_code IN ({product_id_str})
  116. """
  117. params = {"city_uuid": city_uuid}
  118. data = self.db_helper.load_data_with_page(query, params)
  119. return data
  120. def get_order_by_product_ids(self, city_uuid, product_ids):
  121. """获取指定香烟列表的所有售卖记录"""
  122. if not product_ids:
  123. return None
  124. product_ids_str = ",".join([f"'{product_code}'" for product_code in product_ids])
  125. query = f"""
  126. SELECT *
  127. FROM {self._order_tablename}
  128. WHERE city_uuid = :city_uuid
  129. AND product_code IN ({product_ids_str})
  130. """
  131. params = {"city_uuid": city_uuid}
  132. data = self.db_helper.load_data_with_page(query, params)
  133. cust_list = self.get_cust_list(city_uuid)
  134. cust_index = cust_list.set_index("BB_RETAIL_CUSTOMER_CODE")
  135. data = data.join(cust_index, on="cust_code", how="inner")
  136. return data
  137. def get_order_by_product(self, city_uuid, product_id):
  138. query = f"""
  139. SELECT *
  140. FROM {self._order_tablename}
  141. WHERE city_uuid = :city_uuid
  142. AND product_code = :product_id
  143. """
  144. params = {"city_uuid": city_uuid, "product_id": product_id}
  145. data = self.db_helper.load_data_with_page(query, params)
  146. cust_list = self.get_cust_list(city_uuid)
  147. cust_index = cust_list.set_index("BB_RETAIL_CUSTOMER_CODE")
  148. data = data.join(cust_index, on="cust_code", how="inner")
  149. return data
  150. def get_eval_order_by_product(self, city_uuid, product_id):
  151. query = f"""
  152. SELECT *
  153. FROM {self._eval_order_name}
  154. WHERE city_uuid = :city_uuid
  155. AND product_code = :product_id
  156. """
  157. params = {"city_uuid": city_uuid, "product_id": product_id}
  158. data = self.db_helper.load_data_with_page(query, params)
  159. return data
  160. def get_delivery_data_by_product(self, city_uuid, product_id, start_time, end_time):
  161. query = f"""
  162. SELECT *
  163. FROM {self._eval_order_name}
  164. WHERE city_uuid = :city_uuid
  165. AND goods_code = :product_id
  166. AND cycle_begin_date = :start_time
  167. AND cycle_end_date = :end_time
  168. """
  169. params = {
  170. "city_uuid": city_uuid,
  171. "product_id": product_id,
  172. "start_time": start_time,
  173. "end_time": end_time
  174. }
  175. data = self.db_helper.load_data_with_page(query, params)
  176. return data
  177. def get_order_by_cust(self, city_uuid, cust_id):
  178. query = f"""
  179. SELECT *
  180. FROM {self._order_tablename}
  181. WHERE city_uuid = :city_uuid
  182. AND cust_code = :cust_id
  183. """
  184. params = {"city_uuid": city_uuid, "cust_id": cust_id}
  185. data = self.db_helper.load_data_with_page(query, params)
  186. return data
  187. def get_order_by_cust_and_product(self, city_uuid, cust_id, product_id):
  188. query = f"""
  189. SELECT *
  190. FROM {self._order_tablename}
  191. WHERE city_uuid = :city_uuid
  192. AND cust_code = :cust_id
  193. AND product_code =:product_id
  194. """
  195. params = {"city_uuid": city_uuid, "cust_id": cust_id, "product_id": product_id}
  196. data = self.db_helper.load_data_with_page(query, params)
  197. return data
  198. def get_product_from_order(self, city_uuid):
  199. query = f"SELECT DISTINCT product_code FROM {self._order_tablename} WHERE city_uuid = :city_uuid"
  200. params = {"city_uuid": city_uuid}
  201. data = pd.DataFrame(self.db_helper.fetch_all(text(query), params))
  202. return data
  203. # def get_product_from_order(self, city_uuid):
  204. # query = f"SELECT cust_code, product_code FROM {self._order_tablename} WHERE city_uuid = :city_uuid"
  205. # params = {"city_uuid": city_uuid}
  206. # data = pd.DataFrame(self.db_helper.fetch_all(text(query), params))
  207. # cust_list = self.get_cust_list(city_uuid)
  208. # cust_index = cust_list.set_index("BB_RETAIL_CUSTOMER_CODE")
  209. # data = data.join(cust_index, on="cust_code", how="inner")
  210. # data = data["product_code"]
  211. # return data
  212. def get_cust_list(self, city_uuid):
  213. query = f"SELECT DISTINCT BB_RETAIL_CUSTOMER_CODE FROM {self._cust_tablename} WHERE BA_CITY_ORG_CODE = :city_uuid"
  214. params = {"city_uuid": city_uuid}
  215. data = pd.DataFrame(self.db_helper.fetch_all(text(query), params))
  216. return data
  217. def data_preprocess(self, data: pd.DataFrame):
  218. data.drop(["cust_uuid", "longitude", "latitude", "range_radius"], axis=1, inplace=True)
  219. remaining_cols = data.columns.drop(["city_uuid", "cust_code"])
  220. col_with_missing = remaining_cols[data[remaining_cols].isnull().any()].tolist() # 判断有缺失的字段
  221. col_all_missing = remaining_cols[data[remaining_cols].isnull().all()].to_list() # 全部缺失的字段
  222. col_partial_missing = list(set(col_with_missing) - set(col_all_missing)) # 部分缺失的字段
  223. for col in col_partial_missing:
  224. data[col] = data[col].fillna(data[col].mean())
  225. for col in col_all_missing:
  226. data[col] = data[col].fillna(0).infer_objects(copy=False)
  227. if __name__ == "__main__":
  228. dao = MySqlDao()
  229. city_uuid = "00000000000000000000000011445301"
  230. cust_list = dao.get_cust_list(city_uuid)["BB_RETAIL_CUSTOMER_CODE"].tolist()
  231. for i in cust_list:
  232. print(i)