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.RoleParam;
import com.njcn.user.service.IRoleComponentService;
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 com.njcn.web.controller.BaseController;

import java.util.List;

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

    private final IRoleComponentService roleComponentService;

    /**
     * 根据角色id查询相关联的组件
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/selectRoleComponent")
    @ApiOperation("角色组件树")
    @ApiImplicitParam(name = "idList", value = "角色索引集合", required = true)
    public HttpResult<List<String>> selectRoleComponent(@RequestParam("idList") List<String> idList) {
        String methodDescribe = getMethodDescribe("selectRoleComponent");
        LogUtil.njcnDebug(log, "{}，传入的角色id集合：{}", methodDescribe, idList);
        List<String> result = roleComponentService.selectRoleComponent(idList);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(operateType = OperateType.UPDATE)
    @PostMapping("/assignComponentsByRoleIndex")
    @ApiOperation("角色分配组件")
    @ApiImplicitParam(name = "roleFunctionComponent", value = "角色信息", required = true)
    public HttpResult<Boolean> assignComponentsByRoleIndex(@RequestBody @Validated RoleParam.RoleFunctionComponent roleFunctionComponent) {
        String methodDescribe = getMethodDescribe("assignComponentsByRoleIndex");
        LogUtil.njcnDebug(log,  "{},传入的角色Guid：{},传入的组件集合为：{}", methodDescribe,roleFunctionComponent);
        boolean result = roleComponentService.updateRoleComponent(roleFunctionComponent);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

}

