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


import cn.hutool.core.collection.CollectionUtil;
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.DictDataParam;
import com.njcn.gather.system.dictionary.pojo.po.DictData;
import com.njcn.gather.system.dictionary.service.IDictDataService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.pojo.dto.SimpleTreeDTO;
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 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("/listByTypeId")
    @ApiOperation("根据字典类型id查询字典数据")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<Page<DictData>> listByTypeId(@RequestBody @Validated DictDataParam.QueryParam queryParam) {
        String methodDescribe = getMethodDescribe("listByTypeId");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);
        Page<DictData> result = dictDataService.getDictDataByTypeId(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增字典数据")
    @ApiImplicitParam(name = "dictDataParam", value = "字典数据", required = true)
    public HttpResult<Boolean> 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, 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 DictDataParam.UpdateParam updateParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，字典数据为：{}", methodDescribe, updateParam);
        boolean result = dictDataService.updateDictData(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.UPDATE)
    @PostMapping("/delete")
    @ApiOperation("删除字典数据")
    @ApiImplicitParam(name = "ids", value = "字典索引", required = true, dataTypeClass = List.class)
    public HttpResult<Boolean> 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, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

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

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getDicDataByCode")
    @ApiOperation("根据字典code查询字典数据")
    @ApiImplicitParam(name = "code", value = "字典code", required = true)
    public HttpResult<DictData> getDicDataByCode(@RequestParam("code") String code) {
        String methodDescribe = getMethodDescribe("getDicDataByCode");
        DictData result = dictDataService.getDictDataByCode(code);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, 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 = dictDataService.dictDataCache();
        if (CollectionUtil.isNotEmpty(dictData)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, dictData, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.NO_DATA, null, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.DOWNLOAD)
    @PostMapping("/export")
    @ApiOperation("导出字典数据")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public void export(@RequestBody @Validated DictDataParam.QueryParam queryParam) {
        dictDataService.exportDictData(queryParam);
    }
}

