RRoleResourceController.java 1.51 KB
package vion.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.dromara.hutool.core.lang.Opt;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vion.dto.RRoleResourceDTO;
import vion.model.RRoleResource;
import vion.service.IRRoleResourceService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色资源关联
 */
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class RRoleResourceController {

    private final IRRoleResourceService roleResourceService;

    @PostMapping("/rRoleRes")
    @SaCheckPermission(value = "role:resource:edit", orRole = "admin")
    public String saveOrUpdateRUserRole(@RequestBody RRoleResourceDTO dto) {
        roleResourceService.lambdaUpdate().in(RRoleResource::getRoleId, dto.getRoleId()).remove();
        Opt.ofEmptyAble(dto.getResourceIds()).ifPresent(l -> {
            List<RRoleResource> list = l.stream().map(resourceId -> {
                RRoleResource rRoleResource = new RRoleResource();
                rRoleResource.setRoleId(dto.getRoleId());
                rRoleResource.setResourceId(resourceId);
                return rRoleResource;
            }).collect(Collectors.toList());
            roleResourceService.saveBatch(list);
        });
        return "成功";
    }

}