utils.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import yaml
  2. from sqlalchemy import text
  3. def load_config(env='test'):
  4. """ Loads the configuration file
  5. """
  6. with open('config.yaml', 'r') as f:
  7. config = yaml.load(f, Loader=yaml.FullLoader)
  8. print('loading config for env: {}'.format(env))
  9. return config[env]
  10. def truncate_target_db(conn, target_db) -> None:
  11. """ Truncates the target table
  12. """
  13. query = """
  14. SELECT count(1) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{target_db}'
  15. """.format(target_db=target_db)
  16. if conn.execute(text(query)).scalar() == 1:
  17. sql = """
  18. TRUNCATE TABLE {target_db}
  19. """.format(target_db=target_db)
  20. conn.execute(text(sql))
  21. def find_renewal_contract(conn, contract_id):
  22. query = """
  23. select crc1.id, crc1.begin_time, crc1.end_time, crc1.quite_date,
  24. crc2.id, crc2.begin_time, crc2.end_time, crc2.quite_date,
  25. crc3.id, crc3.begin_time, crc3.end_time, crc3.quite_date
  26. from yuxin_contract.cont_renter_contract crc1
  27. left join yuxin_contract.cont_renter_contract crc2 on crc2.contract_pid=crc1.id and crc2.is_delete=0 and crc2.contract_status<>4 and crc2.sign_type=2
  28. left join yuxin_contract.cont_renter_contract crc3 on crc3.contract_pid=crc2.id and crc3.is_delete=0 and crc3.contract_status<>4 and crc3.sign_type=2
  29. where crc1.is_delete=0 and crc1.contract_status<>4 and crc1.sign_type=2
  30. and crc1.contract_pid='{contract_id}'
  31. """.format(contract_id=contract_id)
  32. result = conn.execute(text(query)).fetchone()
  33. if result is None:
  34. return None
  35. if result[11] is not None:
  36. return result[11]
  37. elif result[10] is not None:
  38. return result[10]
  39. elif result[7] is not None:
  40. return result[7]
  41. elif result[6] is not None:
  42. return result[6]
  43. elif result[3] is not None:
  44. return result[3]
  45. elif result[2] is not None:
  46. return result[2]
  47. return None