mysql_dao.py 12 KB

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