feat: 添加用户、角色服务和验证工具类
新增用户服务实现类UsersServiceImpl,包含用户CRUD操作 新增角色服务实现类RolesServiceImpl,包含角色管理功能 新增验证工具类ValidateUtil,提供参数验证功能 更新所有实体类添加@Builder注解 更新所有Mapper接口添加selectInfo和updateInfo方法 更新application.properties添加server.port配置 修复OrdersServiceImpl中Logger使用问题 添加updateInfo方法使用文档
This commit is contained in:
@@ -2,6 +2,8 @@ package com.qf.backend.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -13,14 +15,11 @@ import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.OrdersMapper;
|
||||
import com.qf.backend.service.OrdersService;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
|
||||
@Service
|
||||
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
|
||||
@Autowired
|
||||
private OrdersMapper ordersMapper;
|
||||
@Autowired
|
||||
private Logger logger;
|
||||
private Logger logger = LoggerFactory.getLogger(OrdersServiceImpl.class);
|
||||
|
||||
@Override
|
||||
public Result<Orders> getOrderByNumber(String orderNumber) {
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
|
||||
package com.qf.backend.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.qf.backend.entity.Roles;
|
||||
import com.qf.backend.mapper.RolesMapper;
|
||||
import com.qf.backend.service.RolesService;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 30803
|
||||
*/
|
||||
@Service
|
||||
public class RolesServiceImpl extends ServiceImpl<RolesMapper, Roles> implements RolesService {
|
||||
|
||||
@Autowired
|
||||
private RolesMapper rolesMapper;
|
||||
|
||||
// 根据角色名称查询角色
|
||||
@Override
|
||||
public Roles getRoleByName(String roleName) {
|
||||
try {
|
||||
if (roleName == null) {
|
||||
return null;
|
||||
}
|
||||
Roles roles = rolesMapper.selectOne(new QueryWrapper<Roles>().eq("role_name", roleName));
|
||||
if (roles == null) {
|
||||
return null;
|
||||
}
|
||||
return roles;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// 查询所有角色
|
||||
@Override
|
||||
public List<Roles> listAllRoles() {
|
||||
|
||||
throw new UnsupportedOperationException("Unimplemented method 'listAllRoles'");
|
||||
}
|
||||
// 根据ID查询角色
|
||||
@Override
|
||||
public Roles getRoleById(Long id) {
|
||||
|
||||
throw new UnsupportedOperationException("Unimplemented method 'getRoleById'");
|
||||
}
|
||||
// 根据用户ID查询角色
|
||||
@Override
|
||||
public List<Roles> listRolesByUserId(Long userId) {
|
||||
|
||||
throw new UnsupportedOperationException("Unimplemented method 'listRolesByUserId'");
|
||||
}
|
||||
// 创建角色
|
||||
@Override
|
||||
public boolean createRole(Roles roles) {
|
||||
|
||||
throw new UnsupportedOperationException("Unimplemented method 'createRole'");
|
||||
}
|
||||
// 更新角色
|
||||
@Override
|
||||
public boolean updateRole(Roles roles) {
|
||||
|
||||
throw new UnsupportedOperationException("Unimplemented method 'updateRole'");
|
||||
}
|
||||
// 批量删除角色
|
||||
@Override
|
||||
public boolean batchDeleteRoles(List<Long> ids) {
|
||||
|
||||
throw new UnsupportedOperationException("Unimplemented method 'batchDeleteRoles'");
|
||||
}
|
||||
// 删除角色
|
||||
@Override
|
||||
public boolean deleteRole(Long id) {
|
||||
|
||||
throw new UnsupportedOperationException("Unimplemented method 'deleteRole'");
|
||||
}
|
||||
}
|
||||
244
src/main/java/com/qf/backend/service/impl/UsersServiceImpl.java
Normal file
244
src/main/java/com/qf/backend/service/impl/UsersServiceImpl.java
Normal file
@@ -0,0 +1,244 @@
|
||||
package com.qf.backend.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.qf.backend.common.Result;
|
||||
import com.qf.backend.common.ResultUtils;
|
||||
import com.qf.backend.entity.Users;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.UsersMapper;
|
||||
import com.qf.backend.service.UsersService;
|
||||
|
||||
@Service
|
||||
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {
|
||||
|
||||
@Autowired
|
||||
private UsersMapper usersMapper;
|
||||
|
||||
// 根据用户名查询用户
|
||||
@Override
|
||||
public Result<Users> getUserByUsername(String username) {
|
||||
|
||||
try {
|
||||
if (username == null || username.isEmpty()) {
|
||||
return ResultUtils.fail(ErrorCode.INVALID_PARAM);
|
||||
}
|
||||
Users users = usersMapper.selectByUsername(username);
|
||||
if (users == null) {
|
||||
return ResultUtils.fail(ErrorCode.USER_NOT_FOUND);
|
||||
}
|
||||
return ResultUtils.success(users);
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据邮箱查询用户
|
||||
@Override
|
||||
public Result<Users> getUserByEmail(String email) {
|
||||
|
||||
try {
|
||||
if (email == null || email.isEmpty()) {
|
||||
return ResultUtils.fail(ErrorCode.INVALID_PARAM);
|
||||
}
|
||||
Users users = usersMapper.selectByEmail(email);
|
||||
if (users == null) {
|
||||
return ResultUtils.fail(ErrorCode.USER_NOT_FOUND);
|
||||
}
|
||||
return ResultUtils.success(users);
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
//创建用户
|
||||
@Override
|
||||
public Result<Boolean> createUser(Users users) {
|
||||
|
||||
try {
|
||||
// 调用封装的验证方法
|
||||
Result<Boolean> validationResult = validateUserBeforeCreate(users);
|
||||
if (validationResult != null) {
|
||||
return validationResult;
|
||||
}
|
||||
// 加密密码
|
||||
users.setPassword(new BCryptPasswordEncoder().encode(users.getPassword()));
|
||||
int result = usersMapper.insert(users);
|
||||
if (result <= 0) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
}
|
||||
return ResultUtils.success();
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新用户信息
|
||||
@Override
|
||||
public Result<Boolean> updateUser(Users users) {
|
||||
|
||||
try {
|
||||
// 调用封装的验证方法
|
||||
Result<Boolean> validationResult = validateUserBeforeCreate(users);
|
||||
if (validationResult != null) {
|
||||
return validationResult;
|
||||
}
|
||||
// 更新用户信息
|
||||
int result = usersMapper.updateInfo(users, new UpdateWrapper<Users>().set("username", users.getUsername()).set("email", users.getEmail()).set("phone", users.getPhone()).set("avatar", users.getAvatar()).eq("id", users.getId()));
|
||||
if (result <= 0) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
}
|
||||
return ResultUtils.success();
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除用户
|
||||
@Override
|
||||
public Result<Boolean> deleteUser(Long id) {
|
||||
|
||||
try {
|
||||
if (id == null) {
|
||||
return ResultUtils.fail(ErrorCode.MISSING_PARAM);
|
||||
}
|
||||
// 检查用户是否存在
|
||||
Users users = getUserByIdAndCheckExist(id);
|
||||
if (users == null) {
|
||||
return ResultUtils.fail(ErrorCode.USER_NOT_FOUND);
|
||||
}
|
||||
int result = usersMapper.deleteById(id);
|
||||
if (result <= 0) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
}
|
||||
return ResultUtils.success();
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
// 查询所有用户
|
||||
@Override
|
||||
public Result<List<Users>> listAllUsers() {
|
||||
|
||||
try {
|
||||
List<Users> usersList = usersMapper.selectList(null);
|
||||
if (usersList == null || usersList.isEmpty()) {
|
||||
return ResultUtils.fail(ErrorCode.NOT_FOUND);
|
||||
}
|
||||
return ResultUtils.success(usersList);
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
// 分页查询用户
|
||||
@Override
|
||||
public Result<List<Users>> listUsersByPage(int page, int size) {
|
||||
|
||||
throw new UnsupportedOperationException("Unimplemented method 'listUsersByPage'");
|
||||
}
|
||||
|
||||
// 根据ID查询用户
|
||||
@Override
|
||||
public Result<Users> getUserById(Long id) {
|
||||
|
||||
try {
|
||||
if (id == null) {
|
||||
return ResultUtils.fail(ErrorCode.MISSING_PARAM);
|
||||
}
|
||||
Users users = getUserByIdAndCheckExist(id);
|
||||
if (users == null) {
|
||||
return ResultUtils.fail(ErrorCode.USER_NOT_FOUND);
|
||||
}
|
||||
return ResultUtils.success(users);
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新用户密码
|
||||
@Override
|
||||
public Result<Boolean> updatePassword(Long id, String newPassword) {
|
||||
|
||||
try {
|
||||
if (id == null || newPassword == null || newPassword.isEmpty()) {
|
||||
return ResultUtils.fail(ErrorCode.MISSING_PARAM);
|
||||
}
|
||||
Users users = getUserByIdAndCheckExist(id);
|
||||
if (users == null) {
|
||||
return ResultUtils.fail(ErrorCode.USER_NOT_FOUND);
|
||||
}
|
||||
// 加密新密码
|
||||
users.setPassword(new BCryptPasswordEncoder().encode(newPassword));
|
||||
// 更新密码
|
||||
int result = usersMapper.updateInfo(users, new UpdateWrapper<Users>().set("password", users.getPassword()).eq("id", id));
|
||||
if (result <= 0) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
}
|
||||
return ResultUtils.success();
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户ID查询用户并检查是否存在
|
||||
* @param id 用户ID
|
||||
* @return 用户对象,如果用户不存在则返回null
|
||||
*/
|
||||
private Users getUserByIdAndCheckExist(Long id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>().eq("id", id);
|
||||
return usersMapper.selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证用户信息是否合法且不存在重复
|
||||
*
|
||||
* @param users 用户对象
|
||||
* @return 验证失败时返回错误Result,验证成功时返回null
|
||||
*/
|
||||
private Result<Boolean> validateUserBeforeCreate(Users users) {
|
||||
// 检查用户对象是否为空
|
||||
if (users == null) {
|
||||
return ResultUtils.fail(ErrorCode.MISSING_PARAM);
|
||||
}
|
||||
// 检查必要字段是否为空
|
||||
if (users.getUsername() == null || users.getUsername().isEmpty()) {
|
||||
return ResultUtils.fail(ErrorCode.MISSING_PARAM);
|
||||
}
|
||||
if (users.getEmail() == null || users.getEmail().isEmpty()) {
|
||||
return ResultUtils.fail(ErrorCode.MISSING_PARAM);
|
||||
}
|
||||
if (users.getPassword() == null || users.getPassword().isEmpty()) {
|
||||
return ResultUtils.fail(ErrorCode.MISSING_PARAM);
|
||||
}
|
||||
// 检查用户名是否已存在
|
||||
Users existingUserByUsername = usersMapper.selectByUsername(users.getUsername());
|
||||
if (existingUserByUsername != null) {
|
||||
return ResultUtils.fail(ErrorCode.USER_EXISTED);
|
||||
}
|
||||
// 检查邮箱是否已存在
|
||||
Users existingUserByEmail = usersMapper.selectByEmail(users.getEmail());
|
||||
if (existingUserByEmail != null) {
|
||||
return ResultUtils.fail(ErrorCode.USER_EXISTED);
|
||||
}
|
||||
// 检查手机号是否已存在
|
||||
Users existingUserByPhone = usersMapper.selectByPhone(users.getPhone());
|
||||
if (existingUserByPhone != null) {
|
||||
return ResultUtils.fail(ErrorCode.USER_EXISTED);
|
||||
}
|
||||
return null; // 验证通过
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user