package com.njcn.system.controller;


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.system.pojo.param.DicParam;
import com.njcn.system.pojo.po.Dic;
import com.njcn.system.pojo.vo.DicVO;
import com.njcn.system.service.IDicService;
import com.njcn.user.pojo.param.FunctionParam;
import com.njcn.user.pojo.po.Function;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hany
 * @date 2022/10/18
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/dic")
@Api(tags = "树型字典管理")
@AllArgsConstructor
public class DicController extends BaseController {

    private final IDicService dicService;

    /**
     * 获取树
     * @return
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/dicTree")
    @ApiOperation("获取字典树")
    public HttpResult<List<DicVO>> getDicTree(){
        String methodDescribe = getMethodDescribe("getDicTree");
        List<DicVO> list = dicService.getDicTree(null);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,list,methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/codeDicTree")
    @ApiOperation("根据code获取字典树")
    public HttpResult<List<DicVO>> getCodeDicTree(String code){
        String methodDescribe = getMethodDescribe("getCodeDicTree");
        List<DicVO> list = dicService.getDicTree(code);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,list,methodDescribe);
    }
    /**
     * 新增
     * @param dicParam
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增")
    @ApiImplicitParam(name = "dicParam", value = "新增数据", required = true)
    public HttpResult<Boolean> add(@RequestBody @Validated DicParam dicParam) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，数据为：{}", methodDescribe, dicParam);
        boolean result = dicService.addDic(dicParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    /**
     * 修改
     * @param dicParam
     * @return
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PutMapping("/update")
    @ApiOperation("修改")
    @ApiImplicitParam(name = "dicParam", value = "数据", required = true)
    public HttpResult<Boolean> update(@RequestBody @Validated DicParam.UpdateParam dicParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log,  "{},更新的信息为：{}", methodDescribe,dicParam);
        boolean result = dicService.updateDic(dicParam);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.DELETE)
    @DeleteMapping("/delete")
    @ApiOperation("删除")
    @ApiImplicitParam(name = "id", value = "id", required = true)
    public HttpResult<Boolean> delete(@RequestParam @Validated String id) {
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log,  "{},删除的id为：{}", methodDescribe,id);
        boolean result = dicService.deleteDic(id);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    /**
     * 查询
     * @param id
     * @return
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getDicById")
    @ApiOperation("查看详情")
    @ApiImplicitParam(name = "id", value = "id", required = true)
    public HttpResult<Dic> getDicById(String id){
        String methodDescribe = getMethodDescribe("getDicById");
        LogUtil.njcnDebug(log,  "{},id为：{}", methodDescribe,id);
        Dic dic = dicService.getDicById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,dic,methodDescribe);
    }
}
