Pu Zhibing
2025-03-26 7f26677ab7f9b83697370fa142dd1686cdf4082a
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package com.ruoyi.system.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.BasePage;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.SysRole;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.GetSysRoleByIds;
import com.ruoyi.system.domain.SysMenus;
import com.ruoyi.system.domain.SysRoleMenu;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.domain.dto.RoleAddDto;
import com.ruoyi.system.domain.dto.RoleUpdateDto;
import com.ruoyi.system.domain.vo.RoleInfoVo;
import com.ruoyi.system.mapper.SysMenuMapper;
import com.ruoyi.system.mapper.SysRoleMenuMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserRoleService;
import com.ruoyi.system.service.ISysUserService;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 角色信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色模块")
public class SysRoleController extends BaseController {
    @Resource
    private ISysRoleService roleService;
    
    @Resource
    private ISysUserService userService;
    
    @Resource
    private ISysDeptService deptService;
    
    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;
    
    @Resource
    private ISysUserRoleService sysUserRoleService;
    
    @Resource
    private SysMenuMapper menuMapper;
    
    @Resource
    private TokenService tokenService;
    
    @Resource
    private ISysUserService sysUserService;
    
    
    @ApiOperation(value = "获取角色列表", tags = {"管理后台-账号管理", "门店后台-账号管理"})
    @GetMapping("/list")
    public AjaxResult list() {
        Long userid = tokenService.getLoginUser().getUserid();
        SysUser sysUser = sysUserService.getById(userid);
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<SysRole>()
                .ne(SysRole::getRoleId, 2)
                .eq(SysRole::getDelFlag, 0)
                .eq(SysRole::getStatus, 0);
        if (sysUser.getRoleType() == 2) {
            wrapper.eq(SysRole::getShopId, sysUser.getObjectId());
        }else {
            wrapper.isNull(SysRole::getShopId);
        }
        List<SysRole> list = roleService.list(wrapper);
        return AjaxResult.success(list);
    }
    
    
    @ResponseBody
    @GetMapping("/listPage")
    @ApiOperation(value = "获取角色列表", tags = {"管理后台-权限管理", "门店后台-权限管理"})
    public AjaxResult listPage(String name, BasePage basePage) {
        PageInfo<SysRole> pageInfo = new PageInfo<>(basePage.getPageCurr(), basePage.getPageSize());
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<SysRole>().eq(SysRole::getStatus, 0).eq(SysRole::getDelFlag, 0);
        if (StringUtils.isNotEmpty(name)) {
            wrapper.like(SysRole::getRoleName, name);
        }
        Long userid = tokenService.getLoginUser().getUserid();
        SysUser sysUser = sysUserService.getById(userid);
        if(sysUser.getRoleType() == 2){
            wrapper.eq(SysRole::getShopId, sysUser.getObjectId());
        }else{
            wrapper.isNull(SysRole::getShopId);
        }
        wrapper.ne(SysRole::getRoleId,2);//过滤门店管理员
        PageInfo<SysRole> page = roleService.page(pageInfo, wrapper.orderByDesc(SysRole::getCreateTime));
        for (SysRole record : page.getRecords()) {
            long count = sysUserRoleService.count(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getRoleId, record.getRoleId()));
            record.setNumber(count);
        }
        return AjaxResult.success(page);
    }
    
    
    @ResponseBody
    @PostMapping("/roleAdd")
    @ApiOperation(value = "添加角色", tags = {"管理后台-权限管理", "门店后台-权限管理"})
    public AjaxResult roleAdd(@Validated @RequestBody RoleAddDto dto) {
        SysRole role = new SysRole();
        role.setRoleName(dto.getRoleName());
        LambdaQueryWrapper<SysRole> wrapper = Wrappers.lambdaQuery(SysRole.class)
                .eq(SysRole::getRoleName, dto.getRoleName()).eq(SysRole::getDelFlag, 0);
        Long userid = tokenService.getLoginUser().getUserid();
        SysUser sysUser = sysUserService.getById(userid);
        if(sysUser.getRoleType() == 2){
            wrapper.eq(SysRole::getShopId, sysUser.getObjectId());
        }
        
        long count = roleService.count(wrapper);
        if (count > 0) {
            return AjaxResult.error("角色已存在,请重新输入");
        }
        List<Long> menuIds1 = dto.getMenuIds();
        if (CollectionUtils.isEmpty(menuIds1)) {
            return AjaxResult.error("菜单id不能为空");
        }
        role.setMenuIds(dto.getMenuIds().toArray((new Long[dto.getMenuIds().size()])));
        // 添加角色
        role.setRemark(dto.getRemark());
        role.setCreateBy(SecurityUtils.getUsername());
        role.setCreateTime(new Date());
        
        if(sysUser.getRoleType() == 2){
            role.setShopId(sysUser.getObjectId());
        }
        roleService.insertRole(role);
        return AjaxResult.success();
    }
    
    
    @GetMapping("/roleInfo")
    @ApiOperation(value = "角色详情", tags = {"管理后台-权限管理"})
    public AjaxResult roleInfo(@RequestParam Long id) {
        SysRole role = roleService.selectRoleById(id);
        RoleInfoVo roleInfoVo = new RoleInfoVo();
        roleInfoVo.setRoleId(role.getRoleId());
        roleInfoVo.setRoleName(role.getRoleName());
        // 获取当前角色的菜单id
        List<Long> menusId = sysRoleMenuMapper.selectList(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, id)).stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
        if (menusId.size() == 0) {
            return AjaxResult.success(new ArrayList<>());
        }
        //获取当前的权限菜单
        List<SysMenus> all = menuMapper.getAllInIds(menusId);
        // 第三级
        List<SysMenus> s3 = all.stream().filter(e -> e.getMenuType().equals("F")).collect(Collectors.toList());
        // 第二级
        List<SysMenus> s2 = all.stream().filter(e -> e.getMenuType().equals("C")).collect(Collectors.toList());
        // 第一级
        List<SysMenus> s1 = all.stream().filter(e -> e.getMenuType().equals("M")).collect(Collectors.toList());
        
        for (SysMenus menus : s2) {
            List<SysMenus> collect = s3.stream().filter(e -> e.getParentId().equals(menus.getMenuId())).collect(Collectors.toList());
            menus.setChildren(collect);
        }
        
        for (SysMenus menus : s1) {
            List<SysMenus> collect = s2.stream().filter(e -> e.getParentId().equals(menus.getMenuId())).collect(Collectors.toList());
            menus.setChildren(collect);
        }
        roleInfoVo.setMenus(menusId);
        roleInfoVo.setRemark(role.getRemark());
        return AjaxResult.success(roleInfoVo);
    }
    
    
    @PostMapping("/roleUpdate")
    @ApiOperation(value = "编辑角色", tags = {"管理后台-权限管理"})
    public AjaxResult roleUpdate(@Validated @RequestBody RoleUpdateDto dto) {
        SysRole role = new SysRole();
        role.setRoleName(dto.getRoleName());
        SysRole one = roleService.getOne(new LambdaQueryWrapper<SysRole>().eq(SysRole::getRoleName, dto.getRoleName()));
        if (null != one && !one.getRoleId().equals(dto.getRoleId())) {
            return AjaxResult.error("角色已存在,请重新输入");
        }
        role.setRemark(dto.getRemark());
        role.setUpdateBy(SecurityUtils.getUsername());
        role.setUpdateTime(new Date());
        role.setRoleId(dto.getRoleId());
        roleService.updateRole(role);
        ArrayList<SysRoleMenu> sysRoleMenus = new ArrayList<>();
        List<Long> menuIds = dto.getMenuIds();
        // 移除原来的权限菜单
        if (menuIds.contains(1061L)) {
            sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>()
                    .eq(SysRoleMenu::getRoleId, dto.getRoleId()));
        } else {
            sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>()
                    .eq(SysRoleMenu::getRoleId, dto.getRoleId())
                    .ne(SysRoleMenu::getMenuId, 1061L)
                    .ne(SysRoleMenu::getMenuId, 1062L)
                    .ne(SysRoleMenu::getMenuId, 1065L)
                    .ne(SysRoleMenu::getMenuId, 1073L)
                    .ne(SysRoleMenu::getMenuId, 1161L)
                    .ne(SysRoleMenu::getMenuId, 1203L)
            );
        }
        for (Long menuId : menuIds) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setRoleId(role.getRoleId());
            sysRoleMenus.add(sysRoleMenu);
        }
        sysRoleMenuMapper.batchRoleMenu(sysRoleMenus);
        return AjaxResult.success();
    }
    
    
    /**
     * 删除角色
     */
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleIds}")
    @ApiOperation(value = "删除角色", tags = {"管理后台-权限管理"})
    public AjaxResult remove(@PathVariable Long[] roleIds) {
        return toAjax(roleService.deleteRoleByIds(roleIds));
    }
    
    
    /**
     * 查询已分配用户角色列表
     */
    @GetMapping("/authUser/allocatedList")
    public TableDataInfo allocatedList(SysUser user) {
        startPage();
        List<SysUser> list = userService.selectAllocatedList(user);
        return getDataTable(list);
    }
    
    /**
     * 查询未分配用户角色列表
     */
    @GetMapping("/authUser/unallocatedList")
    public TableDataInfo unallocatedList(SysUser user) {
        startPage();
        List<SysUser> list = userService.selectUnallocatedList(user);
        return getDataTable(list);
    }
    
    
    /**
     * 获取对应角色部门树列表
     */
    @GetMapping(value = "/deptTree/{roleId}")
    public AjaxResult deptTree(@PathVariable("roleId") Long roleId) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
        ajax.put("depts", deptService.selectDeptTreeList(new SysDept()));
        return ajax;
    }
    
    
    /**
     * 根据id集合获取数据
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/getSysRoleByIds")
    public R<List<SysRole>> getSysRoleByIds(@RequestBody GetSysRoleByIds ids) {
        List<SysRole> sysRoleByIds = roleService.getSysRoleByIds(ids.getIds());
        return R.ok(sysRoleByIds);
    }
}