# coding:utf8
from BaseDataMaintenance.model.ots.BaseModel import BaseModel
from BaseDataMaintenance.common.Utils import *
from BaseDataMaintenance.primarykey.startSnowflake import get_guid
import json
from datetime import datetime
from BaseDataMaintenance.dataSource.source import getConnect_ots
from tablestore import *

from BaseDataMaintenance.maintenance.dataflow import Dataflow_dumplicate

ots_client = getConnect_ots()

stage_priority_dict = {
    "立项阶段": "立项阶段",
    "可研阶段": "可研阶段",
    "环评阶段": "环评阶段",
    "稳评阶段": "环评阶段",
    "咨询阶段": "环评阶段",
    "造价阶段": "环评阶段",
    "设计阶段": "设计阶段",
    "勘察阶段": "设计阶段",
    "EPC总承包": "设计阶段",
    "施工图审": "施工准备",
    "施工许可": "施工准备",
    "施工准备": "施工准备",
    "施工在建": "施工在建",
    "竣工阶段": "竣工阶段"
}

global_same_project_list = [
    ['江西省吉安市吉安县天华路小学建设项目', '江西省吉安市吉安县天华路小学建设', '江西省吉安市吉安县天华路小学建设项目室外附属']
]


data_flow = Dataflow_dumplicate(start_delete_listener=False)


class proposedBuilding_tmp(BaseModel):

    def __init__(self, _dict):

        for k, v in _dict.items():
            self.setValue(k, v, True)
        self.table_name = "proposedBuilding_tmp"
        self.dict_enterprise = {}
        self.dict_enterprise_contact = {}
        self.dict_document = {}
        self.data_flow = data_flow
        self.process_id = '0000000'

    def getPrimary_keys(self):
        return ["uuid"]

    # def getFollows(self,list_group):
    #
    #
    #
    #     dict_stage = {}
    #
    #
    #     #sort
    #     list_group.sort(key=lambda x:x.get("page_time",""))
    #
    #
    #     list_follows = []
    #     #dumplicate
    #     set_stage_page_time = set()
    #     for _group in list_group:
    #         current_stage = _group.get("stage","")
    #         current_pageTime = _group.get("page_time","")
    #         if current_stage=="" or current_pageTime=="":
    #             continue
    #         _key = "%s-%s"%(str(current_stage),str(current_pageTime))
    #         if _key in set_stage_page_time:
    #             continue
    #         list_follows.append(_group)
    #         set_stage_page_time.add(_key)
    #     return list_follows

    # for _group in list_group:
    #     _stage = _group["stage"]
    #     if _stage not in dict_stage:
    #         dict_stage[_stage] = []
    #     dict_stage[_stage].append(_group)
    #
    #
    #
    # LIST_STEGES = ["施工在建","施工准备","环评阶段","设计阶段"]
    # # LIST_STEGES = ["立项","可研","环评","稳评","其他咨询服务","造价","勘察设计","施工图审查","施工许可证","施工准备","施工在建","竣工"]
    # for k,v in dict_stage.items():
    #     v.sort(key=lambda x:x.get("page_time",""),reverse=True)
    # list_follows = []
    # last_stage = ""
    # last_pageTime = ""
    # for _STAGE_i in range(len(LIST_STEGES)):
    #     _STAGE = LIST_STEGES[_STAGE_i]
    #     for _group in dict_stage.get(_STAGE,[]):
    #         current_stage = _group.get("stage","")
    #         current_pageTime = _group.get("page_time","")
    #         if last_stage=="":
    #             last_stage = current_stage
    #             last_pageTime = current_pageTime
    #         if last_stage in LIST_STEGES[:_STAGE_i]:
    #             continue
    #         if current_pageTime>last_pageTime:
    #             continue
    #         last_stage = current_stage
    #         last_pageTime = current_pageTime
    #         list_follows.append(_group)
    # list_follows.reverse()
    # return list_follows

    def getEnterprise(self, enterprise_name, columns):
        _time = time.time()
        # if enterprise_name in self.dict_enterprise:
        #     return self.dict_enterprise[enterprise_name]
        enterprise_dict = self.search(ots_client, "enterprise", [("name", str(enterprise_name))], columns)
        self.dict_enterprise[enterprise_name] = enterprise_dict
        # log(str(self.process_id) + " search Enterprise %s takes %.2f" % (str(enterprise_name), time.time() - _time))
        return enterprise_dict

    def getEnterprise_contact(self, enterprise_name, limit=2,
                              columns=["enterprise_name", "contact_person", "phone_no"]):

        _time = time.time()
        if enterprise_name in self.dict_enterprise_contact:
            return self.dict_enterprise_contact[enterprise_name]
        _query = BoolQuery(must_queries=[TermQuery("enterprise_name", enterprise_name),
                                         TermQuery("is_mobile", 1)])

        rows, next_token, total_count, is_all_succee = ots_client.search("enterprise_contact",
                                                                         "enterprise_contact_index",
                                                                         SearchQuery(_query, sort=Sort(sorters=[
                                                                             FieldSort("score", SortOrder.DESC)]),
                                                                                     limit=limit),
                                                                         columns_to_get=ColumnsToGet(columns,
                                                                                                     ColumnReturnType.SPECIFIED))
        list_data = getRow_ots(rows)
        self.dict_enterprise_contact[enterprise_name] = list_data
        return self.dict_enterprise_contact[enterprise_name]

    def get_enterprise_contact(self, enterprise_name, contact_person, phone_no):
        _time = time.time()
        query_list = [TermQuery("enterprise_name", enterprise_name)]
        columns_to_get = ["enterprise_name", 'is_mobile']
        limit = 5
        if contact_person:
            query_list += [TermQuery("contact_person", contact_person)]
            columns_to_get += ["phone_no"]
        else:
            query_list += [TermQuery("phone_no", phone_no)]
            columns_to_get += ["contact_person"]

        _query = BoolQuery(must_queries=query_list)

        rows, next_token, total_count, is_all_succeed = ots_client.search("enterprise_contact", "enterprise_contact_index",
                                                                          SearchQuery(_query, sort=Sort(sorters=[FieldSort("score", SortOrder.DESC)]), limit=limit),
                                                                          columns_to_get=ColumnsToGet(columns_to_get, ColumnReturnType.SPECIFIED))
        list_data = getRow_ots(rows)
        is_mobile = 0
        for data in list_data:
            if contact_person:
                if phone_no and is_mobile:
                    break
                if not phone_no:
                    phone_no = data.get('phone_no', '')
                    is_mobile = int(data.get('is_mobile', 0))
            else:
                if not contact_person:
                    contact_person = data.get('contact_person', '')
                    break
        # log("search enterprise_contact %s %s %s takes %.2f" % (str(enterprise_name), str(contact_person), str(phone_no), time.time() - _time))
        return [enterprise_name, contact_person, phone_no]

    def getStage(self, _stage):

        return stage_priority_dict.get(_stage)

    def addContact(self, _contacts, _type, enterprise_name):
        # add contacts from enterprise table

        enterprise_dict = self.getEnterprise(enterprise_name, ["province", "contacts", "address"])

        enterprise_contact = self.getEnterprise_contact(enterprise_name)
        _time = time.time()
        if enterprise_dict is None:
            enterprise_dict = {}
        cellphone = ""

        _count = 0
        for _contact in enterprise_contact:
            cellphone = _contact.get("phone_no", "")
            contact_name = _contact.get("contact_person", "")
            _dict_contact = {"address": enterprise_dict.get("address", ""), "cellphone": cellphone,
                             "phone": "", "company_name": enterprise_name, "contact_name": contact_name,
                             "type": _type, "id": get_guid()}
            _dict_contact = popNoneFromDict(_dict_contact)
            _count += 1
            _contacts.append(_dict_contact)
            if _count >= 2:
                break
            log("extract contact %s takes %.2f " % (enterprise_name, time.time() - _time))

    def add_contact(self, _contacts, _type, enterprise_name, address, contact_name, cellphone, phone, ):
        _dict_contact = {"address": address, "cellphone": cellphone,
                         "phone": phone, "company_name": enterprise_name, "contact_name": contact_name,
                         "type": _type, "id": 0}
        _dict_contact = popNoneFromDict(_dict_contact)
        _contacts.append(_dict_contact)

    def getContacts(self, ots_client, list_group, set_enterprise, _contacts, is_yeji=0):
        start_time = time.time()
        start_time1 = time.time()

        set_contact = set()

        # 查document，公司名、对应联系方式一次性拿出
        value_list = []
        value_contact_list = []
        role_value_stage_contact_list = []
        roles = ['tenderee', 'agency', 'win_tenderer']
        if not is_yeji:
            for _group in list_group:
                _docid = _group.get("docid", 0)
                if _docid in [None, '']:
                    _docid = 0
                else:
                    _docid = int(_docid)
                stage = _group.get("stage", '')
                if _docid <= 0:
                    continue

                document_dict = self.getDocumentWithDocid(_docid, ["sub_docs_json", "tenderee", "tenderee_contact",
                                                                   "tenderee_phone", "agency", "agency_contact",
                                                                   "agency_phone"])
                if document_dict is not None:
                    for role in roles:
                        if role in ['tenderee', 'agency']:
                            role_value = document_dict.get(role, '')
                            contact_name = document_dict.get(role + '_contact', '')
                            contact_phone = document_dict.get(role + '_phone', '')
                        else:
                            sub_docs_json = document_dict.get("sub_docs_json", '[{}]')
                            role_value = ''
                            contact_name = ''
                            contact_phone = ''
                            for sub_docs in json.loads(sub_docs_json):
                                if role_value == '':
                                    role_value = sub_docs.get("win_tenderer", "")
                                    contact_name = document_dict.get(role + '_manager', '')
                                    contact_phone = document_dict.get(role + '_phone', '')

                        if not role_value:
                            continue
                        role_value_stage_contact_list.append([role, role_value, stage, contact_name, contact_phone])
                        value_list.append(role_value)
                        if ((contact_name == '') ^ (contact_phone == '')) and [role_value, contact_name, contact_phone] not in value_contact_list:
                            value_contact_list.append([role_value, contact_name, contact_phone])

        # 业绩数据直接拿出main中的contacts
        else:
            contacts = list_group[0].get("contacts", '')
            if contacts:
                contacts, contacts_cnt = contacts
            contacts_yeji = list_group[0].get("contacts_yeji", '')
            print('len(contacts_yeji + contacts)', len(contacts_yeji + contacts))
            for c in contacts_yeji+contacts:
                role_value = c.get("company_name", '')
                contact_name = c.get("contact_name", '')
                contact_phone = c.get("phone", '')
                role = c.get('type', '')
                if not contact_phone:
                    contact_phone = c.get("cellphone", '')
                value_list.append(role_value)
                role_value_stage_contact_list.append([role, role_value, None, contact_name, contact_phone])
                value_contact_list.append([role_value, contact_name, contact_phone])
        # print('role_value_stage_contact_list', role_value_stage_contact_list)

        # print('len(value_contact_list)', len(value_contact_list))

        # 查enterprise，一次性查出地址
        value_list = list(set(value_list))
        value_address_dict = {}
        for value in value_list:
            enterprise_dict = self.getEnterprise(value, ["address"])
            if enterprise_dict is None:
                continue
            value_address_dict[value] = enterprise_dict.get('address', '')

        # print('len(value_address_dict)', len(value_address_dict))

        # 查enterprise_contact，一次性查出 有人名无号码/有号码无人名 联系人
        value_contact_dict = {}
        # print('value_contact_list', value_contact_list)
        for i in range(len(value_contact_list)):
            role_value, name, phone = self.get_enterprise_contact(*value_contact_list[i])
            if value_contact_list[i][1]:
                value_contact_dict[role_value + name] = phone
            else:
                value_contact_dict[role_value + phone] = name

        # print('value_address_dict', value_address_dict)
        # print('role_value_stage_contact_list', role_value_stage_contact_list)

        # 生成contact
        for role, role_value, stage, contact_name, contact_phone in role_value_stage_contact_list:
            # if role in ['tenderee', 'agency']:
            address = value_address_dict.get(role_value, '')

            if contact_name and not contact_phone:
                contact_phone = value_contact_dict.get(role_value + contact_name, '')
            if not contact_name and contact_phone:
                contact_name = value_contact_dict.get(role_value + contact_phone, '')

            cellphone = ""
            phone = ""
            if isCellphone(contact_phone):
                cellphone = contact_phone
            else:
                phone = contact_phone
            if not is_yeji:
                if role == 'tenderee':
                    real_role = '业主单位'
                elif role == 'agency':
                    real_role = '代理单位'
                else:
                    if stage:
                        real_role = re.sub('阶段', '', stage) + '单位'
                    else:
                        real_role = '其他单位'
            else:
                real_role = role

            self.add_contact(_contacts, real_role, role_value, address, contact_name, cellphone, phone)
        # print('len(_contacts)', len(_contacts))

        # 同公司无联系人合并至有联系人的
        delete_list = []
        for i in range(len(_contacts)):
            c1 = _contacts[i]
            if not c1.get('contact_name', '') and not c1.get('phone', '') and not c1.get('cellphone'):
                for j in range(i+1, len(_contacts)):
                    c2 = _contacts[j]
                    if c1.get('company_name', '') == c2.get('company_name', '') \
                            and c1.get('type', '') == c2.get('type', ''):
                        delete_list.append(c1)
                        break
        for c in delete_list:
            if c in _contacts:
                _contacts.remove(c)
        # print('len(delete_list)', len(delete_list))
        # print('len(_contacts)', len(_contacts))
        # 去重
        tenderee_count = 0
        list_contact = []
        index = 0
        for _c in _contacts:
            _line = "%s-%s-%s" % (_c.get("company_name", ""), _c.get("cellphone", ""), _c.get("phone", ""))
            # if _c.get("cellphone", "") == "" and _c.get("phone", "") == "":
            #     continue
            if _line not in set_contact:
                _c['id'] = index
                list_contact.append(_c)
                index += 1
                if _c.get("type", "") == "业主单位":
                    tenderee_count += 1
            set_contact.add(_line)
        # log(str(self.process_id) + ' getContacts cost: ' + str(time.time() - start_time))
        return list_contact, tenderee_count

    def getFollows(self, list_group, _contacts, set_enterprise):
        set_follow = set()
        _version_index = 0
        set_project_uuid = set()
        list_follow = []
        if len(list_group) == 0:
            return list_follow
        for _group in list_group:
            _spid = _group.get("shenpi_id")
            _docid = _group.get("docid")
            _stage = _group.get("stage")
            follow_time = _group.get("page_time")
            _docchannel = _group.get("docchannel")
            _doctitle = _group.get("doctitle")
            progress_remark = ""
            if _docid is not None and _docid == 0:
                continue
            if "is_completed" in _group or "proportion" in _group:
                continue

            if _docid is not None and _docid != "":
                document_dict = self.getDocumentWithDocid(_docid, ["status"])
                if document_dict is None or (
                        document_dict.get("status", 0) >= 401 and document_dict.get("status", 0) <= 450):
                    continue
            #     win_tenderer = ""
            #
            #     if document_dict is not None:
            #
            #         for sub_docs in json.loads(document_dict.get("sub_docs_json","[{}]")):
            #             if win_tenderer=="":
            #                 win_tenderer = sub_docs.get("win_tenderer","")
            #
            #         _status = document_dict.get("status")
            #         remain_docid = _docid
            #         if not(_status>=201 and _status<=300):
            #             remain_docid = self.data_flow.getRemainDoc(remain_docid)
            #             log("remain_docid from %s to %s"%(str(_docid),str(remain_docid)))
            #         project_flag = False
            #         if remain_docid is not None:
            #             _project = self.getProjectWithDocid(remain_docid,["zhao_biao_page_time","zhong_biao_page_time","win_tenderer","tenderee"])
            #
            #             if _project is not None:
            #                 _uuid = _project.get("uuid")
            #                 if _uuid in set_project_uuid:
            #                     continue
            #                 progress_remark = ""
            #                 if _project.get("zhao_biao_page_time") is not None:
            #                     follow_time = max(follow_time,_project.get("zhao_biao_page_time"))
            #                     progress_remark += "%s,项目处于%s的招标阶段；"%(_project.get("zhao_biao_page_time"),_stage)
            #                 if _project.get("zhong_biao_page_time") is not None:
            #                     follow_time = max(follow_time,_project.get("zhong_biao_page_time"))
            #                     progress_remark += "截止%s"%(_project.get("zhong_biao_page_time"))
            #                 if _project.get("win_tenderer") is not None:
            #                     progress_remark += "确定%s中标人为%s"%(_stage,_project.get("win_tenderer"))
            #
            #                 if _project.get("win_tenderer") is not None:
            #                     if _project.get("win_tenderer") not in set_enterprise:
            #                         self.addContact(_contacts,"%s单位"%(str(_group.get("stage","")).replace("None","").replace("阶段","")),_project.get("win_tenderer"))
            #                         set_enterprise.add(_project.get("win_tenderer"))
            #
            #                 if _project.get("tenderee") is not None:
            #                     if _project.get("tenderee") not in set_enterprise:
            #                         self.addContact(_contacts,"业主单位",_project.get("tenderee"))
            #                         set_enterprise.add(_project.get("tenderee"))
            #
            #                 project_flag = True
            #                 set_project_uuid.add(_uuid)
            #         if not project_flag:
            #             if document_dict.get("docchannel") in (101,118,119,120):
            #                 progress_remark = "截止%s,项目处于%s；"%(_group["page_time"],str(_stage))
            #                 if win_tenderer!="":
            #                     progress_remark += "确定%s中标人为%s"%(_stage,win_tenderer)
            #             else:
            #                 progress_remark = "截止%s,项目处于%s的招标阶段；"%(_group["page_time"],str(_stage))
            # else:
            #     progress_remark = "截止%s,项目处于%s；"%(_group["page_time"],str(_stage))
            #
            # _key = "%s-%s"%(_group["page_time"],str(_stage))
            # if _key in set_follow:
            #     continue
            # set_follow.add(_key)

            _version_index += 1
            _follow = {"id": get_guid(), "crtime": getCurrent_date(), "docid": _docid, "spid": _spid,
                       "mdate": "%s 00:00:00" % follow_time,
                       "docchannel": _docchannel, "doctitle": _doctitle,
                       "progress": _stage, "progress_remark": progress_remark,
                       "version": "跟进%d" % (_version_index)}

            list_follow.append(_follow)
        list_follow.sort(key=lambda x: x.get("mdate", ""))
        _version_index = 0
        for _follow in list_follow:
            _version_index += 1
            _follow["version"] = "跟进%d" % (_version_index)
        return list_follow

    def getId(self, list_docid, list_spid):

        #
        # list_should_query = []
        # for _docid in list_docid:
        #     _q = TermQuery("docids",_docid)
        #     list_should_query.append(_q)
        # for _spid in list_spid:
        #     _q = TermQuery("spids",_spid)
        #     list_should_query.append(_q)

        return get_guid()

    def getDocumentWithDocid(self, docid, columns=["page_time", "docchannel", "sub_docs_json"]):
        if docid is None or docid == "":
            return None
        # if docid in self.dict_document:
        #     return self.dict_document.get(docid)
        partitionkey = int(docid) % 500 + 1
        document_dict = self.search(ots_client, "document", [("partitionkey", partitionkey), ("docid", int(docid))],
                                    columns)
        self.dict_document[docid] = document_dict
        return document_dict

    def getProjectWithDocid(self, docid, columns):

        bool_query = BoolQuery(must_queries=[TermQuery("docids", docid)])

        rows, next_token, total_count, is_all_succeed = ots_client.search("project2", "project2_index",
                                                                          SearchQuery(bool_query, get_total_count=True,
                                                                                      limit=10),
                                                                          columns_to_get=ColumnsToGet(columns,
                                                                                                      ColumnReturnType.SPECIFIED))
        if len(rows) > 0:
            list_data = getRow_ots(rows)
            return list_data[0]
        return None

    def get_json_from_designed_project_by_same_name(self, main_project, name_list=None):
        name = main_project.get('project_name')
        province = main_project.get('province')
        city = main_project.get('city')
        district = main_project.get('district')
        print('main_project', main_project)

        if name is None:
            return []

        need_col = 'json_list_group'
        search_col = "high_project_name"
        should_q = []

        if re.search(re.escape(name), str(global_same_project_list)):
            for l in global_same_project_list:
                if name in l:
                    name_list = l
                    break
        elif name_list is None:
            name_list = [name]
        for n in name_list:
            should_q.append(MatchPhraseQuery(search_col, n))

        # 设置省市搜索
        if city in ['', None, '-', '未知']:
            if province in ['', None, '-', '未知']:
                location_col = 'province'
                location_val = None
            else:
                location_col = 'province'
                location_val = province
        else:
            location_col = 'city'
            location_val = city
        print('location_col, location_val', location_col, location_val)

        if location_val is None:
            bool_query = BoolQuery(must_queries=[
                BoolQuery(should_queries=should_q),
                BoolQuery(must_not_queries=[TermQuery('status', '404')]),
            ])
        else:
            bool_query = BoolQuery(must_queries=[
                BoolQuery(should_queries=should_q),
                TermQuery(location_col, location_val),
                BoolQuery(must_not_queries=[TermQuery('status', '404')]),
            ])
        rows, next_token, total_count, is_all_succeed = ots_client.search("designed_project", "designed_project_index",
                                                                          SearchQuery(bool_query, get_total_count=True, limit=20),
                                                                          columns_to_get=ColumnsToGet([search_col, need_col, 'status'], ColumnReturnType.SPECIFIED))
        result_list = []
        if len(rows) > 0:
            list_data = getRow_ots(rows)
            for d in list_data:
                if d.get(search_col) in name_list and d.get(need_col) is not None:
                    result_list.append(d.get(need_col))
        return result_list

    def merge_project(self, current_project, same_project_list):
        if not same_project_list:
            return current_project

        # 获取main_project和follows
        same_project_list = [json.loads(x) for x in same_project_list]
        same_main_project_list = []
        same_follow_list = []
        for project in same_project_list:
            main_project = None
            for g in project:
                if g.get('type_id') == 0:
                    main_project = g
            if main_project is None:
                continue
            same_main_project_list.append(main_project)
            project.remove(main_project)
            same_follow_list += project
        log(str(self.process_id) + ' len(same_main_project_list) ' + str(len(same_main_project_list)))
        log(str(self.process_id) + ' len(same_follow_list) ' + str(len(same_follow_list)))

        # 将main_project合并
        for col in current_project[0].keys():
            col_value = current_project[0].get(col)
            if type(col_value) == str:
                none_list = [None, 'None', '', '-']
            else:
                none_list = [None, '', '-', '0', 0.0, 0]
            if current_project[0].get(col) in none_list:
                for p in same_main_project_list:
                    if p.get(col) not in none_list:
                        current_project[0][col] = p.get(col)
                        log(str(self.process_id) + ' add col ' + str(col) + ' ' + str(p.get(col)))
                        break

        # 将follows合并
        current_project += same_follow_list

        return current_project

    def distinct_by_type_id(self, list_group):
        # 拿出main_project
        main_project = None
        for g in list_group:
            if g.get('type_id') == 0:
                main_project = g
        if main_project is None:
            return None
        self.process_id = '<' + main_project.get('project_name') + '> -'

        # 根据id去重
        type_id_g_dict = {}
        list_group.remove(main_project)
        for g in list_group:
            type_id = int(g.get('type_id'))
            if type_id:
                if type_id in type_id_g_dict.keys():
                    type_id_g_dict[type_id] += [g]
                else:
                    type_id_g_dict[type_id] = [g]
        temp_list = []
        for key in type_id_g_dict.keys():
            g_list = type_id_g_dict.get(key)
            if len(g_list) >= 2:
                g_list.sort(key=lambda x: len(x))
                temp_list.append(g_list[-1])
            else:
                temp_list += g_list
        list_group = temp_list

        # 确定格式，main_project + 按page_time排序follows
        list_group.sort(key=lambda x: datetime.strptime(x.get("page_time"), "%Y-%m-%d"))
        list_group = list_group[:500]
        list_group = [main_project] + list_group
        return list_group

    def toDesigned_project_old(self, ots_client):
        if "json_list_group" not in self.getProperties() or self.json_list_group is None or self.json_list_group == "":
            return None
        list_group = json.loads(self.json_list_group)

        # main_project = None
        # for g in list_group:
        #     if g.get('type_id') == 0:
        #         main_project = g
        # if main_project is None:
        #     return None
        main_project = list_group[0]

        # 根据id去重
        type_id_g_dict = {}
        for g in list_group:
            type_id = g.get('type_id')
            if type_id:
                if type_id in type_id_g_dict.keys():
                    type_id_g_dict[type_id] += [g]
                else:
                    type_id_g_dict[type_id] = [g]
        temp_list = []
        for key in type_id_g_dict.keys():
            g_list = type_id_g_dict.get(key)
            if len(g_list) >= 2:
                g_list.sort(key=lambda x: len(x))
                temp_list.append(g_list[-1])
            else:
                temp_list += g_list
        list_group = temp_list
        list_group = [main_project] + list_group

        # 区分历史数据，历史数据中提前取了contacts
        is_history = False
        contacts = None
        if list_group[0].get('is_history') == 1:
            is_history = True
            contacts = list_group[0].get('contacts')

        # 区分四库业绩数据，业绩数据中有contacts，还有业绩详情数据
        is_yeji = False
        contacts_yeji = None
        approval_list, bid_info_list, contract_register_list = [], [], []
        construct_pic_check_list, construct_permit_list, completion_accpet_list = [], [], []
        if list_group[0].get('is_yeji') == 1:
            is_yeji = True
            contacts_yeji = list_group[0].get('contacts_yeji')
            approval_list = list_group[0].get('approval_list')
            bid_info_list = list_group[0].get('bid_info_list')
            contract_register_list = list_group[0].get('contract_register_list')
            construct_pic_check_list = list_group[0].get('construct_pic_check_list')
            construct_permit_list = list_group[0].get('construct_permit_list')
            completion_accpet_list = list_group[0].get('completion_accpet_list')

        # 合并相同名字的project
        print("list_group[0].get('project_name')", list_group[0].get('project_name'))
        json_list = self.get_json_from_designed_project_by_same_name(list_group[0])
        list_group = self.merge_project(list_group, json_list)
        list_group = [json.loads(y) for y in list(set([json.dumps(x) for x in list_group]))]
        self.json_list_group = json.dumps(list_group, ensure_ascii=False)

        is_completed = False
        for _group in list_group:
            if "page_time" not in _group or _group["page_time"] is None:
                _group["page_time"] = ""
            if _group.get("is_completed", 0) == 1:
                is_completed = True

        sub_list_group = list_group[1:]
        sub_list_group.sort(key=lambda x: x["page_time"])
        list_group = [list_group[0]] + sub_list_group

        set_contacts = set()
        list_contacts = []
        set_follows = set()
        _version_index = 1
        crtime = getCurrent_date()
        floor_space = None

        covered_area = 0.0
        project_address = ""
        begintime = ""
        endtime = ""
        project_description = ''
        project_name = ""
        high_project_name = ""
        ordinary_name = ""
        project_follow = ""

        progress = ""

        status = "1"

        update_status = "0"
        update_time = getCurrent_date("%Y-%m-%d %H:%M:%S")
        page_time = ""
        project_code = ""
        project_type = ""
        area = ""
        province = ""
        city = ""
        district = ""

        set_docid = set()
        set_spid = set()
        set_enterprise = set()

        latest_service_time = ""

        list_follows = []

        project_nature = ""
        project_investment = 0.0

        project_structure = ""
        facade_type = ""
        floors_num = 0
        elevator = 0
        engineer_cost = 0.0
        construct_install_fee = 0.0
        facade_type2 = ""
        has_steel = 0
        proportion_unit = ""

        _version_index = 0
        for _group in list_group:
            _docid = _group.get("docid")
            _spid = _group.get("shenpi_id")

            _stage = _group.get("stage")
            follow_time = _group.get("page_time")
            try:
                datetime.strptime(follow_time, "%Y-%m-%d")
            except:
                continue

            _docchannel = _group.get("docchannel")
            _doctitle = _group.get("doctitle")
            progress_remark = ""
            if project_address == "" and _group.get("projectAddress") is not None:
                project_address = _group.get("projectAddress", "")

            if project_nature == "" and _group.get("project_property") is not None:
                project_nature = _group.get("project_property", "")

            if project_investment == 0 and _group.get("total_invest") is not None:
                project_investment = float(_group.get("total_invest", 0))

            if project_structure == "" and _group.get("structure") is not None:
                project_structure = _group.get("structure", "")

            if floors_num == 0 and _group.get("max_floor") is not None:
                floors_num = _group.get("max_floor", 0)

            if facade_type == "" and _group.get("wall_type") is not None:
                facade_type = _group.get("wall_type", "")

            if elevator == 0 and _group.get("has_elevator") is not None:
                elevator = _group.get("has_elevator", 0)

            if engineer_cost == 0 and _group.get("engineer_cost") is not None:
                engineer_cost = float(_group.get("engineer_cost", 0))

            if construct_install_fee == 0 and _group.get("construct_install_fee") is not None:
                construct_install_fee = float(_group.get("construct_install_fee", 0))

            if facade_type2 == "" and _group.get("wall_type2") is not None:
                facade_type2 = _group.get("wall_type2", "")

            if has_steel == 0 and _group.get("has_steel") is not None:
                has_steel = _group.get("has_steel", 0)
            if proportion_unit == "" and _group.get("proportion_unit") is not None:
                proportion_unit = _group.get("proportion_unit", "")

            if _docid is not None and _docid > 0:
                set_docid.add(str(_docid))
            if _spid is not None and _spid > 0:
                set_spid.add(str(_spid))
            if floor_space is None and _group.get("floor_space") is not None:
                floor_space = _group.get("floor_space", "")
            if type(_group.get("proportion")) == str:
                _, proportion_str = extract_proportion(_group.get("proportion"))
                if proportion_str:
                    covered_area = float(proportion_str[:-1])
                    proportion_unit = proportion_str[-1]
            else:
                if covered_area == 0 and _group.get("proportion") is not None:
                    covered_area = float(_group.get("proportion", 0))

            if begintime == "" and _group.get("begin_time") is not None:
                begintime = _group.get("begin_time", "")
            if endtime == "" and _group.get("endtime") is not None:
                endtime = _group.get("endtime", "")
            if project_description == "":
                project_description = _group.get("projectDigest", "")
            if project_code == "":
                project_code = _group.get("project_code", "")
            if project_name == "":
                project_name = _group.get("project_name", "")
                high_project_name = project_name
                ordinary_name = project_name
            if _group.get("page_time", "") > page_time:
                page_time = _group.get("page_time", "")
            if project_type == "" and _group.get("industry") is not None:
                project_type = _group.get("industry", "")
            if area == "":
                area = _group.get("city", "")
            if province == "":
                province = _group.get("province", "")
            if city == "":
                city = _group.get("city", "")
            if district == "":
                district = _group.get("district", "")
            if _group.get("latest_service_time", "") != "" and _group.get("latest_service_time") is not None:
                latest_service_time = _group.get("latest_service_time")

            # _follow = "%s-%s"%(_group["stage"],_group["page_time"])
            # if _follow not in set_follows:
            #     list_follows.append({"id":get_guid(),"crtime":getCurrent_date(),"mdate":"%s 00:00:00"%_group["page_time"],
            #                          "progress":_group["stage"],"progress_remark":"截止%s,该项目处于%s"%(_group["page_time"],str(self.getStage(_group.get("stage","")))),
            #                          "version":"跟进%d"%(_version_index)})
            #     set_follows.add(_follow)
            #     project_follow = "跟进%d"%(_version_index)
            #     _version_index += 1

            if _docid is not None and _docid == 0:
                continue
            if "is_completed" in _group or "proportion" in _group:
                continue

            if _docid is not None and _docid != "":
                document_dict = self.getDocumentWithDocid(_docid, ["status"])
                if document_dict is None or (
                        document_dict.get("status", 0) >= 401 and document_dict.get("status", 0) <= 450):
                    continue
            _prop = str(self.getStage(_group.get("stage", "")))
            if _prop != "" and _prop != "None":
                progress = _prop

            # if is_completed:
            #     progress = "竣工阶段"

            _version_index += 1
            _follow = {"id": get_guid(), "crtime": getCurrent_date(), "docid": _docid, "spid": _spid,
                       "mdate": "%s 00:00:00" % follow_time,
                       "docchannel": _docchannel, "doctitle": _doctitle,
                       "progress": _stage, "progress_remark": progress_remark,
                       "version": "跟进%d" % (_version_index)}

            list_follows.append(_follow)
        if page_time == "":
            page_time = update_time[:10]

        if len(list_follows) == 0:
            return None

        list_follows.sort(key=lambda x: datetime.strptime(x.get("mdate"), "%Y-%m-%d %H:%M:%S"))
        page_time = list_follows[-1].get("mdate").split(' ')[0]

        _version_index = 0
        for _follow in list_follows:
            _version_index += 1
            _follow["version"] = "跟进%d" % (_version_index)

        # list_follows = self.getFollows(list_group,legal_contacts,set_enterprise)

        legal_contacts = []
        if is_history:
            legal_contacts, tenderee_count = contacts
        elif is_yeji:
            legal_contacts, tenderee_count = self.getContacts(ots_client, list_group, set_enterprise, legal_contacts, is_yeji=True)
        else:
            legal_contacts, tenderee_count = self.getContacts(ots_client, list_group, set_enterprise, legal_contacts)

        if len(legal_contacts) == 0:
            print('no legal_contacts')
            return None

        des_project_type = "30"
        if len(list_follows) >= 3:
            des_project_type = "10"
        elif tenderee_count >= 1:
            des_project_type = "20"

        if len(list_follows) == 0:
            return None
        # get the key,if exists then search else generate
        id = self.getId(list(set_docid), list(set_spid))
        partitionkey = id % 200 + 1

        # id排序
        docid_list = list(set_docid)
        docid_list.sort(key=lambda x: x)
        sp_id_list = list(set_spid)
        sp_id_list.sort(key=lambda x: x)

        if project_type == "":
            project_type = "其他"
        project_dict = {"partitionkey": partitionkey, "id": id,
                        "crtime": crtime, "floor_space": floor_space, "project_address": project_address,
                        "begintime": begintime, "endtime": endtime, "project_description": project_description,
                        "project_name": project_name, "ordinary_name": ordinary_name,
                        "high_project_name": high_project_name,
                        "project_follow": project_follow, "page_time": page_time, "progress": progress,
                        "contacts": json.dumps(legal_contacts, ensure_ascii=False),
                        "follows": json.dumps(list_follows, ensure_ascii=False),
                        "follow_number": len(list_follows),
                        "docids": ",".join(docid_list), "spids": ",".join(sp_id_list),
                        "des_project_type": des_project_type, "status": status, "covered_area": covered_area,
                        "update_status": update_status, "update_time": update_time, "project_code": project_code,
                        "project_type": project_type, "area": area, "latest_service_time": latest_service_time,
                        "full_text": "%s%s" % (str(project_name), str(project_description)),
                        "json_list_group": self.json_list_group,
                        "province": province, "city": city, "district": district,
                        "project_follow": "跟进%d" % len(list_follows),
                        "project_nature": project_nature,

                        "project_investment": project_investment,
                        "project_structure": project_structure,
                        "facade_type": facade_type,
                        "floors_num": floors_num,
                        "elevator": elevator,
                        "engineer_cost": engineer_cost,
                        "construct_install_fee": construct_install_fee,
                        "facade_type2": facade_type2,
                        "has_steel": has_steel,
                        "proportion_unit": proportion_unit

                        }

        if is_yeji:
            yeji_info_dict = {
                'approval_list': json.dumps(approval_list, ensure_ascii=False),
                'bid_info_list': json.dumps(bid_info_list, ensure_ascii=False),
                'contract_register_list': json.dumps(contract_register_list, ensure_ascii=False),
                'construct_pic_check_list': json.dumps(construct_pic_check_list, ensure_ascii=False),
                'construct_permit_list': json.dumps(construct_permit_list, ensure_ascii=False),
                'completion_accpet_list': json.dumps(completion_accpet_list, ensure_ascii=False),
            }
            project_dict.update(yeji_info_dict)
        return project_dict

    def toDesigned_project(self, ots_client):
        if "json_list_group" not in self.getProperties() or self.json_list_group is None or self.json_list_group == "":
            return None

        list_group = json.loads(self.json_list_group)
        list_group = self.distinct_by_type_id(list_group)
        if not list_group or len(list_group) == 0:
            return None

        # 区分历史数据，历史数据中提前取了contacts
        is_history = False
        contacts = None
        if list_group[0].get('is_history') == 1:
            is_history = True
            contacts = list_group[0].get('contacts')

        # 从库中搜索相同名字拟在建，并合并
        json_list = self.get_json_from_designed_project_by_same_name(list_group[0])
        log(str(self.process_id) + ' ' + str(list_group[0].get('project_name')) + ' len(same_project_list) ' + str(len(json_list)))
        list_group = self.merge_project(list_group, json_list)
        list_group = self.distinct_by_type_id(list_group)
        if not list_group or len(list_group) == 0:
            return None

        # 重新设置去重合并后的json_list_group
        self.json_list_group = json.dumps(list_group, ensure_ascii=False)

        # 是否完工
        is_completed = False
        for _group in list_group:
            if "page_time" not in _group or _group["page_time"] is None:
                _group["page_time"] = ""
            if _group.get("is_completed", 0) == 1:
                is_completed = True

        # 设置字段
        # main_project字段
        main_project = list_group[0]
        none_list = []
        for _key in main_project.keys():
            if main_project.get(_key) is None:
                none_list.append(_key)
        for _key in none_list:
            main_project.pop(_key)

        floor_space = main_project.get('floor_space', '')
        project_address = main_project.get('projectAddress', '')
        begintime = main_project.get('begin_time', '')
        endtime = main_project.get('end_time', '')
        project_description = main_project.get('projectDigest', '')
        project_name = main_project.get('project_name', '')
        high_project_name = project_name
        ordinary_name = project_name
        project_code = main_project.get('project_code', '')
        project_type = main_project.get('industry', '')
        city = main_project.get('city', '')
        area = city
        province = main_project.get('province', '')
        district = main_project.get('district', '')
        latest_service_time = main_project.get('latest_service_time', '')
        project_nature = main_project.get('project_property', '')
        project_structure = main_project.get("structure", '')
        facade_type = main_project.get("wall_type", '')
        facade_type2 = main_project.get("wall_type2", '')
        proportion_unit = main_project.get("proportion_unit", '')
        covered_area = 0.0
        if type(main_project.get('proportion')) == str:
            _, proportion_str = extract_proportion(main_project.get("proportion"))
            if proportion_str:
                covered_area = float(proportion_str[:-1])
                proportion_unit = proportion_str[-1]
        else:
            covered_area = float(main_project.get('proportion', 0.0))
        project_investment = float(main_project.get('total_invest', 0.0))
        engineer_cost = float(main_project.get('engineer_cost', 0.0))
        construct_install_fee = float(main_project.get('construct_install_fee', 0.0))
        floors_num = main_project.get('max_floor', 0)
        elevator = main_project.get('has_elevator', 0)
        has_steel = main_project.get('has_steel', 0)

        # follows字段
        crtime = getCurrent_date()
        _version_index = 1
        progress = ""
        status = "1"
        update_status = "0"
        update_time = getCurrent_date("%Y-%m-%d %H:%M:%S")
        page_time = ""
        set_docid = set()
        set_spid = set()
        set_enterprise = set()
        list_follows = []
        _version_index = 0
        for _group in list_group:
            _docid = _group.get("docid")
            _spid = _group.get("shenpi_id")
            _stage = _group.get("stage")
            follow_time = _group.get("page_time")
            try:
                datetime.strptime(follow_time, "%Y-%m-%d")
            except:
                continue

            _docchannel = _group.get("docchannel")
            _doctitle = _group.get("doctitle")
            progress_remark = ""

            if _docid is not None and _docid > 0:
                set_docid.add(str(_docid))
            if _spid is not None and _spid > 0:
                set_spid.add(str(_spid))

            if _docid is not None and _docid == 0:
                continue
            if "is_completed" in _group or "proportion" in _group:
                continue

            if _docid is not None and _docid != "":
                document_dict = self.getDocumentWithDocid(_docid, ["status"])
                if document_dict is None or (
                        document_dict.get("status", 0) >= 401 and document_dict.get("status", 0) <= 450):
                    continue
            _prop = str(self.getStage(_group.get("stage", "")))
            if _prop != "" and _prop != "None":
                progress = _prop

            _version_index += 1
            _follow = {"id": get_guid(), "crtime": getCurrent_date(), "docid": _docid, "spid": _spid,
                       "mdate": "%s 00:00:00" % follow_time,
                       "docchannel": _docchannel, "doctitle": _doctitle,
                       "progress": _stage, "progress_remark": progress_remark,
                       "version": "跟进%d" % (_version_index)}

            list_follows.append(_follow)
        if page_time == "":
            page_time = update_time[:10]

        if len(list_follows) == 0:
            return None

        list_follows.sort(key=lambda x: datetime.strptime(x.get("mdate"), "%Y-%m-%d %H:%M:%S"))
        page_time = list_follows[-1].get("mdate").split(' ')[0]

        _version_index = 0
        for _follow in list_follows:
            _version_index += 1
            _follow["version"] = "跟进%d" % (_version_index)

        # 设置联系人字段
        legal_contacts = []
        if is_history:
            legal_contacts, tenderee_count = contacts
        else:
            legal_contacts, tenderee_count = self.getContacts(ots_client, list_group, set_enterprise, legal_contacts)

        if len(legal_contacts) == 0:
            print('no legal_contacts')
            return None

        # 根据follow大小设置拟在建的精选和商业
        des_project_type = "30"
        if len(list_follows) >= 3:
            des_project_type = "10"
        elif tenderee_count >= 1:
            des_project_type = "20"

        if len(list_follows) == 0:
            return None

        # 生成拟在建主键，后续若已存在docid、spid则复用那个id
        id = self.getId([], [])
        partitionkey = id % 200 + 1

        # id排序
        docid_list = list(set_docid)
        docid_list.sort(key=lambda x: x)
        sp_id_list = list(set_spid)
        sp_id_list.sort(key=lambda x: x)

        if project_type == "":
            project_type = "其他"

        progress = None
        for follow in list_follows:
            if follow.get('progress'):
                progress = follow.get('progress')
        if not progress:
            print('no progress')
            print(list_follows)
            return None

        project_dict = {"partitionkey": partitionkey,
                        "id": id,
                        "crtime": crtime,
                        "floor_space": floor_space,
                        "project_address": project_address,
                        "begintime": begintime,
                        "endtime": endtime,
                        "project_description": project_description,
                        "project_name": project_name,
                        "ordinary_name": ordinary_name,
                        "high_project_name": high_project_name,
                        "page_time": page_time,
                        "progress": progress,
                        "contacts": json.dumps(legal_contacts, ensure_ascii=False),
                        "follows": json.dumps(list_follows, ensure_ascii=False),
                        "follow_number": len(list_follows),
                        "docids": ",".join(docid_list),
                        "spids": ",".join(sp_id_list),
                        "des_project_type": des_project_type,
                        "status": status,
                        "covered_area": covered_area,
                        "update_status": update_status,
                        "update_time": update_time,
                        "project_code": project_code,
                        "project_type": project_type,
                        "area": area,
                        "latest_service_time": latest_service_time,
                        "full_text": "%s%s" % (str(project_name), str(project_description)),
                        "json_list_group": self.json_list_group,
                        "province": province,
                        "city": city,
                        "district": district,
                        "project_follow": "跟进%d" % len(list_follows),
                        "project_nature": project_nature,
                        "project_investment": project_investment,
                        "project_structure": project_structure,
                        "facade_type": facade_type,
                        "floors_num": floors_num,
                        "elevator": elevator,
                        "engineer_cost": engineer_cost,
                        "construct_install_fee": construct_install_fee,
                        "facade_type2": facade_type2,
                        "has_steel": has_steel,
                        "proportion_unit": proportion_unit

                        }

        return project_dict

    def to_designed_project_online(self, ots_client):
        if "json_list_group" not in self.getProperties() or self.json_list_group is None or self.json_list_group == "":
            return None

        list_group = json.loads(self.json_list_group)
        list_group = self.distinct_by_type_id(list_group)
        if not list_group or len(list_group) == 0:
            return None

        # 区分历史数据，历史数据中提前取了contacts
        is_history = False
        contacts = None
        if list_group[0].get('is_history') == 1:
            is_history = True
            contacts = list_group[0].get('contacts')

        # 从库中搜索相同名字拟在建，并合并
        json_list = self.get_json_from_designed_project_by_same_name(list_group[0])
        log(str(self.process_id) + ' ' + str(list_group[0].get('project_name')) + ' len(same_project_list) ' + str(len(json_list)))
        list_group = self.merge_project(list_group, json_list)
        list_group = self.distinct_by_type_id(list_group)
        if not list_group or len(list_group) == 0:
            return None

        # 根据type_id去重
        # g_dict = {}
        # main_project = list_group[0]
        # for g in list_group[1:]:
        #     type_id = g.get('docid')
        #     if type_id is None:
        #         type_id = -g.get('spid')
        #     if type_id is None:
        #         continue
        #     if type_id not in g_dict.keys():
        #         g_dict[type_id] = g
        # list_group = [main_project] + [g_dict.get(x) for x in g_dict]

        # 重新设置去重合并后的json_list_group
        self.json_list_group = json.dumps(list_group, ensure_ascii=False)

        # 是否完工
        is_completed = False
        for _group in list_group:
            if "page_time" not in _group or _group["page_time"] is None:
                _group["page_time"] = ""
            if _group.get("is_completed", 0) == 1:
                is_completed = True

        # 设置字段
        # main_project字段
        main_project = list_group[0]
        none_list = []
        for _key in main_project.keys():
            if main_project.get(_key) is None:
                none_list.append(_key)
        for _key in none_list:
            main_project.pop(_key)

        floor_space = main_project.get('floor_space', '')
        project_address = main_project.get('projectAddress', '')
        begintime = main_project.get('begin_time', '')
        endtime = main_project.get('end_time', '')
        project_description = main_project.get('projectDigest', '')
        project_name = main_project.get('project_name', '')
        high_project_name = project_name
        ordinary_name = project_name
        project_code = main_project.get('project_code', '')
        project_type = main_project.get('industry', '')
        city = main_project.get('city', '')
        area = city
        province = main_project.get('province', '')
        district = main_project.get('district', '')
        latest_service_time = main_project.get('latest_service_time', '')
        project_nature = main_project.get('project_property', '')
        project_structure = main_project.get("structure", '')
        facade_type = main_project.get("wall_type", '')
        facade_type2 = main_project.get("wall_type2", '')
        proportion_unit = main_project.get("proportion_unit", '')
        covered_area = 0.0
        if type(main_project.get('proportion')) == str:
            _, proportion_str = extract_proportion(main_project.get("proportion"))
            if proportion_str:
                covered_area = float(proportion_str[:-1])
                proportion_unit = proportion_str[-1]
        else:
            covered_area = float(main_project.get('proportion', 0.0))
        project_investment = float(main_project.get('total_invest', 0.0))
        engineer_cost = float(main_project.get('engineer_cost', 0.0))
        construct_install_fee = float(main_project.get('construct_install_fee', 0.0))
        floors_num = main_project.get('max_floor', 0)
        elevator = main_project.get('has_elevator', 0)
        has_steel = main_project.get('has_steel', 0)

        # follows字段
        crtime = getCurrent_date()
        _version_index = 1
        progress = ""
        status = "1"
        update_status = "0"
        update_time = getCurrent_date("%Y-%m-%d %H:%M:%S")
        page_time = ""
        set_docid = set()
        set_spid = set()
        set_enterprise = set()
        list_follows = []
        _version_index = 0
        for _group in list_group:
            _docid = _group.get("docid")
            _spid = _group.get("shenpi_id")
            _stage = _group.get("stage")
            follow_time = _group.get("page_time")
            try:
                datetime.strptime(follow_time, "%Y-%m-%d")
            except:
                continue

            _docchannel = _group.get("docchannel")
            _doctitle = _group.get("doctitle")
            progress_remark = ""

            if _docid is not None and _docid > 0:
                set_docid.add(str(_docid))
            if _spid is not None and _spid > 0:
                set_spid.add(str(_spid))

            if _docid is not None and _docid == 0:
                continue
            if "is_completed" in _group or "proportion" in _group:
                continue

            if _docid is not None and _docid != "":
                document_dict = self.getDocumentWithDocid(_docid, ["status"])
                if document_dict is None or (
                        document_dict.get("status", 0) >= 401 and document_dict.get("status", 0) <= 450):
                    continue
            _prop = str(self.getStage(_group.get("stage", "")))
            if _prop != "" and _prop != "None":
                progress = _prop

            _version_index += 1
            _follow = {"id": get_guid(), "crtime": getCurrent_date(), "docid": _docid, "spid": _spid,
                       "mdate": "%s 00:00:00" % follow_time,
                       "docchannel": _docchannel, "doctitle": _doctitle,
                       "progress": _stage, "progress_remark": progress_remark,
                       "version": "跟进%d" % (_version_index)}

            list_follows.append(_follow)
        if page_time == "":
            page_time = update_time[:10]

        if len(list_follows) == 0:
            return None

        list_follows.sort(key=lambda x: datetime.strptime(x.get("mdate"), "%Y-%m-%d %H:%M:%S"))
        page_time = list_follows[-1].get("mdate").split(' ')[0]

        _version_index = 0
        for _follow in list_follows:
            _version_index += 1
            _follow["version"] = "跟进%d" % (_version_index)

        # 设置联系人字段
        legal_contacts = []
        if is_history:
            legal_contacts, tenderee_count = contacts
        else:
            legal_contacts, tenderee_count = self.getContacts(ots_client, list_group, set_enterprise, legal_contacts)

        if len(legal_contacts) == 0:
            print('no legal_contacts')
            return None

        # 根据follow大小设置拟在建的精选和商业
        des_project_type = "30"
        if len(list_follows) >= 3:
            des_project_type = "10"
        elif tenderee_count >= 1:
            des_project_type = "20"

        if len(list_follows) == 0:
            return None

        # 生成拟在建主键，后续若已存在docid、spid则复用那个id
        id = self.getId([], [])
        partitionkey = id % 200 + 1

        # id排序
        docid_list = list(set_docid)
        docid_list.sort(key=lambda x: x)
        sp_id_list = list(set_spid)
        sp_id_list.sort(key=lambda x: x)

        if project_type == "":
            project_type = "其他"

        progress = None
        for follow in list_follows:
            if follow.get('progress'):
                progress = follow.get('progress')
        if not progress:
            return None

        project_dict = {"partitionkey": partitionkey,
                        "id": id,
                        "crtime": crtime,
                        "floor_space": floor_space,
                        "project_address": project_address,
                        "begintime": begintime,
                        "endtime": endtime,
                        "project_description": project_description,
                        "project_name": project_name,
                        "ordinary_name": ordinary_name,
                        "high_project_name": high_project_name,
                        "page_time": page_time,
                        "progress": progress,
                        "contacts": json.dumps(legal_contacts, ensure_ascii=False),
                        "follows": json.dumps(list_follows, ensure_ascii=False),
                        "follow_number": len(list_follows),
                        "docids": ",".join(docid_list),
                        "spids": ",".join(sp_id_list),
                        "des_project_type": des_project_type,
                        "status": status,
                        "covered_area": covered_area,
                        "update_status": update_status,
                        "update_time": update_time,
                        "project_code": project_code,
                        "project_type": project_type,
                        "area": area,
                        "latest_service_time": latest_service_time,
                        "full_text": "%s%s" % (str(project_name), str(project_description)),
                        "json_list_group": self.json_list_group,
                        "province": province,
                        "city": city,
                        "district": district,
                        "project_follow": "跟进%d" % len(list_follows),
                        "project_nature": project_nature,
                        "project_investment": project_investment,
                        "project_structure": project_structure,
                        "facade_type": facade_type,
                        "floors_num": floors_num,
                        "elevator": elevator,
                        "engineer_cost": engineer_cost,
                        "construct_install_fee": construct_install_fee,
                        "facade_type2": facade_type2,
                        "has_steel": has_steel,
                        "proportion_unit": proportion_unit

                        }

        return project_dict


def extract_proportion(content, has_preffix=True):
    if not content:
        return "", ""
    # log("content")
    # log(content)
    suffix = "[大概约为是:：【\[\s]*[\d,]+(\.\d+)?[十百千万亿]*([\]】平方kK千万公㎡mM米里顷亩]+2?))"
    reg_dict = {
        0: "(?P<proportion>(总((建筑|建设)面积|长|长度))" + suffix,
        1: "(?P<proportion>((建筑|建设)面积|全长)" + suffix,
        2: "(?P<proportion>((建筑|建设|区域)?面积|全长|项目规模)" + suffix
    }

    if not has_preffix:
        reg_dict[3] = "(?P<proportion>" + suffix

    _proportion = ""
    for i in range(len(list(reg_dict.keys()))):
        if _proportion:
            break
        _pattern = reg_dict.get(i)
        # logging.info('content ' + str(content))
        match = re.search(_pattern, str(content))
        if match:
            _proportion = match.groupdict().get("proportion","")

    if not _proportion:
        return "", ""

    # 统一格式
    multiple_cnt = 1
    digit = ""

    # 确定具体数字
    match = re.search('(?P<d1>[\d,]+)(?P<d2>(\.\d+)?)', _proportion)
    if match:
        # logging.info(str(_proportion) + '  ' + str(match.group()))
        d1 = match.group('d1')
        d2 = match.group('d2')
        try:
            d1 = int(re.sub(',', '', d1))
        except:
            return "", ""
        if d2:
            d2 = Decimal(d2[1:]) / Decimal(str(int(10 ** len(d2[1:]))))
            # print('d1, d2', d1, d2)
            d1 += d2
        digit = d1
    # print('digit', digit)

    # 确定中文倍数
    _proportion2 = re.sub(re.escape(match.group()), '', _proportion)
    match = re.search('[十百千万亿]+', _proportion2)
    _dict = {'十': 10, '百': 100, '千': 1000, '万': 10000, '亿': 100000000}
    if match:
        for c in match.group():
            multiple_cnt *= _dict.get(c)
        _proportion3 = re.sub(re.escape(match.group()), '', _proportion2)
    else:
        _proportion3 = _proportion2
    # print('multiple_cnt2', multiple_cnt)

    # 确定面积/长度
    match = re.search('[平方㎡顷亩]+|[mM]2', _proportion3)
    if match:
        unit = '㎡'
    else:
        unit = 'm'

    # 确定单位倍数
    match = re.search('[平方kK千万公㎡mM米里顷亩]+2?', _proportion3)
    if match:
        if unit == 'm':
            if re.search('[kK千公]', match.group()):
                multiple_cnt *= 1000
            elif re.search('[里]', match.group()):
                multiple_cnt *= Decimal(str(500))
        else:
            if '亩' in match.group():
                multiple_cnt *= Decimal(str(666.67))
            elif '顷' in match.group():
                multiple_cnt *= 10000
            elif re.search('千米|公里|k[mM㎡]', match.group()):
                multiple_cnt *= 1000000
    # print('multiple_cnt1', multiple_cnt)

    # 拼接
    digit = str(digit * multiple_cnt) + unit

    return _proportion, digit


if __name__ == "__main__":
    s = '''
[{"location": "湖南省广坪河靖州县", "area": "华中", "type_id": 0, "tenderee": "靖州苗族侗族自治县水利水电建设管理站", "pb_project_name": "湖南省广坪河靖州县治理", "province": "湖南", "has_elevator": 0, "shenpi_id": 0, "page_time": "2024-08-07", "agency": "湖南云舟项目管理有限公司", "proportion": null, "project_property": "新建", "project_name_refind": "湖南省广坪河靖州县治理", "city": "怀化", "projectDigest": "项目信息，项目名称：湖南省广坪河靖州县治理工程项目初步设计辅助性服务采购项目编号：241BE0246943，招标段/包，标段/包名称：湖南省广坪河靖州县治理工程项目初步设计辅助性服务采购标段/包编号：241BE0246943/01，成交内容，公示时间：2024-08-06 18:04:00，成交内容：湖南省广坪河靖州县治理工程项目初步设计辅助性服务，特殊事项说明：附件：", "project_code": "H431200002410", "project_name": "湖南省广坪河靖州县治理", "industry": "电力工程", "total_invest": 98000000.0, "district": "靖州苗族侗族自治", "doctitle": "湖南省广坪河靖州县治理工程项目初步设计", "partitionkey": 456, "docchannel": 52, "stage": "设计阶段", "proportion_unit": null, "follow_enough": 1, "new_enough": 1, "show_name_refind": "湖南省广坪河靖州县治理", "projectdigest": "项目信息，项目名称：湖南省广坪河靖州县治理工程项目初步设计，项目编号：H431200002410，项目所在行政区：431229，项目行业分类：水利工程，资金来源：投资项目统一代码：2309-431229-04-01-261657，招标项目信息，招标项目编号：H431200002410001，招标项目名称：湖南省广坪河靖州县治理工程项目初步设计，招标项目类型：水利工程，项目业主名称：靖州苗族侗族自治县水利水电建设管理站，招标人名称：靖州苗族侗族自治县水利水电建设管理站，招标代理机构名称：湖南云舟项目管理有限公司，招标方式：公开招标，招标组织形式：委托招标，招标内容与范围：湖南省广坪河靖州县治理工程项目初", "main": 1, "docid": 0, "has_steel": 0}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": 377497794, "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 377497794, "page_time": "2023-11-07", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": "389148035", "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 389148035, "page_time": "2023-11-07", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": "377497794", "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 377497794, "page_time": "2023-11-07", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": "377515320", "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 377515320, "page_time": "2023-11-07", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": "393830455", "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 393830455, "page_time": "2023-11-07", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": 393830455, "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 393830455, "page_time": "2023-11-07", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": 377515320, "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 377515320, "page_time": "2023-11-07", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": 385080781, "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 385080781, "page_time": "2023-11-27", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": "385080781", "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 385080781, "page_time": "2023-11-27", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": "385368406", "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 385368406, "page_time": "2023-11-28", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": 385368406, "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 385368406, "page_time": "2023-11-28", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": "393831140", "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 393831140, "page_time": "2023-11-28", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": 393831140, "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 393831140, "page_time": "2023-11-28", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": 391264178, "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 391264178, "page_time": "2023-11-28", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": "391264178", "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 391264178, "page_time": "2023-11-28", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": 394163150, "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 394163150, "page_time": "2023-12-14", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": "391098602", "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 391098602, "page_time": "2023-12-14", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": 391098602, "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 391098602, "page_time": "2023-12-14", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": "394163150", "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 394163150, "page_time": "2023-12-14", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": 391681029, "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 391681029, "page_time": "2023-12-15", "shenpi_id": null, "section": null}, {"stage": "勘察阶段", "location": "湖南省广坪河靖州县", "type_id": 391694289, "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 391694289, "page_time": "2023-12-15", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": "391681029", "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 391681029, "page_time": "2023-12-15", "shenpi_id": null, "section": null}, {"stage": "勘察阶段", "location": "湖南省广坪河靖州县", "type_id": 392275988, "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 392275988, "page_time": "2023-12-18", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": 392313251, "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 392313251, "page_time": "2023-12-18", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": "392313251", "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 392313251, "page_time": "2023-12-18", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": 393528001, "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 393528001, "page_time": "2023-12-21", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": "393528001", "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 393528001, "page_time": "2023-12-21", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": 513857760, "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 513857760, "page_time": "2024-08-07", "shenpi_id": null, "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": 513880720, "follow_enough": 1, "docid": 513880720, "new_enough": 0, "phase": null, "shenpi_id": null, "page_time": "2024-08-07", "section": null}, {"stage": "设计阶段", "location": "湖南省广坪河靖州县", "type_id": 513859316, "follow_enough": 1, "new_enough": 0, "phase": null, "docid": 513859316, "page_time": "2024-08-07", "shenpi_id": null, "section": null}]
'''
    print('len(json.loads(s))', len(json.loads(s)))
    a = proposedBuilding_tmp({"json_list_group": s})
    _project_dict = a.toDesigned_project(ots_client)
    # start_time = time.time()
    # _project_dict = a.toDesigned_project(ots_client)

    if _project_dict:
        for key in _project_dict.keys():
            print('result', key, str(_project_dict.get(key))[:20])

    # print(json.dumps(_project_dict, ensure_ascii=False))
    # print('len(json.loads(s))', len(json.loads(s)))
    # print('len(json.loads(s1))', len(json.loads(_project_dict.get('json_list_group'))))
    with open('pb_result.txt', 'w', encoding='gbk') as f:
        f.write(_project_dict.get('json_list_group'))
    # with open('pb_result.txt', 'w', encoding='gbk') as f:
    #     _project_dict['follows'] = json.loads(_project_dict.get('follows', ''))
    #     _project_dict['contacts'] = json.loads(_project_dict.get('contacts', ''))
    #     _project_dict['json_list_group'] = json.loads(_project_dict.get('json_list_group', ''))
    #     _project_dict['approval_list'] = json.loads(_project_dict.get('approval_list', json.dumps([])))
    #     _project_dict['bid_info_list'] = json.loads(_project_dict.get('bid_info_list', json.dumps([])))
    #     _project_dict['contract_register_list'] = json.loads(_project_dict.get('contract_register_list', json.dumps([])))
    #     _project_dict['construct_pic_check_list'] = json.loads(_project_dict.get('construct_pic_check_list', json.dumps([])))
    #     _project_dict['construct_permit_list'] = json.loads(_project_dict.get('construct_permit_list', json.dumps([])))
    #     _project_dict['completion_accpet_list'] = json.loads(_project_dict.get('completion_accpet_list', json.dumps([])))
    #
    #     f.write(json.dumps(_project_dict, ensure_ascii=False))
    # print(time.time()-start_time)
    # from BaseDataMaintenance.model.ots.designed_project import designed_project
    # _time = time.time()
    # if _project_dict is not None:
    #     #更新数据
    #     # log("project_dict:"+json.dumps(_project_dict))
    #     _designed_project = designed_project(_project_dict)
    #     _designed_project.update_project(ots_client)
    #     print("updated")
