New file |
| | |
| | | package com.cl.service.impl; |
| | | |
| | | 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.service.impl.ServiceImpl; |
| | | import com.cl.common.constant.DelFlagConstant; |
| | | import com.cl.common.constant.StatusConstant; |
| | | import com.cl.common.context.BaseContext; |
| | | import com.cl.common.exception.user.AddUserException; |
| | | import com.cl.common.exception.user.UserException; |
| | | import com.cl.mapper.UserMapper; |
| | | import com.cl.pojo.dto.AddUserDTO; |
| | | import com.cl.pojo.dto.EditUserDTO; |
| | | import com.cl.pojo.dto.PasswordDTO; |
| | | import com.cl.pojo.entity.User; |
| | | import com.cl.pojo.vo.UserVO; |
| | | import com.cl.service.UserService; |
| | | import com.cl.util.BCryptPasswordEncoder; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.codec.digest.DigestUtils; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.util.Objects; |
| | | |
| | | @Service |
| | | @Slf4j |
| | | public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { |
| | | private final UserMapper userMapper; |
| | | private final TokenBlacklistService tokenBlacklistService; |
| | | |
| | | public UserServiceImpl(UserMapper userMapper, TokenBlacklistService tokenBlacklistService) { |
| | | this.userMapper = userMapper; |
| | | this.tokenBlacklistService = tokenBlacklistService; |
| | | } |
| | | |
| | | @Override |
| | | public void addUser(AddUserDTO addUserDTO) { |
| | | String phone = addUserDTO.getPhone(); |
| | | //查询手机号是否存在 |
| | | User user1 = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getPhone, phone) |
| | | .eq(User::getDelFlag, DelFlagConstant.UNDELETE)); |
| | | if (user1 != null) { |
| | | throw new AddUserException("该用户已存在"); |
| | | } |
| | | log.info("开始添加用户:{}",addUserDTO); |
| | | User user = new User(); |
| | | user.setPhone(phone); |
| | | user.setName(addUserDTO.getName()); |
| | | user.setRemark(addUserDTO.getRemark()); |
| | | user.setPassword(BCryptPasswordEncoder.encode(DigestUtils.md5Hex(phone.substring(phone.length() - 6)))); |
| | | user.setCreateBy(BaseContext.getCurrentUser().getId()); |
| | | user.setCreateTime(LocalDateTime.now()); |
| | | |
| | | boolean save = this.save(user); |
| | | if (!save) { |
| | | throw new UserException("新增用户失败"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public IPage<UserVO> pageList(IPage<User> page, String name, String phone) { |
| | | return userMapper.pageList(page,name,phone); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void editUser(EditUserDTO editUserDTO) { |
| | | User user = userMapper.selectById(editUserDTO.getId()); |
| | | if (user == null) { |
| | | throw new UserException("该用户不存在"); |
| | | } |
| | | LambdaUpdateWrapper<User> updateWrapper= new LambdaUpdateWrapper<>(); |
| | | updateWrapper.eq(User::getId, editUserDTO.getId()); |
| | | updateWrapper.set(User::getName, editUserDTO.getName()); |
| | | |
| | | User phoneUser = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getPhone, editUserDTO.getPhone()) |
| | | .eq(User::getDelFlag, DelFlagConstant.UNDELETE) |
| | | .ne(User::getId, editUserDTO.getId())); |
| | | if (phoneUser != null) { |
| | | throw new UserException("该手机号用户已存在"); |
| | | } |
| | | updateWrapper.set(User::getPhone, editUserDTO.getPhone()); |
| | | |
| | | updateWrapper.set(User::getRemark, editUserDTO.getRemark()); |
| | | updateWrapper.set(User::getUpdateBy, BaseContext.getCurrentUser().getId()); |
| | | updateWrapper.set(User::getUpdateTime, LocalDateTime.now()); |
| | | |
| | | int update = userMapper.update(new User(), updateWrapper); |
| | | if (update != 1) { |
| | | throw new UserException("修改用户失败"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void password(PasswordDTO passwordDTO,String token) { |
| | | User user = userMapper.selectById(BaseContext.getCurrentUser().getId()); |
| | | //校验原密码 |
| | | if (!BCryptPasswordEncoder.matches(passwordDTO.getPassword(), user.getPassword())) { |
| | | //不通过 |
| | | throw new UserException("原密码错误"); |
| | | } |
| | | //修改密码 |
| | | user.setPassword(BCryptPasswordEncoder.encode(passwordDTO.getNewPassword())); |
| | | user.setUpdateBy(user.getId()); |
| | | user.setUpdateTime(LocalDateTime.now()); |
| | | boolean save = this.updateById(user); |
| | | if (!save) { |
| | | throw new UserException("修改密码失败"); |
| | | } |
| | | //将令牌加入黑名单 |
| | | tokenBlacklistService.addToBlacklist(token); |
| | | } |
| | | |
| | | @Override |
| | | public void resetPassword(Integer id) { |
| | | User user = userMapper.selectById(id); |
| | | if (null==user){ |
| | | throw new UserException("该用户不存在"); |
| | | } |
| | | user.setPassword(BCryptPasswordEncoder.encode(DigestUtils.md5Hex(user.getPhone().substring(user.getPhone().length() - 6)))); |
| | | user.setIsFirst(1); |
| | | user.setUpdateBy(BaseContext.getCurrentUser().getId()); |
| | | user.setUpdateTime(LocalDateTime.now()); |
| | | boolean save = this.updateById(user); |
| | | if (!save) { |
| | | throw new UserException("重置密码失败"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void frozen(Integer id) { |
| | | User user = userMapper.selectById(id); |
| | | if (null==user){ |
| | | throw new UserException("用户不存在"); |
| | | } |
| | | user.setStatus(Objects.equals(user.getStatus(), StatusConstant.DISABLE) ? StatusConstant.ENABLE : StatusConstant.DISABLE); |
| | | user.setUpdateBy(BaseContext.getCurrentUser().getId()); |
| | | user.setUpdateTime(LocalDateTime.now()); |
| | | boolean save = this.updateById(user); |
| | | if (!save) { |
| | | throw new UserException("冻结/解冻用户失败"); |
| | | } |
| | | } |
| | | } |