package com.njcn.gather.systemmonitor.disk.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.LogUtil;
import com.njcn.gather.systemmonitor.disk.pojo.param.DiskMonitorParam;
import com.njcn.gather.systemmonitor.disk.pojo.vo.DiskMonitorVO;
import com.njcn.gather.systemmonitor.disk.service.IDiskMonitorJobService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 磁盘监控任务接口。
 */
@Validated
@Slf4j
@Api(tags = "磁盘监控任务")
@RestController
@RequestMapping("/disk-monitor")
@RequiredArgsConstructor
public class DiskMonitorJobController extends BaseController {

    private final IDiskMonitorJobService diskMonitorJobService;

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/job/run")
    @ApiOperation("手动执行磁盘监控")
    @ApiImplicitParam(name = "param", value = "手动执行参数", required = true)
    public HttpResult<DiskMonitorVO.JobRunVO> run(@RequestBody @Valid DiskMonitorParam.JobRunParam param) {
        String methodDescribe = getMethodDescribe("run");
        LogUtil.njcnDebug(log, "{}，执行磁盘监控任务，source={}", methodDescribe, param.getJobSource());
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), diskMonitorJobService.runJob(param), "任务已启动");
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/job/list")
    @ApiOperation("分页查询磁盘监控任务列表")
    @ApiImplicitParam(name = "param", value = "任务分页查询参数", required = true)
    public HttpResult<Page<DiskMonitorVO.JobListVO>> list(@RequestBody @Valid DiskMonitorParam.JobListParam param) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，分页查询磁盘监控任务列表", methodDescribe);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, diskMonitorJobService.listJobs(param), methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/job/{jobId}/detail")
    @ApiOperation("查询磁盘监控任务详情")
    @ApiImplicitParam(name = "jobId", value = "任务ID", required = true)
    public HttpResult<DiskMonitorVO.JobDetailVO> jobDetail(@PathVariable("jobId") Long jobId) {
        String methodDescribe = getMethodDescribe("jobDetail");
        LogUtil.njcnDebug(log, "{}，查询磁盘监控任务详情，jobId={}", methodDescribe, jobId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, diskMonitorJobService.getJobDetail(jobId), methodDescribe);
    }
}
