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

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.IDiskMonitorPolicyService;
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.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 DiskMonitorPolicyController extends BaseController {

    private final IDiskMonitorPolicyService diskMonitorPolicyService;

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/policy/detail")
    @ApiOperation("查询磁盘监控配置详情")
    public HttpResult<DiskMonitorVO.PolicyDetailVO> detail() {
        String methodDescribe = getMethodDescribe("detail");
        LogUtil.njcnDebug(log, "{}，查询磁盘监控配置详情", methodDescribe);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, diskMonitorPolicyService.getPolicyDetail(), methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/policy/save")
    @ApiOperation("保存磁盘监控配置")
    @ApiImplicitParam(name = "param", value = "磁盘监控配置", required = true)
    public HttpResult<Boolean> save(@RequestBody @Valid DiskMonitorParam.PolicySaveParam param) {
        String methodDescribe = getMethodDescribe("save");
        LogUtil.njcnDebug(log, "{}，保存磁盘监控配置", methodDescribe);
        boolean result = diskMonitorPolicyService.savePolicy(param);
        return HttpResultUtil.assembleCommonResponseResult(result ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL, result, methodDescribe);
    }
}
