package cn.estsh.i3plus.core.apiservice.controller;

import cn.estsh.i3plus.icloud.wms.sdk.IWmsScheduleJobCloud;
import cn.estsh.i3plus.platform.common.util.PlatformConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.impp.framework.base.controller.CoreBaseController;
import cn.estsh.impp.framework.boot.quartz.IQuartzJobService;
import cn.estsh.impp.framework.boot.quartz.ScheduleTool;
import cn.estsh.impp.framework.boot.quartz.TaskInfo;
import cn.estsh.impp.framework.boot.util.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description : 定时任务调用demo
 * @Reference :
 * @Author : alwaysfrin
 * @CreateDate : 2018-09-26 10:34
 * @Modify:
 **/
@RestController
@Api(description="定时任务demo")
@RequestMapping(PlatformConstWords.BASE_URL + "/schedule")
public class DemoScheduleController extends CoreBaseController{
    private static final Logger LOGGER = LoggerFactory.getLogger(DemoScheduleController.class);

    /************ 定时任务demo *********/
    @Autowired
    private ScheduleTool scheduleTool;

    @Autowired
    private IWmsScheduleJobCloud wmsScheduleJobCloud;

    /**
     * 根据来源切换定时任务服务
     * @param source CommonEnumUtil.SOFT_TYPE
     * @return
     */
    private IQuartzJobService getJobService(int source){
        IQuartzJobService quartzJobService = scheduleTool;

        if(source == CommonEnumUtil.SOFT_TYPE.CORE.getValue()){
            //平台定时任务
            quartzJobService = scheduleTool;
        }else if(source == CommonEnumUtil.SOFT_TYPE.WMS.getValue()){
            quartzJobService = wmsScheduleJobCloud;
        }
        return quartzJobService;
    }

    @GetMapping(value="/list-task")
    @ApiOperation(value="查询定时任务",notes="查询所有定时任务")
    public ResultBean listTask(int sourceType){
        List<TaskInfo> taskList = getJobService(sourceType).listTask().getResultList();
        if(taskList != null) {
            for (TaskInfo ti : taskList) {
                LOGGER.info("任务：{}",ti);
            }
        }else{
            LOGGER.info("任务列表为空");
        }

        return new ResultBean(true,taskList);
    }

    @GetMapping(value="/add-task")
    @ApiOperation(value="添加定时任务",notes="添加定时任务")
    public ResultBean addTask(int sourceType,
                              String packageName,String className,String jobName,String jobGroup,String cronExpression){
        this.getJobService(sourceType).addTask(packageName,className,jobName,jobGroup,cronExpression,"wms新任务");
        return new ResultBean(true,"");
    }

    @GetMapping(value="/delete-task")
    @ApiOperation(value="删除定时任务",notes="删除定时任务")
    public ResultBean deleteTask(int sourceType,String jobName,String jobGroup){
        this.getJobService(sourceType).deleteTask(jobName,jobGroup);

        return new ResultBean(true);
    }

    @GetMapping(value="/pause-task")
    @ApiOperation(value="暂停定时任务",notes="暂停定时任务")
    public ResultBean pauseJob(int sourceType,String jobName,String jobGroup){
        this.getJobService(sourceType).pauseJob(jobName,jobGroup);

        return new ResultBean(true);
    }

    @GetMapping(value="/resume-task")
    @ApiOperation(value="继续定时任务",notes="继续定时任务")
    public ResultBean resumeJob(int sourceType,String jobName,String jobGroup){
        this.getJobService(sourceType).resumeJob(jobName,jobGroup);

        return new ResultBean(true);
    }

    @GetMapping(value="/execeute-task")
    @ApiOperation(value="立即执行定时任务",notes="立即执行定时任务")
    public ResultBean executeJob(int sourceType,String jobName,String jobGroup){
        this.getJobService(sourceType).executeJob(jobName,jobGroup);

        return new ResultBean(true);
    }

    @GetMapping(value="/stand-by-schedule")
    @ApiOperation(value="定时器待机",notes="定时器待机")
    public ResultBean restartJob(int sourceType,String jobName,String jobGroup){
        this.getJobService(sourceType).standBySchedule();

        return new ResultBean(true);
    }


    @GetMapping(value="/start-schedule")
    @ApiOperation(value="启动定时器",notes="启动定时器")
    public ResultBean startSchedule(int sourceType,String jobName,String jobGroup){
        this.getJobService(sourceType).startSchedule();

        return new ResultBean(true);
    }
}
