package com.njcn.energy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.common.utils.LogUtil;
import com.njcn.energy.pojo.param.DictParam;
import com.njcn.energy.pojo.po.EleDict;
import com.njcn.energy.service.IEleDictService;
import com.njcn.web.controller.BaseController;
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.web.bind.annotation.*;

import java.util.Objects;

/**
 * 类的介绍：
 *
 * @author xuyang
 * @version 1.0.0
 * @createTime 2022/3/24 10:59
 */
@Slf4j
@RestController
@RequestMapping("/eleDict")
@RequiredArgsConstructor
@Api(tags = "字典数据管理")
public class EleDictController extends BaseController {


    private final IEleDictService iEleDictService;

    /**
     * 新增字典表
     * @author cdf
     * @date 2022/3/22
     */
    @PostMapping("/add")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("新增字典表")
    @ApiImplicitParam(name = "dictParam", value = "字典表数据", required = true)
    public HttpResult<Boolean> add(@RequestBody DictParam dictParam){
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，字典表数据数据为：{}", methodDescribe, dictParam);
        iEleDictService.add(dictParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }


    /**
     * 修改字典
     * @author cdf
     * @date 2022/3/22
     */
    @PostMapping("/update")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("修改字典")
    @ApiImplicitParam(name = "dictUpdateParam", value = "字典表数据", required = true)
    public HttpResult<Boolean> update(@RequestBody DictParam.DictUpdateParam dictUpdateParam){
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，字典表数据数据为：{}", methodDescribe, dictUpdateParam);
        iEleDictService.update(dictUpdateParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }


    /**
     * 删除字典
     * @author cdf
     * @date 2022/3/22
     */
    @DeleteMapping("/delete")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("删除字典")
    @ApiImplicitParam(name = "id", value = "字典表索引", required = true)
    public HttpResult<Boolean> delete(@RequestParam("id") String id){
        String methodDescribe = getMethodDescribe("delete");
        iEleDictService.delete(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }


    /**
     * 查询字典列表
     * @author cdf
     * @date 2022/3/22
     */
    @PostMapping("/getList")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("查询字典列表")
    public HttpResult<Page<EleDict>> getList(@RequestBody DictParam.DictQueryParam dictQueryParam){
        String methodDescribe = getMethodDescribe("getList");
        Page<EleDict> res= iEleDictService.getList(dictQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, res, methodDescribe);
    }



    /**
     * 查询字典
     * @author cdf
     * @date 2022/3/22
     */
    @PostMapping("/getById")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("查询字典")
    public HttpResult<EleDict> getById(String id){
        String methodDescribe = getMethodDescribe("getById");
        EleDict res= iEleDictService.getById(id);
        if(Objects.nonNull(res)){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, res, methodDescribe);
        }else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

}
