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 translateToMd5(translate_info):
    #print('translateTomd5')

    if translate_info:
        m = hashlib.md5()
        b = translate_info.encode(encoding = 'utf-8')
        m.update(b)
        return m.hexdigest()
    return


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 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


buildWid("accountdb_bk1")
buildWid("accountdb_bk2")
buildWid("accountdb_bk3")
buildWid("accountdb_bk4")
buildWid("accountdb_bk5")
buildWid("accountdb_bk6")
buildWid("accountdb_bk7")
buildWid("accountdb_bk8")
buildWid("accountdb_bk9")
buildWid("accountdb_bk10")
buildWid("accountdb_bk11")
buildWid("accountdb_bk12")
buildWid("accountdb_bk13")
buildWid("accountdb_bk14")
buildWid("accountdb_bk15")
buildWid("accountdb_bk16")
buildWid("accountdb_bk17")
buildWid("accountdb_bk18")
buildWid("accountdb_bk19")
buildWid("accountdb_bk20")
