提交 | 用户 | 时间
e7c126 1 package com.iailab.module.system.api.user;
H 2
3 import cn.hutool.core.collection.CollUtil;
4 import cn.hutool.core.util.ObjUtil;
5 import com.iailab.framework.common.pojo.CommonResult;
6 import com.iailab.framework.common.util.object.BeanUtils;
d9f9ba 7 import com.iailab.framework.common.util.object.ConvertUtils;
355ca8 8 import com.iailab.framework.datapermission.core.annotation.DataPermission;
e7c126 9 import com.iailab.module.system.api.user.dto.AdminUserRespDTO;
d9f9ba 10 import com.iailab.module.system.controller.admin.user.vo.user.UserSaveReqVO;
e7c126 11 import com.iailab.module.system.dal.dataobject.dept.DeptDO;
H 12 import com.iailab.module.system.dal.dataobject.user.AdminUserDO;
13 import com.iailab.module.system.service.dept.DeptService;
d9f9ba 14 import com.iailab.module.system.service.permission.PermissionService;
e7c126 15 import com.iailab.module.system.service.user.AdminUserService;
H 16 import org.springframework.validation.annotation.Validated;
17 import org.springframework.web.bind.annotation.RestController;
18
19 import javax.annotation.Resource;
20
21 import java.util.*;
22
23 import static com.iailab.framework.common.pojo.CommonResult.success;
24 import static com.iailab.framework.common.util.collection.CollectionUtils.convertSet;
25
26 @RestController // 提供 RESTful API 接口,给 Feign 调用
27 @Validated
28 public class AdminUserApiImpl implements AdminUserApi {
29
30     @Resource
31     private AdminUserService userService;
32     @Resource
33     private DeptService deptService;
d9f9ba 34     @Resource
H 35     private PermissionService permissionService;
e7c126 36
H 37     @Override
355ca8 38     @DataPermission(enable = false) // 关闭数据权限,避免只查看自己时,查询不到部门。
e7c126 39     public CommonResult<AdminUserRespDTO> getUser(Long id) {
H 40         AdminUserDO user = userService.getUser(id);
41         return success(BeanUtils.toBean(user, AdminUserRespDTO.class));
42     }
43
44     @Override
45     public CommonResult<List<AdminUserRespDTO>> getUserListBySubordinate(Long id) {
46         // 1.1 获取用户负责的部门
47         AdminUserDO user = userService.getUser(id);
48         if (user == null) {
49             return success(Collections.emptyList());
50         }
51         ArrayList<Long> deptIds = new ArrayList<>();
52         DeptDO dept = deptService.getDept(user.getDeptId());
53         if (dept == null) {
54             return success(Collections.emptyList());
55         }
56         if (ObjUtil.notEqual(dept.getLeaderUserId(), id)) { // 校验为负责人
57             return success(Collections.emptyList());
58         }
59         deptIds.add(dept.getId());
60         // 1.2 获取所有子部门
61         List<DeptDO> childDeptList = deptService.getChildDeptList(dept.getId());
62         if (CollUtil.isNotEmpty(childDeptList)) {
63             deptIds.addAll(convertSet(childDeptList, DeptDO::getId));
64         }
65
66         // 2. 获取部门对应的用户信息
67         List<AdminUserDO> users = userService.getUserListByDeptIds(deptIds);
68         users.removeIf(item -> ObjUtil.equal(item.getId(), id)); // 排除自己
69         return success(BeanUtils.toBean(users, AdminUserRespDTO.class));
70     }
71
72     @Override
73     public CommonResult<List<AdminUserRespDTO>> getUserList(Collection<Long> ids) {
74         List<AdminUserDO> users = userService.getUserList(ids);
75         return success(BeanUtils.toBean(users, AdminUserRespDTO.class));
76     }
77
78     @Override
79     public CommonResult<List<AdminUserRespDTO>> getUserListByDeptIds(Collection<Long> deptIds) {
80         List<AdminUserDO> users = userService.getUserListByDeptIds(deptIds);
81         return success(BeanUtils.toBean(users, AdminUserRespDTO.class));
82     }
83
84     @Override
85     public CommonResult<List<AdminUserRespDTO>> getUserListByPostIds(Collection<Long> postIds) {
86         List<AdminUserDO> users = userService.getUserListByPostIds(postIds);
87         return success(BeanUtils.toBean(users, AdminUserRespDTO.class));
88     }
89
90     @Override
91     public CommonResult<Boolean> validateUserList(Collection<Long> ids) {
92         userService.validateUserList(ids);
93         return success(true);
94     }
95
d9f9ba 96     @Override
H 97     public void saveOrUpdateUserInfo(AdminUserRespDTO userRespDTO) {
98         AdminUserDO entity = ConvertUtils.sourceToTarget(userRespDTO, AdminUserDO.class);
99         AdminUserDO dto = userService.getUserByUsername(entity.getUsername());
100         UserSaveReqVO bean = BeanUtils.toBean(dto, UserSaveReqVO.class);
101         if (dto != null) {
102             bean.setId(dto.getId());
103             userService.updateUser(bean);
104         } else {
105             userService.createUser(bean);
106         }
107         permissionService.assignUserRole(entity.getId(), userRespDTO.getRoleIdList());
108     }
109
e7c126 110 }