package com.ruoyi.quartz.task;

import com.ruoyi.system.service.ISysLogininforService;
import com.ruoyi.system.service.ISysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    @Autowired
    private ISysOperLogService sysOperLogService;

    @Autowired
    private ISysLogininforService sysLogininforService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    public void linuxDump(String dir, String username, String password, String databaseName) throws Exception {
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdir();
        }
        String sqlName = new SimpleDateFormat("yyyyMMdd").format(new Date());
        File datafile = new File(file + File.separator + sqlName + ".sql");
        if (datafile.exists()) {
            System.out.println(sqlName + "文件名已存在，请更换");
            return;
        }
        //拼接cmd命令  windows下 cmd   Linux下 /bin/sh
        Process exec = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", "/usr/bin/mysqldump -u " + username + " -p" + password + " " + databaseName + " > " + datafile});
        if (exec.waitFor() == 0) {
            System.out.println("数据库备份成功,备份路径为：" + datafile);
        }
    }

    public void winDump(String dir, String username, String password, String databaseName) throws Exception {
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdir();
        }
        String sqlName = new SimpleDateFormat("yyyyMMdd").format(new Date());
        File datafile = new File(file + File.separator + sqlName + ".sql");
        if (datafile.exists()) {
            System.out.println(sqlName + "文件名已存在，请更换");
            return;
        }
        String mysqldump = "mysqldump -u " + username + " -p" + password + " " + databaseName;
        Process exec = Runtime.getRuntime().exec(new String[]{"cmd", "/c", mysqldump + " > " + datafile.getAbsolutePath()});
        if (exec.waitFor() == 0) {
            System.out.println("数据库备份成功,备份路径为：" + datafile);
        }
    }

    public void deleteSysOperLog() {
        sysOperLogService.deleteOperLogJob();
        sysLogininforService.deleteLoginInfoJob();
    }

}
