170 lines
4.8 KiB
Java
170 lines
4.8 KiB
Java
package com.bruce.sams.service.impl;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.bruce.sams.domain.sys.User;
|
|
import com.bruce.sams.common.exception.PasswordIncorrectException;
|
|
import com.bruce.sams.common.exception.UserNotFoundException;
|
|
import com.bruce.sams.mapper.UserMapper;
|
|
import com.bruce.sams.service.UserService;
|
|
import com.bruce.sams.common.utils.PasswordUtil;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.List;
|
|
|
|
@Service
|
|
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
implements UserService {
|
|
|
|
@Autowired
|
|
private UserMapper userMapper;
|
|
|
|
/**
|
|
* 批量导入用户(管理端)
|
|
*
|
|
* @param users 用户列表
|
|
*/
|
|
public void importUsers(List<User> users) {
|
|
for (User user : users) {
|
|
// 检查用户是否存在
|
|
User existUser = userMapper.selectOne(
|
|
new LambdaQueryWrapper<User>().eq(User::getSchoolId, user.getSchoolId()));
|
|
|
|
if (existUser == null) {
|
|
// 加密默认密码 123456
|
|
user.setPassword(PasswordUtil.encode("123456"));
|
|
userMapper.insert(user);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 查询用户列表(学号、用户名、邮箱)
|
|
*
|
|
* @param keyword 关键字
|
|
* @return 用户列表
|
|
*/
|
|
public List<User> listUsers(String keyword) {
|
|
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
|
|
if (keyword != null && !keyword.isEmpty()) {
|
|
queryWrapper.like(User::getUserName, keyword)
|
|
.or().like(User::getSchoolId, keyword)
|
|
.or().like(User::getEmail, keyword);
|
|
}
|
|
return userMapper.selectList(queryWrapper);
|
|
}
|
|
|
|
/**
|
|
* 更新用户信息
|
|
*
|
|
* @param user 用户对象
|
|
*/
|
|
public void updateUser(User user) {
|
|
// 如果要修改密码,先加密
|
|
if (user.getPassword() != null && !user.getPassword().isEmpty()) {
|
|
user.setPassword(PasswordUtil.encode(user.getPassword()));
|
|
}
|
|
userMapper.updateById(user);
|
|
}
|
|
|
|
|
|
/**
|
|
* 管理员重置用户密码(管理端)
|
|
*
|
|
* @param userId 用户ID
|
|
*/
|
|
public void resetPassword(Long userId) {
|
|
User user = userMapper.selectById(userId);
|
|
if (user == null) {
|
|
throw new UserNotFoundException();
|
|
}
|
|
|
|
// 设定默认密码 123456
|
|
user.setPassword(PasswordUtil.encode("123456"));
|
|
userMapper.updateById(user);
|
|
}
|
|
|
|
/**
|
|
* 封禁用户(管理端)
|
|
*
|
|
* @param userId 用户ID
|
|
*/
|
|
public void banUser(Long userId) {
|
|
User user = userMapper.selectById(userId);
|
|
if (user == null) {
|
|
throw new UserNotFoundException();
|
|
}
|
|
|
|
// 更新状态为 banned
|
|
user.setStatus("banned");
|
|
userMapper.updateById(user);
|
|
}
|
|
|
|
/**
|
|
* 用户修改密码(用户端)
|
|
*
|
|
* @param userId 用户ID
|
|
* @param oldPassword 旧密码
|
|
* @param newPassword 新密码
|
|
*/
|
|
public void changePassword(Long userId, String oldPassword, String newPassword) {
|
|
User user = userMapper.selectById(userId);
|
|
if (user == null) {
|
|
throw new UserNotFoundException();
|
|
}
|
|
|
|
// 验证旧密码
|
|
if (!PasswordUtil.matches(oldPassword, user.getPassword())) {
|
|
throw new PasswordIncorrectException();
|
|
}
|
|
|
|
// 加密新密码
|
|
user.setPassword(PasswordUtil.encode(newPassword));
|
|
userMapper.updateById(user);
|
|
}
|
|
|
|
/**
|
|
* 用户修改个人信息(用户端)
|
|
*
|
|
* @param userId 用户ID
|
|
* @param updatedUser 用户提交的新信息(昵称、邮箱、头像)
|
|
*/
|
|
public void updateProfile(Long userId, User updatedUser) {
|
|
User user = userMapper.selectById(userId);
|
|
if (user == null) {
|
|
throw new UserNotFoundException();
|
|
}
|
|
|
|
// 仅允许修改部分字段
|
|
user.setNickName(updatedUser.getNickName());
|
|
user.setEmail(updatedUser.getEmail());
|
|
user.setAvatar(updatedUser.getAvatar());
|
|
|
|
userMapper.updateById(user);
|
|
}
|
|
|
|
/**
|
|
* 获取用户详情(用户端)
|
|
*
|
|
* @param userId 用户ID
|
|
* @return 用户信息
|
|
*/
|
|
public User getUserProfile(Long userId) {
|
|
User user = userMapper.selectById(userId);
|
|
if (user == null) {
|
|
throw new UserNotFoundException();
|
|
}
|
|
return user;
|
|
}
|
|
|
|
/**
|
|
* 删除用户
|
|
*
|
|
* @param userId 用户ID
|
|
*/
|
|
public void deleteUser(Long userId) {
|
|
userMapper.deleteById(userId);
|
|
}
|
|
}
|