package com.njcn.gather.system.dictionary.controller;

import cn.hutool.core.util.StrUtil;
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.system.dictionary.pojo.param.DictPqParam;
import com.njcn.gather.system.dictionary.pojo.po.DictPq;
import com.njcn.gather.system.dictionary.service.IDictPqService;
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.*;

import java.util.List;

/**
 * @author caozehui
 * @date 2024/11/05
 */
@Slf4j
@Api(tags = "电能质量指标字典操作")
@RestController
@RequestMapping("/dictPq")
@RequiredArgsConstructor
public class DictPqController extends BaseController {

    private final IDictPqService dictPqService;

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/list")
    @ApiOperation("查询电能质量指标字典")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<Page<DictPq>> list(@RequestBody @Validated DictPqParam.QueryParam queryParam) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);
        Page<DictPq> result = dictPqService.listDictPqs(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增电能质量指标字典")
    public HttpResult<Boolean> add(@RequestBody @Validated DictPqParam dictPqParam) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，新增数据为：{}", methodDescribe, dictPqParam);
        boolean result = dictPqService.addDictPq(dictPqParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("更新电能质量指标字典")
    @ApiImplicitParam(name = "updateParam", value = "更新参数", required = true)
    public HttpResult<Boolean> update(@RequestBody @Validated DictPqParam.UpdateParam updateParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，更新数据为：{}", methodDescribe, updateParam);
        boolean result = dictPqService.updateDictPq(updateParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("删除电能质量指标字典")
    @ApiImplicitParam(name = "ids", value = "字典索引", required = true)
    public HttpResult<Boolean> delete(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log, "{}，删除ID数据为：{}", methodDescribe, String.join(StrUtil.COMMA, ids));
        boolean result = dictPqService.deleteDictPq(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }
}
