mysql_dao.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from database import MySqlDatabaseHelper
  2. from sqlalchemy import text
  3. class MySqlDao:
  4. _instance = None
  5. def __new__(cls):
  6. if not cls._instance:
  7. cls._instance = super(MySqlDao, cls).__new__(cls)
  8. cls._instance._initialized = False
  9. return cls._instance
  10. def __init__(self):
  11. if self._initialized:
  12. return
  13. self.db_helper = MySqlDatabaseHelper()
  14. self._product_tablename = "tads_brandcul_product_info"
  15. self._cust_tablename = "tads_brandcul_cust_info"
  16. self._order_tablename = "tads_brandcul_cust_order"
  17. self._mock_order_tablename = "yunfu_mock_data"
  18. self._initialized = True
  19. def load_product_data(self, city_uuid):
  20. """从数据库中读取商品信息"""
  21. query = f"SELECT * FROM {self._product_tablename} WHERE city_uuid = :city_uuid"
  22. params = {"city_uuid": city_uuid}
  23. data = self.db_helper.load_data_with_page(query, params)
  24. return data
  25. def load_cust_data(self, city_uuid):
  26. """从数据库中读取商户信息"""
  27. query = f"SELECT * FROM {self._cust_tablename} WHERE BA_CITY_ORG_CODE = :city_uuid"
  28. params = {"city_uuid": city_uuid}
  29. data = self.db_helper.load_data_with_page(query, params)
  30. return data
  31. def load_order_data(self, city_uuid):
  32. """从数据库中读取订单信息"""
  33. query = f"SELECT * FROM {self._order_tablename} WHERE city_uuid = :city_uuid"
  34. params = {"city_uuid": city_uuid}
  35. data = self.db_helper.load_data_with_page(query, params)
  36. data.drop('stat_month', axis=1, inplace=True)
  37. data.drop('city_uuid', axis=1, inplace=True)
  38. # 去除重复值和填补缺失值
  39. data.drop_duplicates(inplace=True)
  40. data.fillna(0, inplace=True)
  41. data = data.infer_objects(copy=False)
  42. return data
  43. def load_mock_order_data(self, city_uuid):
  44. """从数据库中读取mock的订单信息"""
  45. query = f"SELECT * FROM {self._mock_order_tablename}"
  46. params = {"city_uuid": city_uuid}
  47. data = self.db_helper.load_data_with_page(query, params)
  48. # 去除重复值和填补缺失值
  49. data.drop_duplicates(inplace=True)
  50. data.fillna(0, inplace=True)
  51. data = data.infer_objects(copy=False)
  52. return data
  53. def get_cust_list(self, city_uuid):
  54. """获取商户列表"""
  55. data = self.load_cust_data(city_uuid)
  56. cust_list = data["BB_RETAIL_CUSTOMER_CODE"].to_list()
  57. if len(cust_list) == 0:
  58. return []
  59. return cust_list
  60. def get_product_by_id(self, city_uuid, product_id):
  61. """根据city_uuid 和 product_id 从表中获取拼柜信息"""
  62. query = text(f"""
  63. SELECT *
  64. FROM {self._product_tablename}
  65. WHERE city_uuid = :city_uuid
  66. AND product_code = :product_id
  67. """)
  68. params = {"city_uuid": city_uuid, "product_id": product_id}
  69. data = self.db_helper.fetch_one(query, params)
  70. return data
  71. def get_cust_by_ids(self, city_uuid, cust_id_list):
  72. """根据零售户列表查询其信息"""
  73. if not cust_id_list:
  74. return None
  75. cust_id_str = ",".join([f"'{cust_id}'" for cust_id in cust_id_list])
  76. query = text(f"""
  77. SELECT *
  78. FROM {self._cust_tablename}
  79. WHERE BA_CITY_ORG_CODE = :city_uuid
  80. AND BB_RETAIL_CUSTOMER_CODE IN ({cust_id_str})
  81. """)
  82. params = {"city_uuid": city_uuid}
  83. data = self.db_helper.fetch_all(query, params)
  84. return data
  85. if __name__ == "__main__":
  86. dao = MySqlDao()
  87. # city_uuid = "00000000000000000000000011445301"
  88. city_uuid = "00000000000000000000000011441801"
  89. cust_id_list = ["441800100006", "441800100051", "441800100811"]
  90. cust_list = dao.get_cust_by_ids(city_uuid, cust_id_list)
  91. print(len(cust_list))