package com.njcn.user.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.user.pojo.param.ComponentParam;
import com.njcn.user.pojo.po.Component;
import com.njcn.user.pojo.vo.ComponentVO;
import com.njcn.user.service.IComponentService;
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;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hongawen
 * @since 2021-12-13
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/component")
@Api(tags = "组件信息管理")
@AllArgsConstructor
public class ComponentController extends BaseController {

    private final IComponentService componentService;

    /**
     * 新增组件
     * @param componentParam 组件数据
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增组件")
    @ApiImplicitParam(name = "componentParam", value = "组件数据", required = true)
    public HttpResult<Boolean> add(@RequestBody @Validated ComponentParam componentParam) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，用户数据为：{}", methodDescribe, componentParam);
        boolean result = componentService.addComponent(componentParam);
        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 = componentService.deleteComponent(id);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

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

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/componentTree")
    @ApiOperation("组件树")
    public HttpResult<List<ComponentVO>> getComponentTree(){
        String methodDescribe = getMethodDescribe("getComponentTree");
        List<ComponentVO> list = componentService.getComponentTree();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,list,methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/userComponentTree")
    @ApiOperation("用户组件树")
    public HttpResult<List<ComponentVO>> getUserComponentTree(){
        String methodDescribe = getMethodDescribe("getUserComponentTree");
        List<ComponentVO> list = componentService.getUserComponentTree();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,list,methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getFatherComponent")
    @ApiOperation("获取父组件节点")
    public HttpResult<List<Component>> getFatherComponent(@RequestParam @Validated String systemType){
        String methodDescribe = getMethodDescribe("getFatherComponent");
        List<Component> list = componentService.getFatherComponent(systemType);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,list,methodDescribe);
    }

}

