SystemRoleServiceImpl.java
10.7 KB
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
package com.diligrp.rider.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.diligrp.rider.common.enums.AdminRoleScopeEnum;
import com.diligrp.rider.common.exception.BizException;
import com.diligrp.rider.dto.AdminRoleSaveDTO;
import com.diligrp.rider.entity.AdminUser;
import com.diligrp.rider.entity.Substation;
import com.diligrp.rider.entity.SysRole;
import com.diligrp.rider.entity.SysRoleMenu;
import com.diligrp.rider.mapper.AdminUserMapper;
import com.diligrp.rider.mapper.SubstationMapper;
import com.diligrp.rider.mapper.SysRoleMapper;
import com.diligrp.rider.mapper.SysRoleMenuMapper;
import com.diligrp.rider.service.SystemRoleService;
import com.diligrp.rider.vo.AdminRoleVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@Service
@RequiredArgsConstructor
public class SystemRoleServiceImpl implements SystemRoleService {
/** 内置角色编码(city_id=0 且 code 在此集合内,不允许编辑/删除) */
private static final Set<String> BUILT_IN_CODES = Set.of("platform_admin", "substation_admin");
private final SysRoleMapper sysRoleMapper;
private final SysRoleMenuMapper sysRoleMenuMapper;
private final AdminUserMapper adminUserMapper;
private final SubstationMapper substationMapper;
// ----------------------------------------------------------------
// 平台侧:只看/操作 city_id=0 的全局角色
// ----------------------------------------------------------------
@Override
public List<AdminRoleVO> list(boolean includeDisabled) {
List<SysRole> roles = sysRoleMapper.selectList(new LambdaQueryWrapper<SysRole>()
.eq(SysRole::getCityId, 0L)
.eq(!includeDisabled, SysRole::getStatus, 1)
.orderByAsc(SysRole::getId));
return toVOList(roles);
}
@Override
public void add(AdminRoleSaveDTO dto) {
validateScope(dto.getRoleScope());
ensureUniqueCode(dto.getCode(), null, 0L);
SysRole role = buildRole(dto, 0L);
sysRoleMapper.insert(role);
}
@Override
public void edit(AdminRoleSaveDTO dto) {
SysRole role = requireRole(dto.getId());
if (!role.getCityId().equals(0L)) {
throw new BizException("平台侧不允许编辑分站专属角色");
}
if (isBuiltIn(role)) {
throw new BizException("内置角色不允许编辑");
}
validateScope(dto.getRoleScope());
if (!role.getRoleScope().equals(dto.getRoleScope())) {
throw new BizException("角色范围不允许修改");
}
ensureUniqueCode(dto.getCode(), role.getId(), 0L);
role.setCode(dto.getCode().trim());
role.setName(dto.getName().trim());
sysRoleMapper.updateById(role);
}
@Override
public void ban(Long id) {
SysRole role = requireRole(id);
if (!role.getCityId().equals(0L)) {
throw new BizException("平台侧不允许操作分站专属角色");
}
if (isBuiltIn(role)) {
throw new BizException("内置角色不允许禁用");
}
ensureNotBound(role.getId(), "当前角色已绑定账号,不能禁用");
sysRoleMapper.update(null, new LambdaUpdateWrapper<SysRole>()
.eq(SysRole::getId, id).set(SysRole::getStatus, 0));
}
@Override
public void cancelBan(Long id) {
SysRole role = requireRole(id);
if (!role.getCityId().equals(0L)) {
throw new BizException("平台侧不允许操作分站专属角色");
}
sysRoleMapper.update(null, new LambdaUpdateWrapper<SysRole>()
.eq(SysRole::getId, id).set(SysRole::getStatus, 1));
}
@Override
@Transactional
public void del(Long id) {
SysRole role = requireRole(id);
if (!role.getCityId().equals(0L)) {
throw new BizException("平台侧不允许删除分站专属角色");
}
if (isBuiltIn(role)) {
throw new BizException("内置角色不允许删除");
}
ensureNotBound(role.getId(), "当前角色已绑定账号,不能删除");
sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>()
.eq(SysRoleMenu::getRoleId, role.getId()));
sysRoleMapper.deleteById(role.getId());
}
// ----------------------------------------------------------------
// 分站侧:只看/操作本 cityId 的角色
// ----------------------------------------------------------------
@Override
public List<AdminRoleVO> listByCityId(Long cityId) {
List<SysRole> roles = sysRoleMapper.selectList(new LambdaQueryWrapper<SysRole>()
.eq(SysRole::getCityId, cityId)
.eq(SysRole::getStatus, 1)
.orderByAsc(SysRole::getId));
return toVOList(roles);
}
@Override
public void addForCity(AdminRoleSaveDTO dto, Long cityId) {
// 分站角色只能是 SUBSTATION scope
if (!AdminRoleScopeEnum.SUBSTATION.name().equals(dto.getRoleScope())) {
throw new BizException("分站角色范围只能是 SUBSTATION");
}
ensureUniqueCode(dto.getCode(), null, cityId);
SysRole role = buildRole(dto, cityId);
sysRoleMapper.insert(role);
}
@Override
public void editForCity(AdminRoleSaveDTO dto, Long cityId) {
SysRole role = requireRole(dto.getId());
requireOwnedByCity(role, cityId);
ensureUniqueCode(dto.getCode(), role.getId(), cityId);
role.setCode(dto.getCode().trim());
role.setName(dto.getName().trim());
sysRoleMapper.updateById(role);
}
@Override
public void banForCity(Long id, Long cityId) {
SysRole role = requireRole(id);
requireOwnedByCity(role, cityId);
ensureNotBoundForCity(role.getId(), cityId, "当前角色已绑定账号,不能禁用");
sysRoleMapper.update(null, new LambdaUpdateWrapper<SysRole>()
.eq(SysRole::getId, id).set(SysRole::getStatus, 0));
}
@Override
public void cancelBanForCity(Long id, Long cityId) {
SysRole role = requireRole(id);
requireOwnedByCity(role, cityId);
sysRoleMapper.update(null, new LambdaUpdateWrapper<SysRole>()
.eq(SysRole::getId, id).set(SysRole::getStatus, 1));
}
@Override
@Transactional
public void delForCity(Long id, Long cityId) {
SysRole role = requireRole(id);
requireOwnedByCity(role, cityId);
ensureNotBoundForCity(role.getId(), cityId, "当前角色已绑定账号,不能删除");
sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>()
.eq(SysRoleMenu::getRoleId, role.getId()));
sysRoleMapper.deleteById(role.getId());
}
// ----------------------------------------------------------------
// 私有工具方法
// ----------------------------------------------------------------
private SysRole requireRole(Long id) {
if (id == null || id < 1) {
throw new BizException("角色ID不能为空");
}
SysRole role = sysRoleMapper.selectById(id);
if (role == null) {
throw new BizException("角色不存在");
}
return role;
}
private void requireOwnedByCity(SysRole role, Long cityId) {
if (!cityId.equals(role.getCityId())) {
throw new BizException("无权操作其他租户的角色");
}
}
private void validateScope(String scope) {
for (AdminRoleScopeEnum value : AdminRoleScopeEnum.values()) {
if (value.name().equals(scope)) {
return;
}
}
throw new BizException("角色范围不合法");
}
private void ensureUniqueCode(String code, Long excludeId, Long cityId) {
SysRole duplicate = sysRoleMapper.selectOne(new LambdaQueryWrapper<SysRole>()
.eq(SysRole::getCode, code.trim())
.eq(SysRole::getCityId, cityId)
.ne(excludeId != null, SysRole::getId, excludeId)
.last("LIMIT 1"));
if (duplicate != null) {
throw new BizException("角色编码已存在");
}
}
private void ensureNotBound(Long roleId, String message) {
if (countAdminUsers(roleId) > 0 || countSubstations(roleId) > 0) {
throw new BizException(message);
}
}
private void ensureNotBoundForCity(Long roleId, Long cityId, String message) {
Long count = substationMapper.selectCount(new LambdaQueryWrapper<Substation>()
.eq(Substation::getRoleId, roleId)
.eq(Substation::getCityId, cityId));
if (count != null && count > 0) {
throw new BizException(message);
}
}
private SysRole buildRole(AdminRoleSaveDTO dto, Long cityId) {
SysRole role = new SysRole();
role.setCode(dto.getCode().trim());
role.setName(dto.getName().trim());
role.setRoleScope(dto.getRoleScope());
role.setCityId(cityId);
role.setStatus(1);
role.setCreateTime(System.currentTimeMillis() / 1000);
return role;
}
private List<AdminRoleVO> toVOList(List<SysRole> roles) {
List<AdminRoleVO> result = new ArrayList<>();
for (SysRole role : roles) {
result.add(toVO(role));
}
return result;
}
private AdminRoleVO toVO(SysRole role) {
AdminRoleVO vo = new AdminRoleVO();
vo.setId(role.getId());
vo.setCode(role.getCode());
vo.setName(role.getName());
vo.setRoleScope(role.getRoleScope());
vo.setStatus(role.getStatus());
vo.setBuiltIn(isBuiltIn(role));
vo.setAdminUserCount(countAdminUsers(role.getId()));
vo.setSubstationCount(countSubstations(role.getId()));
vo.setCreateTime(role.getCreateTime());
return vo;
}
private boolean isBuiltIn(SysRole role) {
// 只有全局角色(city_id=0)且编码匹配才是内置角色
return role.getCityId() != null && role.getCityId() == 0L
&& BUILT_IN_CODES.contains(role.getCode());
}
private long countAdminUsers(Long roleId) {
Long count = adminUserMapper.selectCount(new LambdaQueryWrapper<AdminUser>()
.eq(AdminUser::getRoleId, roleId));
return count == null ? 0L : count;
}
private long countSubstations(Long roleId) {
Long count = substationMapper.selectCount(new LambdaQueryWrapper<Substation>()
.eq(Substation::getRoleId, roleId));
return count == null ? 0L : count;
}
}