import pypinyin
import json
import pymysql
import hashlib

print('runing........')

host_name = "127.0.0.1"
user_name = "root"
password = "keji178"
database_name= "accountdb" + "1"
table_name = "accounts"

selected_count = 0


database_config = {
    'host_name' : host_name,
    'user_name' : user_name,
    'password'  : password,
    'database_name' : database_name

}

select_config = [
    'country',
    'province',
    'city',
    'sex',
    'nickname',
    'idx',
    'accountid'
]


def getDatabaseCursor(config):
    #print('getDatabaseCursor')

    db = pymysql.connect(config['host_name'],config['user_name'],config['password'],config['database_name'])
    return db.cursor()



def getSelectInfo(min_idx, max_idx, s_config):
    #print('getSelectInfo')

    try:
        cursor = getDatabaseCursor(s_config)
        select_sql = 'select  country, province, city, sex, nickname , idx, accountid \
        from accounts where idx > %s and idx <= %s and accountid is not null' % (min_idx,max_idx)
        #print(select_sql)
        cursor.execute(select_sql)
        ret = cursor.fetchall()
    except:
        print('sql error!')
        return

    return ret


def saveTofile(save_info, save_name):
    #print('saveTofile')

    if save_info and save_name :
        with open(save_name,'a') as f:
            f.write("%s\n"%(save_info))
    return

def saveJsonToFile(save_info, save_name):

    if save_info and save_name:
        with open(save_name, 'w') as file_obj:
            json.dump(save_info, file_obj)
    return


def createNewDict(old_file_name):

    with open("city.json",'r') as load_f:
        load_dict = json.load(load_f)

    new_dict = {}
    for one_dict_info in load_dict:
        if one_dict_info:
            new_dict[one_dict_info['pinyin']] = one_dict_info['name']
    #print(new_dict)

    save_name = 'new_city.josn'
    saveJsonToFile(new_dict, save_name)


    return



def buildWid(databaseName):

    global selected_count
    selected_count = 0
    database_config['database_name'] = databaseName
    select_min_idx = 10000 + selected_count
    select_max_idx = select_min_idx + 10

    finsh_flag = 1;
    select_count = 0;
    save_file_name = 'updata_wid_test.txt'


    while select_count < 1000 and finsh_flag:
        #print(select_count)

        min_idx = select_min_idx + select_count * 10
        max_idx = select_max_idx + select_count * 10

        select_info = getSelectInfo(min_idx,max_idx,database_config)



        if select_info:
            #print(len(select_info))
            for player_info in select_info:

                country = player_info[0]
                province = player_info[1]
                city = player_info[2]
                sex = player_info[3]
                nickname = player_info[4]
                idx = player_info[5]
                accountid = player_info[6]

                nickname_string = ''
                if nickname == '' or not nickname:
                    continue

                if nickname:
                    nickname_string=str(nickname,'utf-8')

                str1 = '%s%s%s%s'%(country, province, city, sex)
                new_str = str1 + nickname_string
                md5_str = translateToMd5(new_str)
                update_sql = "UPDATE %s SET wid = '%s' where accountid = '%s';"%(table_name,md5_str,accountid)
                saveTofile(update_sql,save_file_name)

                selected_count+= 1
        else:
            print('finsh_flag')
            finsh_flag = 0
        select_count += 1;

    print('buidWid END!')
    return



def pinyinToChinese(word):

    s = ''


    return s

print(pypinyin.pinyin('你好'))

list1 = ['shang','hai']



def pinyin_2_hanzi(pinyinList):
    from Pinyin2Hanzi import DefaultDagParams
    from Pinyin2Hanzi import dag
    dagParams = DefaultDagParams()
    result = dag(dagParams, pinyinList, path_num=1, log=True)#10代表侯选值个数
    for item in result:
        socre = item.score
        res = item.path # 转换结果
        print(socre, res)

if __name__ == '__main__':

    list2 = ['shanghai']

    '''
    for line in open('123.txt', 'r').readlines():
        line = line.strip('[')
        line = line.rstrip()
        line = line.strip(']')
        line = line.replace("'", "")
        entries = line.split(',')
        # print((entries))
        # print(type(entries))
        entries = entries[:-1]
        # print(entries)
        pin = []
        for i in range(len(entries)):
            entries[i] = entries[i].replace(' ', '')
            pin.append(entries[i])
        #print(pin)

    '''


    #pinyin_2_hanzi(list2)


#createNewDict('new_city.json')
