package com.jiayue.ipfcst.console.controller;

import com.jiayue.ipfcst.aop.SaveValidate;
import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.data.entity.Quartz;
import com.jiayue.ipfcst.console.service.InitJobClassService;
import com.jiayue.ipfcst.console.service.QuartzService;
import com.jiayue.ipfcst.console.service.SysParameterService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.Map;

/**
 * 定时控制类
 *
 * @author bizy
 * @version 1.0
 * @since 2018/10/22 11:29
 */
@RestController
@Slf4j
public class QuartzController {

    @Autowired
    QuartzService quartzService;

    @Autowired
    InitJobClassService initJobClassService;

    @Autowired
    SysParameterService sysParameterService;

    /**
     * 添加定时任务
     *
     * @param qt 定时任务对象
     * @return 状态bean
     */
    @SaveValidate
    @SneakyThrows
    @PostMapping(value = "quartzs")
    public ResponseVO addQuartz(@RequestBody final Quartz qt) {
        final Quartz quartz = quartzService.findByClass(qt.getExecuteClass());
        if (null != quartz) {
            return ResponseVO.fail("添加失败，任务已存在！");
        } else {
            final boolean flag = quartzService.save(qt);
            if (flag) {
                return ResponseVO.success("添加成功！");
            } else {
                return ResponseVO.fail("添加失败！");
            }
        }

    }

    /**
     * 修改定时任务状态
     *
     * @param qt 定时任务对象
     * @return 状态bean
     */
    @SaveValidate
    @SneakyThrows
    @PutMapping(value = "quartzs")
    public ResponseVO stateQuartz(@RequestBody final Quartz qt) {
        final boolean flag = quartzService.save(qt);
        if (flag) {
            return ResponseVO.success("修改成功！");
        } else {
            return ResponseVO.success("修改成功！");
        }
    }

    /**
     * 删除定时任务
     *
     * @param ids
     * @return
     */
    @SneakyThrows
    @RequestMapping(value = "quartzs/delete/{ids}", method = RequestMethod.DELETE)
    public ResponseVO deleteQuartzById(@PathVariable final String ids) {
        if (quartzService.deleteById(ids)) {
            return ResponseVO.success("删除成功!");
        }
        return ResponseVO.fail("删除失败!没有查询到id对应的定时任务");
    }

    /**
     * 根据jobName模糊查询
     *
     * @param page    页数
     * @param size    每页几条
     * @param jobName jobName
     * @return Map<String, Object>
     */
    @SneakyThrows
    @GetMapping(value = "quartzs/{page}/{size}/{jobName}/{jobType}")
    public ResponseVO getQuartzs(
            @PathVariable final Integer page, @PathVariable final Integer size, @PathVariable String jobName,
            @PathVariable String jobType) {
        if (jobName.equals("1")) {
            jobName = "";
        }
        if (jobType.equals("1")) {
            jobType = "";
        }
        final Map<String, Object> map = quartzService.getQuartz(jobName, jobType, page, size);

        return ResponseVO.success(map);
    }

  @SneakyThrows
  @GetMapping(value = "initJobClasses/initJob")
  public ResponseVO initJobClass() throws SQLException {
    Boolean b = quartzService.initJobClass();
    if (!b){
      return ResponseVO.fail("初始化失败，请检查sql文件是否存在");
    }
    return ResponseVO.success("初始化成功！");
  }
}
