package com.njcn.system.controller;

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.HttpResultUtil;
import com.njcn.common.utils.LogUtil;
import com.njcn.system.pojo.param.EventDictParam;
import com.njcn.system.pojo.po.ReportDict;
import com.njcn.system.pojo.vo.EventReportDictVO;
import com.njcn.system.service.IEventDictService;
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;
import java.util.Objects;

/**
 * @author hany
 * @createTime 2022/10/08
 */
@Slf4j
@RestController
@RequestMapping("/reportDict")
@RequiredArgsConstructor
@Api(tags = "报告模板字典树管理")
public class EventDictController extends BaseController {

    private final IEventDictService iEventDictService;

    /**
     * 新增字典
     * @param eventDictParam 参数
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/addDict")
    @ApiOperation("新增字典表")
    @ApiImplicitParam(name = "eventDictParam", value = "字典表数据", required = true)
    public HttpResult<Boolean> addDict(@RequestBody @Validated EventDictParam eventDictParam) {
        String methodDescribe = getMethodDescribe("addDict");
        LogUtil.njcnDebug(log, "{}，字典表数据数据为：{}", methodDescribe, eventDictParam);
        boolean res = iEventDictService.addDict(eventDictParam);
        if(res){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,true,methodDescribe);
        } else{
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false,methodDescribe);
        }
    }

    /**
     * 修改字典
     * @param dictUpdateParam 参数
     */
    @PutMapping("/updateDict")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @ApiOperation("更新字典表")
    @ApiImplicitParam(name = "dictUpdateParam", value = "更新字典表实体", required = true)
    public HttpResult<Boolean> updateDict(@RequestBody EventDictParam.DictUpdateParam dictUpdateParam){
        String methodDescribe = getMethodDescribe("updateDict");
        LogUtil.njcnDebug(log, "{}，字典表数据数据为：{}", methodDescribe, dictUpdateParam);
        boolean res = iEventDictService.updateDict(dictUpdateParam);
        if(res){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,true,methodDescribe);
        } else{
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false,methodDescribe);
        }
    }

    /**
     * 删除字典
     * @param id id
     */
    @DeleteMapping("/deleteDict")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("删除字典表")
    @ApiImplicitParam(name = "id", value = "字典表索引", required = true)
    public HttpResult<Boolean> deleteDict(@RequestParam @Validated String id){
        String methodDescribe = getMethodDescribe("deleteDict");
        LogUtil.njcnDebug(log, "{}，字典id为：{}", methodDescribe, id);
        boolean res = iEventDictService.delete(id);
        if (res){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true,methodDescribe);
        }else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false,methodDescribe);
        }
    }

    /**
     * 查询字典列表
     * @param dictQueryParam 参数
     */
    @PostMapping("/getList")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("查询字典列表")
    @ApiImplicitParam(name = "dictQueryParam", value = "查询字典列表实体", required = true)
    public HttpResult<Page<ReportDict>> getList(@RequestBody EventDictParam.DictQueryParam dictQueryParam){
        String methodDescribe = getMethodDescribe("getList");
        Page<ReportDict> res = iEventDictService.getList(dictQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, res, methodDescribe);
    }


    /**
     * 查询字典列表_新
     * @param dictQueryParam 参数
     */
    @PostMapping("/getReportDictList")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("查询字典列表_新")
    @ApiImplicitParam(name = "dictQueryParam", value = "查询字典列表实体", required = true)
    public HttpResult<List<EventReportDictVO>> getReportDictList(@RequestBody EventDictParam.DictQueryParam dictQueryParam){
        String methodDescribe = getMethodDescribe("getList");
        List<EventReportDictVO> res = iEventDictService.getReportDictList(dictQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, res, methodDescribe);
    }

    /**
     * 查询字典
     * @param id id
     */
    @PostMapping("/getDictById")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("查询字典")
    public HttpResult<ReportDict> getDictById(String id){
        String methodDescribe = getMethodDescribe("getDictById");
        ReportDict res = iEventDictService.getDictById(id);
        if (Objects.nonNull(res)){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, res, methodDescribe);
        }else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL,null, methodDescribe);
        }
    }

    /**
     * 字典树
     */
    @OperateInfo
    @PostMapping("/DictTree")
    @ApiOperation("字典树")
    public HttpResult<List<EventReportDictVO>> getDictTree(@RequestParam @Validated String type){
        String methodDescribe = getMethodDescribe("getDictTree");
        List<EventReportDictVO> list = iEventDictService.getDictTree(type);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,list,methodDescribe);
    }
}
