import os
import pymysql

target_paths = ['F:/jd_fdm_data', 'G:/jd_fdm_data']

if __name__ == "__main__":
    db = pymysql.connect(host='39.101.194.63', port=23306,
                         user='root', passwd='passok123A', db='jd_data', charset='utf8mb4')
    cursor = db.cursor()
    for target_path in target_paths:
        for file_name in os.listdir(target_path):
            sql_select = "SELECT id, experiment, variable, model, level, data_size FROM jd_data.data_detail " \
                         "WHERE url LIKE '%{}';".format(file_name)
            cursor.execute(sql_select)
            result = cursor.fetchall()
            total_num = len(result)
            if total_num == 1:
                row = result[0]
                row_id = row[0]
                experiment = row[1]
                variable = row[2]
                model = row[3]
                level = row[4]
                data_size = row[5]
                if model == 'miroc_es2h':
                    level = '1850'
                new_name = "{} + {} + {} + {}".format(experiment, variable, model, level)
                print("文件{}对应的表记录为：{}".format(file_name, new_name))
                os.rename("/".join([target_path, file_name]), "/".join([target_path, new_name]) + ".zip")
            else:
                print("未找到文件{}对应的表记录".format(file_name))
    db.close()
