package com.foreverwin.mesnac.quartz.controller;

import com.foreverwin.mesnac.quartz.model.SystemJobLog;
import com.foreverwin.mesnac.quartz.service.SystemJobLogService;
import com.foreverwin.modular.core.util.R;
import com.foreverwin.modular.core.util.FrontPage;
import com.foreverwin.modular.core.util.CommonMethods;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.RequestMapping;
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-LOG")
public class SystemJobLogController {

    @Autowired
    public SystemJobLogService systemJobLogService;

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

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

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

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

    /**
     * 修改
     * @param systemJobLog  传递的实体
     * @return  null 失败  实体成功
     */
    @PutMapping
    public R updateById(@RequestBody SystemJobLog systemJobLog) {
        return R.ok(systemJobLogService.updateById(systemJobLog));
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public R removeById(@PathVariable("id") String id){
        return R.ok(systemJobLogService.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(systemJobLogService.removeByIds(ids));
    }
}