package cn.com.ctop.check.controller;

import cn.com.ctop.check.entity.CtopCheckTaskList;
import cn.com.ctop.check.service.ICtopCheckTaskListService;
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.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
* 检查数据任务列表
*/
@Slf4j
@RestController
@RequestMapping("/ctop/checkTaskList")
public class CtopCheckTaskListCtrl {

    @Autowired
    private ICtopCheckTaskListService ctopCheckTaskListService;

    /**
     * 分页列表查询
     */
    @GetMapping(value = "/list")
    public Result<IPage<CtopCheckTaskList>> queryPageList(CtopCheckTaskList ctopCheckTaskList,
                                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                          HttpServletRequest req) {
        Result<IPage<CtopCheckTaskList>> result = new Result<>();
        //过滤已删除
        ctopCheckTaskList.setIsDelete(false);
        QueryWrapper<CtopCheckTaskList> queryWrapper = QueryGenerator.initQueryWrapper(ctopCheckTaskList, req.getParameterMap());
        queryWrapper.orderByDesc("create_time");
        Page<CtopCheckTaskList> page = new Page<>(pageNo, pageSize);
        IPage<CtopCheckTaskList> pageList = ctopCheckTaskListService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 添加
     */
    @PostMapping(value = "/add")
    public Result<CtopCheckTaskList> add(@RequestBody CtopCheckTaskList ctopCheckTaskList) {
        Result<CtopCheckTaskList> result = new Result<>();
        try {
            ctopCheckTaskListService.save(ctopCheckTaskList);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 编辑
     */
    @PostMapping(value = "/edit")
    public Result<CtopCheckTaskList> edit(@RequestBody CtopCheckTaskList ctopCheckTaskList) {
        Result<CtopCheckTaskList> result = new Result<>();
        CtopCheckTaskList ctopCheckTaskListEntity = ctopCheckTaskListService.getById(ctopCheckTaskList.getId());
        if (ctopCheckTaskListEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = ctopCheckTaskListService.updateById(ctopCheckTaskList);
            if (ok) {
                result.success("修改成功!");
            }
        }

        return result;
    }

    /**
     * 通过id删除
     */
    @PostMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) Integer id) {
        try {
            CtopCheckTaskList ctopCheckTaskList=new CtopCheckTaskList();
            ctopCheckTaskList.setId(id);
            ctopCheckTaskList.setIsDelete(true);
            ctopCheckTaskListService.updateById(ctopCheckTaskList);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error("删除失败!");
        }
        return Result.ok("删除成功!");
    }

    /**
     * 通过id查询
     */
    @GetMapping(value = "/queryById")
    public Result<CtopCheckTaskList> queryById(@RequestParam(name = "id", required = true) Integer id) {
        Result<CtopCheckTaskList> result = new Result<>();
        CtopCheckTaskList ctopCheckTaskList = ctopCheckTaskListService.getById(id);
        if (ctopCheckTaskList == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(ctopCheckTaskList);
            result.setSuccess(true);
        }
        return result;
    }
}
