#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time     : 2020/5/26 14:20
# @Author   : hj
# @File     :business_job_es.py
# @software :PyCharm
from db.mod_db import EsBase
index = "bigdata_ic_gsb_label_op"

class hxMessage:
    def __init__(self, es=None):
        if es is None:
            self.esBase = EsBase()
        else:
            self.esBase = es
    def hxSearch(self,re_item):
        print('re_item', re_item)
        if re_item == []:
            querybody = {
                "query": {
                    "bool": {
                        "must": [
                            {
                                "match_all": {}
                            }
                        ]
                    }
                },
                "size": 10000
            }
        else:
            querybody = {
                "query": {
                    "bool": {
                        "must": [
                        ]
                    }
                },
                "size": 10000
            }
            if "qiyeguimo" in re_item:
                dsl = {
                    "terms": {
                        "qiyeguimo": re_item['qiyeguimo']
                    }
                }
                querybody["query"]["bool"]["must"].append(dsl)

            if 'qiyeleixing' in re_item:
                dsl = {
                    "terms": {
                        "qiyeleixing": re_item['qiyeleixing']
                    }}
                querybody['query']['bool']['must'].append(dsl)

            if 'chenglinianxian' in re_item:
                years_value = re_item['chenglinianxian']
                re_years_value_list = []
                for years in years_value:
                    if years[0:4] in ['青年公司', '长青公司', '成熟公司', '长寿公司']:
                        re_years_value = years[0:4]
                    else:
                        re_years_value = years[0:3]
                    re_years_value_list.append(re_years_value)
                dsl = {
                    "terms": {
                        "chenglinianxian": re_years_value_list

                    }
                }
                querybody['query']['bool']['must'].append(dsl)

            if 'hangyeleibie' in re_item:
                dsl = {
                    "terms": {
                        "hangyeleibie": re_item['hangyeleibie']
                    }
                }
                querybody['query']['bool']['must'].append(dsl)

            if 'jingyingzhuangtai' in re_item:
                dsl = {
                    "terms": {
                        "jingyingzhuangtai": re_item['jingyingzhuangtai']
                    }
                }
                querybody['query']['bool']['must'].append(dsl)

            if 'jingyinghangye' in re_item:
                dsl = {
                    "terms": {
                        "jingyinghangye": re_item['jingyinghangye']
                    }
                }
                querybody['query']['bool']['must'].append(dsl)

            if 'jingyingdiqu' in re_item:
                dsl = {
                    "terms": {
                        "jingyingdiqu": re_item['jingyingdiqu']
                    }
                }
                querybody['query']['bool']['must'].append(dsl)

            if 'fengongsishuliang' in re_item:
                branches = re_item['fengongsishuliang']
                re_branches_list = []
                for branch_item in branches:
                    if branch_item[0:4] in ['枝繁叶茂', '草木稀疏', '一枝独秀']:
                        re_branches = branch_item[0:4]
                    else:
                        re_branches = branch_item
                    re_branches_list.append(re_branches)
                dsl = {
                    "terms": {
                        "fengongsishuliang": re_branches_list
                    }
                }
                querybody['query']['bool']['must'].append(dsl)

            if 'zhuanli' in re_item:
                patent_nu = re_item['zhuanli']
                for zhuanli_item in patent_nu:
                    if zhuanli_item[0:2] == '专利':
                        re_patent_nu = zhuanli_item[0:4]
                    else:
                        re_patent_nu = zhuanli_item[0:4]
                    dsl = {
                        "wildcard": {
                            'zhuanli': "*" + re_patent_nu + "*"
                        }
                    }
                    querybody['query']['bool']['must'].append(dsl)

            if 'shangbiao' in re_item:
                trademark = re_item['shangbiao']
                for trademark_item in trademark:
                    if trademark_item[0:2] == '商标':
                        re_trademark = trademark_item[0:2]
                    else:
                        re_trademark = trademark_item[0:2]
                    dsl = {
                        "wildcard": {
                            "shangbiao": "*" + re_trademark + "*"
                        }
                    }
                    querybody['query']['bool']['must'].append(dsl)

            if 'zuopinzhuzuoquan' in re_item:
                work_nu = re_item['zuopinzhuzuoquan']
                for work_item in work_nu:
                    if work_item[0:4] == '作品著作':
                        re_work_nu = work_item[0:4]
                    else:
                        re_work_nu = work_item[0:4]
                    dsl = {
                        "wildcard": {
                            "zuopinzhuzuoquan": "*" + re_work_nu + "*"
                        }
                    }
                    querybody['query']['bool']['must'].append(dsl)

            if 'suozaidiqu' in re_item:
                address_nu = re_item['suozaidiqu']
                for address_item in address_nu:
                    dsl = {
                        "wildcard": {
                            "suozaidiqu": "*" + address_item + "*"
                        }
                    }
                    querybody['query']['bool']['must'].append(dsl)

            if 'ruanjianzhuzuoquan' in re_item:
                soft_nu = re_item['ruanjianzhuzuoquan']
                for soft_item in soft_nu:
                    soft_label = soft_item[0:4]
                    dsl = {
                        "wildcard": {
                            "ruanjianzhuzuoquan": "*" + soft_label + "*"
                        }
                    }
                    querybody['query']['bool']['must'].append(dsl)

            if 'zhengzhao' in re_item:
                cliense_nu = re_item['zhengzhao']
                dsl = {
                    "terms": {
                        "zhengzhao": cliense_nu
                    }
                }
                querybody['query']['bool']['must'].append(dsl)

            if 'chanpindalei' in re_item:
                product_cateory = re_item['chanpindalei']
                dsl = {
                    "terms": {
                        "chanpindalei": product_cateory
                    }
                }
                querybody['query']['bool']['must'].append(dsl)

            if 'fengxian' in re_item:
                fengxian_label = re_item['fengxian']
                dsl = {
                    "terms": {
                        "risk_label": fengxian_label
                    }
                }
                querybody['query']['bool']['must'].append(dsl)

        print('查询语法：', querybody)
        return self.esBase.select(index=index,body=querybody)