mysql_dao.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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._cust_table_name = "tads_brandcul_retail_cust_label" # 商户信息表
  16. self._analysis_table_name = "tads_brandcul_analysis_index" # 指标分析表
  17. self.cust_table_dao = self.CustTableDao(self.db_helper, self._cust_table_name)
  18. self._initialized = True
  19. class CustTableDao:
  20. """商户数据操作类"""
  21. def __init__(self, db_helper: MySqlDatabaseHelper, tablename):
  22. self.db_helper = db_helper
  23. self.tablename = tablename
  24. def load_data(self, features, corp_uuid):
  25. """读取商户数据"""
  26. features_column = ",".join(features)
  27. query = f"SELECT {features_column} FROM {self.tablename} WHERE corp_uuid = :corp_uuid"
  28. params = {"corp_uuid": corp_uuid}
  29. data = self.db_helper.load_data_with_page(query, params)
  30. return data
  31. def get_column_unique_value(self, column, corp_uuid):
  32. """获取指定列的唯一值列表"""
  33. query = f"SELECT {column} FROM {self.tablename} WHERE corp_uuid = :corp_uuid"
  34. params = {"corp_uuid", corp_uuid}
  35. data = self.db_helper.load_data_with_page(query, params)
  36. return data
  37. if __name__ == '__main__':
  38. features = ["cust_code", "cust_name", "busi_place_area_section"]
  39. dao = MySqlDao()
  40. corp_uuid = "00000000000000000000000011440101"
  41. data = dao.cust_table_dao.get_column_unique_value("busi_place_area_section", corp_uuid)
  42. print(data)