# -*- coding: utf-8 -*- # @Time : 2022/12/1 9:13 # @Author : XuJiakai # @File : search_winhc_latest_date # @Software: PyCharm from sdk import get_es_sdk from utils import map_2_json_str from utils.dim_name_mapping import get_latest_date_map from sdk.WinhcAllClient import get_all_client from utils.base_utils import tuple_max from utils.datetime_utils import datetime_format_transform import re from log import get_log log = get_log("winhc_latest_date") all_client = get_all_client() hbase_client = all_client.get_hbase_client() es_sdk = get_es_sdk("new") old_es_sdk = get_es_sdk("old") old_es_index = ['wenshu_detail2'] date_part = re.compile('\\d{4}年\\d{2}月\\d{2}日') def get_latest_date(index: str, company_id_f: str, company_id: str, latest_date_f: str): dsl = { "size": 1, "_source": [latest_date_f], "query": { "bool": { "must": [ { "term": { company_id_f: { "value": company_id } } }, { "term": { "deleted": { "value": 0 } } } ] } } , "sort": [ { latest_date_f: { "order": "desc" } } ] } if index in old_es_index: res = old_es_sdk.query(index=index, doc_type=None, dsl=dsl) pass else: res = es_sdk.query(index=index, doc_type='_doc', dsl=dsl) if len(res) == 0 or latest_date_f not in res[0]: return None latest_date = res[0][latest_date_f] # if date_part.match(latest_date) if date_part.match(latest_date): latest_date = datetime_format_transform(latest_date, '%Y年%m月%d日', "%Y-%m-%d %H:%M:%S") pass return latest_date pass latest_date_map = get_latest_date_map() def search_latest_date(company_id: str): result_data = {} for i in latest_date_map: str = latest_date_map[i] max_date = None for j in str.split(','): tmp_str = j.split(':') index = tmp_str[0] company_id_f = tmp_str[1] latest_date_f = tmp_str[2] tmp_date = get_latest_date(index=index, company_id_f=company_id_f, company_id=company_id, latest_date_f=latest_date_f) max_date = tuple_max(max_date, tmp_date) pass result_data[i] = max_date pass tmp_res = hbase_client.get_record('ng_rt_company', company_id) if tmp_res is not None and 'APPROVED_TIME' in tmp_res: result_data['基本信息'] = tmp_res['APPROVED_TIME'] log.info('fetch hbase data: {}'.format(tmp_res)) else: result_data['基本信息'] = None return result_data pass if __name__ == '__main__': # d = get_latest_date(index='winhc_index_rt_company_punishment_info', company_id_f='company_id', # company_id='059f83641cc4df8b9577cb1e2d89939e', latest_date_f='decision_date') # print(d) d = search_latest_date(company_id='6e13b126ee0c5fcd8fe454693ab4bbda') print(map_2_json_str(d)) pass