whycxzp
2021-04-15 e4e8380597b5abdd2ede656cf5291c5d760b1149
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
52
53
54
55
56
57
58
59
60
61
62
package com.whyc.service;
 
import com.whyc.mapper.UserRoleMapper;
import com.whyc.pojo.Role;
import com.whyc.pojo.User;
import com.whyc.pojo.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
 
/**
 *  用户对应角色
 */
@Service
public class UserRoleService {
 
    @Resource
    private UserRoleMapper mapper;
 
    public List<User> getUserWithNoRole() {
        return mapper.getUserWithNoRole();
    }
 
    public List<UserRole> getUserWithRole() {
        List<UserRole> userRoles = mapper.getUserWithRole();
        return userRoles;
    }
 
    public Map<String,List<UserRole>> getUserWithRoleMap() {
        HashMap<String, List<UserRole>> map = new HashMap<>();
        List<UserRole> userRoleList = new LinkedList<>();
 
        List<UserRole> userRoles= mapper.getUserWithRoleMap();
        for (UserRole temp:userRoles){
            String[] userIds = temp.getUserIds().split(",");
            String[] userNames = temp.getUserNames().split(",");
            for (int i = 0; i < userIds.length; i++) {
                UserRole userRole = new UserRole();
                userRole.setUser(new User(Integer.parseInt(userIds[i]),userNames[i]));
                userRole.setRoleId(temp.getRoleId());
                userRole.setRoleName(temp.getRoleName());
                userRoleList.add(userRole);
            }
            map.put(temp.getRoleName(),userRoleList);
        }
 
        return map;
    }
 
    public boolean bindingUserWithRole(int userId,int roleId) {
        return mapper.insert(new UserRole(userId,roleId))>0;
    }
 
    public boolean bindingUserWithRoleBatch(List<UserRole> userRoles) {
        return mapper.insertBatchSomeColumn(userRoles)==userRoles.size();
    }
}