123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- package com.qmrb.system.service.impl;
- import cn.hutool.core.collection.CollectionUtil;
- import cn.hutool.core.lang.Assert;
- import cn.hutool.core.util.StrUtil;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.qmrb.system.common.constant.SecurityConstants;
- import com.qmrb.system.common.constant.SystemConstants;
- import com.qmrb.system.converter.UserConverter;
- import com.qmrb.system.framework.security.util.SecurityUtils;
- import com.qmrb.system.mapper.SysUserMapper;
- import com.qmrb.system.pojo.bo.UserAuthInfo;
- import com.qmrb.system.pojo.bo.UserBO;
- import com.qmrb.system.pojo.bo.UserFormBO;
- import com.qmrb.system.pojo.entity.SysUser;
- import com.qmrb.system.pojo.entity.SysUserRole;
- import com.qmrb.system.pojo.form.UserForm;
- import com.qmrb.system.pojo.query.UserPageQuery;
- import com.qmrb.system.pojo.vo.UserExportVO;
- import com.qmrb.system.pojo.vo.UserInfoVO;
- import com.qmrb.system.pojo.vo.UserPageVO;
- import com.qmrb.system.service.SysMenuService;
- import com.qmrb.system.service.SysRoleService;
- import com.qmrb.system.service.SysUserRoleService;
- import com.qmrb.system.service.SysUserService;
- import com.qmrb.system.utils.MqUtil;
- import lombok.RequiredArgsConstructor;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.security.crypto.password.PasswordEncoder;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.util.*;
- import java.util.stream.Collectors;
- /**
- * 用户业务实现类
- *
- * @author haoxr
- * @date 2022/1/14
- */
- @Service
- @RequiredArgsConstructor
- public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
- private final PasswordEncoder passwordEncoder;
- private final SysUserRoleService userRoleService;
- private final UserConverter userConverter;
- private final SysMenuService menuService;
- private final SysRoleService roleService;
- @SuppressWarnings("rawtypes")
- private final RedisTemplate redisTemplate;
- @Value("${application.default-dept-id}")
- private Long defaultDeptId;
- @Value("${application.default-role-id}")
- private Long defaultRoleId;
- /**
- * 获取用户分页列表
- *
- * @param queryParams
- * @return
- */
- @Override
- public IPage<UserPageVO> getUserPage(UserPageQuery queryParams) {
- // 参数构建
- int pageNum = queryParams.getPageNum();
- int pageSize = queryParams.getPageSize();
- Page<UserBO> page = new Page<>(pageNum, pageSize);
- // 查询数据
- Page<UserBO> userBoPage = this.baseMapper.getUserPage(page, queryParams);
- // 实体转换
- Page<UserPageVO> userVoPage = userConverter.bo2Vo(userBoPage);
- return userVoPage;
- }
- /**
- * 获取用户详情
- *
- * @param userId
- * @return
- */
- @Override
- public UserForm getUserFormData(Long userId) {
- UserFormBO userFormBO = this.baseMapper.getUserDetail(userId);
- // 实体转换po->form
- UserForm userForm = userConverter.bo2Form(userFormBO);
- return userForm;
- }
- /**
- * 新增用户
- *
- * @param userForm 用户表单对象
- * @return
- */
- @Override
- public boolean saveUser(UserForm userForm) {
- String username = userForm.getUsername();
- long count = this.count(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username));
- Assert.isTrue(count == 0, "用户名已存在");
- // 实体转换 form->entity
- SysUser entity = userConverter.form2Entity(userForm);
- // 设置默认加密密码
- String defaultEncryptPwd = passwordEncoder.encode(SystemConstants.DEFAULT_PASSWORD);
- entity.setPassword(defaultEncryptPwd);
- // 新增用户
- boolean result = this.save(entity);
- if (result) {
- // 保存用户角色
- userRoleService.saveUserRoles(entity.getId(), userForm.getRoleIds());
- }
- return result;
- }
- /**
- * 更新用户
- *
- * @param userId 用户ID
- * @param userForm 用户表单对象
- * @return
- */
- @Override
- @Transactional
- public boolean updateUser(Long userId, UserForm userForm) {
- String username = userForm.getUsername();
- long count = this.count(new LambdaQueryWrapper<SysUser>()
- .eq(SysUser::getUsername, username)
- .ne(SysUser::getId, userId)
- );
- Assert.isTrue(count == 0, "用户名已存在");
- // form -> entity
- SysUser entity = userConverter.form2Entity(userForm);
- // 修改用户
- boolean result = this.updateById(entity);
- if (result) {
- // 保存用户角色
- userRoleService.saveUserRoles(entity.getId(), userForm.getRoleIds());
- }
- return result;
- }
- /**
- * 删除用户
- *
- * @param idsStr 用户ID,多个以英文逗号(,)分割
- * @return
- */
- @Override
- public boolean deleteUsers(String idsStr) {
- Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的用户数据为空");
- // 逻辑删除
- List<Long> ids = Arrays.asList(idsStr.split(",")).stream()
- .map(idStr -> Long.parseLong(idStr)).collect(Collectors.toList());
- boolean result = this.removeByIds(ids);
- return result;
- }
- /**
- * 修改用户密码
- *
- * @param userId 用户ID
- * @param password 用户密码
- * @return true|false
- */
- @Override
- public boolean updatePassword(Long userId, String password) {
- return this.update(new LambdaUpdateWrapper<SysUser>()
- .eq(SysUser::getId, userId)
- .set(SysUser::getPassword, passwordEncoder.encode(password))
- );
- }
- /**
- * 根据用户名获取认证信息
- *
- * @param username 用户名
- * @return 用户认证信息 {@link UserAuthInfo}
- */
- @Override
- public UserAuthInfo getUserAuthInfo(String username) {
- UserAuthInfo userAuthInfo = this.baseMapper.getUserAuthInfo(username);
- if (userAuthInfo != null) {
- Set<String> roles = userAuthInfo.getRoles();
- if (CollectionUtil.isNotEmpty(roles)) {
- Set<String> perms = menuService.listRolePerms(roles);
- userAuthInfo.setPerms(perms);
- }
- // 获取最大范围的数据权限
- Integer dataScope = roleService.getMaximumDataScope(roles);
- userAuthInfo.setDataScope(dataScope);
- }
- return userAuthInfo;
- }
- /**
- * 获取导出用户列表
- *
- * @param queryParams
- * @return
- */
- @Override
- public List<UserExportVO> listExportUsers(UserPageQuery queryParams) {
- List<UserExportVO> list = this.baseMapper.listExportUsers(queryParams);
- return list;
- }
- /**
- * 获取登录用户信息
- *
- * @return
- */
- @Override
- public UserInfoVO getUserLoginInfo() {
- // 登录用户entity
- SysUser user = this.getOne(new LambdaQueryWrapper<SysUser>()
- .eq(SysUser::getUsername, SecurityUtils.getUser().getUsername())
- .select(
- SysUser::getId,
- SysUser::getNickname,
- SysUser::getMobile,
- SysUser::getAvatar
- )
- );
- // entity->VO
- UserInfoVO userInfoVO = userConverter.entity2UserInfoVo(user);
- // 用户角色集合
- Set<String> roles = SecurityUtils.getRoles();
- userInfoVO.setRoles(roles);
- userInfoVO.setPhone(user.getMobile());
- // 用户权限集合
- @SuppressWarnings("unchecked")
- Set<String> perms = (Set<String>) redisTemplate.opsForValue().get(SecurityConstants.USER_PERMS_CACHE_PREFIX+ user.getId());
- userInfoVO.setPerms(perms);
- return userInfoVO;
- }
- @Override
- public Map<String, String> getUserNameByDoctorNo(Set<String> doctorNos) {
- LambdaQueryWrapper<SysUser> wrapper=new LambdaQueryWrapper<>();
- wrapper.in(SysUser::getUsername,doctorNos);
- return this.list(wrapper).stream()
- .collect(Collectors.toMap(SysUser::getUsername, SysUser::getNickname));
- }
- /**
- * 同步医生数据到数据库中
- */
- @Override
- public void syncDoctorInfo(String reqMsg,String serviceId) {
- String msgByEsb = MqUtil.getMsgByEsb(reqMsg, serviceId);
- List<String> columns = Arrays.asList("Staff_Name", "Staff_Code", "ID_Number","Dept_name", "Sex_Name", "Mobile_No", "level_name");
- List<Map<String, String>> maps = MqUtil.parseXml(msgByEsb, columns);
- //查询是否存在,存在更新,不存在新增
- for (Map<String, String> map : maps) {
- SysUser doctor=new SysUser();
- doctor.setUsername(map.get("Staff_Code"));
- doctor.setNickname(map.get("Staff_Name"));
- // doctor.setPassword(passwordEncoder.encode(SystemConstants.DEFAULT_PASSWORD));
- doctor.setMobile(map.get("Mobile_No"));
- doctor.setGender("男".equals(map.get("Sex_Name"))?0:1);
- doctor.setCardNo(map.get("ID_Number"));
- doctor.setDeptId(defaultDeptId);
- doctor.setLevelName(map.get("level_name"));
- SysUser staffCode = this.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, map.get("Staff_Code")));
- if (staffCode==null){
- //不存在新增
- this.save(doctor);
- }else {
- //存在更新
- this.update(doctor,new LambdaUpdateWrapper<SysUser>().eq(SysUser::getUsername, map.get("Staff_Code")));
- }
- //获取当前用户
- SysUser doctorByNo = this.getOne(new LambdaUpdateWrapper<SysUser>()
- .eq(SysUser::getUsername, map.get("Staff_Code")));
- //添加权限前删除原有权限
- userRoleService.remove(new LambdaUpdateWrapper<SysUserRole>()
- .eq(SysUserRole::getUserId, doctorByNo.getId()));
- //添加权限
- SysUserRole userRole = userRoleService.getOne(new LambdaQueryWrapper<SysUserRole>()
- .eq(SysUserRole::getUserId, doctorByNo.getId())
- .eq(SysUserRole::getRoleId, defaultRoleId));
- if (userRole==null){
- userRoleService.save(new SysUserRole(doctorByNo.getId(), defaultRoleId));
- }
- }
- }
- public void saveHisUserBatch(List<SysUser> userList) {
- this.saveBatch(userList);
- List<SysUserRole> defaultUserRoleList = new ArrayList<>();
- for (int i = 0; i < userList.size(); i++) {
- SysUser user = userList.get(i);
- //保存默认的用户角色关系
- defaultUserRoleList.add(new SysUserRole(user.getId(), defaultRoleId));
- }
- userRoleService.saveBatch(defaultUserRoleList);
- }
- @Override
- public boolean updateUserForm(Long userId, SysUser user) {
- String username = user.getUsername();
- SysUser entity = this.getOne(new LambdaQueryWrapper<SysUser>()
- .eq(SysUser::getId, userId)
- );
- Assert.isTrue(entity != null, "用户名已存在");
- // form -> entity
- if(StrUtil.isNotBlank(user.getAvatar())){
- entity.setAvatar(user.getAvatar());
- }
- if(StrUtil.isNotBlank(user.getMobile())){
- entity.setMobile(user.getMobile());
- }
-
- // 修改用户
- boolean result = this.updateById(entity);
-
- return result;
- }
- }
|