fangyuan
2022-11-17 059eebfe1c54750e74e290ed7503e2cbf3f2f740
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.ycl.service.user.impl;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.dto.UmsMenuNode;
import com.ycl.entity.dict.DataDictionary;
import com.ycl.entity.user.*;
import com.ycl.mapper.dict.DataDictionaryMapper;
import com.ycl.mapper.user.UmsMenuMapper;
import com.ycl.mapper.user.UmsResourceMapper;
import com.ycl.mapper.user.UmsRoleMapper;
import com.ycl.service.user.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 后台角色管理Service实现类
 * Created by macro on 2018/9/30.
 */
@Service
public class UmsRoleServiceImpl extends ServiceImpl<UmsRoleMapper, UmsRole>implements UmsRoleService {
    @Autowired
    private UmsAdminCacheService adminCacheService;
    @Autowired
    private UmsRoleMenuRelationService roleMenuRelationService;
    @Autowired
    private UmsRoleResourceRelationService roleResourceRelationService;
 
    private UmsMenuService umsMenuService;
    @Autowired
    public void setUmsMenuService(UmsMenuService umsMenuService) {
        this.umsMenuService = umsMenuService;
    }
 
    @Resource
    private UmsMenuMapper umsMenuMapper;
    @Resource
    private UmsResourceMapper umsResourceMapper;
    @Resource
    private DataDictionaryMapper dataDictionaryMapper;
    @Override
    public boolean create(UmsRole role) {
        role.setCreateTime(new Date());
        role.setAdminCount(0);
        role.setSort(0);
        return save(role);
    }
 
    @Override
    public boolean delete(List<Long> ids) {
        boolean success = removeByIds(ids);
        adminCacheService.delResourceListByRoleIds(ids);
        return success;
    }
 
    @Override
    public Page<UmsRole> list(String keyword, Integer pageSize, Integer pageNum) {
        Page<UmsRole> page = new Page<>(pageNum,pageSize);
        QueryWrapper<UmsRole> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<UmsRole> lambda = wrapper.lambda();
        if(StrUtil.isNotEmpty(keyword)){
            lambda.like(UmsRole::getName,keyword);
        }
        Page<UmsRole> resultPage = page(page, wrapper);
        List<UmsRole> roles = resultPage.getRecords();
        List<Long> types = roles.stream().map(UmsRole::getType).collect(Collectors.toList());
 
        LambdaQueryWrapper<DataDictionary> dicQuery = new LambdaQueryWrapper<>();
        dicQuery.in(DataDictionary::getId, types);
        List<DataDictionary> dics = dataDictionaryMapper.selectList(dicQuery);
        for (UmsRole role : roles) {
            for (DataDictionary dic : dics) {
                if (dic.getId().equals(role.getType())) {
                    role.setTypeText(dic.getName());
                }
            }
        }
        return resultPage;
    }
 
    @Override
    public List<UmsMenuNode> getMenuList(Long adminId) {
        List<UmsMenu> menuList = umsMenuMapper.getMenuList(adminId);
        List<UmsMenuNode> result = menuList.stream()
                .filter(menu -> menu.getParentId().equals(0L))
                .map(menu -> umsMenuService.covertMenuNode(menu, menuList)).collect(Collectors.toList());
        return result;
    }
 
 
    @Override
    public List<UmsMenu> listMenu(Long roleId) {
        return umsMenuMapper.getMenuListByRoleId(roleId);
    }
 
    @Override
    public List<UmsResource> listResource(Long roleId) {
        return umsResourceMapper.getResourceListByRoleId(roleId);
    }
 
    @Override
    public int allocMenu(Long roleId, List<Long> menuIds) {
        //先删除原有关系
        QueryWrapper<UmsRoleMenuRelation> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UmsRoleMenuRelation::getRoleId,roleId);
        roleMenuRelationService.remove(wrapper);
        //批量插入新关系
        List<UmsRoleMenuRelation> relationList = new ArrayList<>();
        for (Long menuId : menuIds) {
            UmsRoleMenuRelation relation = new UmsRoleMenuRelation();
            relation.setRoleId(roleId);
            relation.setMenuId(menuId);
            relationList.add(relation);
        }
        roleMenuRelationService.saveBatch(relationList);
        return menuIds.size();
    }
 
    @Override
    public int allocResource(Long roleId, List<Long> resourceIds) {
        //先删除原有关系
        QueryWrapper<UmsRoleResourceRelation> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UmsRoleResourceRelation::getRoleId,roleId);
        roleResourceRelationService.remove(wrapper);
        //批量插入新关系
        List<UmsRoleResourceRelation> relationList = new ArrayList<>();
        for (Long resourceId : resourceIds) {
            UmsRoleResourceRelation relation = new UmsRoleResourceRelation();
            relation.setRoleId(roleId);
            relation.setResourceId(resourceId);
            relationList.add(relation);
        }
        roleResourceRelationService.saveBatch(relationList);
        adminCacheService.delResourceListByRole(roleId);
        return resourceIds.size();
    }
 
    @Override
    public Boolean updateStatusBatch(List<Long> ids, Integer status) {
        List<UmsRole> roles = new ArrayList<>();
        for (Long id : ids) {
            UmsRole umsRole =  UmsRole.builder().
                    id(id).status(status).build();
            roles.add(umsRole);
        }
        updateBatchById(roles,roles.size());
        adminCacheService.delResourceListByRoleIds(ids);
        return true;
    }
}