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.exception.BusinessException;
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.DictTreeParam;
import com.njcn.system.pojo.po.SysDicTreePO;
import com.njcn.system.pojo.vo.DictTreeVO;
import com.njcn.system.service.SysDicTreePOService;
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 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 SysDicTreePOService sysDicTreePOService;

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增字典树数据")
    @ApiImplicitParam(name = "dictTreeParam", value = "字典数据", required = true)
    public HttpResult<Object> add(@RequestBody @Validated DictTreeParam dictTreeParam) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，字典数据为：{}", methodDescribe, dictTreeParam);
        boolean result = sysDicTreePOService.addDictTree(dictTreeParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }
    
    /**
     * 修改
     * @param dicParam 修改参数
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PutMapping("/update")
    @ApiOperation("修改")
    @ApiImplicitParam(name = "dicParam", value = "数据", required = true)
    public HttpResult<Boolean> update(@RequestBody @Validated DictTreeParam.DictTreeUpdateParam dicParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log,  "{},更新的信息为：{}", methodDescribe,dicParam);
        boolean result = sysDicTreePOService.updateDictTree(dicParam);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    /**
     * 删除
     * @param id id
     */
    @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 = sysDicTreePOService.deleteDictTree(id);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/query")
    @ApiOperation("根据pid查询字典树")
    public HttpResult<List<DictTreeVO>> query(@RequestParam("pid") String pid) {
        String methodDescribe = getMethodDescribe("query");
        LogUtil.njcnDebug(log, "{}，字典数据为：{}", methodDescribe, pid);
        List<DictTreeVO> result = sysDicTreePOService.queryByPid(pid);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);

    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/queryByCodeList")
    @ApiOperation("根据Code查询字典树")
    public HttpResult<List<SysDicTreePO>> queryByCodeList(@RequestParam("code") String code) {
        String methodDescribe = getMethodDescribe("queryByCodeList");
        List<SysDicTreePO> result = sysDicTreePOService.queryByCodeList(code);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);

    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/queryByCode")
    @ApiOperation("根据Code查询字典树")
    public HttpResult<DictTreeVO> queryByCode(@RequestParam("code") String code) {
        String methodDescribe = getMethodDescribe("queryByCode");
        LogUtil.njcnDebug(log, "{}，字典数据为：{}", methodDescribe, code);
        DictTreeVO result = sysDicTreePOService.queryByCode(code);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);

    }
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/queryByid")
    @ApiOperation("根据id查询字典树最底层")
    public HttpResult<List<DictTreeVO>> queryLastLevelById(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("queryLastLevelById");
        LogUtil.njcnDebug(log, "{}，字典数据为：{}", methodDescribe, id);
        List<DictTreeVO> result = sysDicTreePOService.queryLastLevelById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);

    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/queryById")
    @ApiOperation("根据id查询数据")
    public HttpResult<SysDicTreePO> queryById(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("queryById");
        LogUtil.njcnDebug(log, "{}，字典数据为：{}", methodDescribe, id);
        SysDicTreePO result = sysDicTreePOService.queryById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/queryAll")
    @ApiOperation("查询所有树形字典")
    public HttpResult<List<SysDicTreePO>> queryAll() {
        String methodDescribe = getMethodDescribe("queryAll");
        List<SysDicTreePO> result = sysDicTreePOService.queryAll();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/queryAllByType")
    @ApiOperation("分类查询所有树形字典")
    public HttpResult<List<SysDicTreePO>> queryAllByType(@RequestParam("type")Integer type) {
        String methodDescribe = getMethodDescribe("queryAllByType");
        List<SysDicTreePO> result = sysDicTreePOService.queryAllByType(type);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/queryTree")
    @ApiOperation("树形字典树结构展示")
    public HttpResult<List<SysDicTreePO>> queryTree() {
        String methodDescribe = getMethodDescribe("queryTree");
        List<SysDicTreePO> result = sysDicTreePOService.queryTree();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/queryFirstNode")
    @ApiOperation("查询所有类型大类节点")
    public HttpResult<List<SysDicTreePO>> queryFirstNodeList(@RequestParam("type") Integer type) {
        String methodDescribe = getMethodDescribe("queryFirstNodeList");
        List<SysDicTreePO> result = sysDicTreePOService.queryFirstNodeList(type);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/queryDictType")
    @ApiOperation("获取指标类型")
    @ApiImplicitParams ({
        @ApiImplicitParam(name = "lineType", value = "0:治理监测点 1:电能质量监测点", required = true),
        @ApiImplicitParam(name = "conType", value = "接线方式", required = true)
    })
    public HttpResult<List<SysDicTreePO>> queryDictType(@RequestParam @Validated Integer lineType, @RequestParam(required = false) @Validated Integer conType) {
        String methodDescribe = getMethodDescribe("queryDictType");
        if (conType == null) {
            throw new BusinessException("监测点缺失接线方式");
        }
        List<SysDicTreePO> result = sysDicTreePOService.queryDictType(lineType,conType);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

}

