RUserRoleController.java 1.42 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.RUserRoleDTO;
import vion.model.RUserRole;
import vion.service.IRUserRoleService;

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

/**
 * 用户角色关联
 */
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class RUserRoleController {

    private final IRUserRoleService rUserRoleService;

    @PostMapping("/rUserRole")
    @SaCheckPermission(value = "user:role:edit", orRole = "admin")
    public String saveOrUpdateRUserRole(@RequestBody RUserRoleDTO dto) {
        rUserRoleService.lambdaUpdate().in(RUserRole::getUserId, dto.getUserId()).remove();
        Opt.ofEmptyAble(dto.getRoleIds()).ifPresent(l -> {
            List<RUserRole> list = l.stream().map(roleId -> {
                RUserRole rUserRole = new RUserRole();
                rUserRole.setUserId(dto.getUserId());
                rUserRole.setRoleId(roleId);
                return rUserRole;
            }).collect(Collectors.toList());
            rUserRoleService.saveBatch(list);
        });
        return "成功";
    }
}