package org.springblade.modules.quartz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.quartz.entity.JobLog;
import org.springblade.modules.quartz.service.IJobLogService;
import org.springblade.modules.quartz.vo.JobLogVO;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 定时任务调度日志表 控制器
 *
 * @author BladeX
 * @since 2023-02-17
 */
@RestController
@AllArgsConstructor
@RequestMapping("quartz/joblog")
@Api(value = "定时任务调度日志表", tags = "定时任务调度日志表接口")
public class JobLogController extends BladeController {

	private final IJobLogService jobLogService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入jobLog")
	public R<JobLog> detail(JobLog jobLog) {
		JobLog detail = jobLogService.getOne(Condition.getQueryWrapper(jobLog));
		return R.data(detail);
	}

	/**
	 * 分页 定时任务调度日志表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入jobLog")
	public R<IPage<JobLog>> list(JobLog jobLog, Query query) {
		IPage<JobLog> pages = jobLogService.page(Condition.getPage(query), Condition.getQueryWrapper(jobLog));
		return R.data(pages);
	}

	/**
	 * 自定义分页 定时任务调度日志表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入jobLog")
	public R<IPage<JobLogVO>> page(JobLogVO jobLog, Query query) {
		IPage<JobLogVO> pages = jobLogService.selectJobLogPage(Condition.getPage(query), jobLog);
		return R.data(pages);
	}

	/**
	 * 新增 定时任务调度日志表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入jobLog")
	public R save(@Valid @RequestBody JobLog jobLog) {
		return R.status(jobLogService.save(jobLog));
	}

	/**
	 * 修改 定时任务调度日志表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入jobLog")
	public R update(@Valid @RequestBody JobLog jobLog) {
		return R.status(jobLogService.updateById(jobLog));
	}

	/**
	 * 新增或修改 定时任务调度日志表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入jobLog")
	public R submit(@Valid @RequestBody JobLog jobLog) {
		return R.status(jobLogService.saveOrUpdate(jobLog));
	}

	
	/**
	 * 删除 定时任务调度日志表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(jobLogService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 根据调度编号获取详细信息
	 */
	@GetMapping(value = "/{jobLogId}")
	public R getInfo(@PathVariable Long jobLogId) {
		return data(jobLogService.selectJobLogById(jobLogId));
	}

	/**
	 * 清空定时任务调度日志
	 */
	@DeleteMapping("/clean")
	public R clean() {
		jobLogService.cleanJobLog();
		return success("操作成功");
	}

}
