package com.njcn.product.system.dict.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.dto.SimpleTreeDTO;
import com.njcn.common.pojo.enums.common.DataStateEnum;
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.DictTypeParam;
import com.njcn.product.system.dict.pojo.po.DictType;
import com.njcn.product.system.dict.service.IDictTypeService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hongawen
 * @since 2021-12-13
 */
@Slf4j
@Api(tags = "字典类型表操作")
@RestController
@RequestMapping("/dictType")
@RequiredArgsConstructor
public class DictTypeController extends BaseController {

    private final IDictTypeService dictTypeService;


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

    /**
     * 查询所有字典类型数据
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/listAll")
    @ApiOperation("查询所有字典类型数据")
    public HttpResult<List<DictType>> listAll() {
        String methodDescribe = getMethodDescribe("listAll");
        LogUtil.njcnDebug(log, "{}", methodDescribe);
        List<DictType> dictTypeList = dictTypeService.list(new LambdaQueryWrapper<DictType>().eq(DictType::getState, DataStateEnum.ENABLE.getCode()));
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, dictTypeList, methodDescribe);
    }


    /**
     * 新增字典类型
     *
     * @param dictTypeParam 字典类型数据
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增字典类型")
    @ApiImplicitParam(name = "dictTypeParam", value = "字典类型数据", required = true)
    public HttpResult<Object> add(@RequestBody @Validated DictTypeParam dictTypeParam) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，字典类型数据为：{}", methodDescribe, dictTypeParam);
        boolean result = dictTypeService.addDictType(dictTypeParam);
        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 DictTypeParam.DictTypeUpdateParam updateParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，字典类型数据为：{}", methodDescribe, updateParam);
        boolean result = dictTypeService.updateDictType(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.DELETE)
    @PostMapping("/delete")
    @ApiOperation("删除字典类型")
    @ApiImplicitParam(name = "ids", value = "字典索引", required = true)
    public HttpResult<Object> delete(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log, "{}，字典ID数据为：{}", methodDescribe, String.join(StrUtil.COMMA, ids));
        boolean result = dictTypeService.deleteDictType(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 获取所有字典数据基础信息
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/dictDataCache")
    @ApiOperation("获取所有字典数据基础信息")
    public HttpResult<List<SimpleTreeDTO>> dictDataCache() {
        String methodDescribe = getMethodDescribe("dictDataCache");
        LogUtil.njcnDebug(log, "{}，获取所有字典数据基础信息", methodDescribe);
        List<SimpleTreeDTO> dictData = dictTypeService.dictDataCache();
        if (CollectionUtil.isNotEmpty(dictData)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, dictData, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.NO_DATA, null, methodDescribe);
        }
    }

}

