|
@@ -0,0 +1,312 @@
|
|
|
+package com.fire.admin.modules.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fire.admin.dto.UserDTO;
|
|
|
+import com.fire.admin.exception.PreBaseException;
|
|
|
+import com.fire.admin.modules.mapper.SysUserMapper;
|
|
|
+import com.fire.admin.modules.service.ISysDeptService;
|
|
|
+import com.fire.admin.modules.service.ISysMenuService;
|
|
|
+import com.fire.admin.modules.service.ISysUserRoleService;
|
|
|
+import com.fire.admin.modules.service.ISysUserService;
|
|
|
+import com.fire.admin.util.JwtUtil;
|
|
|
+import com.fire.admin.util.PreSecurityUser;
|
|
|
+import com.fire.admin.util.PreUtil;
|
|
|
+import com.fire.dto.system.SysUser;
|
|
|
+import com.fire.dto.system.SysUserRole;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
+import org.springframework.security.authentication.AuthenticationManager;
|
|
|
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @classname: SysUserServiceImpl
|
|
|
+ * @description TODO 用户表 服务实现类
|
|
|
+ * @author: liu liu
|
|
|
+ * @create: 2020-08-13 14:51
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ISysUserRoleService userRoleService;
|
|
|
+ @Autowired
|
|
|
+ private ISysDeptService deptService;
|
|
|
+ @Autowired
|
|
|
+ private ISysMenuService menuService;
|
|
|
+ @Lazy
|
|
|
+ @Autowired
|
|
|
+ private AuthenticationManager authenticationManager;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: TODO 分页查询用户信息(含有角色信息)
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: liu liu
|
|
|
+ * @date: 2020/8/31 15:51
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public IPage<SysUser> getUsersWithRolePage(Page page, UserDTO userDTO) {
|
|
|
+ if (userDTO != null && userDTO.getDeptId() != null) {
|
|
|
+ userDTO.setDeptList(deptService.selectDeptIds(userDTO.getDeptId()));
|
|
|
+ }
|
|
|
+ return baseMapper.getUserVosPage(page, userDTO);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: TODO 保存用户以及角色部门等信息
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: liu liu
|
|
|
+ * @date: 2020/8/31 15:51
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public boolean insertUser(UserDTO userDto) {
|
|
|
+ SysUser sysUser = new SysUser();
|
|
|
+ BeanUtils.copyProperties(userDto, sysUser);
|
|
|
+ // 默认密码 123456
|
|
|
+ sysUser.setPassword(PreUtil.encode("123456"));
|
|
|
+ log.info("新增用户数据为:【{}】", sysUser.toString());
|
|
|
+ baseMapper.insertUser(sysUser);
|
|
|
+ List<SysUserRole> userRoles = userDto.getRoleList().stream().map(item -> {
|
|
|
+ SysUserRole sysUserRole = new SysUserRole();
|
|
|
+ sysUserRole.setRoleId(item);
|
|
|
+ sysUserRole.setUserId(sysUser.getUserId());
|
|
|
+ return sysUserRole;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ return userRoleService.saveBatch(userRoles);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: TODO 更新用户以及角色部门等信息
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: liu liu
|
|
|
+ * @date: 2020/8/31 15:51
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public boolean updateUser(UserDTO userDto) {
|
|
|
+ SysUser sysUser = new SysUser();
|
|
|
+ BeanUtils.copyProperties(userDto, sysUser);
|
|
|
+ baseMapper.updateById(sysUser);
|
|
|
+ userRoleService.remove(Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getUserId, sysUser.getUserId()));
|
|
|
+ List<SysUserRole> userRoles = userDto.getRoleList().stream().map(item -> {
|
|
|
+ SysUserRole sysUserRole = new SysUserRole();
|
|
|
+ sysUserRole.setRoleId(item);
|
|
|
+ sysUserRole.setUserId(sysUser.getUserId());
|
|
|
+ return sysUserRole;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ return userRoleService.saveBatch(userRoles);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: TODO 删除用户信息
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: liu liu
|
|
|
+ * @date: 2020/8/31 15:51
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public boolean removeUser(Integer userId) {
|
|
|
+ baseMapper.deleteById(userId);
|
|
|
+ return userRoleService.remove(Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getUserId, userId));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: TODO 重置密码
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: liu liu
|
|
|
+ * @date: 2020/8/31 15:51
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public boolean restPass(Integer userId) {
|
|
|
+ return baseMapper.updateById(new SysUser().setPassword("123456").setUserId(userId)) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: TODO 通过用户名查找用户个人信息
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: liu liu
|
|
|
+ * @date: 2020/8/31 15:51
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public SysUser findByUserInfoName(String username) {
|
|
|
+ SysUser sysUser = baseMapper.selectOne(Wrappers.<SysUser>lambdaQuery().select(SysUser::getUserId, SysUser::getUsername, SysUser::getPhone, SysUser::getEmail, SysUser::getPassword, SysUser::getDeptId, SysUser::getJobId, SysUser::getAvatar, SysUser::getType).eq(SysUser::getUsername, username));
|
|
|
+ // 获取部门
|
|
|
+ sysUser.setDeptName(deptService.selectDeptNameByDeptId(sysUser.getDeptId()));
|
|
|
+ // 获取岗位
|
|
|
+// sysUser.setJobName(jobService.selectJobNameByJobId(sysUser.getJobId()));
|
|
|
+ return sysUser;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: TODO 根据用户id查询权限
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: liu liu
|
|
|
+ * @date: 2020/8/31 15:52
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Set<String> findPermsByUserId(Integer userId) {
|
|
|
+ return menuService.findPermsByUserId(userId).stream().filter(StrUtil::isNotEmpty).collect(Collectors.toSet());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: TODO 通过用户id查询角色集合
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: liu liu
|
|
|
+ * @date: 2020/8/31 15:52
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Set<String> findRoleIdByUserId(Integer userId) {
|
|
|
+ return userRoleService.selectUserRoleListByUserId(userId).stream().map(sysUserRole -> "ROLE_" + sysUserRole.getRoleId()).collect(Collectors.toSet());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: TODO 账户密码登录
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: liu liu
|
|
|
+ * @date: 2020/8/31 15:52
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String login(String username, String password) {
|
|
|
+ //用户验证
|
|
|
+ Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
|
|
|
+ //存储认证信息
|
|
|
+ SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
|
+ //生成token
|
|
|
+ PreSecurityUser userDetail = (PreSecurityUser) authentication.getPrincipal();
|
|
|
+ return JwtUtil.generateToken(userDetail);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: TODO 注册用户
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: liu liu
|
|
|
+ * @date: 2020/8/31 15:52
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public boolean register(UserDTO userDTO) {
|
|
|
+ // 查询用户名是否存在
|
|
|
+ SysUser byUserInfoName = findSecurityUser(userDTO.getUsername());
|
|
|
+ if (ObjectUtil.isNotNull(byUserInfoName)) {
|
|
|
+ throw new PreBaseException("账户名已被注册");
|
|
|
+ }
|
|
|
+ SysUser securityUser = findSecurityUser(userDTO.getPhone());
|
|
|
+ if (ObjectUtil.isNotNull(securityUser)) {
|
|
|
+ throw new PreBaseException("手机号已被注册");
|
|
|
+ }
|
|
|
+ userDTO.setDeptId(6);
|
|
|
+ userDTO.setLockFlag("0");
|
|
|
+ SysUser sysUser = new SysUser();
|
|
|
+ // 对象拷贝
|
|
|
+ BeanUtil.copyProperties(userDTO, sysUser);
|
|
|
+ // 加密后的密码
|
|
|
+ sysUser.setPassword(PreUtil.encode(userDTO.getPassword()));
|
|
|
+ baseMapper.insertUser(sysUser);
|
|
|
+ SysUserRole sysUserRole = new SysUserRole();
|
|
|
+ sysUserRole.setRoleId(14);
|
|
|
+ sysUserRole.setUserId(sysUser.getUserId());
|
|
|
+ return userRoleService.save(sysUserRole);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: TODO 修改用户信息
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: liu liu
|
|
|
+ * @date: 2020/8/31 15:52
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public boolean updateUserInfo(SysUser sysUser) {
|
|
|
+ return baseMapper.updateById(sysUser) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: TODO 通过用户去查找用户(id/用户名/手机号)
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: liu liu
|
|
|
+ * @date: 2020/8/31 15:52
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public SysUser findSecurityUserByUser(SysUser sysUser) {
|
|
|
+ LambdaQueryWrapper<SysUser> select = Wrappers.<SysUser>lambdaQuery().select(SysUser::getUserId, SysUser::getUsername, SysUser::getPassword);
|
|
|
+ if (StrUtil.isNotEmpty(sysUser.getUsername())) {
|
|
|
+ select.eq(SysUser::getUsername, sysUser.getUsername());
|
|
|
+ } else if (StrUtil.isNotEmpty(sysUser.getPhone())) {
|
|
|
+ select.eq(SysUser::getPhone, sysUser.getPhone());
|
|
|
+ } else if (ObjectUtil.isNotNull(sysUser.getUserId()) && sysUser.getUserId() != 0) {
|
|
|
+ select.eq(SysUser::getUserId, sysUser.getUserId());
|
|
|
+ }
|
|
|
+ return baseMapper.selectOne(select);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: 获取区域管理员和驿站管理员
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: liu liu
|
|
|
+ * @date: 2020/8/17 16:15
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<SysUser> findUserType(String type) {
|
|
|
+ LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.select(SysUser::getUserId, SysUser::getUsername)
|
|
|
+ .eq(SysUser::getType, type);
|
|
|
+ return baseMapper.selectList(wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: TODO 获取用户的职权范围
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: liu liu
|
|
|
+ * @date: 2020/9/2 15:13
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public int findType(Integer userId) {
|
|
|
+ LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.select(SysUser::getType).eq(SysUser::getUserId, userId);
|
|
|
+ return baseMapper.selectOne(wrapper).getType();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private SysUser findSecurityUser(String userIdOrUserNameOrPhone) {
|
|
|
+ LambdaQueryWrapper<SysUser> select = Wrappers.<SysUser>lambdaQuery().select(SysUser::getUserId, SysUser::getUsername, SysUser::getPassword).eq(SysUser::getUsername, userIdOrUserNameOrPhone).or().eq(SysUser::getPhone, userIdOrUserNameOrPhone).or().eq(SysUser::getUserId, userIdOrUserNameOrPhone);
|
|
|
+ return baseMapper.selectOne(select);
|
|
|
+ }
|
|
|
+}
|