search_winhc_latest_date.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2022/12/1 9:13
  3. # @Author : XuJiakai
  4. # @File : search_winhc_latest_date
  5. # @Software: PyCharm
  6. from sdk import get_es_sdk
  7. from utils import map_2_json_str
  8. from utils.dim_name_mapping import get_latest_date_map
  9. from sdk.WinhcAllClient import get_all_client
  10. from utils.base_utils import tuple_max
  11. all_client = get_all_client()
  12. hbase_client = all_client.get_hbase_client()
  13. es_sdk = get_es_sdk("new")
  14. old_es_sdk = get_es_sdk("old")
  15. old_es_index = ['wenshu_detail2']
  16. def get_latest_date(index: str, company_id_f: str, company_id: str, latest_date_f: str):
  17. dsl = {
  18. "size": 1,
  19. "_source": [latest_date_f],
  20. "query": {
  21. "bool": {
  22. "must": [
  23. {
  24. "term": {
  25. company_id_f: {
  26. "value": company_id
  27. }
  28. }
  29. },
  30. # {
  31. # "term": {
  32. # "deleted": {
  33. # "value": 0
  34. # }
  35. # }
  36. # }
  37. ]
  38. }
  39. }
  40. , "sort": [
  41. {
  42. latest_date_f: {
  43. "order": "desc"
  44. }
  45. }
  46. ]
  47. }
  48. if index in old_es_index:
  49. res = old_es_sdk.query(index=index, doc_type=None, dsl=dsl)
  50. pass
  51. else:
  52. res = es_sdk.query(index=index, doc_type='_doc', dsl=dsl)
  53. if len(res) == 0:
  54. return None
  55. return res[0][latest_date_f]
  56. pass
  57. latest_date_map = get_latest_date_map()
  58. def search_latest_date(company_id: str):
  59. result_data = {}
  60. for i in latest_date_map:
  61. str = latest_date_map[i]
  62. max_date = None
  63. for j in str.split(','):
  64. tmp_str = j.split(':')
  65. index = tmp_str[0]
  66. company_id_f = tmp_str[1]
  67. latest_date_f = tmp_str[2]
  68. tmp_date = get_latest_date(index=index, company_id_f=company_id_f, company_id=company_id,
  69. latest_date_f=latest_date_f)
  70. max_date = tuple_max(max_date, tmp_date)
  71. pass
  72. result_data[i] = max_date
  73. pass
  74. tmp_res = hbase_client.get_record('ng_company', company_id)
  75. if tmp_res is not None and 'APPROVED_TIME' in tmp_res:
  76. result_data['基本信息'] = tmp_res['APPROVED_TIME']
  77. else:
  78. result_data['基本信息'] = None
  79. return result_data
  80. pass
  81. if __name__ == '__main__':
  82. # d = get_latest_date(index='winhc_index_rt_company_punishment_info', company_id_f='company_id',
  83. # company_id='059f83641cc4df8b9577cb1e2d89939e', latest_date_f='decision_date')
  84. # print(d)
  85. d = search_latest_date(company_id='eda3ee85ad34656bd45bc587b21b6c26')
  86. print(map_2_json_str(d))
  87. pass