package cn.cslg.report.common.utils;

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.system.ApplicationHome;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.InputStream;

@Slf4j
public class BackupUtils {

    private final static String BACKUP_CONFIG = "backup-config.yml";

    private static Backup getBackupConfig() {
        try {
            ApplicationHome ah = new ApplicationHome(BackupUtils.class);
            File file = ah.getSource();
            String rootPath = file.getParentFile().toString() + FileUtils.FILE_SEPARATOR + BACKUP_CONFIG;
            Yaml yaml = new Yaml();
            InputStream in = FileUtil.getInputStream(rootPath);
            Backup backup = yaml.loadAs(in, Backup.class);
            if (in != null) {
                in.close();
            }
            return backup;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        System.out.println(getBackupConfig());
    }

    public void run() {
        Backup backup = getBackupConfig();
        if (backup == null) {
            log.info("数据库备份配置文件不存在");
            return;
        }
        log.info("数据库备份配置文件：{}", backup);
        if (backup.isEnable()) {
            String path = FileUtils.getStaticPath(FileUtils.BACKUP_FILE);
            String fileName = DateUtils.getNowTimeFormat(DateUtils.YYYYMMDDHHMMSS) + ".sql";
            try {
                String cmd = String.format("mysqldump --skip-opt -h%s -P%s -u%s -p%s %s > %s", backup.getHost(), backup.getPort(), backup.getUsername(), backup.getPassword(), backup.getDb(), path + FileUtils.FILE_SEPARATOR + fileName);
                String[] command;
                if (backup.getOs() == 0) {
                    command = new String[]{"cmd", "/c", String.valueOf(cmd)};
                } else {
                    command = new String[]{"/bin/sh", "-c", String.valueOf(cmd)};
                }
                Runtime.getRuntime().exec(command);
                log.info("备份数据库成功");
            } catch (Exception e) {
                e.printStackTrace();
                log.error("备份数据库失败");
            }
        }
    }

    public static class Backup {
        private String host;
        private String port;
        private String db;
        private String username;
        private String password;
        private int os;
        private boolean enable;

        @Override
        public String toString() {
            return "Backup{" +
                    "host='" + host + '\'' +
                    ", port='" + port + '\'' +
                    ", db='" + db + '\'' +
                    ", username='" + username + '\'' +
                    ", password='" + password + '\'' +
                    ", os=" + os +
                    ", enable=" + enable +
                    '}';
        }

        public boolean isEnable() {
            return enable;
        }

        public void setEnable(boolean enable) {
            this.enable = enable;
        }

        public String getHost() {
            return host;
        }

        public void setHost(String host) {
            this.host = host;
        }

        public String getPort() {
            return port;
        }

        public void setPort(String port) {
            this.port = port;
        }

        public String getDb() {
            return db;
        }

        public void setDb(String db) {
            this.db = db;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public int getOs() {
            return os;
        }

        public void setOs(int os) {
            this.os = os;
        }
    }

}
