from dao.ic_gsb_branch_es import BranchEs
from dao.company_es import CompanyEs
from common import time_trans

class CompanyBase:
    companyes = CompanyEs()
    branches = BranchEs()

    #企业分支机构
    def companybranch(self,company_name):

        company_branchs = []
        branch_result = self.branches.select_branch(company_name)
        for item in branch_result['hits']['hits']:
            # print(item)
            # print(item['_id'])
            company_branchs.append(item['_source']['branch_name'])

        return company_branchs

    #查询企业基本信息
    def companyinfo(self,company_name):
        company_dict = {}
        company_result = self.companyes.select_baise(company_name)
        if company_result['hits']['hits']:
            for row in company_result['hits']['hits']:
                company_dict["companyname"] = row['_source']["company_name"]  # 企业名称
                company_dict["gsb_province"] = row['_source']["base"]  # 所属省
                company_dict["gsb_shi"] =  row['_source']["gsb_city"]   # 所属城市或者区
                company_dict["legal_persion"] = row['_source']["legal_person"]  # 法定代表人
                company_dict["reg_capital"] = row['_source']["reg_capital"]  # 注册资本
                company_dict["reg_unit"] = row['_source']["reg_unit"]  # 注册资本单位（万人民币）
                company_dict["reg_status"] = row['_source']["reg_status"]  # 营业状态
                company_dict["estiblish_time"] = row["_source"]["estiblish_time"]   # 成立时间（原本时间戳格式）
                company_dict["company_org_type"] = row['_source']["company_org_type"]  # 企业类型
                company_dict["gsb_company_cate_1"] = row['_source']["gsb_company_cate_1"]  # 所属行业大类
                company_dict["gsb_company_cate_2"] = row['_source']["gsb_company_cate_2"]   # 所属行业中类
                company_dict["gsb_company_cate_3"] =  row['_source']["gsb_company_cate_3"]  # 所属行业小类
                company_dict["business_scope"] = row['_source']["business_scope"]  # 企业经营范围
            return company_dict

    #查找相似企业
    def similar_company(self,cinfo):
        querybody = self.companyes.select_company_body()
        if cinfo["reg_status"]:
            dsl = {"terms": {"reg_status": ['存续', "在业"]}}
            querybody['query']['bool']['must'].append(dsl)
        if cinfo["gsb_company_cate_1"]:
            dsl = {"term": {"gsb_company_cate_1": cinfo["gsb_company_cate_1"]}}
            querybody['query']['bool']['must'].append(dsl)
        if cinfo["company_org_type"]:
            dsl = {"term": {"company_org_type": cinfo["company_org_type"]}}
            querybody['query']['bool']['must'].append(dsl)
        if cinfo["gsb_province"]:
            dsl = {"term": {"base": cinfo["gsb_province"]}}
            querybody['query']['bool']['must'].append(dsl)
        if cinfo["gsb_company_cate_2"]:
            dsl = {"term": {"gsb_company_cate_2": cinfo["gsb_company_cate_2"]}}
            querybody['query']['bool']['must'].append(dsl)
        if cinfo["gsb_company_cate_3"]:
            dsl = {"term": {"gsb_company_cate_3": cinfo["gsb_company_cate_3"]}}
            querybody['query']['bool']['must'].append(dsl)
        if cinfo["gsb_shi"]:
            dsl = {"term": {"gsb_city": cinfo["gsb_shi"]}}
            querybody['query']['bool']['must'].append(dsl)
        if cinfo["estiblish_time"]:
            begin_e_time, end_e_time = time_trans.TimestampChange(cinfo["estiblish_time"],1)
            dsl = {"range": {"estiblish_time": {"gte": begin_e_time, "lte": end_e_time}}}
            querybody['query']['bool']['must'].append(dsl)
        if cinfo["reg_capital"]:
            ori_cap = float(cinfo["reg_capital"])
            new_cap_sub = str(ori_cap * 0.2)
            new_cap_add = str(ori_cap * 1.8)
            dsl = {"range": {"reg_capital": {"gte": new_cap_sub, "lte": new_cap_add}}}
            querybody['query']['bool']['must'].append(dsl)
        if cinfo["reg_unit"]:
            dsl = {"term": {"reg_unit": cinfo["reg_unit"]}}
            querybody['query']['bool']['must'].append(dsl)
        company_list = self.companyes.select_company(querybody)

        namelist = []
        if len(company_list['hits']['hits']) > 0:
            for row in company_list['hits']['hits']:
                data = {"company_name":row['_source']["company_name"],  "credit_code":row['_source']["credit_code"]}
                namelist.append(data)
        company_list_result = namelist[:3]
        return company_list_result



