SysUserServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package com.qmrb.system.service.impl;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.core.lang.Assert;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  7. import com.baomidou.mybatisplus.core.metadata.IPage;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  10. import com.qmrb.system.common.constant.SecurityConstants;
  11. import com.qmrb.system.common.constant.SystemConstants;
  12. import com.qmrb.system.converter.UserConverter;
  13. import com.qmrb.system.framework.security.util.SecurityUtils;
  14. import com.qmrb.system.mapper.SysUserMapper;
  15. import com.qmrb.system.pojo.bo.UserAuthInfo;
  16. import com.qmrb.system.pojo.bo.UserBO;
  17. import com.qmrb.system.pojo.bo.UserFormBO;
  18. import com.qmrb.system.pojo.entity.SysUser;
  19. import com.qmrb.system.pojo.entity.SysUserRole;
  20. import com.qmrb.system.pojo.form.UserForm;
  21. import com.qmrb.system.pojo.query.UserPageQuery;
  22. import com.qmrb.system.pojo.vo.UserExportVO;
  23. import com.qmrb.system.pojo.vo.UserInfoVO;
  24. import com.qmrb.system.pojo.vo.UserPageVO;
  25. import com.qmrb.system.service.SysMenuService;
  26. import com.qmrb.system.service.SysRoleService;
  27. import com.qmrb.system.service.SysUserRoleService;
  28. import com.qmrb.system.service.SysUserService;
  29. import com.qmrb.system.utils.MqUtil;
  30. import lombok.RequiredArgsConstructor;
  31. import org.springframework.beans.factory.annotation.Value;
  32. import org.springframework.data.redis.core.RedisTemplate;
  33. import org.springframework.security.crypto.password.PasswordEncoder;
  34. import org.springframework.stereotype.Service;
  35. import org.springframework.transaction.annotation.Transactional;
  36. import java.util.*;
  37. import java.util.stream.Collectors;
  38. /**
  39. * 用户业务实现类
  40. *
  41. * @author haoxr
  42. * @date 2022/1/14
  43. */
  44. @Service
  45. @RequiredArgsConstructor
  46. public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
  47. private final PasswordEncoder passwordEncoder;
  48. private final SysUserRoleService userRoleService;
  49. private final UserConverter userConverter;
  50. private final SysMenuService menuService;
  51. private final SysRoleService roleService;
  52. @SuppressWarnings("rawtypes")
  53. private final RedisTemplate redisTemplate;
  54. @Value("${application.default-dept-id}")
  55. private Long defaultDeptId;
  56. @Value("${application.default-role-id}")
  57. private Long defaultRoleId;
  58. /**
  59. * 获取用户分页列表
  60. *
  61. * @param queryParams
  62. * @return
  63. */
  64. @Override
  65. public IPage<UserPageVO> getUserPage(UserPageQuery queryParams) {
  66. // 参数构建
  67. int pageNum = queryParams.getPageNum();
  68. int pageSize = queryParams.getPageSize();
  69. Page<UserBO> page = new Page<>(pageNum, pageSize);
  70. // 查询数据
  71. Page<UserBO> userBoPage = this.baseMapper.getUserPage(page, queryParams);
  72. // 实体转换
  73. Page<UserPageVO> userVoPage = userConverter.bo2Vo(userBoPage);
  74. return userVoPage;
  75. }
  76. /**
  77. * 获取用户详情
  78. *
  79. * @param userId
  80. * @return
  81. */
  82. @Override
  83. public UserForm getUserFormData(Long userId) {
  84. UserFormBO userFormBO = this.baseMapper.getUserDetail(userId);
  85. // 实体转换po->form
  86. UserForm userForm = userConverter.bo2Form(userFormBO);
  87. return userForm;
  88. }
  89. /**
  90. * 新增用户
  91. *
  92. * @param userForm 用户表单对象
  93. * @return
  94. */
  95. @Override
  96. public boolean saveUser(UserForm userForm) {
  97. String username = userForm.getUsername();
  98. long count = this.count(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username));
  99. Assert.isTrue(count == 0, "用户名已存在");
  100. // 实体转换 form->entity
  101. SysUser entity = userConverter.form2Entity(userForm);
  102. // 设置默认加密密码
  103. String defaultEncryptPwd = passwordEncoder.encode(SystemConstants.DEFAULT_PASSWORD);
  104. entity.setPassword(defaultEncryptPwd);
  105. // 新增用户
  106. boolean result = this.save(entity);
  107. if (result) {
  108. // 保存用户角色
  109. userRoleService.saveUserRoles(entity.getId(), userForm.getRoleIds());
  110. }
  111. return result;
  112. }
  113. /**
  114. * 更新用户
  115. *
  116. * @param userId 用户ID
  117. * @param userForm 用户表单对象
  118. * @return
  119. */
  120. @Override
  121. @Transactional
  122. public boolean updateUser(Long userId, UserForm userForm) {
  123. String username = userForm.getUsername();
  124. long count = this.count(new LambdaQueryWrapper<SysUser>()
  125. .eq(SysUser::getUsername, username)
  126. .ne(SysUser::getId, userId)
  127. );
  128. Assert.isTrue(count == 0, "用户名已存在");
  129. // form -> entity
  130. SysUser entity = userConverter.form2Entity(userForm);
  131. // 修改用户
  132. boolean result = this.updateById(entity);
  133. if (result) {
  134. // 保存用户角色
  135. userRoleService.saveUserRoles(entity.getId(), userForm.getRoleIds());
  136. }
  137. return result;
  138. }
  139. /**
  140. * 删除用户
  141. *
  142. * @param idsStr 用户ID,多个以英文逗号(,)分割
  143. * @return
  144. */
  145. @Override
  146. public boolean deleteUsers(String idsStr) {
  147. Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的用户数据为空");
  148. // 逻辑删除
  149. List<Long> ids = Arrays.asList(idsStr.split(",")).stream()
  150. .map(idStr -> Long.parseLong(idStr)).collect(Collectors.toList());
  151. boolean result = this.removeByIds(ids);
  152. return result;
  153. }
  154. /**
  155. * 修改用户密码
  156. *
  157. * @param userId 用户ID
  158. * @param password 用户密码
  159. * @return true|false
  160. */
  161. @Override
  162. public boolean updatePassword(Long userId, String password) {
  163. return this.update(new LambdaUpdateWrapper<SysUser>()
  164. .eq(SysUser::getId, userId)
  165. .set(SysUser::getPassword, passwordEncoder.encode(password))
  166. );
  167. }
  168. /**
  169. * 根据用户名获取认证信息
  170. *
  171. * @param username 用户名
  172. * @return 用户认证信息 {@link UserAuthInfo}
  173. */
  174. @Override
  175. public UserAuthInfo getUserAuthInfo(String username) {
  176. UserAuthInfo userAuthInfo = this.baseMapper.getUserAuthInfo(username);
  177. if (userAuthInfo != null) {
  178. Set<String> roles = userAuthInfo.getRoles();
  179. if (CollectionUtil.isNotEmpty(roles)) {
  180. Set<String> perms = menuService.listRolePerms(roles);
  181. userAuthInfo.setPerms(perms);
  182. }
  183. // 获取最大范围的数据权限
  184. Integer dataScope = roleService.getMaximumDataScope(roles);
  185. userAuthInfo.setDataScope(dataScope);
  186. }
  187. return userAuthInfo;
  188. }
  189. /**
  190. * 获取导出用户列表
  191. *
  192. * @param queryParams
  193. * @return
  194. */
  195. @Override
  196. public List<UserExportVO> listExportUsers(UserPageQuery queryParams) {
  197. List<UserExportVO> list = this.baseMapper.listExportUsers(queryParams);
  198. return list;
  199. }
  200. /**
  201. * 获取登录用户信息
  202. *
  203. * @return
  204. */
  205. @Override
  206. public UserInfoVO getUserLoginInfo() {
  207. // 登录用户entity
  208. SysUser user = this.getOne(new LambdaQueryWrapper<SysUser>()
  209. .eq(SysUser::getUsername, SecurityUtils.getUser().getUsername())
  210. .select(
  211. SysUser::getId,
  212. SysUser::getNickname,
  213. SysUser::getMobile,
  214. SysUser::getAvatar
  215. )
  216. );
  217. // entity->VO
  218. UserInfoVO userInfoVO = userConverter.entity2UserInfoVo(user);
  219. // 用户角色集合
  220. Set<String> roles = SecurityUtils.getRoles();
  221. userInfoVO.setRoles(roles);
  222. userInfoVO.setPhone(user.getMobile());
  223. // 用户权限集合
  224. @SuppressWarnings("unchecked")
  225. Set<String> perms = (Set<String>) redisTemplate.opsForValue().get(SecurityConstants.USER_PERMS_CACHE_PREFIX+ user.getId());
  226. userInfoVO.setPerms(perms);
  227. return userInfoVO;
  228. }
  229. @Override
  230. public Map<String, String> getUserNameByDoctorNo(Set<String> doctorNos) {
  231. LambdaQueryWrapper<SysUser> wrapper=new LambdaQueryWrapper<>();
  232. wrapper.in(SysUser::getUsername,doctorNos);
  233. return this.list(wrapper).stream()
  234. .collect(Collectors.toMap(SysUser::getUsername, SysUser::getNickname));
  235. }
  236. /**
  237. * 同步医生数据到数据库中
  238. */
  239. @Override
  240. public void syncDoctorInfo(String reqMsg,String serviceId) {
  241. String msgByEsb = MqUtil.getMsgByEsb(reqMsg, serviceId);
  242. List<String> columns = Arrays.asList("Staff_Name", "Staff_Code", "ID_Number","Dept_name", "Sex_Name", "Mobile_No", "level_name");
  243. List<Map<String, String>> maps = MqUtil.parseXml(msgByEsb, columns);
  244. //查询是否存在,存在更新,不存在新增
  245. for (Map<String, String> map : maps) {
  246. SysUser doctor=new SysUser();
  247. doctor.setUsername(map.get("Staff_Code"));
  248. doctor.setNickname(map.get("Staff_Name"));
  249. // doctor.setPassword(passwordEncoder.encode(SystemConstants.DEFAULT_PASSWORD));
  250. doctor.setMobile(map.get("Mobile_No"));
  251. doctor.setGender("男".equals(map.get("Sex_Name"))?0:1);
  252. doctor.setCardNo(map.get("ID_Number"));
  253. doctor.setDeptId(defaultDeptId);
  254. doctor.setLevelName(map.get("level_name"));
  255. SysUser staffCode = this.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, map.get("Staff_Code")));
  256. if (staffCode==null){
  257. //不存在新增
  258. this.save(doctor);
  259. }else {
  260. //存在更新
  261. this.update(doctor,new LambdaUpdateWrapper<SysUser>().eq(SysUser::getUsername, map.get("Staff_Code")));
  262. }
  263. //获取当前用户
  264. SysUser doctorByNo = this.getOne(new LambdaUpdateWrapper<SysUser>()
  265. .eq(SysUser::getUsername, map.get("Staff_Code")));
  266. //添加权限前删除原有权限
  267. userRoleService.remove(new LambdaUpdateWrapper<SysUserRole>()
  268. .eq(SysUserRole::getUserId, doctorByNo.getId()));
  269. //添加权限
  270. SysUserRole userRole = userRoleService.getOne(new LambdaQueryWrapper<SysUserRole>()
  271. .eq(SysUserRole::getUserId, doctorByNo.getId())
  272. .eq(SysUserRole::getRoleId, defaultRoleId));
  273. if (userRole==null){
  274. userRoleService.save(new SysUserRole(doctorByNo.getId(), defaultRoleId));
  275. }
  276. }
  277. }
  278. public void saveHisUserBatch(List<SysUser> userList) {
  279. this.saveBatch(userList);
  280. List<SysUserRole> defaultUserRoleList = new ArrayList<>();
  281. for (int i = 0; i < userList.size(); i++) {
  282. SysUser user = userList.get(i);
  283. //保存默认的用户角色关系
  284. defaultUserRoleList.add(new SysUserRole(user.getId(), defaultRoleId));
  285. }
  286. userRoleService.saveBatch(defaultUserRoleList);
  287. }
  288. @Override
  289. public boolean updateUserForm(Long userId, SysUser user) {
  290. String username = user.getUsername();
  291. SysUser entity = this.getOne(new LambdaQueryWrapper<SysUser>()
  292. .eq(SysUser::getId, userId)
  293. );
  294. Assert.isTrue(entity != null, "用户名已存在");
  295. // form -> entity
  296. if(StrUtil.isNotBlank(user.getAvatar())){
  297. entity.setAvatar(user.getAvatar());
  298. }
  299. if(StrUtil.isNotBlank(user.getMobile())){
  300. entity.setMobile(user.getMobile());
  301. }
  302. // 修改用户
  303. boolean result = this.updateById(entity);
  304. return result;
  305. }
  306. }