def writeToDict(data):
    #must after load data  转变为词典行数据
    new_data=[]
    #001005  6888   www.axiata.com  Axiata Group Berhad Axiata Group Berhad Axiata  None
    for item in data:
        tup = item.split('\t')
        type=tup[0]
        if type == "ASTOCK":
            type = "_a股"
        elif type == "HKSTOCK":
            type = "_港股"
        elif type == "USSTOCK":
            type = "_美股"
        sitename=tup[1]
        aliass=tup[2].split('#')
        url=tup[3]
        # #同花顺/*Cate=pretreat;Seg=;Value=onto_value:prop=_股票简称;infos=id:300033;$
        if re.match(r"\d+$", sitename.lower()) and True or False:
            new_data.append(GetOneDictLine.getOnePostDictLineWithId(sitename,type,sitename.lower()))
        else:
            new_data.append(GetOneDictLine.getOneDictLineWithId(sitename,type,sitename.lower()))
        for alias in aliass:
            if alias != "":
                if re.match(r"\d+$", alias.lower()) and True or False:
                    new_data.append(GetOneDictLine.getOnePostDictLineWithId(alias,type,sitename.lower()))
                else:
                    new_data.append(GetOneDictLine.getOneDictLineWithId(alias,type,sitename.lower()))
    
    
    is_update = False
    is_update =  FindTheChangedPart.run(new_data,setting.SITE_NAME_FILE)
    return is_update
def getTrustProduct():
    
    new_data = []
    typeMap = {"银行理财产品":"_理财产品", "信托":"_信托产品"}
    sql = """
    select SEQ id,f003v_pub205,F016V_PUB205 full_name,SECNAME_PUB205 short_name
    from PUB205 
    where f003v_pub205 in ('银行理财产品','信托')
    """
    data = getData(sql)
    if data is None:
        failExit("access postgresql error!")        
    for item in data:
        cols=item.split('\t');
        id = ''
        type = ''
        for col in cols:
            if(col == cols[0]):
                id = col
                continue
            elif(col == cols[1]):
                type = typeMap[col]
                continue
            elif(id != '' and col != ''):
                new_data.append(GetOneDictLine.getOneDictLineWithId(col,type,id));  
    return new_data   
def changeToDictLine(dictType,data,from_where):
    new_data=[]
    for line in data:
        cols = line.split('\t')
        id = ''
        for col in cols:
            if(col == cols[0]):
                id = col
                if dictType=="_上证a股代码" or dictType=="_深证a股代码":
                    new_data.append(GetOneDictLine.getOneDictLineWithId(col,dictType,cols[0],""));
                continue
            elif(col != ''):
                if(id != ''):
                    new_data.append(GetOneDictLine.getOneDictLineWithId(col,dictType,id,from_where));  
                else:
                    new_data.append(GetOneDictLine.getOneDictLine(col,dictType,from_where));     
    return new_data
def getBusinessNameFromSql():
    words = []
    type = "_业务名称"
    sql = """
    select distinct F002 from stk039
    where isvalid = '1'
    and f001 = '2'
    """
    data = getData(sql)
    if data is None:
        failExit("access postgresql error!")
    for item in data:
        if item.find('(') < 0 and item.find('(') < 0:
            for subProduct in re.split(",|,|;|;|、", item):
                words.append(subProduct.strip())
                #print subProduct.strip()
        else:
            words.append(item.strip())
            #print item.strip()


    sql = """
    select distinct products from
    (
      select f001V_stk219 as products, 
                   t1.declaredate_stk219 as valid_date,
                                max(t1.declaredate_stk219) over (partition by t1.orgid_stk219) as latest
                                    from STK219 t1 join stk001 t2 on t2.comcode = t1.ORGID_STK219 
                                    where t1.isvalid = 1 and t2.isvalid = 1 and f001V_stk219 not like '%B、C、D%'
    ) t where valid_date = latest """

    data = getData(sql)
    if data is None:
        failExit("access postgresql error!")
    for item in data:
        item = item.replace('  ', '')
        item = item.replace('  ', '')
        if item.find('(') < 0 and item.find('(') < 0:
            for product in re.split(",|,|;|;|、", item):
                words.append(product.strip())
                #print product.strip()
        else:
            for product in item.strip().split('、'):
                if product.find('(') < 0 and product.find('(') < 0:
                    for subProduct in re.split(",|,|;|;|、", product):
                        words.append(subProduct.strip())
                        #print subProduct.strip()
                else:
                    words.append(product.strip())
                    #print product.strip()
                 
    new_data = []                    
    for item in words:
        if item != "" and not isDigit(item.lower()):
             new_data.append(GetOneDictLine.getOneDictLine(item,type)) 
    return new_data
def getSecuritiesBusiness():
    new_data = []
    type="_研究机构"
    sql = """
    SELECT Y.ORGID_YB001 "id",P.ORGNAME_PUB203 "name",p.f022v_pub203 "short_name" FROM YB001 Y,PUB203 P 
    WHERE Y.ORGID_YB001 = P.ORGID_PUB203 
    AND Y.ISVALID = 1 
    AND P.ISVALID = 1
    """
    data = getData(sql)
    if data is None:
        failExit("access postgresql error!")        
    for line in data:
        cols=line.split('\t');
        if len(cols)==3:
            if cols[1] != '':
                new_data.append(GetOneDictLine.getOneDictLineWithId(cols[1],type,cols[0])); 
            if cols[2] != '':    
                new_data.append(GetOneDictLine.getOneDictLineWithId(cols[2],type,cols[0])); 
    return new_data
def getAnalystName():
    new_data = []
    type="_分析师姓名"
    sql = """
    select distinct t.f003v_yb002 from yb002 t where t.f003v_yb002 != ''
    """
    data = getData(sql)
    if data is None:
        failExit("access postgresql error!")        
    for line in data:
        new_data.append(GetOneDictLine.getOneDictLine(line,type));  
    return new_data
def dealWithConcept():
    mysqlClient = MySqlClient.from_setting(setting.MYSQL_SERVERS["52"])
    type = "_所属概念"
    sql = """
    SELECT t.id, t.`name` FROM `concept_list` t 
    """
    data = mysqlClient.readlines(mysqlClient.conn, sql)
    if data is None:
        return False
    
    new_data = []
    for item in data:
        tup = item.split('\t')
        if len(tup) == 2 :
            new_data.append(GetOneDictLine.getOneDictLineWithId(tup[1],type,tup[0].lower()))
            
    return FindTheChangedPart.run(new_data,setting.STOCK_CONCEPT_FILE)