package com.njcn.cssystem.controller.baseinfo;

import com.njcn.common.pojo.annotation.OperateInfo;
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.HttpResultUtil;
import com.njcn.cssystem.pojo.param.CsAlarmSetParam;
import com.njcn.cssystem.pojo.po.CsAlarmSet;
import com.njcn.cssystem.service.ICsAlarmSetService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xy
 * @since 2026-05-08
 */
@RestController
@Slf4j
@RequestMapping("/csAlarmSet")
@Api(tags = "告警配置管理")
@AllArgsConstructor
public class CsAlarmSetController extends BaseController {

    private final ICsAlarmSetService csAlarmSetService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/add")
    @ApiOperation("新增告警配置")
    @ApiImplicitParam(name = "param", value = "告警配置参数", required = true)
    public HttpResult<String> add(@RequestBody @Validated CsAlarmSetParam param) {
        String methodDescribe = getMethodDescribe("add");
        boolean result = csAlarmSetService.add(param);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, "新增成功", methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, "新增失败", methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/update")
    @ApiOperation("修改告警配置")
    @ApiImplicitParam(name = "param", value = "告警配置参数", required = true)
    public HttpResult<String> update(@RequestBody @Validated CsAlarmSetParam param) {
        String methodDescribe = getMethodDescribe("update");
        boolean result = csAlarmSetService.update(param);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, "修改成功", methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, "修改失败", methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/delete")
    @ApiOperation("删除告警配置")
    @ApiImplicitParam(name = "id", value = "配置ID", required = true)
    public HttpResult<String> delete(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("delete");
        boolean result = csAlarmSetService.delete(id);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, "删除成功", methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, "删除失败", methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getById")
    @ApiOperation("根据ID查询告警配置")
    @ApiImplicitParam(name = "id", value = "配置ID", required = true)
    public HttpResult<CsAlarmSet> getById(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getById");
        CsAlarmSet vo = csAlarmSetService.getById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, vo, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/listAll")
    @ApiOperation("查询所有告警配置")
    public HttpResult<List<CsAlarmSet>> listAll() {
        String methodDescribe = getMethodDescribe("listAll");
        List<CsAlarmSet> list = csAlarmSetService.listAll();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/toggleActive")
    @ApiOperation("切换告警配置启用状态")
    @ApiImplicitParam(name = "id", value = "配置ID", required = true)
    public HttpResult<String> toggleActive(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("toggleActive");
        boolean result = csAlarmSetService.toggleActive(id);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, "切换成功", methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, "切换失败", methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getEnabledConfig")
    @ApiOperation("获取当前启用的告警配置")
    public HttpResult<CsAlarmSet> getEnabledConfig() {
        String methodDescribe = getMethodDescribe("getEnabledConfig");
        CsAlarmSet vo = csAlarmSetService.getEnabledConfig();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, vo, methodDescribe);
    }

}

