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.InitJobClass;
import com.jiayue.ipfcst.console.service.InitJobClassService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;

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

    @Autowired
    InitJobClassService initJobClassService;

    /**
     * 添加任务class
     *
     * @param initJobClass 任务class对象
     * @return 状态bean
     */
    @SaveValidate(desc = "添加")
    @SneakyThrows
    @PostMapping(value = "initJobClasses")
    public ResponseVO addInitJobClass(
            @RequestBody final InitJobClass initJobClass) {
        final InitJobClass i1 = initJobClassService.findByName(initJobClass.getJobClassName());
        final InitJobClass i2 = initJobClassService.findByPath(initJobClass.getJobClassPath());

        if (i1 != null || i2 != null) {
            return ResponseVO.fail("添加失败！别名或者执行类相同！");
        } else {
            initJobClassService.save(initJobClass);
        }
        return ResponseVO.success("添加成功！");

    }

    /**
     * 更新任务class
     *
     * @param initJobClass 任务class对象
     * @return 状态bean
     */
    @SaveValidate
    @SneakyThrows
    @PutMapping(value = "initJobClasses")
    public ResponseVO updateInitJobClass(
            @RequestBody final InitJobClass initJobClass) {

        initJobClassService.save(initJobClass);
        return ResponseVO.success("添加成功！");

    }

    /**
     * 删除任务class
     *
     * @param ids id集合
     * @return ResponseVO
     */
    @SneakyThrows
    @RequestMapping(value = "initJobClasses/delete/{ids}", method = RequestMethod.DELETE)
    public ResponseVO deleteInitJobClassById(@PathVariable final String ids) {

        if (initJobClassService.deleteById(ids)) {
            return ResponseVO.success("删除成功!");
        }
        return ResponseVO.fail("删除失败!没有查询到id对应的任务class");

    }

    /**
     * 根据jobName模糊查询
     *
     * @param page         页数
     * @param size         每页几条
     * @param jobClassName jobClassName
     * @return Map<String, Object>
     */
    @SneakyThrows
    @GetMapping(value = "initJobClasses/{page}/{size}/{jobClassName}")
    public ResponseVO getInitJobClasss(
            @PathVariable final Integer page, @PathVariable final Integer size, @PathVariable String jobClassName) {

        if (jobClassName.equals("1")) {
            jobClassName = "";
        }
        final Map<String, Object> map = initJobClassService.getInitJobClass(jobClassName, page, size);
        return ResponseVO.success(map);

    }

    /**
     * 根据jobName模糊查询
     *
     * @param jt jobType
     * @return Map<String, Object>
     */
    @SneakyThrows
    @GetMapping(value = "initJobClasses/jobType/{jt}")
    public ResponseVO getJobClassByType(
            @PathVariable final String jt) {
        final Map<String, Object> map = new Hashtable<>();

        final List<InitJobClass> list = initJobClassService.findByJobClassType(jt);
        map.put("list", list);

        return ResponseVO.success(map);
    }

    /**
     * 查询全部
     *
     * @return Map<String, Object>
     */
    @SneakyThrows
    @GetMapping(value = "initJobClasses/getAllJobClass")
    public ResponseVO getAllJobClass() {
        final Map<String, Object> map = new Hashtable<>();

        final List<InitJobClass> list = initJobClassService.findAll();
        map.put("list", list);

        return ResponseVO.success(map);
    }


}
