package com.njcn.product.system.dict.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.HttpResultUtil;
import com.njcn.common.utils.LogUtil;

import com.njcn.product.system.dict.pojo.param.DictDataParam;
import com.njcn.product.system.dict.pojo.po.DictData;
import com.njcn.product.system.dict.pojo.vo.DictDataVO;
import com.njcn.product.system.dict.service.IDictDataService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
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 springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hongawen
 * @since 2021-12-13
 */
@Validated
@Slf4j
@Api(tags = "字典数据操作")
@RestController
@RequestMapping("/dictData")
@RequiredArgsConstructor
public class DictDataController extends BaseController {

    private final IDictDataService dictDataService;

    /**
     * 分页查询字典类型数据
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/list")
    @ApiOperation("查询字典数据")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<Page<DictDataVO>> list(@RequestBody @Validated DictDataParam.DictDataQueryParam queryParam) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);
        Page<DictDataVO> result = dictDataService.listDictData(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 新增字典数据
     *
     * @param dictDataParam 字典数据
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增字典数据")
    @ApiImplicitParam(name = "dictDataParam", value = "字典数据", required = true)
    public HttpResult<Object> add(@RequestBody @Validated DictDataParam dictDataParam) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，字典数据为：{}", methodDescribe, dictDataParam);
        boolean result = dictDataService.addDictData(dictDataParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 修改字典数据
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改字典数据")
    @ApiImplicitParam(name = "updateParam", value = "字典数据", required = true)
    public HttpResult<Object> update(@RequestBody @Validated DictDataParam.DictDataUpdateParam updateParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，字典数据为：{}", methodDescribe, updateParam);
        boolean result = dictDataService.updateDictData(updateParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }


    /**
     * 批量删除字典数据
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/delete")
    @ApiOperation("删除字典数据")
    @ApiImplicitParam(name = "ids", value = "字典索引", required = true, dataTypeClass = List.class)
    public HttpResult<Object> delete(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log, "{}，字典ID数据为：{}", methodDescribe, String.join(StrUtil.COMMA, ids));
        boolean result = dictDataService.deleteDictData(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 根据字典类型id分页查询字典数据
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/getTypeIdData")
    @ApiOperation("根据字典类型id查询字典数据")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<Page<DictDataVO>> getTypeIdData(@RequestBody @Validated DictDataParam.DicTypeIdQueryParam queryParam) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);
        Page<DictDataVO> result = dictDataService.getTypeIdData(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getDicDataById")
    @ApiOperation("根据字典id查询字典数据")
    @ApiImplicitParam(name = "dicIndex", value = "查询参数", required = true)
    public HttpResult<DictData> getDicDataById(@RequestParam("dicIndex") String dicIndex) {
        String methodDescribe = getMethodDescribe("getDicDataById");
        DictData result = dictDataService.getDicDataById(dicIndex);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getDicDataByTypeName")
    @ApiOperation("根据字典类型名称查询字典数据")
    @ApiImplicitParam(name = "dictTypeName", value = "查询参数", required = true)
    public HttpResult<List<DictData>> getDicDataByTypeName(@RequestParam("dictTypeName") String dictTypeName) {
        String methodDescribe = getMethodDescribe("getDicDataByTypeName");
        List<DictData> result = dictDataService.getDicDataByTypeName(dictTypeName);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getDicDataByName")
    @ApiOperation("根据字典名称查询字典数据")
    @ApiImplicitParam(name = "dicName", value = "查询参数", required = true)
    public HttpResult<DictData> getDicDataByName(@RequestParam("dicName") String dicName) {
        String methodDescribe = getMethodDescribe("getDicDataByName");
        DictData result = dictDataService.getDicDataByName(dicName);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getDicDataByNameAndType")
    @ApiOperation("根据字典名称查询字典数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dicName", value = "查询参数", required = true),
            @ApiImplicitParam(name = "typeName", value = "查询参数", required = true)
    })
    public HttpResult<DictData> getDicDataByNameAndType(@RequestParam("dicName") String dicName,@RequestParam("typeName") String typeName) {
        String methodDescribe = getMethodDescribe("getDicDataByNameAndType");
        DictData result = dictDataService.getDicDataByNameAndType(dicName,typeName);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getDicDataByCodeAndType")
    @ApiOperation("根据字典Code和字典类型查询字典数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dicDataCode", value = "查询参数", required = true),
            @ApiImplicitParam(name = "dicTypeCode", value = "查询参数", required = true)
    })
    public HttpResult<DictData> getDicDataByCodeAndType(@RequestParam("dicDataCode") String dicCode,@RequestParam("dicTypeCode") String typeCode) {
        String methodDescribe = getMethodDescribe("getDicDataByCodeAndType");
        DictData result = dictDataService.getDicDataByCodeAndType(dicCode,typeCode);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getDicDataByCode")
    @ApiOperation("根据字典code查询字典数据")
    @ApiImplicitParam(name = "code", value = "查询参数", required = true)
    public HttpResult<DictData> getDicDataByCode(@RequestParam("code") String code) {
        String methodDescribe = getMethodDescribe("getDicDataByCode");
        DictData result = dictDataService.getDicDataByCode(code);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    /**
     * 后台新增字典数据
     *
     * @param dicTypeName 类型名称
     * @param dicDataName 数据名称
     * @return 新增后的字典数据
     */
    @ApiIgnore
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/addDicData")
    @ApiOperation("后台新增字典数据")
    public HttpResult<DictData> addDicData(String dicTypeName, String dicDataName) {
        String methodDescribe = getMethodDescribe("addDicData");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, dictDataService.addDictData(dicTypeName,dicDataName), methodDescribe);
    }

    /**
     * 根据字典类型名称&数据名称获取字典数据
     *
     * @param dicTypeName 字典类型名称
     * @param dicDataName 字典数据名称
     * @return 字典数据
     */
    @ApiIgnore
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getDicDataByNameAndTypeName")
    @ApiOperation("根据字典类型名称&数据名称获取字典数据")
    public HttpResult<DictData> getDicDataByNameAndTypeName(String dicTypeName, String dicDataName) {
        String methodDescribe = getMethodDescribe("getDicDataByNameAndTypeName");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, dictDataService.getDicDataByNameAndTypeName(dicTypeName,dicDataName), methodDescribe);
    }

    @ApiIgnore
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getDicDataByTypeCode")
    @ApiOperation("根据字典类型code查询字典数据")
    @ApiImplicitParam(name = "dictTypeCode", value = "查询参数", required = true)
    public HttpResult<List<DictData>> getDicDataByTypeCode(@RequestParam("dictTypeCode") String dictTypeCode) {
        String methodDescribe = getMethodDescribe("getDicDataByTypeCode");
        List<DictData> result = dictDataService.getDicDataByTypeCode(dictTypeCode);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


}

