Commit b33302791198d4d29a5784e5e1605aa7dc6345e9
1 parent
26f0664e
增加接口
Showing
5 changed files
with
168 additions
and
13 deletions
itcast-auth/itcast-auth-server/src/main/java/com/itheima/authority/biz/service/auth/UserService.java
| ... | ... | @@ -2,6 +2,7 @@ package com.itheima.authority.biz.service.auth; |
| 2 | 2 | |
| 3 | 3 | import com.baomidou.mybatisplus.core.metadata.IPage; |
| 4 | 4 | import com.baomidou.mybatisplus.extension.service.IService; |
| 5 | +import com.itheima.authority.dto.auth.HierarchyDTO; | |
| 5 | 6 | import com.itheima.authority.dto.auth.LoginDTO; |
| 6 | 7 | import com.itheima.authority.dto.auth.UserUpdatePasswordDTO; |
| 7 | 8 | import com.itheima.authority.entity.auth.User; |
| ... | ... | @@ -50,4 +51,6 @@ public interface UserService extends IService<User> { |
| 50 | 51 | void updateUserRole(User user); |
| 51 | 52 | |
| 52 | 53 | R<LoginDTO> current(); |
| 54 | + | |
| 55 | + HierarchyDTO findHierarchy(Long id); | |
| 53 | 56 | } | ... | ... |
itcast-auth/itcast-auth-server/src/main/java/com/itheima/authority/biz/service/auth/impl/UserServiceImpl.java
| ... | ... | @@ -15,10 +15,7 @@ import com.itheima.authority.biz.service.auth.UserRoleService; |
| 15 | 15 | import com.itheima.authority.biz.service.auth.UserService; |
| 16 | 16 | import com.itheima.authority.biz.service.core.OrgService; |
| 17 | 17 | import com.itheima.authority.biz.service.core.StationService; |
| 18 | -import com.itheima.authority.dto.auth.LoginDTO; | |
| 19 | -import com.itheima.authority.dto.auth.ResourceQueryDTO; | |
| 20 | -import com.itheima.authority.dto.auth.UserDTO; | |
| 21 | -import com.itheima.authority.dto.auth.UserUpdatePasswordDTO; | |
| 18 | +import com.itheima.authority.dto.auth.*; | |
| 22 | 19 | import com.itheima.authority.entity.auth.*; |
| 23 | 20 | import com.itheima.authority.entity.core.Org; |
| 24 | 21 | import com.itheima.authority.entity.core.Station; |
| ... | ... | @@ -31,11 +28,14 @@ import com.itheima.tools.database.mybatis.auth.DataScopeType; |
| 31 | 28 | import com.itheima.tools.database.mybatis.conditions.Wraps; |
| 32 | 29 | import com.itheima.tools.database.mybatis.conditions.query.LbqWrapper; |
| 33 | 30 | import com.itheima.tools.dozer.DozerUtils; |
| 31 | +import com.itheima.tools.exception.BizException; | |
| 32 | +import com.itheima.tools.exception.code.ExceptionCode; | |
| 34 | 33 | import com.itheima.tools.utils.BizAssert; |
| 35 | 34 | import lombok.SneakyThrows; |
| 36 | 35 | import lombok.extern.slf4j.Slf4j; |
| 37 | 36 | import org.apache.commons.codec.digest.DigestUtils; |
| 38 | 37 | import org.apache.commons.lang3.StringUtils; |
| 38 | +import org.springframework.beans.BeanUtils; | |
| 39 | 39 | import org.springframework.beans.factory.annotation.Autowired; |
| 40 | 40 | import org.springframework.stereotype.Service; |
| 41 | 41 | import org.springframework.util.CollectionUtils; |
| ... | ... | @@ -237,6 +237,98 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us |
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | /** |
| 240 | + * 获取人员关系结构 | |
| 241 | + * | |
| 242 | + * @param id | |
| 243 | + * @return | |
| 244 | + */ | |
| 245 | + @Override | |
| 246 | + public HierarchyDTO findHierarchy(Long id) { | |
| 247 | + HierarchyDTO hierarchyDTO = new HierarchyDTO(); | |
| 248 | + User user = this.getById(id); | |
| 249 | + Station station = stationService.getById(user.getStationId()); | |
| 250 | + if (station != null) { | |
| 251 | + user.setStationName(station.getName()); | |
| 252 | + } | |
| 253 | + log.info("当前用户:{}", user); | |
| 254 | + Stack<User> stack = new Stack<>(); | |
| 255 | + getSuperiorStack(stack, user, 1); | |
| 256 | + | |
| 257 | + List<User> list = new ArrayList<>(); | |
| 258 | + getDownwardList(list, user); | |
| 259 | + | |
| 260 | + buildTree(stack, list, hierarchyDTO); | |
| 261 | + return hierarchyDTO; | |
| 262 | + } | |
| 263 | + | |
| 264 | + private void buildTree(Stack<User> stack, List<User> list, HierarchyDTO hierarchyDTO) { | |
| 265 | + if (stack == null || stack.size() == 0) { | |
| 266 | + return; | |
| 267 | + } | |
| 268 | + User user = stack.pop(); | |
| 269 | + if (stack.size() == 0) { | |
| 270 | + BeanUtils.copyProperties(user, hierarchyDTO); | |
| 271 | + hierarchyDTO.setSelf(true); | |
| 272 | + hierarchyDTO.setChildren(list.stream().map(item -> { | |
| 273 | + HierarchyDTO hierarchy = new HierarchyDTO(); | |
| 274 | + BeanUtils.copyProperties(item, hierarchy); | |
| 275 | + return hierarchy; | |
| 276 | + }).collect(Collectors.toList())); | |
| 277 | + } else { | |
| 278 | + BeanUtils.copyProperties(user, hierarchyDTO); | |
| 279 | + | |
| 280 | + HierarchyDTO hierarchy = new HierarchyDTO(); | |
| 281 | + hierarchyDTO.setChildren(Arrays.asList(new HierarchyDTO[]{hierarchy})); | |
| 282 | + | |
| 283 | + buildTree(stack, list, hierarchy); | |
| 284 | + } | |
| 285 | + | |
| 286 | + } | |
| 287 | + | |
| 288 | + /** | |
| 289 | + * 获取下级的员工集合 | |
| 290 | + * | |
| 291 | + * @param list | |
| 292 | + * @param user | |
| 293 | + */ | |
| 294 | + private void getDownwardList(List<User> list, User user) { | |
| 295 | + LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>(); | |
| 296 | + wrapper.eq(User::getSuperior, user.getId()); | |
| 297 | + List<User> users = this.list(wrapper); | |
| 298 | + list.addAll(users.stream().map(item -> { | |
| 299 | + Station station = stationService.getById(item.getStationId()); | |
| 300 | + if (station != null) { | |
| 301 | + item.setStationName(station.getName()); | |
| 302 | + } | |
| 303 | + return item; | |
| 304 | + }).collect(Collectors.toList())); | |
| 305 | + } | |
| 306 | + | |
| 307 | + /** | |
| 308 | + * 获取上级关系,默认最多20级,防止内存泄露 | |
| 309 | + * | |
| 310 | + * @param stack | |
| 311 | + * @param user | |
| 312 | + * @param depth | |
| 313 | + */ | |
| 314 | + private void getSuperiorStack(Stack stack, User user, int depth) { | |
| 315 | + stack.add(user); | |
| 316 | + if (depth > 20) { | |
| 317 | + return; | |
| 318 | + } | |
| 319 | + Long superior = user.getSuperior(); | |
| 320 | + if (null == superior || superior < 0L) { | |
| 321 | + return; | |
| 322 | + } | |
| 323 | + User userSuperior = this.getById(superior); | |
| 324 | + Station station = stationService.getById(userSuperior.getStationId()); | |
| 325 | + if (station != null) { | |
| 326 | + userSuperior.setStationName(station.getName()); | |
| 327 | + } | |
| 328 | + getSuperiorStack(stack, userSuperior, ++depth); | |
| 329 | + } | |
| 330 | + | |
| 331 | + /** | |
| 240 | 332 | * 修改密码 |
| 241 | 333 | * |
| 242 | 334 | * @param data |
| ... | ... | @@ -356,6 +448,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us |
| 356 | 448 | |
| 357 | 449 | user.setPassword(DigestUtils.md5Hex(user.getPassword())); |
| 358 | 450 | user.setPasswordErrorNum(0); |
| 451 | + | |
| 452 | + if (user.getSuperior() == null) { | |
| 453 | + user.setSuperior(-1L); | |
| 454 | + } | |
| 455 | + | |
| 359 | 456 | super.save(user); |
| 360 | 457 | |
| 361 | 458 | List<Long> roles = user.getRoles(); |
| ... | ... | @@ -405,6 +502,17 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us |
| 405 | 502 | if (StrUtil.isNotEmpty(user.getPassword())) { |
| 406 | 503 | user.setPassword(DigestUtils.md5Hex(user.getPassword())); |
| 407 | 504 | } |
| 505 | + User userDb = this.getById(user); | |
| 506 | + if (user.getSuperior() != null) { | |
| 507 | + if (!user.getSuperior().equals(userDb.getSuperior())) { | |
| 508 | + boolean flag = checkSuperior(user.getSuperior(), user.getId()); | |
| 509 | + if (!flag) { | |
| 510 | + throw new BizException(ExceptionCode.BAD_REQUEST.getCode(), "直属上级已经是当前用户的下属!"); | |
| 511 | + } | |
| 512 | + } | |
| 513 | + } else { | |
| 514 | + user.setSuperior(-1L); | |
| 515 | + } | |
| 408 | 516 | super.updateById(user); |
| 409 | 517 | |
| 410 | 518 | updateUserRole(user); |
| ... | ... | @@ -425,4 +533,24 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us |
| 425 | 533 | ); |
| 426 | 534 | return super.removeByIds(ids); |
| 427 | 535 | } |
| 536 | + | |
| 537 | + private boolean checkSuperior(Long superiorId, Long userId) { | |
| 538 | + if (userId.equals(superiorId)) { | |
| 539 | + return false; | |
| 540 | + } | |
| 541 | + LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>(); | |
| 542 | + wrapper.eq(User::getSuperior, userId); | |
| 543 | + List<User> users = this.list(wrapper); | |
| 544 | + for (User user : users) { | |
| 545 | + if (user.getId().equals(superiorId)) { | |
| 546 | + return false; | |
| 547 | + } else { | |
| 548 | + boolean flag = checkSuperior(superiorId, user.getId()); | |
| 549 | + if (!flag) { | |
| 550 | + return false; | |
| 551 | + } | |
| 552 | + } | |
| 553 | + } | |
| 554 | + return true; | |
| 555 | + } | |
| 428 | 556 | } | ... | ... |
itcast-auth/itcast-auth-server/src/main/java/com/itheima/authority/biz/strategy/impl/CustomizeDataScope.java
| ... | ... | @@ -24,7 +24,7 @@ public class CustomizeDataScope implements AbstractDataScopeHandler { |
| 24 | 24 | @Override |
| 25 | 25 | public List<Long> getOrgIds(List<Long> orgList, Long userId) { |
| 26 | 26 | if (orgList == null || orgList.isEmpty()) { |
| 27 | - throw new BizException(ExceptionCode.BASE_VALID_PARAM.getCode(), "自定义数据权限类型时,组织不能为空"); | |
| 27 | + throw new BizException(ExceptionCode.BAD_REQUEST.getCode(), "自定义数据权限类型时,组织不能为空"); | |
| 28 | 28 | } |
| 29 | 29 | for (Long org : orgList) { |
| 30 | 30 | List<Org> children = orgService.findChildren(org); | ... | ... |
itcast-auth/itcast-auth-server/src/main/java/com/itheima/authority/controller/auth/RoleController.java
| ... | ... | @@ -173,7 +173,7 @@ public class RoleController extends BaseController { |
| 173 | 173 | |
| 174 | 174 | |
| 175 | 175 | @ApiOperation(value = "给角色配置权限", notes = "给角色配置权限") |
| 176 | - @PostMapping("/authority") | |
| 176 | + @PutMapping("/authority") | |
| 177 | 177 | @SysLog("给角色配置权限") |
| 178 | 178 | public R<Boolean> saveRoleAuthority(@RequestBody RoleAuthoritySaveDTO roleAuthoritySaveDTO) { |
| 179 | 179 | return success(roleAuthorityService.saveRoleAuthority(roleAuthoritySaveDTO)); | ... | ... |
itcast-auth/itcast-auth-server/src/main/java/com/itheima/authority/controller/auth/UserController.java
| ... | ... | @@ -2,12 +2,16 @@ package com.itheima.authority.controller.auth; |
| 2 | 2 | |
| 3 | 3 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| 4 | 4 | import com.itheima.authority.biz.service.auth.RoleService; |
| 5 | +import com.itheima.authority.biz.service.auth.UserGroupService; | |
| 6 | +import com.itheima.authority.biz.service.auth.UserGroupUserService; | |
| 5 | 7 | import com.itheima.authority.biz.service.auth.UserService; |
| 6 | 8 | import com.itheima.authority.biz.service.core.OrgService; |
| 7 | 9 | import com.itheima.authority.biz.service.core.StationService; |
| 8 | 10 | import com.itheima.authority.dto.auth.*; |
| 9 | 11 | import com.itheima.authority.entity.auth.Role; |
| 10 | 12 | import com.itheima.authority.entity.auth.User; |
| 13 | +import com.itheima.authority.entity.auth.UserGroup; | |
| 14 | +import com.itheima.authority.entity.auth.UserGroupUser; | |
| 11 | 15 | import com.itheima.authority.entity.core.Org; |
| 12 | 16 | import com.itheima.authority.entity.core.Station; |
| 13 | 17 | import com.itheima.authority.vo.ImportResultVO; |
| ... | ... | @@ -18,11 +22,6 @@ import com.itheima.tools.database.mybatis.conditions.Wraps; |
| 18 | 22 | import com.itheima.tools.database.mybatis.conditions.query.LbqWrapper; |
| 19 | 23 | import com.itheima.tools.dozer.DozerUtils; |
| 20 | 24 | import com.itheima.tools.log.annotation.SysLog; |
| 21 | -import com.itheima.tools.user.feign.UserQuery; | |
| 22 | -import com.itheima.tools.user.model.SysOrg; | |
| 23 | -import com.itheima.tools.user.model.SysRole; | |
| 24 | -import com.itheima.tools.user.model.SysStation; | |
| 25 | -import com.itheima.tools.user.model.SysUser; | |
| 26 | 25 | import io.swagger.annotations.Api; |
| 27 | 26 | import io.swagger.annotations.ApiImplicitParam; |
| 28 | 27 | import io.swagger.annotations.ApiImplicitParams; |
| ... | ... | @@ -35,12 +34,13 @@ import org.springframework.validation.annotation.Validated; |
| 35 | 34 | import org.springframework.web.bind.annotation.*; |
| 36 | 35 | import org.springframework.web.multipart.MultipartFile; |
| 37 | 36 | |
| 37 | +import java.util.Collection; | |
| 38 | 38 | import java.util.HashMap; |
| 39 | 39 | import java.util.List; |
| 40 | 40 | import java.util.Map; |
| 41 | 41 | import java.util.stream.Collectors; |
| 42 | 42 | |
| 43 | -import static com.itheima.tools.exception.code.ExceptionCode.BASE_VALID_PARAM; | |
| 43 | +import static com.itheima.tools.exception.code.ExceptionCode.BAD_REQUEST; | |
| 44 | 44 | |
| 45 | 45 | /** |
| 46 | 46 | * <p> |
| ... | ... | @@ -58,6 +58,10 @@ public class UserController extends BaseController { |
| 58 | 58 | @Autowired |
| 59 | 59 | private UserService userService; |
| 60 | 60 | @Autowired |
| 61 | + private UserGroupService userGroupService; | |
| 62 | + @Autowired | |
| 63 | + private UserGroupUserService userGroupUserService; | |
| 64 | + @Autowired | |
| 61 | 65 | private OrgService orgService; |
| 62 | 66 | @Autowired |
| 63 | 67 | private RoleService roleService; |
| ... | ... | @@ -182,6 +186,9 @@ public class UserController extends BaseController { |
| 182 | 186 | @SysLog("查询用户") |
| 183 | 187 | public R<User> get(@PathVariable Long id) { |
| 184 | 188 | User item = userService.getById(id); |
| 189 | + if (null == item) { | |
| 190 | + return success(null); | |
| 191 | + } | |
| 185 | 192 | List<Role> role = roleService.findRoleByUserId(item.getId()); |
| 186 | 193 | if (!CollectionUtils.isEmpty(role)) { |
| 187 | 194 | List<String> roleNames = role.stream().map(roleItem -> roleItem.getName()).collect(Collectors.toList()); |
| ... | ... | @@ -197,6 +204,15 @@ public class UserController extends BaseController { |
| 197 | 204 | if (org != null) { |
| 198 | 205 | item.setOrgName(org.getName()); |
| 199 | 206 | } |
| 207 | + List<UserGroupUser> userGroupUsers = userGroupUserService.getGroupByUserId(item.getId()); | |
| 208 | + if (!CollectionUtils.isEmpty(userGroupUsers)) { | |
| 209 | + List<Long> groupIds = userGroupUsers.stream().map(UserGroupUser::getGroupId).collect(Collectors.toList()); | |
| 210 | + Collection<UserGroup> userGroups = userGroupService.listByIds(groupIds); | |
| 211 | + if (!CollectionUtils.isEmpty(userGroups)) { | |
| 212 | + List<String> userGroupsNames = userGroups.stream().map(UserGroup::getName).collect(Collectors.toList()); | |
| 213 | + item.setUserGroupsNames(userGroupsNames); | |
| 214 | + } | |
| 215 | + } | |
| 200 | 216 | log.info("getById({}) result:{}", id, item); |
| 201 | 217 | return success(item); |
| 202 | 218 | } |
| ... | ... | @@ -289,7 +305,7 @@ public class UserController extends BaseController { |
| 289 | 305 | @ApiOperation("导入") |
| 290 | 306 | public R<? extends Object> importExcel(@RequestParam(value = "file") MultipartFile file) { |
| 291 | 307 | if (file.isEmpty()) { |
| 292 | - return fail(BASE_VALID_PARAM.build("导入内容为空")); | |
| 308 | + return fail(BAD_REQUEST.build("导入内容为空")); | |
| 293 | 309 | } |
| 294 | 310 | Long begin = System.currentTimeMillis(); |
| 295 | 311 | ImportResultVO importResultVO = userService.importExcel(file); |
| ... | ... | @@ -300,6 +316,14 @@ public class UserController extends BaseController { |
| 300 | 316 | return R.success(importResultVO); |
| 301 | 317 | } |
| 302 | 318 | |
| 319 | + @ApiOperation(value = "查询用户上下级结构", notes = "查询用户上下级结构") | |
| 320 | + @GetMapping("hierarchy/{id}") | |
| 321 | + @SysLog("查询用户上下级结构") | |
| 322 | + public R<HierarchyDTO> hierarchy(@PathVariable Long id) { | |
| 323 | + HierarchyDTO hierarchyDTO = userService.findHierarchy(id); | |
| 324 | + return success(hierarchyDTO); | |
| 325 | + } | |
| 326 | + | |
| 303 | 327 | /** |
| 304 | 328 | * 获取当前登录信息 |
| 305 | 329 | * | ... | ... |