package com.zc.business.controller;

import com.ruoyi.common.config.RuoYiConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * 数据库定时备份任务
 * 在backup文件夹中备份最近七日的数据库文件, 备份文件夹 与当前程序同一目录
 */
@Component
@Slf4j
public class DcMYSQLJob {
    //private final Environment environment;

    private static final String IP = "10.0.81.202";

    private static final String PORT = "3306";

    private static final String USER = "root";

    private static final String PASSWORD = "mysql123!@#";

    //public DcMYSQLJob(Environment environment) {
    //    this.environment = environment;
    //}

    @Scheduled(cron = "0 0 22 * * ?")
    //@Scheduled(cron = "0 10 * * * ?")
    public void backupAthena() throws IOException {
        String url = "jdbc:mysql://10.0.81.202:3306/athena?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true";

        extracted(url, "athena");
    }

    @Scheduled(cron = "0 30 22 * * ?")
    //@Scheduled(cron = "0 20 * * * ?")
    public void backupJiHeDC() throws IOException {
        // 数据库配置信息
        String url = "jdbc:mysql://10.0.81.202:3306/jihe-dc?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true";

        extracted(url, "jihe-dc");
    }

    @Scheduled(cron = "0 0 23 * * ?")
    //@Scheduled(cron = "0 30 * * * ?")
    public void backupJiHeDCPro() throws IOException {
        // 数据库配置信息
        String url = "jdbc:mysql://10.0.81.202:3306/jihe-dc-pro?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true";

        extracted(url, "jihe-dc-pro");
    }

    private static void extracted(String url, String nameOfTheDatabase) throws IOException {
        LocalDateTime now = LocalDateTime.now();
        log.info("*******************时间:【{}】, 系统开启定时任务数据库备份*******************", now);

        // 第三个 :号下标
        int subStrIndex = url.indexOf(":", url.indexOf(":", url.indexOf(":") + 1) + 1);

        // IP
        String host = url.substring(url.indexOf("//") + 2, subStrIndex);

        // 端口
        String subStr2 = url.substring(subStrIndex);

        String port = subStr2.substring(1, subStr2.indexOf("/"));

        // 数据库名
        String dataBaseName = subStr2.substring(subStr2.indexOf("/") + 1, subStr2.indexOf("?"));

        // 环境
        String os = System.getProperties().getProperty("os.name");

        log.info("备份环境信息:【{}】, 用户名:【{}】,密码:【{}】, 地址:【{}】, 端口:【{}】,数据库:【{}】", os, url, PASSWORD, host, port, dataBaseName);


        LocalDate localDate = LocalDate.now();

        String filePath = RuoYiConfig.getDownloadMysqlPath();

        Date date = new Date();

        String format = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss ").format(date).replace(" ", "");

        String fileName = nameOfTheDatabase + "_" + format + ".sql";

        File file = new File(filePath, fileName);

        file.getParentFile().mkdirs();

        file.createNewFile();

        // 备份今天数据库
        DcMYSQLUtil.backup(IP, PORT, USER, PASSWORD, dataBaseName, file);


        // 删除七天前数据库备份文件 LocalDate
        LocalDate before = localDate.minusDays(7);
        String fileBeforeName = nameOfTheDatabase + "-" + before + ".sql";
        File fileBefore = new File(filePath, fileBeforeName);
        if (fileBefore.exists()) {
            fileBefore.delete();
        }
        log.info("*******************时间:【{}】, 系统结束定时任务数据库备份*******************", now);
    }


}