mysql_dao.py 12 KB

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