import os
import time

import pymysql

target_paths = ['F:/jd_data', 'G:/jd_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()
    count = 0
    for target_path in target_paths:
        for file_name in os.listdir(target_path):
            count += 1
            hierarchy = file_name.replace(".zip", "").split(" + ")
            experiment = hierarchy[0]
            variable = hierarchy[1]
            model = hierarchy[2]
            sql_select = "SELECT id, experiment, variable, model, level, data_size, downloaded_time " \
                         "FROM jd_data.data_detail " \
                         "WHERE experiment = '{}' AND variable = '{}' AND model = '{}'".format(experiment, variable, model)
            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]
                downloaded_time = row[6]
                f_size = os.path.getsize("/".join([target_path, file_name]))
                if f_size >= data_size:
                    print("[No. {}] {}校验通过，文件大小符合".format(str(count), file_name))
                    if f_size != data_size:
                        print("-------------------- id: {}, f_size={}, data_size={} --------------------".format(row_id, f_size, data_size))
                    # if downloaded_time is None:
                    #     f_mtime = os.path.getmtime("/".join([target_path, file_name]))
                    #     f_mtime_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(f_mtime))
                    #     sql_update = "UPDATE jd_data.data_detail " \
                    #                  "SET downloaded_time = '{}' WHERE id = '{}';".format(f_mtime_str, row_id)
                    #     cursor.execute(sql_update)
                    #     db.commit()
                else:
                    print("{}校验不通过，文件大小不够".format(file_name))
                    sql_update = "UPDATE jd_data.data_detail " \
                                 "SET downloaded = 0, downloaded_time = null WHERE id = '{}';".format(row_id)
                    cursor.execute(sql_update)
                    db.commit()

    db.close()