package com.njcn.gather.system.dictionary.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.LogUtil;
import com.njcn.gather.system.dictionary.pojo.param.DictTreeParam;
import com.njcn.gather.system.dictionary.pojo.po.DictTree;
import com.njcn.gather.system.dictionary.service.IDictTreeService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
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 java.util.List;


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

    private final IDictTreeService dictTreeService;

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getTreeByCode")
    @ApiOperation("按照code查询字典树")
    @ApiImplicitParam(name = "code", value = "查询参数", required = true)
    public HttpResult<List<DictTree>> getTreeByCode(@RequestParam("code") String code) {
        String methodDescribe = getMethodDescribe("getTreeByCode");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, code);
        List<DictTree> result = dictTreeService.getTreeByCode(code);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getTreeByName")
    @ApiOperation("按照name模糊查询字典树")
    @ApiImplicitParam(name = "keyword", value = "查询参数", required = true)
    public HttpResult<List<DictTree>> getTreeByName(@RequestParam("name") String name) {
        String methodDescribe = getMethodDescribe("getTreeByName");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, name);
        List<DictTree> result = dictTreeService.getTreeByName(name);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增字典树数据")
    @ApiImplicitParam(name = "dictTreeParam", value = "字典数据", required = true)
    public HttpResult<Boolean> add(@RequestBody @Validated DictTreeParam dictTreeParam) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，字典数据为：{}", methodDescribe, dictTreeParam);
        boolean result = dictTreeService.addDictTree(dictTreeParam);
        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 = "dicParam", value = "数据", required = true)
    public HttpResult<Boolean> update(@RequestBody @Validated DictTreeParam.UpdateParam dicParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{},更新的信息为：{}", methodDescribe, dicParam);
        boolean result = dictTreeService.updateDictTree(dicParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.DELETE)
    @PostMapping("/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 = dictTreeService.deleteDictTree(id);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }
}


