Air
2024-11-04 a1f06d31b7b4cac569c34bdfbb68de77f2858ffe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.nova.sankuai.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nova.sankuai.domain.entity.Role;
import com.nova.sankuai.domain.entity.RoleUser;
import com.nova.sankuai.infra.mapper.RoleUserMapper;
import com.nova.sankuai.service.IRoleService;
import com.nova.sankuai.service.IRoleUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * <p>
 * description
 * </p>
 *
 * @author denglb 2021/11/19
 */
@Service
@AllArgsConstructor
public class RoleUserServiceImpl extends ServiceImpl<RoleUserMapper, RoleUser> implements IRoleUserService {
 
    private RoleUserMapper roleUserMapper;
 
    private IRoleService roleService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void assignRoles(List<Long> roleIds, Long userId) {
        roleUserMapper.deleteByUserId(userId);
        if (roleIds.size() > 0) {
            roleIds = roleIds.stream().distinct().collect(Collectors.toList());
            roleUserMapper.assignRoles(roleIds, userId);
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setDefaultRole(Long userId, Integer type) {
        Role role = roleService.lambdaQuery().eq(Role::getRoleCode, type).one();
        List<Long> roleIds = new ArrayList<Long>() {{
            add(role.getId());
        }};
        roleUserMapper.assignRoles(roleIds, userId);
    }
}