import shutil

import pymysql
import os

dl_paths = [
    'G:/cmip6',
    'H:/cmip6',
    'I:/cmip6',
    'J:/cmip6'
]

if __name__ == '__main__':
    db = pymysql.connect(host='39.101.194.63', port=23306,
                         user='root', passwd='passok123A', db='nfm', charset='utf8mb4')
    cursor = db.cursor()
    SQL_SELECT = "SELECT FILE_NAME, DATA_NODE FROM jd_data.cmip6_full_url WHERE downloaded = 1"
    cursor.execute(SQL_SELECT)
    dl_dict = {}
    tps = cursor.fetchall()
    db.close()
    for tp in tps:
        dl_dict[tp[0]] = tp[1]
    for dl_path in dl_paths:
        dl_file_names = os.listdir(dl_path)
        for file_name in dl_file_names:
            if file_name.endswith('.nc'):
                if file_name in dl_dict:
                    target_path = dl_dict[file_name]
                    target_full_path = dl_path + '/' + target_path
                    if not os.path.exists(target_full_path):
                        os.mkdir(target_full_path)
                    file_name_source_full_path = dl_path + '/' + file_name
                    shutil.move(file_name_source_full_path, target_full_path)
                    print(f"移动文件 {file_name} 到 {target_full_path}")
