package com.foreverwin.mesnac.quartz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.quartz.dto.SystemJobDTO;
import com.foreverwin.mesnac.quartz.model.SystemJob;
import com.foreverwin.mesnac.quartz.service.SystemJobService;
import com.foreverwin.modular.core.util.FrontPage;
import com.foreverwin.modular.core.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author Ervin Chen
 * @since 2020-10-21
 */
@RestController
@RequestMapping("/Z-SYSTEM-JOB")
public class SystemJobController {

    @Autowired
    public SystemJobService systemJobService;

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public R getSystemJobById(@PathVariable String id) {
        return R.ok( systemJobService.getById(id));
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getSystemJobList(SystemJob systemJob){
        List<SystemJob> result;
        QueryWrapper<SystemJob> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(systemJob);
        result = systemJobService.list(queryWrapper);
        return R.ok(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<SystemJob> frontPage, SystemJob systemJob){
        IPage result;
        QueryWrapper<SystemJob> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(systemJob);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(SystemJob::getHandle, frontPage.getGlobalQuery())
                .or().like(SystemJob::getSite, frontPage.getGlobalQuery())
                .or().like(SystemJob::getName, frontPage.getGlobalQuery())
                .or().like(SystemJob::getDescription, frontPage.getGlobalQuery())
                .or().like(SystemJob::getJobGroup, frontPage.getGlobalQuery())
                .or().like(SystemJob::getJobParam, frontPage.getGlobalQuery())
                .or().like(SystemJob::getRemark, frontPage.getGlobalQuery())
                .or().like(SystemJob::getCreateUserBo, frontPage.getGlobalQuery())
                .or().like(SystemJob::getUpdateUserBo, frontPage.getGlobalQuery())
    );
        }
        result = systemJobService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

    /**
     * 新增
     * @param systemJobDTO  传递的实体
     * @return  null 失败  实体成功
     */
    @PostMapping("/save")
    public R save(@RequestBody SystemJobDTO systemJobDTO) {
        return R.ok(systemJobService.save(systemJobDTO));
    }

    /**
     * 修改
     * @param systemJobDTO  传递的实体
     * @return  null 失败  实体成功
     */
    @PostMapping("/update")
    public R updateById(@RequestBody SystemJobDTO systemJobDTO) {
        return R.ok(systemJobService.updateById(systemJobDTO));
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @GetMapping("/delete")
    public R removeById(String id){
        return R.ok(systemJobService.removeById(id));
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/delete-batch")
    public R removeByIds(List<String> ids){
        return R.ok(systemJobService.removeByIds(ids));
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/pageSystemJob")
    public R pageSystemJob(FrontPage<SystemJobDTO> frontPage, SystemJobDTO jobDTO){
        IPage result;
        result = systemJobService.pageSystemJob(frontPage, jobDTO);
        return R.ok(result);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/getExtSystemJobList")
    public R getExtSystemJobList(){
        List<SystemJobDTO> result;
        result = systemJobService.listSystemJob();
        return R.ok(result);
    }

    /**
     * 暂停任务
     *
     * @param id 主键
     * @return
     */
    @ResponseBody
    @GetMapping("/pause/{id:.+}")
    public R pause(@PathVariable String id) {
        return R.ok(systemJobService.pause(id));
    }

    /**
     * 恢复任务
     *
     * @param id 主键
     * @return
     */
    @ResponseBody
    @GetMapping("/resume/{id:.+}")
    public R resume(@PathVariable String id) {
        return R.ok(systemJobService.resume(id));
    }
}