UserService.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package com.huaxu.service;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.huaxu.common.ToolUtil;
  4. import com.huaxu.dao.UserMapper;
  5. import com.huaxu.entity.UserEntity;
  6. import com.huaxu.entity.UserRoleEntity;
  7. import com.huaxu.entity.UserTagEntity;
  8. import com.huaxu.model.LoginUser;
  9. import com.huaxu.model.ProgramItem;
  10. import com.huaxu.util.ByteArrayUtils;
  11. import com.huaxu.util.RedisUtil;
  12. import com.huaxu.util.UserUtil;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Service;
  15. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  16. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  17. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  18. import javax.annotation.Resource;
  19. import org.springframework.transaction.annotation.Transactional;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.Arrays;
  23. /**
  24. *
  25. * 用户Service接口
  26. * @author: WYY
  27. * @date 2020-10-22 17:35
  28. */
  29. @Service
  30. public class UserService extends ServiceImpl<UserMapper,UserEntity> {
  31. @Resource
  32. private UserMapper userMapper;
  33. @Resource
  34. private UserTagService userTagService;
  35. @Resource
  36. private UserRoleService userRoleService;
  37. @Autowired
  38. private RedisUtil redisUtil;
  39. /**
  40. * 自定义分页查询,含关联实体对像
  41. */
  42. public IPage<UserEntity> findPage(IPage<UserEntity> page, UserEntity userEntity) {
  43. LoginUser currentUser = UserUtil.getCurrentUser();
  44. userEntity.setTenantId(currentUser.getTenantId());
  45. userEntity.setProgramItems(currentUser.getProgramItemList());
  46. userEntity.setUserType(currentUser.getType());
  47. Page<UserEntity> userPage = userMapper.findPage(page, userEntity);
  48. return userPage;
  49. }
  50. /**
  51. * 查列表
  52. */
  53. public List<UserEntity> findList(UserEntity userEntity) {
  54. LoginUser currentUser = UserUtil.getCurrentUser();
  55. userEntity.setTenantId(currentUser.getTenantId());
  56. return userMapper.findList(userEntity);
  57. }
  58. /**
  59. * 批量删除
  60. */
  61. @Transactional(rollbackFor = Exception.class)
  62. public boolean delUserByIds(Long[] ids) {
  63. for (Long id : ids) {
  64. UserEntity userEntity = findUserById(id);
  65. if (userEntity != null) {
  66. //將禁用的角色放入redis,作为登出判断
  67. if (userEntity.getEnableState().equals("0")) {
  68. String roleKey = "disableUser:" + id;
  69. redisUtil.setExpire(roleKey.getBytes(), ByteArrayUtils.objectToBytes("1").get(), 60 * 60 * 24);//15分钟过期
  70. }
  71. if (userEntity.getEnableState().equals("1")) {
  72. String roleKey = "disableUser:" + id;
  73. redisUtil.del(roleKey.getBytes());
  74. }
  75. }
  76. }
  77. return this.removeByIds(Arrays.asList(ids));
  78. }
  79. public boolean checkMobileUnique(String phone) {
  80. LoginUser currentUser = UserUtil.getCurrentUser();
  81. int count = this.count(new QueryWrapper<UserEntity>().eq("phone", phone).eq("tenant_id",currentUser.getTenantId()));
  82. if (count > 0) {
  83. return true;
  84. }
  85. return false;
  86. }
  87. public boolean checkMobileUnique(UserEntity user) {
  88. LoginUser currentUser = UserUtil.getCurrentUser();
  89. Long userId = ToolUtil.isEmpty(user.getId()) ? -1L : user.getId();
  90. UserEntity info = this.getOne(new QueryWrapper<UserEntity>().eq("phone", user.getPhone()).eq("tenant_id",currentUser.getTenantId()));
  91. if (ToolUtil.isNotEmpty(info) && !info.getId().equals(userId)) {
  92. return true;
  93. }
  94. return false;
  95. }
  96. /**
  97. * 单个删除
  98. */
  99. public boolean delUserById(Long id) {
  100. UserEntity userEntity = findUserById(id);
  101. if (userEntity != null) {
  102. //將禁用的角色放入redis,作为登出判断
  103. if (userEntity.getEnableState().equals("0")) {
  104. String roleKey = "disableUser:" + id;
  105. redisUtil.setExpire(roleKey.getBytes(), ByteArrayUtils.objectToBytes("1").get(), 60 * 60 * 24);//15分钟过期
  106. }
  107. if (userEntity.getEnableState().equals("1")) {
  108. String roleKey = "disableUser:" + id;
  109. redisUtil.del(roleKey.getBytes());
  110. }
  111. }
  112. return this.removeById(id);
  113. }
  114. /**
  115. * 保存
  116. */
  117. public boolean addUser(UserEntity user) {
  118. if (this.save(user)) {
  119. Long pkId = user.getId();
  120. //添加用户标签
  121. if (user.getUserTags() != null) {
  122. for (Long item : user.getUserTags()) {
  123. UserTagEntity userTagEntity = new UserTagEntity();
  124. userTagEntity.setCreateBy(user.getCreateBy());
  125. userTagEntity.setUpdateBy(user.getUpdateBy());
  126. userTagEntity.setDateCreate(user.getDateCreate());
  127. userTagEntity.setDateUpdate(user.getDateUpdate());
  128. userTagEntity.setStatus(1);
  129. userTagEntity.setTagId(Long.valueOf(item));
  130. userTagEntity.setUserId(pkId);
  131. userTagService.addUserTag(userTagEntity);
  132. }
  133. }
  134. //添加角色
  135. if (user.getRoleId() != null) {
  136. UserRoleEntity userRoleEntity = new UserRoleEntity();
  137. userRoleEntity.setCreateBy(user.getCreateBy());
  138. userRoleEntity.setUpdateBy(user.getUpdateBy());
  139. userRoleEntity.setDateCreate(user.getDateCreate());
  140. userRoleEntity.setDateUpdate(user.getDateUpdate());
  141. userRoleEntity.setStatus(1);
  142. userRoleEntity.setRoleId(Long.valueOf(user.getRoleId()));
  143. userRoleEntity.setUserId(pkId);
  144. userRoleService.addUserRole(userRoleEntity);
  145. }
  146. return true;
  147. }
  148. return false;
  149. }
  150. /**
  151. * 修改根居ID
  152. */
  153. public boolean updateUserById(UserEntity user) {
  154. if (this.updateById(user)) {
  155. UserEntity userEntity = findUserById(user.getId());
  156. if (userEntity != null) {
  157. //將禁用的角色放入redis,作为登出判断
  158. if (userEntity.getEnableState().equals("0")) {
  159. String roleKey = "disableUser:" + user.getId();
  160. redisUtil.setExpire(roleKey.getBytes(), ByteArrayUtils.objectToBytes("1").get(), 60 * 60 * 24);//15分钟过期
  161. }
  162. if (userEntity.getEnableState().equals("1")) {
  163. String roleKey = "disableUser:" + user.getId();
  164. redisUtil.del(roleKey.getBytes());
  165. }
  166. }
  167. //更新关联附件信息
  168. Long pkId = user.getId();
  169. //添加用户标签
  170. if (user.getUserTags() != null && user.getUserTags().size() > 0) {
  171. //先删除之前的标签
  172. List<UserTagEntity> userTagEntitys = new ArrayList<>();
  173. UserTagEntity userTagDelete = new UserTagEntity();
  174. userTagDelete.setUserId(pkId);
  175. userTagEntitys = userTagService.findList(userTagDelete);
  176. Long[] ids = new Long[userTagEntitys.size()];
  177. for (int i = 0; i < userTagEntitys.size(); i++) {
  178. ids[i] = userTagEntitys.get(i).getId();
  179. }
  180. userTagService.delUserTagByIds(ids);
  181. //新增标签
  182. for (Long item : user.getUserTags()) {
  183. UserTagEntity userTagEntity = new UserTagEntity();
  184. userTagEntity.setCreateBy(user.getCreateBy());
  185. userTagEntity.setUpdateBy(user.getUpdateBy());
  186. userTagEntity.setDateCreate(user.getDateCreate());
  187. userTagEntity.setDateUpdate(user.getDateUpdate());
  188. userTagEntity.setStatus(1);
  189. userTagEntity.setTagId(Long.valueOf(item));
  190. userTagEntity.setUserId(pkId);
  191. userTagService.addUserTag(userTagEntity);
  192. }
  193. }
  194. //修改角色
  195. if (user.getRoleId() != null) {
  196. List<UserRoleEntity> userRoleEntities = new ArrayList<>();
  197. UserRoleEntity userRoleEntity = new UserRoleEntity();
  198. userRoleEntity.setUserId(pkId);
  199. userRoleEntities = userRoleService.findList(userRoleEntity);
  200. if (userRoleEntities.size() > 0) {
  201. Long userRoleId = userRoleEntities.get(0).getId();
  202. userRoleService.delUserRoleById(userRoleId);
  203. }
  204. //新增角色
  205. UserRoleEntity userRole = new UserRoleEntity();
  206. userRole.setCreateBy(user.getCreateBy());
  207. userRole.setUpdateBy(user.getUpdateBy());
  208. userRole.setDateCreate(user.getDateCreate());
  209. userRole.setDateUpdate(user.getDateUpdate());
  210. userRole.setStatus(1);
  211. userRole.setRoleId(Long.valueOf(user.getRoleId()));
  212. userRole.setUserId(pkId);
  213. userRoleService.addUserRole(userRole);
  214. }
  215. return true;
  216. }
  217. return false;
  218. }
  219. /**
  220. * 根居ID获取对象
  221. */
  222. public UserEntity findUserById(Long id) {
  223. List<UserTagEntity> userTagEntitys = new ArrayList<>();
  224. UserTagEntity userTags = new UserTagEntity();
  225. userTags.setUserId(id);
  226. userTagEntitys = userTagService.findList(userTags);
  227. List<Long> ids = new ArrayList<>();
  228. for (int i = 0; i < userTagEntitys.size(); i++) {
  229. ids.add(userTagEntitys.get(i).getTagId());
  230. }
  231. UserEntity userEntity = userMapper.findUserById(id);
  232. userEntity.setUserTags(ids);
  233. return userEntity;
  234. }
  235. }