package org.jeecg.modules.demo.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.config.TenantContext;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.demo.mainsite.entity.MainSite;
import org.jeecg.modules.demo.mainsite.service.IMainSiteService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

@Slf4j
@Component
public class JobGo  implements Job {

    @Autowired
    private IMainSiteService mainSiteService;


    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改
     */
    private String parameter;

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String TenantId = parameter.split("&&&")[1];
        parameter =  parameter.split("&&&")[0];
        log.info(" Job Execution key："+jobExecutionContext.getJobDetail().getKey());
        log.info( String.format("welcome %s! Jeecg-Boot 带参数定时任务 SampleParamJob !   时间:" + DateUtils.now(),parameter));
        QueryWrapper<MainSite> queryWrapper = new QueryWrapper<MainSite>();
        Page<MainSite> page = new Page<MainSite>(1, 1);
        queryWrapper.eq("main_site_url",parameter);
        TenantContext.setTenant(TenantId);
        IPage<MainSite> pageList = mainSiteService.page(page, queryWrapper);
        List<MainSite> records = pageList.getRecords();
        MainSite mainSite = null;
        if(records.size() >= 1){
            mainSite = records.get(0);
        }
        if(!ObjectUtils.isEmpty(mainSite)){
            String pram  = mainSite.getMainSiteUrl()+
                    " --junit " +
                    "--timeout=20  " +
                    "trs:TRSadmin123!@#$@tcp(192.168.1.45:56033)/waillian_system?charset=utf8mb4" +
                    "RegularExpression" +
                    mainSite.getRegularExpression() +
                    "RegularExpression"+
                    mainSite.getStateUrl()+
                    "RegularExpression"+
                    mainSite.getTenantId()
                    ;
            String systemTypeGo = mainSite.getSystemTypeGo();
            if(!ObjectUtils.isEmpty(systemTypeGo)){
                if(systemTypeGo.equals("1")){
                    try {
                        pram = mainSite.getGoFile() + " " + pram;
                        System.out.println(pram);
                        String s = executeCmdWin(pram);
                        System.out.println(s);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(systemTypeGo.equals("2")){
                    try {
                        pram = mainSite.getGoFile()  + " " + pram;
                        System.out.println(pram);
                        String s = executeCmdLinux(pram);
                        System.out.println(s);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public String executeCmdWin(String command) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("cmd /c " + command);
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
        String line = null;
        StringBuilder build = new StringBuilder();
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            //build.append(line);
        }
        return build.toString();
    }
    public String executeCmdLinux(String command) throws IOException {
        //创建Runtime对象
        Runtime rt = Runtime.getRuntime();
        //执行命令
        Process proc = rt.exec("./"+command);
        //获取命令输出流并将其打印出来
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        StringBuilder build = new StringBuilder();
        String line = null;
        while ((line = stdInput.readLine()) != null) {
            build.append(line);
        }
        return build.toString();
    }
}
