package cn.com.ctop.check.controller;

import cn.com.ctop.check.entity.CtopCheckTaskExecuteLog;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.com.ctop.check.service.ICtopCheckTaskExecuteLogService;
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/checkTaskExecuteLog")
public class CtopCheckTaskExecuteLogCtrl {

   @Autowired
   private ICtopCheckTaskExecuteLogService ctopCheckTaskExecuteLogService;

   /**
    * 分页列表查询
    */
   @GetMapping(value = "/list")
   public Result<IPage<CtopCheckTaskExecuteLog>> queryPageList(CtopCheckTaskExecuteLog ctopCheckTaskExecuteLog,
                                                               @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                               @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                               HttpServletRequest req) {
       Result<IPage<CtopCheckTaskExecuteLog>> result = new Result<>();
       QueryWrapper<CtopCheckTaskExecuteLog> queryWrapper = QueryGenerator.initQueryWrapper(ctopCheckTaskExecuteLog, req.getParameterMap());
       queryWrapper.orderByDesc("create_time");
       Page<CtopCheckTaskExecuteLog> page = new Page<>(pageNo, pageSize);
       IPage<CtopCheckTaskExecuteLog> pageList = ctopCheckTaskExecuteLogService.page(page, queryWrapper);
       pageList.getRecords().forEach(it->it.setExeTime((int)(it.getEndDate()-it.getStartDate())/1000));
       result.setSuccess(true);
       result.setResult(pageList);
       return result;
   }

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

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

   /**
    *  通过id删除
    */
   @PostMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) Long id) {
       try {
           ctopCheckTaskExecuteLogService.removeById(id);
       } catch (Exception e) {
           log.error(e.getMessage());
           return Result.error("删除失败!");
       }
       return Result.ok("删除成功!");
   }


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

}
