UserService.java 6.5 KB

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