
from BaseDataMaintenance.model.ots.BaseModel import BaseModel
import json
from BaseDataMaintenance.common.Utils import log
import traceback


ENTERPRISE_NAME = "name"
ENTERPRISE_STATUS = "status"
ENTERPRISE_bid_number = "bid_number"
ENTERPRISE_tyc_id = "tyc_id"

class Enterprise(BaseModel):

    def __init__(self,_dict):

        for k,v in _dict.items():
            self.setValue(k,v,True)
        self.table_name = "enterprise"

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


class f_dumplicate_contacts():

    def __init__(self):
        pass

    def process(self,name,contacts):
        if contacts is None:
            return(contacts,1)

        try:
            list_contacts = json.loads(contacts)
            _set = set()
            _phone_set = set()
            new_list_contacts = []
            list_contacts.sort(key=lambda x:len(x.get("contact_person","")),reverse=True)
            for _conta in list_contacts:
                contact_person = _conta.get("contact_person","")
                mobile_no = _conta.get("mobile_no","")
                phone_no = _conta.get("phone_no","")
                if contact_person=="" and (mobile_no in _phone_set or phone_no in _phone_set):
                    continue
                _key = "%s-%s-%s"%(contact_person,mobile_no,phone_no)
                if _key in _set:
                    continue
                if mobile_no!="":
                    _phone_set.add(mobile_no)
                if phone_no!="":
                    _phone_set.add(phone_no)
                new_list_contacts.append(_conta)
                _set.add(_key)
            if len(new_list_contacts)!=len(list_contacts):
                log(name)
            new_list_contacts.sort(key=lambda x:x.get("level",0),reverse=True)
            return(json.dumps(new_list_contacts,ensure_ascii=False),1)
        except Exception as e:
            traceback.print_exc()
            log(contacts)
            return(None,0)

import re
from BaseDataMaintenance.dataSource.source import getConnect_ots
def dumplicate_contacts():
    str_names = '''
    深圳市国际招标有限公司
    中国人民解放军
    永明项目管理有限公司
    中招国际招标有限公司
    中科高盛咨询集团有限公司
    中航技国际经贸发展有限公司
    中化商务有限公司
    亿诚建设项目管理有限公司
    中国石化国际事业有限公司
    公诚管理咨询有限公司
    '''
    list_names = re.split("[\s\n]",str_names)
    ots_client = getConnect_ots()
    dc = f_dumplicate_contacts()
    for _name in list_names:
        if _name.strip()=="":
            continue
        _dict = {"name":_name}
        _enterp = Enterprise(_dict)
        _enterp.fix_columns(ots_client,["contacts"],True)
        # print(_enterp.getProperties())
        new_contacts,success = dc.process(_dict.get("name"),_enterp.getProperties().get("contacts"))
        print(success)
        # print(new_contacts)
        break


if __name__=="__main__":
    dumplicate_contacts()
    pass

