UserService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. package com.huaxu.service;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.huaxu.common.ToolUtil;
  4. import com.huaxu.dao.OrgMapper;
  5. import com.huaxu.dao.UserMapper;
  6. import com.huaxu.dto.UserListDto;
  7. import com.huaxu.entity.Org;
  8. import com.huaxu.entity.UserEntity;
  9. import com.huaxu.entity.UserRoleEntity;
  10. import com.huaxu.entity.UserTagEntity;
  11. import com.huaxu.model.LoginUser;
  12. import com.huaxu.model.ProgramItem;
  13. import com.huaxu.util.ByteArrayUtils;
  14. import com.huaxu.util.RedisUtil;
  15. import com.huaxu.util.UserUtil;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.stereotype.Service;
  18. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  19. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  20. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  21. import javax.annotation.Resource;
  22. import org.springframework.transaction.annotation.Transactional;
  23. import java.util.*;
  24. import java.util.stream.Collectors;
  25. /**
  26. *
  27. * 用户Service接口
  28. * @author: WYY
  29. * @date 2020-10-22 17:35
  30. */
  31. @Service
  32. public class UserService extends ServiceImpl<UserMapper,UserEntity> {
  33. @Resource
  34. private UserMapper userMapper;
  35. @Resource
  36. private UserTagService userTagService;
  37. @Resource
  38. private UserRoleService userRoleService;
  39. @Resource
  40. private OrgMapper orgMapper;
  41. @Autowired
  42. private RedisUtil redisUtil;
  43. /**
  44. * 自定义分页查询,含关联实体对像
  45. */
  46. public IPage<UserEntity> findPage(IPage<UserEntity> page, UserEntity userEntity) {
  47. LoginUser currentUser = UserUtil.getCurrentUser();
  48. userEntity.setTenantId(currentUser.getTenantId());
  49. userEntity.setProgramItems(currentUser.getProgramItemList());
  50. userEntity.setUserType(currentUser.getType());
  51. //1是公司,2是公司及以下,3部门,4部门及以下,5自定义
  52. userEntity.setPermissonType(currentUser.getPermissonType());
  53. Page<UserEntity> userPage = userMapper.findPage(page, userEntity);
  54. return userPage;
  55. }
  56. /**
  57. * 查列表
  58. */
  59. public List<UserEntity> findList(UserEntity userEntity) {
  60. LoginUser currentUser = UserUtil.getCurrentUser();
  61. userEntity.setTenantId(currentUser.getTenantId());
  62. return userMapper.findList(userEntity);
  63. }
  64. /**
  65. * 批量删除
  66. */
  67. @Transactional(rollbackFor = Exception.class)
  68. public boolean delUserByIds(Long[] ids) {
  69. for (Long id : ids) {
  70. UserEntity userEntity = findUserById(id);
  71. if (userEntity != null) {
  72. //將禁用的角色放入redis,作为登出判断
  73. if (userEntity.getEnableState().equals("0")) {
  74. String roleKey = "disableUser:" + id;
  75. redisUtil.setExpire(roleKey.getBytes(), ByteArrayUtils.objectToBytes("1").get(), 60 * 60 * 24);//15分钟过期
  76. }
  77. if (userEntity.getEnableState().equals("1")) {
  78. String roleKey = "disableUser:" + id;
  79. redisUtil.del(roleKey.getBytes());
  80. }
  81. }
  82. }
  83. return this.removeByIds(Arrays.asList(ids));
  84. }
  85. public boolean checkMobileUnique(String phone) {
  86. LoginUser currentUser = UserUtil.getCurrentUser();
  87. int count = this.count(new QueryWrapper<UserEntity>().eq("phone", phone).eq("tenant_id",currentUser.getTenantId()));
  88. if (count > 0) {
  89. return true;
  90. }
  91. return false;
  92. }
  93. public boolean checkMobileUnique(UserEntity user) {
  94. LoginUser currentUser = UserUtil.getCurrentUser();
  95. Long userId = ToolUtil.isEmpty(user.getId()) ? -1L : user.getId();
  96. UserEntity info = this.getOne(new QueryWrapper<UserEntity>().eq("phone", user.getPhone()).eq("tenant_id",currentUser.getTenantId()));
  97. if (ToolUtil.isNotEmpty(info) && !info.getId().equals(userId)) {
  98. return true;
  99. }
  100. return false;
  101. }
  102. /**
  103. * 单个删除
  104. */
  105. public boolean delUserById(Long id) {
  106. UserEntity userEntity = findUserById(id);
  107. if (userEntity != null) {
  108. //將禁用的角色放入redis,作为登出判断
  109. if (userEntity.getEnableState().equals("0")) {
  110. String roleKey = "disableUser:" + id;
  111. redisUtil.setExpire(roleKey.getBytes(), ByteArrayUtils.objectToBytes("1").get(), 60 * 60 * 24);//15分钟过期
  112. }
  113. if (userEntity.getEnableState().equals("1")) {
  114. String roleKey = "disableUser:" + id;
  115. redisUtil.del(roleKey.getBytes());
  116. }
  117. }
  118. return this.removeById(id);
  119. }
  120. /**
  121. * 保存
  122. */
  123. public boolean addUser(UserEntity user) {
  124. if (this.save(user)) {
  125. Long pkId = user.getId();
  126. //添加用户标签
  127. if (user.getUserTags() != null) {
  128. for (Long item : user.getUserTags()) {
  129. UserTagEntity userTagEntity = new UserTagEntity();
  130. userTagEntity.setCreateBy(user.getCreateBy());
  131. userTagEntity.setUpdateBy(user.getUpdateBy());
  132. userTagEntity.setDateCreate(user.getDateCreate());
  133. userTagEntity.setDateUpdate(user.getDateUpdate());
  134. userTagEntity.setStatus(1);
  135. userTagEntity.setTagId(Long.valueOf(item));
  136. userTagEntity.setUserId(pkId);
  137. userTagService.addUserTag(userTagEntity);
  138. }
  139. }
  140. //添加角色
  141. if (user.getRoleId() != null) {
  142. UserRoleEntity userRoleEntity = new UserRoleEntity();
  143. userRoleEntity.setCreateBy(user.getCreateBy());
  144. userRoleEntity.setUpdateBy(user.getUpdateBy());
  145. userRoleEntity.setDateCreate(user.getDateCreate());
  146. userRoleEntity.setDateUpdate(user.getDateUpdate());
  147. userRoleEntity.setStatus(1);
  148. userRoleEntity.setRoleId(Long.valueOf(user.getRoleId()));
  149. userRoleEntity.setUserId(pkId);
  150. userRoleService.addUserRole(userRoleEntity);
  151. }
  152. return true;
  153. }
  154. return false;
  155. }
  156. /**
  157. * 修改根居ID
  158. */
  159. public boolean updateUserById(UserEntity user) {
  160. if (this.updateById(user)) {
  161. UserEntity userEntity = findUserById(user.getId());
  162. if (userEntity != null) {
  163. //將禁用的角色放入redis,作为登出判断
  164. if (userEntity.getEnableState().equals("0")) {
  165. String roleKey = "disableUser:" + user.getId();
  166. redisUtil.setExpire(roleKey.getBytes(), ByteArrayUtils.objectToBytes("1").get(), 60 * 60 * 24);//15分钟过期
  167. }
  168. if (userEntity.getEnableState().equals("1")) {
  169. String roleKey = "disableUser:" + user.getId();
  170. redisUtil.del(roleKey.getBytes());
  171. }
  172. }
  173. //更新关联附件信息
  174. Long pkId = user.getId();
  175. //添加用户标签
  176. if (user.getUserTags() != null && user.getUserTags().size() > 0) {
  177. //先删除之前的标签
  178. List<UserTagEntity> userTagEntitys = new ArrayList<>();
  179. UserTagEntity userTagDelete = new UserTagEntity();
  180. userTagDelete.setUserId(pkId);
  181. userTagEntitys = userTagService.findList(userTagDelete);
  182. Long[] ids = new Long[userTagEntitys.size()];
  183. for (int i = 0; i < userTagEntitys.size(); i++) {
  184. ids[i] = userTagEntitys.get(i).getId();
  185. }
  186. userTagService.delUserTagByIds(ids);
  187. //新增标签
  188. for (Long item : user.getUserTags()) {
  189. UserTagEntity userTagEntity = new UserTagEntity();
  190. userTagEntity.setCreateBy(user.getCreateBy());
  191. userTagEntity.setUpdateBy(user.getUpdateBy());
  192. userTagEntity.setDateCreate(user.getDateCreate());
  193. userTagEntity.setDateUpdate(user.getDateUpdate());
  194. userTagEntity.setStatus(1);
  195. userTagEntity.setTagId(Long.valueOf(item));
  196. userTagEntity.setUserId(pkId);
  197. userTagService.addUserTag(userTagEntity);
  198. }
  199. }
  200. //修改角色
  201. if (user.getRoleId() != null) {
  202. List<UserRoleEntity> userRoleEntities = new ArrayList<>();
  203. UserRoleEntity userRoleEntity = new UserRoleEntity();
  204. userRoleEntity.setUserId(pkId);
  205. userRoleEntities = userRoleService.findList(userRoleEntity);
  206. if (userRoleEntities.size() > 0) {
  207. Long userRoleId = userRoleEntities.get(0).getId();
  208. userRoleService.delUserRoleById(userRoleId);
  209. }
  210. //新增角色
  211. UserRoleEntity userRole = new UserRoleEntity();
  212. userRole.setCreateBy(user.getCreateBy());
  213. userRole.setUpdateBy(user.getUpdateBy());
  214. userRole.setDateCreate(user.getDateCreate());
  215. userRole.setDateUpdate(user.getDateUpdate());
  216. userRole.setStatus(1);
  217. userRole.setRoleId(Long.valueOf(user.getRoleId()));
  218. userRole.setUserId(pkId);
  219. userRoleService.addUserRole(userRole);
  220. }
  221. return true;
  222. }
  223. return false;
  224. }
  225. /**
  226. * 根居ID获取对象
  227. */
  228. public UserEntity findUserById(Long id) {
  229. List<UserTagEntity> userTagEntitys = new ArrayList<>();
  230. UserTagEntity userTags = new UserTagEntity();
  231. userTags.setUserId(id);
  232. userTagEntitys = userTagService.findList(userTags);
  233. List<Long> ids = new ArrayList<>();
  234. for (int i = 0; i < userTagEntitys.size(); i++) {
  235. ids.add(userTagEntitys.get(i).getTagId());
  236. }
  237. UserEntity userEntity = userMapper.findUserById(id);
  238. userEntity.setUserTags(ids);
  239. return userEntity;
  240. }
  241. public List<Integer> getUserIdsByOrgId(List<Integer> orgIds) {
  242. List<Integer>userIds=new ArrayList<>();
  243. if(orgIds.size()>0){
  244. userIds=userMapper.getUserIdsByOrgId(orgIds);
  245. }
  246. return userIds;
  247. }
  248. public List<UserEntity> findUserIdsByOrgId(List<Integer> orgIds)
  249. {
  250. List<UserEntity> userEntities = new ArrayList<>();
  251. if(orgIds.size()>0){
  252. userEntities=userMapper.findUserIdsByOrgId(orgIds);
  253. }
  254. return userEntities;
  255. }
  256. public List<UserListDto> findUserList()
  257. {
  258. List<UserListDto> userListDtos = new ArrayList<>();
  259. LoginUser currentUser = UserUtil.getCurrentUser();
  260. UserEntity userEntity=new UserEntity();
  261. userEntity.setTenantId(currentUser.getTenantId());
  262. userEntity.setProgramItems(currentUser.getProgramItemList());
  263. userEntity.setUserType(currentUser.getType());
  264. //1是公司,2是公司及以下,3部门,4部门及以下,5自定义
  265. userEntity.setPermissonType(currentUser.getPermissonType());
  266. List<UserEntity>userEntities=userMapper.findUserList(userEntity);
  267. List<UserEntity>companyUser=new ArrayList<>();
  268. List<UserEntity>departmentUser=new ArrayList<>();
  269. userEntities.forEach(user->{
  270. if(user.getDeptOrgId()!=null){
  271. departmentUser.add(user);
  272. }else{
  273. companyUser.add(user);
  274. }
  275. });
  276. Map<Long, List<UserEntity>> companyGroup = companyUser.stream()
  277. .collect(Collectors.groupingBy(d -> d.getCompanyOrgId()));
  278. Map<Long, List<UserEntity>> departMentGroup = departmentUser.stream()
  279. .collect(Collectors.groupingBy(d -> d.getDeptOrgId()));
  280. Iterator<List<UserEntity>> companyIterator = companyGroup.values().iterator();
  281. Iterator<List<UserEntity>> departmentIterator = departMentGroup.values().iterator();
  282. while(companyIterator.hasNext()){
  283. List<UserEntity> users = companyIterator.next();
  284. UserListDto userListDto=new UserListDto();
  285. UserEntity user = users.get(0);
  286. userListDto.setLabel(user.getCompanyOrgName());
  287. userListDto.setId(user.getCompanyOrgId());
  288. userListDto.setValue(user.getCompanyOrgName());
  289. userListDto.setChildren(setChildrens(users));
  290. userListDtos.add(userListDto);
  291. }
  292. while(departmentIterator.hasNext()){
  293. List<UserEntity> users = departmentIterator.next();
  294. UserListDto userListDto=new UserListDto();
  295. UserEntity user = users.get(0);
  296. userListDto.setLabel(user.getDeptOrgName());
  297. userListDto.setId(user.getDeptOrgId());
  298. userListDto.setValue(user.getDeptOrgName());
  299. userListDto.setChildren(setChildrens(users));
  300. userListDtos.add(userListDto);
  301. }
  302. return userListDtos;
  303. }
  304. private List<UserListDto> setChildrens(List<UserEntity> users){
  305. List<UserListDto> childrens=new ArrayList<>();
  306. users.forEach(u->{
  307. UserListDto children=new UserListDto();
  308. children.setLabel(u.getUsername());
  309. children.setId(u.getId());
  310. children.setValue(u.getUsername());
  311. childrens.add(children);
  312. });
  313. return childrens;
  314. }
  315. public List<UserEntity> findUserIdsByUserIds(List<Integer> userIds) {
  316. List<UserEntity> userEntities = new ArrayList<>();
  317. if(userIds.size()>0){
  318. userEntities=userMapper.findUserIdsByUserIds(userIds);
  319. }
  320. return userEntities;
  321. }
  322. public List<UserEntity> findUserIdsForOrg() {
  323. LoginUser currentUser = UserUtil.getCurrentUser();
  324. Integer compayId = currentUser.getCompanyId();
  325. Integer deptId = currentUser.getDepartmentId();
  326. String ids = "";
  327. if (deptId != null)
  328. ids = orgMapper.findParentOrgByChildId(deptId);
  329. //查询公司
  330. String comps = orgMapper.findParentOrgByChildId(compayId);
  331. String[] compids = comps.split(",");
  332. List<Long> compList = new ArrayList<>();
  333. for (String item : compids) {
  334. if (!item.equals(compayId.toString()))
  335. compList.add(Long.valueOf(item));
  336. }
  337. List<Long> idList = new ArrayList<>();
  338. String[] as = ids.split(",");
  339. for (String item : as) {
  340. idList.add(Long.valueOf(item));
  341. }
  342. List<UserEntity> list = userMapper.findUserIdsForOrg(compayId, deptId, idList,compList);
  343. return list;
  344. }
  345. public List<Integer> findUserIdsByPermissonOrg(String tenantId,Integer companyOrgId,Integer deptOrgId){
  346. List<Integer> userIds = new ArrayList<>();
  347. //1是公司,2是公司及以下,3部门,4部门及以下,5自定义
  348. if(companyOrgId == null){
  349. return userIds;
  350. }
  351. //查询租户下所有公司部门
  352. List<Org> orgs = orgMapper.findOrgByTenant(tenantId);
  353. Map<Integer ,Org> orgMap = orgs.stream().collect(Collectors.toMap(Org::getId, a -> a, (k1, k2) -> k1));
  354. List<Integer> companyOrgIds = new ArrayList<>();
  355. List<Integer> deptOrgIds = new ArrayList<>();
  356. Integer deptOrgParentId =deptOrgId;
  357. if(deptOrgId != null){
  358. //权限类型为4部门及以下
  359. while(true){
  360. Org org = orgMap.get(deptOrgParentId);
  361. if(org == null ){
  362. break;
  363. }
  364. if(org.getOrgType().equals("company") ){
  365. break;
  366. }
  367. deptOrgIds.add(deptOrgParentId);
  368. deptOrgParentId = org.getParentOrgId();
  369. }
  370. }
  371. //权限类型为2是公司及以下
  372. Integer companyOrgParentId = companyOrgId ;
  373. while(true){
  374. Org org = orgMap.get(companyOrgParentId);
  375. if(org == null ){
  376. break;
  377. }
  378. companyOrgIds.add(companyOrgParentId);
  379. companyOrgParentId = org.getParentOrgId();
  380. if(org.getParentOrgId() == 0){
  381. break;
  382. }
  383. }
  384. //查询客户id
  385. if(companyOrgId != null && companyOrgIds.size() > 0){
  386. userIds = userMapper.findUserIdsByPermissonOrg(tenantId,companyOrgId,deptOrgId,companyOrgIds,deptOrgIds);
  387. }
  388. return userIds;
  389. }
  390. }