医院废弃物信息化管理平台
“mitao”
2025-12-24 3763145f34d7706923d55edc32f5cfbc8bc08b92
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
package cn.besteam.modules.system.biz;
 
import cn.besteam.common.constant.CannotDeleteRoleEnum;
import cn.besteam.common.constant.ErrorCode;
import cn.besteam.common.exception.BusinessException;
import cn.besteam.modules.shiro.authc.util.EnvUtils;
import cn.besteam.modules.system.entity.*;
import cn.besteam.modules.system.mapper.SysRoleMapper;
import cn.besteam.modules.system.service.*;
import cn.besteam.modules.system.vo.BatchReqVO;
import cn.besteam.modules.system.vo.SysRoleVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
/**
 * @author ltri
 * @date 2020/1/7 2:30 PM
 */
@Service
public class SysRoleBiz {
 
    @Autowired
    private ISysRoleService roleService;
 
    @Autowired
    private ISysUserRoleService userRoleService;
 
    @Autowired
    private SysRoleMapper roleMapper;
 
    @Autowired
    private ISysRolePermissionService rolePermissionService;
 
    @Autowired
    private ISysUserService sysUserService;
 
    @Autowired
    private ISysPermissionService sysPermissionService;
 
    /**
     * 列表查询
     *
     * @param keyword
     * @param type
     * @return
     */
    public List<SysRoleVO> listRole(String keyword, Integer type) {
 
        SysUserRole sysUserRole = userRoleService.getOne(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getUserId, EnvUtils.getUser().getId()));
        if (sysUserRole == null) {
            return new ArrayList<>();
        }
        SysPermission sysPermission = null;
        if(type!=null) {
            if (type.equals(1) || type.equals(-1)) {
                sysPermission = sysPermissionService.getOne(new QueryWrapper<SysPermission>().lambda().eq(SysPermission::getPerms, "role:operate:list"));
            } else {
                sysPermission = sysPermissionService.getOne(new QueryWrapper<SysPermission>().lambda().eq(SysPermission::getPerms, "role:user:list"));
            }
            if (sysPermission == null) {
                return new ArrayList<>();
            }
            SysRolePermission rolePermission = rolePermissionService.getOne(new QueryWrapper<SysRolePermission>().lambda().eq(SysRolePermission::getRoleId, sysUserRole.getRoleId()).eq(SysRolePermission::getPermissionId, sysPermission.getId()));
            if (type.equals(1) || type.equals(2)) {
                if (rolePermission == null) {
                    return new ArrayList<>();
                }
            }
        }
        List<SysRoleVO> sysRoleList = roleMapper.listRole(keyword, type);
        //获取角色id集合
        List<Integer> roleIds = sysRoleList.stream().map(SysRoleVO::getId).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(roleIds)) {
            //获取存在角色关联的set
            Set<Integer> set = userRoleService.list(Wrappers.<SysUserRole>lambdaQuery().in(SysUserRole::getRoleId, roleIds)).stream().map(SysUserRole::getRoleId).collect(Collectors.toSet());
            //返回删除标识符
            sysRoleList.forEach(sysRoleVO -> {
                sysRoleVO.setDelFlag(set.contains(sysRoleVO.getId()) ? 0 : 1);
            });
        }
        return sysRoleList;
    }
 
    /**
     * 角色管理-新增/编辑
     *
     * @param dto
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(SysRole dto) {
 
        List<Integer> roleTypeList = userRoleService.getRoleTypeList(EnvUtils.getUser().getId());
        if (CollectionUtils.isEmpty(roleTypeList)) {
            throw new BusinessException(ErrorCode.ERR_500, "无操作权限");
        }
        if (!roleTypeList.contains(dto.getType())) {
            throw new BusinessException(ErrorCode.ERR_500, "无操作权限");
        }
        //新增
        if (dto.getId() == null || dto.getId() <= 0) {
            if (StringUtils.isNotBlank(dto.getRoleName())) {
                int count = roleService.count(Wrappers.<SysRole>lambdaQuery().eq(SysRole::getRoleName, dto.getRoleName()));
                if (count > 0) {
                    throw new BusinessException(ErrorCode.ERR_500, "角色名称重复,请重新输入");
                }
            }
            SysRole sysRole = new SysRole();
            BeanUtils.copyProperties(dto, sysRole);
            roleService.save(sysRole);
            //
            SysRolePermission sysRolePermission = new SysRolePermission(sysRole.getId(), 6);
            rolePermissionService.save(sysRolePermission);
        } else {
            SysRole sysRole = roleService.getById(dto.getId());
            if (sysRole == null) {
                throw new BusinessException(ErrorCode.ROLE_NOT_FOUND);
            }
            SysRole sysRole1 = roleService.getOne(new QueryWrapper<SysRole>().lambda().eq(SysRole::getRoleName, dto.getRoleName()));
            if (sysRole1 != null && !sysRole.getId().equals(sysRole1.getId())) {
                throw new BusinessException(ErrorCode.ERR_500, "角色名称重复,请重新输入");
            }
            if (CannotDeleteRoleEnum.SET.contains(dto.getRoleName())) {
                throw new BusinessException(ErrorCode.ROLE_CAN_NOT_EDIT);
            }
            BeanUtils.copyProperties(dto, sysRole);
            roleService.updateById(sysRole);
        }
    }
 
    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRole(String id) {
 
        SysRole sysRole = roleService.getById(id);
        if (sysRole == null) {
            throw new BusinessException(ErrorCode.ROLE_NOT_FOUND);
        }
        if (CannotDeleteRoleEnum.SET.contains(sysRole.getRoleName())) {
            throw new BusinessException(ErrorCode.ROLE_CAN_NOT_DELETE);
        }
        int count = userRoleService.count(Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getRoleId, id));
        if (count > 0) {
            throw new BusinessException(ErrorCode.ROLE_CAN_NOT_DELETE);
        }
        //删除角色和权限关联
        rolePermissionService.remove(Wrappers.<SysRolePermission>lambdaQuery().eq(SysRolePermission::getRoleId, id));
        //删除角色
        return roleService.removeById(id);
    }
 
    /**
     * 批量删除
     *
     * @param vo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDelete(BatchReqVO vo) {
 
        if (StringUtils.isEmpty(vo.getIds())) {
            throw new BusinessException(ErrorCode.SYSTEM_REQUEST_PARAM_ERROR);
        }
        List<String> deleteIds = Arrays.asList(vo.getIds().split(","));
        List<SysRole> list = roleService.list(Wrappers.<SysRole>lambdaQuery().in(SysRole::getId, deleteIds));
        list.forEach(sysRole -> {
            if (CannotDeleteRoleEnum.SET.contains(sysRole.getRoleName())) {
                throw new BusinessException(ErrorCode.ROLE_CAN_NOT_DELETE);
            }
        });
        deleteIds.forEach(i -> {
            int count = userRoleService.count(Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getRoleId, i));
            if (count > 0) {
                throw new BusinessException(ErrorCode.ROLE_CAN_NOT_DELETE);
            }
        });
        //删除角色和权限关联
        rolePermissionService.remove(Wrappers.<SysRolePermission>lambdaQuery().in(SysRolePermission::getRoleId, deleteIds));
        //删除角色
        return roleService.remove(Wrappers.<SysRole>lambdaQuery().in(SysRole::getId, deleteIds));
    }
 
    public Integer countUserByRole(Integer roleId) {
 
        Integer count = 0;
        List<SysUserRole> list = userRoleService.list(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getRoleId, roleId));
        for (SysUserRole sysUserRole : list) {
            SysUser sysUser = sysUserService.getById(sysUserRole.getUserId());
            if (sysUser != null && sysUser.getDelFlag().equals("0")) {
                count++;
            }
        }
        return count;
    }
 
}