feat: 添加用户登录服务及JWT认证功能
refactor: 重构实体类并添加Lombok注解 docs: 更新数据库表结构文档 style: 清理无用代码并优化格式 fix: 修复用户详情服务中的联系方式更新方法 build: 更新pom.xml配置并添加Lombok插件 test: 添加用户登录测试用例 chore: 添加开发和生产环境配置文件
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.qf.backend.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.qf.backend.common.Result;
|
||||
import com.qf.backend.entity.Permissions;
|
||||
|
||||
import java.util.List;
|
||||
@@ -15,60 +16,60 @@ public interface PermissionsService extends IService<Permissions> {
|
||||
* @param permissionCode 权限编码
|
||||
* @return 权限信息
|
||||
*/
|
||||
Permissions getPermissionByCode(String permissionCode);
|
||||
Result<Permissions> getPermissionByCode(String permissionCode);
|
||||
|
||||
/**
|
||||
* 创建权限
|
||||
* @param permissions 权限信息
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean createPermission(Permissions permissions);
|
||||
Result<Boolean> createPermission(Permissions permissions);
|
||||
|
||||
/**
|
||||
* 更新权限信息
|
||||
* @param permissions 权限信息
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean updatePermission(Permissions permissions);
|
||||
Result<Boolean> updatePermission(Permissions permissions);
|
||||
|
||||
/**
|
||||
* 删除权限
|
||||
* @param id 权限ID
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean deletePermission(Long id);
|
||||
Result<Boolean> deletePermission(Long id);
|
||||
|
||||
/**
|
||||
* 查询所有权限
|
||||
* @return 权限列表
|
||||
*/
|
||||
List<Permissions> listAllPermissions();
|
||||
Result<List<Permissions>> listAllPermissions();
|
||||
|
||||
/**
|
||||
* 根据权限ID查询权限
|
||||
* @param id 权限ID
|
||||
* @return 权限信息
|
||||
*/
|
||||
Permissions getPermissionById(Long id);
|
||||
Result<Permissions> getPermissionById(Long id);
|
||||
|
||||
/**
|
||||
* 批量删除权限
|
||||
* @param ids 权限ID列表
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean batchDeletePermissions(List<Long> ids);
|
||||
Result<Boolean> batchDeletePermissions(List<Long> ids);
|
||||
|
||||
/**
|
||||
* 根据菜单ID查询权限
|
||||
* @param menuId 菜单ID
|
||||
* @return 权限列表
|
||||
*/
|
||||
List<Permissions> listPermissionsByMenuId(Long menuId);
|
||||
Result<List<Permissions>> listPermissionsByMenuId(Long menuId);
|
||||
|
||||
/**
|
||||
* 根据权限类型查询权限
|
||||
* @param permissionType 权限类型
|
||||
* @return 权限列表
|
||||
*/
|
||||
List<Permissions> listPermissionsByType(String permissionType);
|
||||
Result<List<Permissions>> listPermissionsByType(String permissionType);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.qf.backend.service;
|
||||
import java.util.List;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.qf.backend.common.Result;
|
||||
import com.qf.backend.entity.Roles;
|
||||
|
||||
/**
|
||||
@@ -15,53 +16,53 @@ public interface RolesService extends IService<Roles> {
|
||||
* @param roleName 角色名称
|
||||
* @return 角色信息
|
||||
*/
|
||||
Roles getRoleByName(String roleName);
|
||||
Result<Roles> getRoleByName(String roleName);
|
||||
|
||||
/**
|
||||
* 创建角色
|
||||
* @param roles 角色信息
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean createRole(Roles roles);
|
||||
Result<Boolean> createRole(Roles roles);
|
||||
|
||||
/**
|
||||
* 更新角色信息
|
||||
* @param roles 角色信息
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean updateRole(Roles roles);
|
||||
Result<Boolean> updateRole(Roles roles);
|
||||
|
||||
/**
|
||||
* 删除角色
|
||||
* @param id 角色ID
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean deleteRole(Long id);
|
||||
Result<Boolean> deleteRole(Long id);
|
||||
|
||||
/**
|
||||
* 查询所有角色
|
||||
* @return 角色列表
|
||||
*/
|
||||
List<Roles> listAllRoles();
|
||||
Result<List<Roles>> listAllRoles();
|
||||
|
||||
/**
|
||||
* 根据角色ID查询角色
|
||||
* @param id 角色ID
|
||||
* @return 角色信息
|
||||
*/
|
||||
Roles getRoleById(Long id);
|
||||
Result<Roles> getRoleById(Long id);
|
||||
|
||||
/**
|
||||
* 批量删除角色
|
||||
* @param ids 角色ID列表
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean batchDeleteRoles(List<Long> ids);
|
||||
Result<Boolean> batchDeleteRoles(List<Long> ids);
|
||||
|
||||
/**
|
||||
* 根据用户ID查询其拥有的角色列表
|
||||
* @param userId 用户ID
|
||||
* @return 角色列表
|
||||
*/
|
||||
List<Roles> listRolesByUserId(Long userId);
|
||||
Result<List<Roles>> listRolesByUserId(Long userId);
|
||||
}
|
||||
|
||||
@@ -44,12 +44,4 @@ public interface UserDetailsService extends IService<UserDetails> {
|
||||
*/
|
||||
Result<UserDetails> getUserDetailsById(Long id);
|
||||
|
||||
/**
|
||||
* 更新用户联系方式
|
||||
* @param userId 用户ID
|
||||
* @param phone 手机号
|
||||
* @param email 邮箱
|
||||
* @return 是否成功
|
||||
*/
|
||||
Result<Boolean> updateContactInfo(Long userId, String phone, String email);
|
||||
}
|
||||
|
||||
22
src/main/java/com/qf/backend/service/UserLoginService.java
Normal file
22
src/main/java/com/qf/backend/service/UserLoginService.java
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
|
||||
*/
|
||||
|
||||
package com.qf.backend.service;
|
||||
|
||||
import com.qf.backend.common.Result;
|
||||
import com.qf.backend.dto.LoginUser;
|
||||
|
||||
/**
|
||||
* 用户登录服务接口
|
||||
*/
|
||||
public interface UserLoginService {
|
||||
/**
|
||||
* 用户登录
|
||||
* @param username 用户名
|
||||
* @param password 密码
|
||||
* @return 登录结果
|
||||
*/
|
||||
Result<LoginUser> login(String username, String password);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.qf.backend.service.impl;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -10,10 +11,12 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.qf.backend.common.Result;
|
||||
import com.qf.backend.common.ResultUtils;
|
||||
import com.qf.backend.entity.OrderItems;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.OrderItemsMapper;
|
||||
import com.qf.backend.service.OrderItemsService;
|
||||
@@ -26,117 +29,398 @@ public class OrderItemsServiceImpl implements OrderItemsService {
|
||||
|
||||
@Override
|
||||
public Result<List<OrderItems>> getOrderItemsByOrderId(Long orderId) {
|
||||
// 参数校验
|
||||
if (orderId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单ID不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
List<OrderItems> orderItems = orderItemsMapper.selectByOrderId(orderId);
|
||||
if (orderItems == null || orderItems.isEmpty()) {
|
||||
return ResultUtils.fail(ErrorCode.ORDER_NOT_FOUND);
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "订单商品不存在");
|
||||
}
|
||||
return ResultUtils.success(orderItems);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.ORDER_NOT_FOUND);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询订单商品失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<OrderItems>> getOrderItemsByProductId(Long productId) {
|
||||
// 参数校验
|
||||
if (productId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品ID不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
List<OrderItems> orderItems = orderItemsMapper.selectByProductId(productId);
|
||||
if (orderItems == null || orderItems.isEmpty()) {
|
||||
return ResultUtils.fail(ErrorCode.ORDER_NOT_FOUND);
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "订单商品不存在");
|
||||
}
|
||||
return ResultUtils.success(orderItems);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.ORDER_NOT_FOUND);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询订单商品失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public BaseMapper<OrderItems> getBaseMapper() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'getBaseMapper'");
|
||||
return orderItemsMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<OrderItems> getEntityClass() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'getEntityClass'");
|
||||
return OrderItems.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getMap(Wrapper<OrderItems> queryWrapper) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'getMap'");
|
||||
try {
|
||||
return orderItemsMapper.selectMaps(queryWrapper).get(0);
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "获取Map数据失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> V getObj(Wrapper<OrderItems> queryWrapper, Function<? super Object, V> mapper) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'getObj'");
|
||||
try {
|
||||
OrderItems orderItem = orderItemsMapper.selectOne(queryWrapper);
|
||||
if (orderItem == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "订单商品不存在");
|
||||
}
|
||||
return mapper.apply(orderItem);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "获取对象数据失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public OrderItems getOne(Wrapper<OrderItems> queryWrapper, boolean throwEx) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'getOne'");
|
||||
try {
|
||||
OrderItems orderItem = orderItemsMapper.selectOne(queryWrapper);
|
||||
if (orderItem == null && throwEx) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "订单商品不存在");
|
||||
}
|
||||
return orderItem;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "获取单个对象失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<OrderItems> getOneOpt(Wrapper<OrderItems> queryWrapper, boolean throwEx) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'getOneOpt'");
|
||||
try {
|
||||
OrderItems orderItem = orderItemsMapper.selectOne(queryWrapper);
|
||||
if (orderItem == null && throwEx) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "订单商品不存在");
|
||||
}
|
||||
return Optional.ofNullable(orderItem);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "获取Optional对象失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean saveBatch(Collection<OrderItems> entityList, int batchSize) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'saveBatch'");
|
||||
try {
|
||||
if (entityList == null || entityList.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "实体列表不能为空");
|
||||
}
|
||||
if (batchSize <= 0) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "批处理大小必须大于0");
|
||||
}
|
||||
|
||||
// 简单的批量保存实现
|
||||
for (OrderItems entity : entityList) {
|
||||
orderItemsMapper.insert(entity);
|
||||
}
|
||||
return true;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量保存失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean saveOrUpdate(OrderItems entity) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'saveOrUpdate'");
|
||||
try {
|
||||
if (entity == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "实体不能为空");
|
||||
}
|
||||
|
||||
if (entity.getId() == null) {
|
||||
// 新增
|
||||
return orderItemsMapper.insert(entity) > 0;
|
||||
} else {
|
||||
// 更新
|
||||
return orderItemsMapper.updateById(entity) > 0;
|
||||
}
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "保存或更新失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean saveOrUpdateBatch(Collection<OrderItems> entityList, int batchSize) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'saveOrUpdateBatch'");
|
||||
try {
|
||||
if (entityList == null || entityList.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "实体列表不能为空");
|
||||
}
|
||||
if (batchSize <= 0) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "批处理大小必须大于0");
|
||||
}
|
||||
|
||||
for (OrderItems entity : entityList) {
|
||||
saveOrUpdate(entity);
|
||||
}
|
||||
return true;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量保存或更新失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateBatchById(Collection<OrderItems> entityList, int batchSize) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'updateBatchById'");
|
||||
try {
|
||||
if (entityList == null || entityList.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "实体列表不能为空");
|
||||
}
|
||||
if (batchSize <= 0) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "批处理大小必须大于0");
|
||||
}
|
||||
|
||||
for (OrderItems entity : entityList) {
|
||||
if (entity.getId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "实体ID不能为空");
|
||||
}
|
||||
orderItemsMapper.updateById(entity);
|
||||
}
|
||||
return true;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量更新失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public Result<Boolean> createOrderItem(OrderItems orderItems) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'createOrderItem'");
|
||||
// 参数校验
|
||||
if (orderItems == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单商品信息不能为空");
|
||||
}
|
||||
if (orderItems.getOrderId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单ID不能为空");
|
||||
}
|
||||
if (orderItems.getProductId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品ID不能为空");
|
||||
}
|
||||
if (orderItems.getQuantity() == null || orderItems.getQuantity() <= 0) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "商品数量必须大于0");
|
||||
}
|
||||
if (orderItems.getPrice() == null || orderItems.getPrice().compareTo(BigDecimal.ZERO) < 0) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "商品价格不能为负数");
|
||||
}
|
||||
|
||||
try {
|
||||
int result = orderItemsMapper.insert(orderItems);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "创建订单商品失败");
|
||||
}
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建订单商品失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> updateOrderItem(OrderItems orderItems) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'updateOrderItem'");
|
||||
// 参数校验
|
||||
if (orderItems == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单商品信息不能为空");
|
||||
}
|
||||
if (orderItems.getId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单商品ID不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
// 检查是否存在
|
||||
OrderItems existingItem = orderItemsMapper.selectById(orderItems.getId());
|
||||
if (existingItem == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "订单商品不存在");
|
||||
}
|
||||
|
||||
int result = orderItemsMapper.updateById(orderItems);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "更新订单商品失败");
|
||||
}
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新订单商品失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> deleteOrderItem(Long id) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'deleteOrderItem'");
|
||||
// 参数校验
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单商品ID不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
// 检查是否存在
|
||||
OrderItems orderItem = orderItemsMapper.selectById(id);
|
||||
if (orderItem == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "订单商品不存在");
|
||||
}
|
||||
|
||||
int result = orderItemsMapper.deleteById(id);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "删除订单商品失败");
|
||||
}
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除订单商品失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<OrderItems> getOrderItemById(Long id) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'getOrderItemById'");
|
||||
// 参数校验
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单商品ID不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
OrderItems orderItem = orderItemsMapper.selectById(id);
|
||||
if (orderItem == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "订单商品不存在");
|
||||
}
|
||||
return ResultUtils.success(orderItem);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询订单商品失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> batchCreateOrderItems(List<OrderItems> orderItemsList) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'batchCreateOrderItems'");
|
||||
// 参数校验
|
||||
if (orderItemsList == null || orderItemsList.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单商品列表不能为空");
|
||||
}
|
||||
if (orderItemsList.size() > 100) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "批量创建数量不能超过100个");
|
||||
}
|
||||
|
||||
try {
|
||||
// 逐个创建订单商品
|
||||
for (OrderItems orderItem : orderItemsList) {
|
||||
// 验证每个订单商品
|
||||
if (orderItem.getOrderId() == null || orderItem.getProductId() == null ||
|
||||
orderItem.getQuantity() == null || orderItem.getQuantity() <= 0 ||
|
||||
orderItem.getPrice() == null || orderItem.getPrice().compareTo(BigDecimal.ZERO) < 0) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "订单商品信息不完整或无效");
|
||||
}
|
||||
|
||||
int result = orderItemsMapper.insert(orderItem);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "批量创建订单商品失败");
|
||||
}
|
||||
}
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量创建订单商品失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> deleteOrderItemsByOrderId(Long orderId) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'deleteOrderItemsByOrderId'");
|
||||
// 参数校验
|
||||
if (orderId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单ID不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
int result = orderItemsMapper.delete(
|
||||
new QueryWrapper<OrderItems>().eq("order_id", orderId)
|
||||
);
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除订单下所有商品失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Double> calculateOrderTotal(Long orderId) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'calculateOrderTotal'");
|
||||
// 参数校验
|
||||
if (orderId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单ID不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
List<OrderItems> orderItems = orderItemsMapper.selectByOrderId(orderId);
|
||||
if (orderItems == null || orderItems.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "订单商品不存在");
|
||||
}
|
||||
|
||||
double totalAmount = 0.0;
|
||||
for (OrderItems item : orderItems) {
|
||||
totalAmount += item.getPrice().multiply(new BigDecimal(item.getQuantity())).doubleValue();
|
||||
}
|
||||
return ResultUtils.success(totalAmount);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "计算订单总金额失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<OrderItems>> getOrderItemsBySkuId(Long skuId) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'getOrderItemsBySkuId'");
|
||||
// 参数校验
|
||||
if (skuId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "SKU ID不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
List<OrderItems> orderItems = orderItemsMapper.selectList(
|
||||
new QueryWrapper<OrderItems>().eq("sku_id", skuId)
|
||||
);
|
||||
if (orderItems == null || orderItems.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "该SKU的订单商品不存在");
|
||||
}
|
||||
return ResultUtils.success(orderItems);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询SKU订单商品失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
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;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.qf.backend.common.Result;
|
||||
import com.qf.backend.entity.OrderStatusHistory;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.OrderStatusHistoryMapper;
|
||||
import com.qf.backend.service.OrderStatusHistoryService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
|
||||
|
||||
@Service
|
||||
public class OrderStatusHistoryServiceImpl extends ServiceImpl<OrderStatusHistoryMapper, OrderStatusHistory> implements OrderStatusHistoryService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(OrderStatusHistoryServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private OrderStatusHistoryMapper orderStatusHistoryMapper;
|
||||
|
||||
@Override
|
||||
public Result<List<OrderStatusHistory>> getHistoryByOrderId(Long orderId) {
|
||||
logger.info("根据订单ID查询订单状态历史记录,订单ID:{}", orderId);
|
||||
if (ValidateUtil.isEmpty(orderId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单ID不能为空");
|
||||
}
|
||||
try {
|
||||
List<OrderStatusHistory> historyList = orderStatusHistoryMapper.selectList( new QueryWrapper<OrderStatusHistory>().eq("order_id", orderId));
|
||||
return Result.success(historyList);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询订单状态历史记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> createStatusHistory(OrderStatusHistory orderStatusHistory) {
|
||||
logger.info("创建订单状态历史记录,订单ID:{}", orderStatusHistory.getOrderId());
|
||||
if (ValidateUtil.isEmpty(orderStatusHistory.getOrderId())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单ID不能为空");
|
||||
}
|
||||
try {
|
||||
int result = orderStatusHistoryMapper.insert(orderStatusHistory);
|
||||
if (result > 0) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建订单状态历史记录失败");
|
||||
}
|
||||
return Result.success(true);
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建订单状态历史记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> updateStatusHistory(OrderStatusHistory orderStatusHistory) {
|
||||
logger.info("更新订单状态历史记录,订单ID:{}", orderStatusHistory.getOrderId());
|
||||
if (ValidateUtil.isEmpty(orderStatusHistory.getOrderId())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单ID不能为空");
|
||||
}
|
||||
try {
|
||||
boolean result = orderStatusHistoryMapper.updateById(orderStatusHistory) > 0;
|
||||
return Result.success(result);
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新订单状态历史记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> deleteStatusHistory(Long id) {
|
||||
logger.info("删除订单状态历史记录,ID:{}", id);
|
||||
if (ValidateUtil.isEmpty(id)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "ID不能为空");
|
||||
}
|
||||
try {
|
||||
boolean result = orderStatusHistoryMapper.deleteById(id) > 0;
|
||||
return Result.success(result);
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除订单状态历史记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<OrderStatusHistory> getStatusHistoryById(Long id) {
|
||||
logger.info("根据ID查询订单状态历史记录,ID:{}", id);
|
||||
if (ValidateUtil.isEmpty(id)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "ID不能为空");
|
||||
}
|
||||
try {
|
||||
OrderStatusHistory history = orderStatusHistoryMapper.selectById(id);
|
||||
return Result.success(history);
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询订单状态历史记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> batchCreateStatusHistory(List<OrderStatusHistory> historyList) {
|
||||
logger.info("批量创建订单状态历史记录,数量:{}", historyList.size());
|
||||
if (ValidateUtil.isEmpty(historyList)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单状态历史记录列表不能为空");
|
||||
}
|
||||
try {
|
||||
for (OrderStatusHistory history : historyList) {
|
||||
int result = orderStatusHistoryMapper.insert(history);
|
||||
if (result > 0) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量创建订单状态历史记录失败");
|
||||
}
|
||||
}
|
||||
return Result.success(true);
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量创建订单状态历史记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<OrderStatusHistory>> getHistoryByOrderIdAndStatus(Long orderId, Integer status) {
|
||||
logger.info("根据订单ID和状态查询订单状态历史记录,订单ID:{},状态:{}", orderId, status);
|
||||
if (ValidateUtil.isEmpty(orderId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单ID不能为空");
|
||||
}
|
||||
try {
|
||||
List<OrderStatusHistory> historyList = orderStatusHistoryMapper.selectList( new QueryWrapper<OrderStatusHistory>().eq("order_id", orderId).eq("status", status));
|
||||
return Result.success(historyList);
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询订单状态历史记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<OrderStatusHistory> getLatestStatusHistory(Long orderId) {
|
||||
logger.info("根据订单ID查询最新订单状态历史记录,订单ID:{}", orderId);
|
||||
if (ValidateUtil.isEmpty(orderId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单ID不能为空");
|
||||
}
|
||||
try {
|
||||
OrderStatusHistory history = orderStatusHistoryMapper.selectOne( new QueryWrapper<OrderStatusHistory>().eq("order_id", orderId).orderByDesc("id").last("limit 1"));
|
||||
return Result.success(history);
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询最新订单状态历史记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> deleteHistoryByOrderId(Long orderId) {
|
||||
logger.info("根据订单ID删除订单状态历史记录,订单ID:{}", orderId);
|
||||
if (ValidateUtil.isEmpty(orderId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单ID不能为空");
|
||||
}
|
||||
try {
|
||||
boolean result = orderStatusHistoryMapper.delete( new QueryWrapper<OrderStatusHistory>().eq("order_id", orderId)) > 0;
|
||||
return Result.success(result);
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除订单状态历史记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.qf.backend.service.impl;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
@@ -7,86 +9,293 @@ import org.slf4j.LoggerFactory;
|
||||
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.common.Result;
|
||||
import com.qf.backend.common.ResultUtils;
|
||||
import com.qf.backend.entity.Orders;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.OrdersMapper;
|
||||
import com.qf.backend.service.OrdersService;
|
||||
|
||||
@Service
|
||||
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
|
||||
private final Logger logger = LoggerFactory.getLogger(OrdersServiceImpl.class);
|
||||
@Autowired
|
||||
private OrdersMapper ordersMapper;
|
||||
private Logger logger = LoggerFactory.getLogger(OrdersServiceImpl.class);
|
||||
|
||||
@Override
|
||||
public Result<Orders> getOrderByNumber(String orderNumber) {
|
||||
logger.info("根据订单号查询订单: {}", orderNumber);
|
||||
// 参数校验
|
||||
if (orderNumber == null || orderNumber.trim().isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单编号不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
Orders orders = ordersMapper.selectByOrderNumber(orderNumber);
|
||||
if (orders == null) {
|
||||
logger.warn("订单号 {} 不存在", orderNumber);
|
||||
return ResultUtils.fail(ErrorCode.ORDER_NOT_FOUND);
|
||||
throw new BusinessException(ErrorCode.ORDER_NOT_FOUND, "订单不存在: " + orderNumber);
|
||||
}
|
||||
return ResultUtils.success(orders);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.ORDER_NOT_FOUND);
|
||||
logger.error("查询订单失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询订单失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<Orders>> getOrdersByUserId(Long userId) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'getOrdersByUserId'");
|
||||
// 参数校验
|
||||
if (userId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
List<Orders> ordersList = ordersMapper.selectList(
|
||||
new QueryWrapper<Orders>().eq("user_id", userId).orderByDesc("create_time")
|
||||
);
|
||||
return ResultUtils.success(ordersList);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询用户订单失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询用户订单失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> createOrder(Orders orders) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'createOrder'");
|
||||
// 参数校验
|
||||
if (orders == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单信息不能为空");
|
||||
}
|
||||
if (orders.getUserId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID不能为空");
|
||||
}
|
||||
if (orders.getOrderNo() == null || orders.getOrderNo().trim().isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单编号不能为空");
|
||||
}
|
||||
if (orders.getTotalAmount() == null || orders.getTotalAmount().compareTo(BigDecimal.ZERO) < 0) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "订单金额不能为负数");
|
||||
}
|
||||
|
||||
try {
|
||||
// 检查订单编号是否已存在
|
||||
Orders existingOrder = ordersMapper.selectByOrderNumber(orders.getOrderNo());
|
||||
if (existingOrder != null) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "订单编号已存在: " + orders.getOrderNo());
|
||||
}
|
||||
|
||||
// 设置默认值
|
||||
if (orders.getOrderStatus() == null) {
|
||||
orders.setOrderStatus(0); // 默认为待支付状态
|
||||
}
|
||||
if (orders.getCreatedAt() == null) {
|
||||
orders.setCreatedAt(new Date());
|
||||
}
|
||||
|
||||
int result = ordersMapper.insert(orders);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "创建订单失败");
|
||||
}
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("创建订单失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建订单失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> updateOrder(Orders orders) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'updateOrder'");
|
||||
// 参数校验
|
||||
if (orders == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单信息不能为空");
|
||||
}
|
||||
if (orders.getId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单ID不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
// 检查订单是否存在
|
||||
Orders existingOrder = ordersMapper.selectById(orders.getId());
|
||||
if (existingOrder == null) {
|
||||
throw new BusinessException(ErrorCode.ORDER_NOT_FOUND, "订单不存在");
|
||||
}
|
||||
|
||||
// 更新时间戳
|
||||
orders.setUpdatedAt(new Date());
|
||||
|
||||
int result = ordersMapper.updateById(orders);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "更新订单失败");
|
||||
}
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("更新订单失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新订单失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> deleteOrder(Long id) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'deleteOrder'");
|
||||
// 参数校验
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单ID不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
// 检查订单是否存在
|
||||
Orders orders = ordersMapper.selectById(id);
|
||||
if (orders == null) {
|
||||
throw new BusinessException(ErrorCode.ORDER_NOT_FOUND, "订单不存在");
|
||||
}
|
||||
|
||||
int result = ordersMapper.deleteById(id);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "删除订单失败");
|
||||
}
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("删除订单失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除订单失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Orders> getOrderById(Long id) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'getOrderById'");
|
||||
// 参数校验
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单ID不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
Orders orders = ordersMapper.selectById(id);
|
||||
if (orders == null) {
|
||||
throw new BusinessException(ErrorCode.ORDER_NOT_FOUND, "订单不存在");
|
||||
}
|
||||
return ResultUtils.success(orders);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询订单失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询订单失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<Orders>> listOrdersByPage(int page, int size) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'listOrdersByPage'");
|
||||
// 参数校验
|
||||
if (page < 1) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "页码不能小于1");
|
||||
}
|
||||
if (size < 1 || size > 100) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "每页大小必须在1-100之间");
|
||||
}
|
||||
|
||||
try {
|
||||
// 简单分页实现
|
||||
int offset = (page - 1) * size;
|
||||
List<Orders> ordersList = ordersMapper.selectList(
|
||||
new QueryWrapper<Orders>().last("LIMIT " + offset + ", " + size).orderByDesc("create_time")
|
||||
);
|
||||
return ResultUtils.success(ordersList);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("分页查询订单失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "分页查询订单失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<Orders>> getOrdersByShopId(Long shopId) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'getOrdersByShopId'");
|
||||
// 参数校验
|
||||
if (shopId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "店铺ID不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
List<Orders> ordersList = ordersMapper.selectList(
|
||||
new QueryWrapper<Orders>().eq("shop_id", shopId).orderByDesc("create_time")
|
||||
);
|
||||
return ResultUtils.success(ordersList);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询店铺订单失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询店铺订单失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> updateOrderStatus(Long orderId, Integer status) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'updateOrderStatus'");
|
||||
// 参数校验
|
||||
if (orderId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单ID不能为空");
|
||||
}
|
||||
if (status == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单状态不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
// 检查订单是否存在
|
||||
Orders orders = ordersMapper.selectById(orderId);
|
||||
if (orders == null) {
|
||||
throw new BusinessException(ErrorCode.ORDER_NOT_FOUND, "订单不存在");
|
||||
}
|
||||
|
||||
// 检查状态是否合法(假设状态值为0-5)
|
||||
if (status < 0 || status > 5) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "无效的订单状态值");
|
||||
}
|
||||
|
||||
// 更新状态
|
||||
Orders updateOrder = new Orders();
|
||||
updateOrder.setId(orderId);
|
||||
updateOrder.setOrderStatus(status);
|
||||
updateOrder.setUpdatedAt(new Date());
|
||||
|
||||
int result = ordersMapper.updateById(updateOrder);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "更新订单状态失败");
|
||||
}
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("更新订单状态失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新订单状态失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<Orders>> getOrdersByStatus(Integer status) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'getOrdersByStatus'");
|
||||
// 参数校验
|
||||
if (status == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单状态不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
List<Orders> ordersList = ordersMapper.selectList(
|
||||
new QueryWrapper<Orders>().eq("status", status).orderByDesc("create_time")
|
||||
);
|
||||
return ResultUtils.success(ordersList);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询订单失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询订单失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,215 @@
|
||||
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.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.qf.backend.entity.Payments;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.PaymentsMapper;
|
||||
import com.qf.backend.service.PaymentsService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
|
||||
/**
|
||||
* 支付服务实现类
|
||||
*/
|
||||
@Service
|
||||
public class PaymentsServiceImpl extends ServiceImpl<PaymentsMapper, Payments> implements PaymentsService {
|
||||
|
||||
@Autowired
|
||||
private PaymentsMapper paymentsMapper;
|
||||
|
||||
@Override
|
||||
public Payments getPaymentByOrderId(Long orderId) {
|
||||
if (ValidateUtil.isEmpty(orderId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单ID不能为空");
|
||||
}
|
||||
try {
|
||||
return paymentsMapper.selectOne(
|
||||
new QueryWrapper<Payments>().eq("order_id", orderId));
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询支付记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Payments getPaymentByTransactionId(String transactionId) {
|
||||
if (ValidateUtil.isEmpty(transactionId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "支付流水号不能为空");
|
||||
}
|
||||
try {
|
||||
return paymentsMapper.selectOne(
|
||||
new QueryWrapper<Payments>().eq("transaction_id", transactionId));
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询支付记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean createPayment(Payments payments) {
|
||||
if (ValidateUtil.isEmpty(payments)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "支付信息不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(payments.getOrderId())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单ID不能为空");
|
||||
}
|
||||
if (payments.getAmount() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "支付金额不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(payments.getPaymentMethod())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "支付方式不能为空");
|
||||
}
|
||||
try {
|
||||
int result = paymentsMapper.insert(payments);
|
||||
return result > 0;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建支付记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updatePayment(Payments payments) {
|
||||
if (ValidateUtil.isEmpty(payments)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "支付信息不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(payments.getId())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "支付ID不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查支付记录是否存在
|
||||
Payments existing = paymentsMapper.selectById(payments.getId());
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "支付记录不存在");
|
||||
}
|
||||
int result = paymentsMapper.updateById(payments);
|
||||
return result > 0;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新支付记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deletePayment(Long id) {
|
||||
if (ValidateUtil.isEmpty(id)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "支付ID不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查支付记录是否存在
|
||||
Payments existing = paymentsMapper.selectById(id);
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "支付记录不存在");
|
||||
}
|
||||
int result = paymentsMapper.deleteById(id);
|
||||
return result > 0;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除支付记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Payments getPaymentById(Long id) {
|
||||
if (ValidateUtil.isEmpty(id)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "支付ID不能为空");
|
||||
}
|
||||
try {
|
||||
Payments payment = paymentsMapper.selectById(id);
|
||||
if (payment == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "支付记录不存在");
|
||||
}
|
||||
return payment;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询支付记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Payments> getPaymentsByUserId(Long userId) {
|
||||
if (ValidateUtil.isEmpty(userId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID不能为空");
|
||||
}
|
||||
try {
|
||||
return paymentsMapper.selectList(
|
||||
new QueryWrapper<Payments>().eq("user_id", userId));
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询支付记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Payments> getPaymentsByStatus(Integer status) {
|
||||
if (ValidateUtil.isEmpty(status)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "支付状态不能为空");
|
||||
}
|
||||
try {
|
||||
return paymentsMapper.selectList(
|
||||
new QueryWrapper<Payments>().eq("status", status));
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询支付记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updatePaymentStatus(Long paymentId, Integer status) {
|
||||
if (ValidateUtil.isEmpty(paymentId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "支付ID不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(status)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "支付状态不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查支付记录是否存在
|
||||
Payments existing = paymentsMapper.selectById(paymentId);
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "支付记录不存在");
|
||||
}
|
||||
// 更新状态
|
||||
existing.setPaymentStatus(status);
|
||||
int result = paymentsMapper.updateById(existing);
|
||||
return result > 0;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新支付状态失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Payments> listPaymentsByPage(int page, int size) {
|
||||
if (page < 1) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "页码不能小于1");
|
||||
}
|
||||
if (size < 1 || size > 100) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "每页数量必须在1-100之间");
|
||||
}
|
||||
try {
|
||||
Page<Payments> paymentPage = new Page<>(page, size);
|
||||
Page<Payments> resultPage = paymentsMapper.selectPage(paymentPage, null);
|
||||
return resultPage.getRecords();
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "分页查询支付记录失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
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;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
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.Permissions;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.PermissionsMapper;
|
||||
import com.qf.backend.service.PermissionsService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
|
||||
@Service
|
||||
public class PermissionsServiceImpl extends ServiceImpl<PermissionsMapper, Permissions> implements PermissionsService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PermissionsServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private PermissionsMapper permissionsMapper;
|
||||
|
||||
// 根据权限编码查询权限
|
||||
@Override
|
||||
public Result<Permissions> getPermissionByCode(String permissionCode) {
|
||||
logger.info("根据权限编码查询权限: {}", permissionCode);
|
||||
|
||||
try {
|
||||
if (ValidateUtil.isEmpty(permissionCode)) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "权限编码不能为空");
|
||||
}
|
||||
|
||||
QueryWrapper<Permissions> queryWrapper = new QueryWrapper<Permissions>().eq("permission_code", permissionCode);
|
||||
Permissions permission = permissionsMapper.selectOne(queryWrapper);
|
||||
if (permission == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "权限不存在: " + permissionCode);
|
||||
}
|
||||
return ResultUtils.success(permission);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询权限失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询权限失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建权限
|
||||
@Override
|
||||
public Result<Boolean> createPermission(Permissions permissions) {
|
||||
logger.info("创建权限: 权限对象");
|
||||
|
||||
try {
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(permissions, "permissionCode", "permissionName", "permissionType");
|
||||
|
||||
int result = permissionsMapper.insert(permissions);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "创建权限失败");
|
||||
}
|
||||
|
||||
logger.info("权限创建成功");
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证权限信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证权限信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("创建权限失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建权限失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新权限信息
|
||||
@Override
|
||||
public Result<Boolean> updatePermission(Permissions permissions) {
|
||||
logger.info("更新权限信息: 权限ID = {}", permissions.getId());
|
||||
|
||||
try {
|
||||
if (permissions == null || permissions.getId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "权限信息或权限ID不能为空");
|
||||
}
|
||||
|
||||
// 检查权限是否存在
|
||||
Permissions existingPermission = getPermissionByIdAndCheckExist(permissions.getId());
|
||||
if (existingPermission == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "权限不存在");
|
||||
}
|
||||
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(permissions, "permissionCode", "permissionName", "permissionType");
|
||||
|
||||
// 更新权限信息
|
||||
int result = permissionsMapper.updateById(permissions);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "更新权限信息失败");
|
||||
}
|
||||
|
||||
logger.info("权限信息更新成功: 权限ID = {}", permissions.getId());
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证权限信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证权限信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("更新权限信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新权限信息失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除权限
|
||||
@Override
|
||||
public Result<Boolean> deletePermission(Long id) {
|
||||
logger.info("删除权限: 权限ID = {}", id);
|
||||
|
||||
try {
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "权限ID不能为空");
|
||||
}
|
||||
// 检查权限是否存在
|
||||
Permissions permission = getPermissionByIdAndCheckExist(id);
|
||||
if (permission == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "权限不存在");
|
||||
}
|
||||
|
||||
int result = permissionsMapper.deleteById(id);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "删除权限失败");
|
||||
}
|
||||
|
||||
logger.info("权限删除成功: 权限ID = {}", id);
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("删除权限失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除权限失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 查询所有权限
|
||||
@Override
|
||||
public Result<List<Permissions>> listAllPermissions() {
|
||||
logger.info("查询所有权限列表");
|
||||
|
||||
try {
|
||||
List<Permissions> permissionsList = permissionsMapper.selectList(null);
|
||||
logger.info("查询到 {} 个权限", permissionsList.size());
|
||||
return ResultUtils.success(permissionsList);
|
||||
} catch (Exception e) {
|
||||
logger.error("查询权限列表失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询权限列表失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据权限ID查询权限
|
||||
@Override
|
||||
public Result<Permissions> getPermissionById(Long id) {
|
||||
logger.info("根据ID查询权限: 权限ID = {}", id);
|
||||
|
||||
try {
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "权限ID不能为空");
|
||||
}
|
||||
|
||||
Permissions permission = getPermissionByIdAndCheckExist(id);
|
||||
if (permission == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "权限不存在");
|
||||
}
|
||||
|
||||
return ResultUtils.success(permission);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询权限失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询权限失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 批量删除权限
|
||||
@Override
|
||||
public Result<Boolean> batchDeletePermissions(List<Long> ids) {
|
||||
logger.info("批量删除权限: 权限ID列表 = {}", ids);
|
||||
|
||||
try {
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "权限ID列表不能为空");
|
||||
}
|
||||
|
||||
int result = permissionsMapper.deleteBatchIds(ids);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "批量删除权限失败");
|
||||
}
|
||||
|
||||
logger.info("批量删除权限成功: 删除了 {} 个权限", result);
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("批量删除权限失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量删除权限失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据菜单ID查询权限
|
||||
@Override
|
||||
public Result<List<Permissions>> listPermissionsByMenuId(Long menuId) {
|
||||
logger.info("根据菜单ID查询权限: 菜单ID = {}", menuId);
|
||||
|
||||
try {
|
||||
if (menuId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "菜单ID不能为空");
|
||||
}
|
||||
|
||||
QueryWrapper<Permissions> queryWrapper = new QueryWrapper<Permissions>().eq("menu_id", menuId);
|
||||
List<Permissions> permissionsList = permissionsMapper.selectList(queryWrapper);
|
||||
logger.info("查询到 {} 个权限", permissionsList.size());
|
||||
return ResultUtils.success(permissionsList);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("根据菜单ID查询权限失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "根据菜单ID查询权限失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据权限类型查询权限
|
||||
@Override
|
||||
public Result<List<Permissions>> listPermissionsByType(String permissionType) {
|
||||
logger.info("根据权限类型查询权限: 权限类型 = {}", permissionType);
|
||||
|
||||
try {
|
||||
if (ValidateUtil.isEmpty(permissionType)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "权限类型不能为空");
|
||||
}
|
||||
|
||||
QueryWrapper<Permissions> queryWrapper = new QueryWrapper<Permissions>().eq("permission_type", permissionType);
|
||||
List<Permissions> permissionsList = permissionsMapper.selectList(queryWrapper);
|
||||
logger.info("查询到 {} 个权限", permissionsList.size());
|
||||
return ResultUtils.success(permissionsList);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("根据权限类型查询权限失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "根据权限类型查询权限失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据权限ID查询权限并检查是否存在
|
||||
* @param id 权限ID
|
||||
* @return 权限对象,如果权限不存在则返回null
|
||||
*/
|
||||
private Permissions getPermissionByIdAndCheckExist(Long id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
QueryWrapper<Permissions> queryWrapper = new QueryWrapper<Permissions>().eq("id", id);
|
||||
return permissionsMapper.selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
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.common.Result;
|
||||
import com.qf.backend.common.ResultUtils;
|
||||
import com.qf.backend.entity.ProductAttributeValues;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.ProductAttributeValuesMapper;
|
||||
import com.qf.backend.service.ProductAttributeValuesService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
|
||||
/**
|
||||
* 商品属性值服务实现类
|
||||
*/
|
||||
@Service
|
||||
public class ProductAttributeValuesServiceImpl extends ServiceImpl<ProductAttributeValuesMapper, ProductAttributeValues> implements ProductAttributeValuesService {
|
||||
|
||||
@Autowired
|
||||
private ProductAttributeValuesMapper productAttributeValuesMapper;
|
||||
|
||||
@Override
|
||||
public Result<List<ProductAttributeValues>> getAttributeValuesByProductId(Long productId) {
|
||||
if (ValidateUtil.isEmpty(productId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品ID不能为空");
|
||||
}
|
||||
try {
|
||||
List<ProductAttributeValues> attributeValues = productAttributeValuesMapper.selectList(
|
||||
new QueryWrapper<ProductAttributeValues>().eq("product_id", productId));
|
||||
return ResultUtils.success(attributeValues);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询商品属性值失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<ProductAttributeValues>> getAttributeValuesByAttributeId(Long attributeId) {
|
||||
if (ValidateUtil.isEmpty(attributeId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性ID不能为空");
|
||||
}
|
||||
try {
|
||||
List<ProductAttributeValues> attributeValues = productAttributeValuesMapper.selectList(
|
||||
new QueryWrapper<ProductAttributeValues>().eq("attribute_id", attributeId));
|
||||
return ResultUtils.success(attributeValues);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询属性值失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> createAttributeValue(ProductAttributeValues productAttributeValues) {
|
||||
if (ValidateUtil.isEmpty(productAttributeValues)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性值信息不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(productAttributeValues.getProductId())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品ID不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(productAttributeValues.getAttributeId())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性ID不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(productAttributeValues.getAttributeValue())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性值不能为空");
|
||||
}
|
||||
try {
|
||||
int result = productAttributeValuesMapper.insert(productAttributeValues);
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建属性值失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> updateAttributeValue(ProductAttributeValues productAttributeValues) {
|
||||
if (ValidateUtil.isEmpty(productAttributeValues)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性值信息不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(productAttributeValues.getId())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性值ID不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(productAttributeValues.getAttributeValue())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性值不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查属性值是否存在
|
||||
ProductAttributeValues existing = productAttributeValuesMapper.selectById(productAttributeValues.getId());
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "属性值不存在");
|
||||
}
|
||||
int result = productAttributeValuesMapper.updateById(productAttributeValues);
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新属性值失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> deleteAttributeValue(Long id) {
|
||||
if (ValidateUtil.isEmpty(id)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性值ID不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查属性值是否存在
|
||||
ProductAttributeValues existing = productAttributeValuesMapper.selectById(id);
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "属性值不存在");
|
||||
}
|
||||
int result = productAttributeValuesMapper.deleteById(id);
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除属性值失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<ProductAttributeValues> getAttributeValueById(Long id) {
|
||||
if (ValidateUtil.isEmpty(id)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性值ID不能为空");
|
||||
}
|
||||
try {
|
||||
ProductAttributeValues attributeValue = productAttributeValuesMapper.selectById(id);
|
||||
if (attributeValue == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "属性值不存在");
|
||||
}
|
||||
return ResultUtils.success(attributeValue);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询属性值失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> batchCreateAttributeValues(List<ProductAttributeValues> attributeValues) {
|
||||
if (ValidateUtil.isEmpty(attributeValues)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性值列表不能为空");
|
||||
}
|
||||
if (attributeValues.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性值列表不能为空");
|
||||
}
|
||||
try {
|
||||
for (ProductAttributeValues attributeValue : attributeValues) {
|
||||
if (ValidateUtil.isEmpty(attributeValue.getProductId())) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "属性值列表中存在商品ID为空的记录");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(attributeValue.getAttributeId())) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "属性值列表中存在属性ID为空的记录");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(attributeValue.getAttributeValue())) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "属性值列表中存在属性值为空的记录");
|
||||
}
|
||||
productAttributeValuesMapper.insert(attributeValue);
|
||||
}
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量创建属性值失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<ProductAttributeValues> getAttributeValueByProductAndAttribute(Long productId, Long attributeId) {
|
||||
if (ValidateUtil.isEmpty(productId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品ID不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(attributeId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性ID不能为空");
|
||||
}
|
||||
try {
|
||||
ProductAttributeValues attributeValue = productAttributeValuesMapper.selectOne(
|
||||
new QueryWrapper<ProductAttributeValues>()
|
||||
.eq("product_id", productId)
|
||||
.eq("attribute_id", attributeId));
|
||||
return ResultUtils.success(attributeValue);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询商品属性值失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> deleteAttributeValuesByProductId(Long productId) {
|
||||
if (ValidateUtil.isEmpty(productId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品ID不能为空");
|
||||
}
|
||||
try {
|
||||
int result = productAttributeValuesMapper.delete(
|
||||
new QueryWrapper<ProductAttributeValues>().eq("product_id", productId));
|
||||
return ResultUtils.success(result >= 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除商品属性值失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
package com.qf.backend.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
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.ProductAttributes;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.ProductAttributesMapper;
|
||||
import com.qf.backend.service.ProductAttributesService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 商品属性服务实现类
|
||||
*/
|
||||
@Service
|
||||
public class ProductAttributesServiceImpl extends ServiceImpl<ProductAttributesMapper, ProductAttributes> implements ProductAttributesService {
|
||||
|
||||
@Autowired
|
||||
private ProductAttributesMapper productAttributesMapper;
|
||||
|
||||
@Override
|
||||
public Result<List<ProductAttributes>> getAttributesByCategoryId(Long categoryId) {
|
||||
if (ValidateUtil.isEmpty(categoryId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类ID不能为空");
|
||||
}
|
||||
try {
|
||||
List<ProductAttributes> attributes = productAttributesMapper.selectList(
|
||||
new QueryWrapper<ProductAttributes>().eq("category_id", categoryId));
|
||||
return ResultUtils.success(attributes);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询商品属性失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<ProductAttributes>> getAttributesByName(String attributeName) {
|
||||
if (ValidateUtil.isEmpty(attributeName)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性名称不能为空");
|
||||
}
|
||||
try {
|
||||
List<ProductAttributes> attributes = productAttributesMapper.selectList(
|
||||
new QueryWrapper<ProductAttributes>().like("attribute_name", attributeName));
|
||||
return ResultUtils.success(attributes);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询商品属性失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> createAttribute(ProductAttributes productAttributes) {
|
||||
if (ValidateUtil.isEmpty(productAttributes)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性信息不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(productAttributes.getAttributeName())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性名称不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(productAttributes.getCategoryId())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类ID不能为空");
|
||||
}
|
||||
try {
|
||||
int result = productAttributesMapper.insert(productAttributes);
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建商品属性失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> updateAttribute(ProductAttributes productAttributes) {
|
||||
if (ValidateUtil.isEmpty(productAttributes)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性信息不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(productAttributes.getId())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性ID不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(productAttributes.getAttributeName())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性名称不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查属性是否存在
|
||||
ProductAttributes existing = productAttributesMapper.selectById(productAttributes.getId());
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "属性不存在");
|
||||
}
|
||||
int result = productAttributesMapper.updateById(productAttributes);
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新商品属性失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> deleteAttribute(Long id) {
|
||||
if (ValidateUtil.isEmpty(id)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性ID不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查属性是否存在
|
||||
ProductAttributes existing = productAttributesMapper.selectById(id);
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "属性不存在");
|
||||
}
|
||||
int result = productAttributesMapper.deleteById(id);
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除商品属性失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<ProductAttributes> getAttributeById(Long id) {
|
||||
if (ValidateUtil.isEmpty(id)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性ID不能为空");
|
||||
}
|
||||
try {
|
||||
ProductAttributes attribute = productAttributesMapper.selectById(id);
|
||||
if (attribute == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "属性不存在");
|
||||
}
|
||||
return ResultUtils.success(attribute);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询商品属性失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> batchDeleteAttributes(List<Long> ids) {
|
||||
if (ValidateUtil.isEmpty(ids)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性ID列表不能为空");
|
||||
}
|
||||
if (ids.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性ID列表不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查所有属性是否存在
|
||||
for (Long id : ids) {
|
||||
if (ValidateUtil.isEmpty(id)) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "属性ID列表中存在空值");
|
||||
}
|
||||
ProductAttributes existing = productAttributesMapper.selectById(id);
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "属性ID为" + id + "的属性不存在");
|
||||
}
|
||||
}
|
||||
int result = productAttributesMapper.deleteBatchIds(ids);
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量删除商品属性失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<ProductAttributes>> getAttributesByType(String attributeType) {
|
||||
if (ValidateUtil.isEmpty(attributeType)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "属性类型不能为空");
|
||||
}
|
||||
try {
|
||||
List<ProductAttributes> attributes = productAttributesMapper.selectList(
|
||||
new QueryWrapper<ProductAttributes>().eq("attribute_type", attributeType));
|
||||
return ResultUtils.success(attributes);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询商品属性失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<ProductAttributes>> getAttributesBySearchable(Boolean searchable) {
|
||||
try {
|
||||
List<ProductAttributes> attributes = productAttributesMapper.selectList(
|
||||
new QueryWrapper<ProductAttributes>().eq("searchable", searchable));
|
||||
return ResultUtils.success(attributes);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询商品属性失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
package com.qf.backend.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
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.ProductCategories;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.ProductCategoriesMapper;
|
||||
import com.qf.backend.service.ProductCategoriesService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 商品分类服务实现类
|
||||
*/
|
||||
@Service
|
||||
public class ProductCategoriesServiceImpl extends ServiceImpl<ProductCategoriesMapper, ProductCategories> implements ProductCategoriesService {
|
||||
|
||||
@Autowired
|
||||
private ProductCategoriesMapper productCategoriesMapper;
|
||||
|
||||
@Override
|
||||
public Result<ProductCategories> getCategoryByName(String categoryName) {
|
||||
if (ValidateUtil.isEmpty(categoryName)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类名称不能为空");
|
||||
}
|
||||
try {
|
||||
ProductCategories category = productCategoriesMapper.selectOne(
|
||||
new QueryWrapper<ProductCategories>().eq("category_name", categoryName));
|
||||
return ResultUtils.success(category);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询商品分类失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<ProductCategories>> getSubCategoriesByParentId(Long parentId) {
|
||||
try {
|
||||
List<ProductCategories> categories = productCategoriesMapper.selectList(
|
||||
new QueryWrapper<ProductCategories>().eq("parent_id", parentId));
|
||||
return ResultUtils.success(categories);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询子分类失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> createCategory(ProductCategories productCategories) {
|
||||
if (ValidateUtil.isEmpty(productCategories)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类信息不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(productCategories.getCategoryName())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类名称不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查分类名称是否已存在
|
||||
ProductCategories existing = productCategoriesMapper.selectOne(
|
||||
new QueryWrapper<ProductCategories>().eq("category_name", productCategories.getCategoryName()));
|
||||
if (existing != null) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "分类名称已存在");
|
||||
}
|
||||
int result = productCategoriesMapper.insert(productCategories);
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建商品分类失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> updateCategory(ProductCategories productCategories) {
|
||||
if (ValidateUtil.isEmpty(productCategories)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类信息不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(productCategories.getId())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类ID不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(productCategories.getCategoryName())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类名称不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查分类是否存在
|
||||
ProductCategories existing = productCategoriesMapper.selectById(productCategories.getId());
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "分类不存在");
|
||||
}
|
||||
// 检查分类名称是否已存在(排除当前分类)
|
||||
ProductCategories nameExists = productCategoriesMapper.selectOne(
|
||||
new QueryWrapper<ProductCategories>()
|
||||
.eq("category_name", productCategories.getCategoryName())
|
||||
.ne("id", productCategories.getId()));
|
||||
if (nameExists != null) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "分类名称已存在");
|
||||
}
|
||||
int result = productCategoriesMapper.updateById(productCategories);
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新商品分类失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> deleteCategory(Long id) {
|
||||
if (ValidateUtil.isEmpty(id)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类ID不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查分类是否存在
|
||||
ProductCategories existing = productCategoriesMapper.selectById(id);
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "分类不存在");
|
||||
}
|
||||
// 检查是否有子分类
|
||||
List<ProductCategories> subCategories = productCategoriesMapper.selectList(
|
||||
new QueryWrapper<ProductCategories>().eq("parent_id", id));
|
||||
if (!subCategories.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "该分类下存在子分类,无法删除");
|
||||
}
|
||||
int result = productCategoriesMapper.deleteById(id);
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除商品分类失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<ProductCategories>> listRootCategories() {
|
||||
try {
|
||||
List<ProductCategories> rootCategories = productCategoriesMapper.selectList(
|
||||
new QueryWrapper<ProductCategories>().isNull("parent_id").or().eq("parent_id", 0));
|
||||
return ResultUtils.success(rootCategories);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询根分类失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<ProductCategories> getCategoryById(Long id) {
|
||||
if (ValidateUtil.isEmpty(id)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类ID不能为空");
|
||||
}
|
||||
try {
|
||||
ProductCategories category = productCategoriesMapper.selectById(id);
|
||||
if (category == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "分类不存在");
|
||||
}
|
||||
return ResultUtils.success(category);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询商品分类失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> batchDeleteCategories(List<Long> ids) {
|
||||
if (ValidateUtil.isEmpty(ids)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类ID列表不能为空");
|
||||
}
|
||||
if (ids.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类ID列表不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查所有分类是否存在以及是否有子分类
|
||||
for (Long id : ids) {
|
||||
if (ValidateUtil.isEmpty(id)) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "分类ID列表中存在空值");
|
||||
}
|
||||
ProductCategories existing = productCategoriesMapper.selectById(id);
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "分类ID为" + id + "的分类不存在");
|
||||
}
|
||||
// 检查是否有子分类
|
||||
List<ProductCategories> subCategories = productCategoriesMapper.selectList(
|
||||
new QueryWrapper<ProductCategories>().eq("parent_id", id));
|
||||
if (!subCategories.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "分类ID为" + id + "的分类下存在子分类,无法删除");
|
||||
}
|
||||
}
|
||||
int result = productCategoriesMapper.deleteBatchIds(ids);
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量删除商品分类失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<ProductCategories>> listAllCategoriesWithTree() {
|
||||
try {
|
||||
// 查询所有分类
|
||||
List<ProductCategories> allCategories = productCategoriesMapper.selectList(null);
|
||||
// 构建树形结构
|
||||
List<ProductCategories> treeCategories = buildCategoryTree(allCategories, null);
|
||||
return ResultUtils.success(treeCategories);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询商品分类树失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建分类树形结构
|
||||
* @param allCategories 所有分类列表
|
||||
* @param parentId 父分类ID
|
||||
* @return 树形分类列表
|
||||
*/
|
||||
private List<ProductCategories> buildCategoryTree(List<ProductCategories> allCategories, Long parentId) {
|
||||
List<ProductCategories> tree = new ArrayList<>();
|
||||
for (ProductCategories category : allCategories) {
|
||||
Long categoryParentId = category.getParentId();
|
||||
if ((parentId == null && (categoryParentId == null || categoryParentId == 0)) || (parentId != null && parentId.equals(categoryParentId))) {
|
||||
// 递归查找子分类
|
||||
List<ProductCategories> children = buildCategoryTree(allCategories, category.getId());
|
||||
category.setChildren(children);
|
||||
tree.add(category);
|
||||
}
|
||||
}
|
||||
return tree;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,319 @@
|
||||
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;
|
||||
|
||||
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.ProductImages;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.ProductImagesMapper;
|
||||
import com.qf.backend.service.ProductImagesService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
|
||||
@Service
|
||||
public class ProductImagesServiceImpl extends ServiceImpl<ProductImagesMapper, ProductImages> implements ProductImagesService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ProductImagesServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private ProductImagesMapper productImagesMapper;
|
||||
|
||||
@Override
|
||||
public Result<List<ProductImages>> getImagesByProductId(Long productId) {
|
||||
logger.info("根据商品ID查询图片: {}", productId);
|
||||
|
||||
try {
|
||||
if (productId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品ID不能为空");
|
||||
}
|
||||
|
||||
List<ProductImages> images = productImagesMapper.selectList(new QueryWrapper<ProductImages>().eq("product_id", productId).orderByAsc("sort"));
|
||||
logger.info("查询到 {} 张图片", images.size());
|
||||
return ResultUtils.success(images);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询图片失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询图片失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<ProductImages> getMainImageByProductId(Long productId) {
|
||||
logger.info("根据商品ID查询主图: {}", productId);
|
||||
|
||||
try {
|
||||
if (productId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品ID不能为空");
|
||||
}
|
||||
|
||||
ProductImages mainImage = productImagesMapper.selectOne(new QueryWrapper<ProductImages>().eq("product_id", productId).eq("is_main", 1));
|
||||
if (mainImage == null) {
|
||||
logger.warn("商品 {} 没有设置主图", productId);
|
||||
return ResultUtils.success(null);
|
||||
}
|
||||
return ResultUtils.success(mainImage);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询主图失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询主图失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> createImage(ProductImages productImages) {
|
||||
logger.info("创建商品图片: 商品ID = {}", productImages.getProductId());
|
||||
|
||||
try {
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(productImages, "productId", "imageUrl");
|
||||
|
||||
// 检查是否设置了主图,如果设置了主图,则将其他图片设置为非主图
|
||||
if (productImages.getIsMain() != null && productImages.getIsMain() == 1) {
|
||||
// 将该商品的所有图片设置为非主图
|
||||
productImagesMapper.update(null, new UpdateWrapper<ProductImages>().eq("product_id", productImages.getProductId()).set("is_main", 0));
|
||||
}
|
||||
|
||||
int result = productImagesMapper.insert(productImages);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "创建商品图片失败");
|
||||
}
|
||||
|
||||
logger.info("商品图片创建成功");
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证图片信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证图片信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("创建商品图片失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建商品图片失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> updateImage(ProductImages productImages) {
|
||||
logger.info("更新图片信息: 图片ID = {}", productImages.getId());
|
||||
|
||||
try {
|
||||
if (productImages == null || productImages.getId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "图片信息或图片ID不能为空");
|
||||
}
|
||||
|
||||
// 检查图片是否存在
|
||||
ProductImages existingImage = getImageByIdAndCheckExist(productImages.getId());
|
||||
if (existingImage == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "图片不存在");
|
||||
}
|
||||
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(productImages, "productId", "imageUrl");
|
||||
|
||||
// 检查是否设置了主图,如果设置了主图,则将其他图片设置为非主图
|
||||
if (productImages.getIsMain() != null && productImages.getIsMain() == 1) {
|
||||
// 将该商品的所有图片设置为非主图
|
||||
productImagesMapper.update(null, new UpdateWrapper<ProductImages>().eq("product_id", productImages.getProductId()).set("is_main", 0));
|
||||
}
|
||||
|
||||
// 更新图片信息
|
||||
int result = productImagesMapper.updateById(productImages);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "更新图片信息失败");
|
||||
}
|
||||
|
||||
logger.info("图片信息更新成功: 图片ID = {}", productImages.getId());
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证图片信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证图片信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("更新图片信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新图片信息失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> deleteImage(Long id) {
|
||||
logger.info("删除图片: 图片ID = {}", id);
|
||||
|
||||
try {
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "图片ID不能为空");
|
||||
}
|
||||
// 检查图片是否存在
|
||||
ProductImages image = getImageByIdAndCheckExist(id);
|
||||
if (image == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "图片不存在");
|
||||
}
|
||||
|
||||
int result = productImagesMapper.deleteById(id);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "删除图片失败");
|
||||
}
|
||||
|
||||
logger.info("图片删除成功: 图片ID = {}", id);
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("删除图片失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除图片失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<ProductImages> getImageById(Long id) {
|
||||
logger.info("根据ID查询图片: 图片ID = {}", id);
|
||||
|
||||
try {
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "图片ID不能为空");
|
||||
}
|
||||
|
||||
ProductImages image = getImageByIdAndCheckExist(id);
|
||||
if (image == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "图片不存在");
|
||||
}
|
||||
|
||||
return ResultUtils.success(image);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询图片失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询图片失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> batchCreateImages(List<ProductImages> images) {
|
||||
logger.info("批量创建商品图片: 图片数量 = {}", images.size());
|
||||
|
||||
try {
|
||||
if (images == null || images.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "图片列表不能为空");
|
||||
}
|
||||
|
||||
// 批量创建图片
|
||||
for (ProductImages image : images) {
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(image, "productId", "imageUrl");
|
||||
|
||||
// 检查是否设置了主图,如果设置了主图,则将其他图片设置为非主图
|
||||
if (image.getIsMain() != null && image.getIsMain() == 1) {
|
||||
// 将该商品的所有图片设置为非主图
|
||||
productImagesMapper.update(null, new UpdateWrapper<ProductImages>().eq("product_id", image.getProductId()).set("is_main", 0));
|
||||
}
|
||||
|
||||
productImagesMapper.insert(image);
|
||||
}
|
||||
|
||||
logger.info("批量创建商品图片成功: 图片数量 = {}", images.size());
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证图片信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证图片信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("批量创建商品图片失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量创建商品图片失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> deleteImagesByProductId(Long productId) {
|
||||
logger.info("根据商品ID删除所有图片: {}", productId);
|
||||
|
||||
try {
|
||||
if (productId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品ID不能为空");
|
||||
}
|
||||
|
||||
int result = productImagesMapper.delete(new QueryWrapper<ProductImages>().eq("product_id", productId));
|
||||
logger.info("删除了 {} 张图片", result);
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("根据商品ID删除所有图片失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "根据商品ID删除所有图片失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> setMainImage(Long productId, Long imageId) {
|
||||
logger.info("设置主图: 商品ID = {}, 图片ID = {}", productId, imageId);
|
||||
|
||||
try {
|
||||
if (productId == null || imageId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品ID和图片ID不能为空");
|
||||
}
|
||||
|
||||
// 检查图片是否存在
|
||||
ProductImages image = getImageByIdAndCheckExist(imageId);
|
||||
if (image == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "图片不存在");
|
||||
}
|
||||
|
||||
// 检查图片是否属于该商品
|
||||
if (!image.getProductId().equals(productId)) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "图片不属于该商品");
|
||||
}
|
||||
|
||||
// 将该商品的所有图片设置为非主图
|
||||
productImagesMapper.update(null, new UpdateWrapper<ProductImages>().eq("product_id", productId).set("is_main", 0));
|
||||
|
||||
// 将指定图片设置为主图
|
||||
ProductImages mainImage = new ProductImages();
|
||||
mainImage.setId(imageId);
|
||||
mainImage.setIsMain(1);
|
||||
int result = productImagesMapper.updateById(mainImage);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "设置主图失败");
|
||||
}
|
||||
|
||||
logger.info("主图设置成功: 商品ID = {}, 图片ID = {}", productId, imageId);
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("设置主图失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "设置主图失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据图片ID查询图片并检查是否存在
|
||||
* @param id 图片ID
|
||||
* @return 图片对象,如果图片不存在则返回null
|
||||
*/
|
||||
private ProductImages getImageByIdAndCheckExist(Long id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
QueryWrapper<ProductImages> queryWrapper = new QueryWrapper<ProductImages>().eq("id", id);
|
||||
return productImagesMapper.selectOne(queryWrapper);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,371 @@
|
||||
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;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
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.ProductInventories;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.ProductInventoriesMapper;
|
||||
import com.qf.backend.service.ProductInventoriesService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
|
||||
@Service
|
||||
public class ProductInventoriesServiceImpl extends ServiceImpl<ProductInventoriesMapper, ProductInventories> implements ProductInventoriesService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ProductInventoriesServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private ProductInventoriesMapper productInventoriesMapper;
|
||||
|
||||
@Override
|
||||
public Result<List<ProductInventories>> getInventoriesByProductId(Long productId) {
|
||||
logger.info("根据商品ID查询库存: {}", productId);
|
||||
|
||||
try {
|
||||
if (productId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品ID不能为空");
|
||||
}
|
||||
|
||||
List<ProductInventories> inventories = productInventoriesMapper.selectList(new QueryWrapper<ProductInventories>().eq("product_id", productId));
|
||||
logger.info("查询到 {} 条库存记录", inventories.size());
|
||||
return ResultUtils.success(inventories);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询库存失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询库存失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<ProductInventories> getInventoryBySkuId(Long skuId) {
|
||||
logger.info("根据SKU ID查询库存: {}", skuId);
|
||||
|
||||
try {
|
||||
if (skuId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "SKU ID不能为空");
|
||||
}
|
||||
|
||||
ProductInventories inventory = productInventoriesMapper.selectOne(new QueryWrapper<ProductInventories>().eq("sku_id", skuId));
|
||||
if (inventory == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "库存不存在: SKU ID = " + skuId);
|
||||
}
|
||||
|
||||
return ResultUtils.success(inventory);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询库存失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询库存失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> createInventory(ProductInventories productInventories) {
|
||||
logger.info("创建库存记录: SKU ID = {}", productInventories.getSkuId());
|
||||
|
||||
try {
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(productInventories, "skuId", "currentStock");
|
||||
|
||||
// 检查库存数量是否合法
|
||||
if (productInventories.getCurrentStock() < 0) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "库存数量不能为负数");
|
||||
}
|
||||
|
||||
// 检查是否已存在该SKU的库存记录
|
||||
ProductInventories existingInventory = productInventoriesMapper.selectOne(new QueryWrapper<ProductInventories>().eq("sku_id", productInventories.getSkuId()));
|
||||
if (existingInventory != null) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "该SKU的库存记录已存在");
|
||||
}
|
||||
|
||||
int result = productInventoriesMapper.insert(productInventories);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "创建库存记录失败");
|
||||
}
|
||||
|
||||
logger.info("库存记录创建成功");
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证库存信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证库存信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("创建库存记录失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建库存记录失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> updateInventory(ProductInventories productInventories) {
|
||||
logger.info("更新库存信息: 库存ID = {}", productInventories.getId());
|
||||
|
||||
try {
|
||||
if (productInventories == null || productInventories.getId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "库存信息或库存ID不能为空");
|
||||
}
|
||||
|
||||
// 检查库存是否存在
|
||||
ProductInventories existingInventory = getInventoryByIdAndCheckExist(productInventories.getId());
|
||||
if (existingInventory == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "库存不存在");
|
||||
}
|
||||
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(productInventories, "skuId", "currentStock");
|
||||
|
||||
// 检查库存数量是否合法
|
||||
if (productInventories.getCurrentStock() < 0) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "库存数量不能为负数");
|
||||
}
|
||||
|
||||
// 更新库存信息
|
||||
int result = productInventoriesMapper.updateById(productInventories);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "更新库存信息失败");
|
||||
}
|
||||
|
||||
logger.info("库存信息更新成功: 库存ID = {}", productInventories.getId());
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证库存信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证库存信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("更新库存信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新库存信息失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> deleteInventory(Long id) {
|
||||
logger.info("删除库存记录: 库存ID = {}", id);
|
||||
|
||||
try {
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "库存ID不能为空");
|
||||
}
|
||||
// 检查库存是否存在
|
||||
ProductInventories inventory = getInventoryByIdAndCheckExist(id);
|
||||
if (inventory == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "库存不存在");
|
||||
}
|
||||
|
||||
int result = productInventoriesMapper.deleteById(id);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "删除库存记录失败");
|
||||
}
|
||||
|
||||
logger.info("库存记录删除成功: 库存ID = {}", id);
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("删除库存记录失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除库存记录失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<ProductInventories> getInventoryById(Long id) {
|
||||
logger.info("根据ID查询库存: 库存ID = {}", id);
|
||||
|
||||
try {
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "库存ID不能为空");
|
||||
}
|
||||
|
||||
ProductInventories inventory = getInventoryByIdAndCheckExist(id);
|
||||
if (inventory == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "库存不存在");
|
||||
}
|
||||
|
||||
return ResultUtils.success(inventory);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询库存失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询库存失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> increaseInventory(Long skuId, Integer quantity) {
|
||||
logger.info("增加库存: SKU ID = {}, 数量 = {}", skuId, quantity);
|
||||
|
||||
try {
|
||||
if (skuId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "SKU ID不能为空");
|
||||
}
|
||||
if (quantity == null || quantity <= 0) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "增加数量必须大于0");
|
||||
}
|
||||
|
||||
// 检查库存是否存在
|
||||
ProductInventories inventory = productInventoriesMapper.selectOne(new QueryWrapper<ProductInventories>().eq("sku_id", skuId));
|
||||
if (inventory == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "库存不存在: SKU ID = " + skuId);
|
||||
}
|
||||
|
||||
// 增加库存
|
||||
inventory.setCurrentStock(inventory.getCurrentStock() + quantity);
|
||||
int result = productInventoriesMapper.updateById(inventory);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "增加库存失败");
|
||||
}
|
||||
|
||||
logger.info("库存增加成功: SKU ID = {}, 新库存 = {}", skuId, inventory.getCurrentStock());
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("增加库存失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "增加库存失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> decreaseInventory(Long skuId, Integer quantity) {
|
||||
logger.info("减少库存: SKU ID = {}, 数量 = {}", skuId, quantity);
|
||||
|
||||
try {
|
||||
if (skuId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "SKU ID不能为空");
|
||||
}
|
||||
if (quantity == null || quantity <= 0) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "减少数量必须大于0");
|
||||
}
|
||||
|
||||
// 检查库存是否存在
|
||||
ProductInventories inventory = productInventoriesMapper.selectOne(new QueryWrapper<ProductInventories>().eq("sku_id", skuId));
|
||||
if (inventory == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "库存不存在: SKU ID = " + skuId);
|
||||
}
|
||||
|
||||
// 检查库存是否充足
|
||||
if (inventory.getCurrentStock() < quantity) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "库存不足: 当前库存 = " + inventory.getCurrentStock() + ", 需要 = " + quantity);
|
||||
}
|
||||
|
||||
// 减少库存
|
||||
inventory.setCurrentStock(inventory.getCurrentStock() - quantity);
|
||||
int result = productInventoriesMapper.updateById(inventory);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "减少库存失败");
|
||||
}
|
||||
|
||||
logger.info("库存减少成功: SKU ID = {}, 新库存 = {}", skuId, inventory.getCurrentStock());
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("减少库存失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "减少库存失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> checkInventorySufficient(Long skuId, Integer quantity) {
|
||||
logger.info("检查库存是否充足: SKU ID = {}, 需要数量 = {}", skuId, quantity);
|
||||
|
||||
try {
|
||||
if (skuId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "SKU ID不能为空");
|
||||
}
|
||||
if (quantity == null || quantity <= 0) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "需要数量必须大于0");
|
||||
}
|
||||
|
||||
// 检查库存是否存在
|
||||
ProductInventories inventory = productInventoriesMapper.selectOne(new QueryWrapper<ProductInventories>().eq("sku_id", skuId));
|
||||
if (inventory == null) {
|
||||
logger.warn("库存不存在: SKU ID = {}", skuId);
|
||||
return ResultUtils.success(false);
|
||||
}
|
||||
|
||||
boolean isSufficient = inventory.getCurrentStock() >= quantity;
|
||||
logger.info("库存检查结果: SKU ID = {}, 当前库存 = {}, 需要数量 = {}, 是否充足 = {}", skuId, inventory.getCurrentStock(), quantity, isSufficient);
|
||||
return ResultUtils.success(isSufficient);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("检查库存是否充足失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "检查库存是否充足失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> batchUpdateInventory(List<ProductInventories> inventoryUpdates) {
|
||||
logger.info("批量更新库存: 更新数量 = {}", inventoryUpdates.size());
|
||||
|
||||
try {
|
||||
if (inventoryUpdates == null || inventoryUpdates.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "库存更新列表不能为空");
|
||||
}
|
||||
|
||||
// 批量更新库存
|
||||
for (ProductInventories inventory : inventoryUpdates) {
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(inventory, "id", "currentStock");
|
||||
|
||||
// 检查库存数量是否合法
|
||||
if (inventory.getCurrentStock() < 0) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "库存数量不能为负数");
|
||||
}
|
||||
|
||||
// 检查库存是否存在
|
||||
ProductInventories existingInventory = getInventoryByIdAndCheckExist(inventory.getId());
|
||||
if (existingInventory == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "库存不存在: 库存ID = " + inventory.getId());
|
||||
}
|
||||
|
||||
// 更新库存
|
||||
productInventoriesMapper.updateById(inventory);
|
||||
}
|
||||
|
||||
logger.info("批量更新库存成功: 更新数量 = {}", inventoryUpdates.size());
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证库存信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证库存信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("批量更新库存失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量更新库存失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据库存ID查询库存并检查是否存在
|
||||
* @param id 库存ID
|
||||
* @return 库存对象,如果库存不存在则返回null
|
||||
*/
|
||||
private ProductInventories getInventoryByIdAndCheckExist(Long id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
QueryWrapper<ProductInventories> queryWrapper = new QueryWrapper<ProductInventories>().eq("id", id);
|
||||
return productInventoriesMapper.selectOne(queryWrapper);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,332 @@
|
||||
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;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
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.ProductSkus;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.ProductSkusMapper;
|
||||
import com.qf.backend.service.ProductSkusService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
|
||||
@Service
|
||||
public class ProductSkusServiceImpl extends ServiceImpl<ProductSkusMapper, ProductSkus> implements ProductSkusService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ProductSkusServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private ProductSkusMapper productSkusMapper;
|
||||
|
||||
@Override
|
||||
public Result<List<ProductSkus>> getSkusByProductId(Long productId) {
|
||||
logger.info("根据商品ID查询SKU: {}", productId);
|
||||
|
||||
try {
|
||||
if (productId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品ID不能为空");
|
||||
}
|
||||
|
||||
List<ProductSkus> skus = productSkusMapper.selectList(new QueryWrapper<ProductSkus>().eq("product_id", productId));
|
||||
logger.info("查询到 {} 个SKU", skus.size());
|
||||
return ResultUtils.success(skus);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询SKU失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询SKU失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<ProductSkus> getSkuByCode(String skuCode) {
|
||||
logger.info("根据SKU编码查询SKU: {}", skuCode);
|
||||
|
||||
try {
|
||||
if (ValidateUtil.isEmpty(skuCode)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "SKU编码不能为空");
|
||||
}
|
||||
|
||||
ProductSkus sku = productSkusMapper.selectOne(new QueryWrapper<ProductSkus>().eq("sku_code", skuCode));
|
||||
if (sku == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "SKU不存在: " + skuCode);
|
||||
}
|
||||
return ResultUtils.success(sku);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询SKU失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询SKU失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> createSku(ProductSkus productSkus) {
|
||||
logger.info("创建SKU: SKU编码 = {}", productSkus.getSkuCode());
|
||||
|
||||
try {
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(productSkus, "productId", "skuCode", "skuSpecs", "price", "stock");
|
||||
|
||||
// 检查SKU编码是否已存在
|
||||
ProductSkus existingSku = productSkusMapper.selectOne(new QueryWrapper<ProductSkus>().eq("sku_code", productSkus.getSkuCode()));
|
||||
if (existingSku != null) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "SKU编码已存在");
|
||||
}
|
||||
|
||||
int result = productSkusMapper.insert(productSkus);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "创建SKU失败");
|
||||
}
|
||||
|
||||
logger.info("SKU创建成功");
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证SKU信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证SKU信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("创建SKU失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建SKU失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> updateSku(ProductSkus productSkus) {
|
||||
logger.info("更新SKU信息: SKU ID = {}", productSkus.getId());
|
||||
|
||||
try {
|
||||
if (productSkus == null || productSkus.getId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "SKU信息或SKU ID不能为空");
|
||||
}
|
||||
|
||||
// 检查SKU是否存在
|
||||
ProductSkus existingSku = getSkuByIdAndCheckExist(productSkus.getId());
|
||||
if (existingSku == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "SKU不存在");
|
||||
}
|
||||
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(productSkus, "productId", "skuCode", "skuSpecs", "price", "stock");
|
||||
|
||||
// 检查SKU编码是否已存在(排除当前SKU)
|
||||
ProductSkus skuWithSameCode = productSkusMapper.selectOne(new QueryWrapper<ProductSkus>().eq("sku_code", productSkus.getSkuCode()).ne("id", productSkus.getId()));
|
||||
if (skuWithSameCode != null) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "SKU编码已存在");
|
||||
}
|
||||
|
||||
// 更新SKU信息
|
||||
int result = productSkusMapper.updateById(productSkus);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "更新SKU信息失败");
|
||||
}
|
||||
|
||||
logger.info("SKU信息更新成功: SKU ID = {}", productSkus.getId());
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证SKU信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证SKU信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("更新SKU信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新SKU信息失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> deleteSku(Long id) {
|
||||
logger.info("删除SKU: SKU ID = {}", id);
|
||||
|
||||
try {
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "SKU ID不能为空");
|
||||
}
|
||||
// 检查SKU是否存在
|
||||
ProductSkus sku = getSkuByIdAndCheckExist(id);
|
||||
if (sku == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "SKU不存在");
|
||||
}
|
||||
|
||||
int result = productSkusMapper.deleteById(id);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "删除SKU失败");
|
||||
}
|
||||
|
||||
logger.info("SKU删除成功: SKU ID = {}", id);
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("删除SKU失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除SKU失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<ProductSkus> getSkuById(Long id) {
|
||||
logger.info("根据ID查询SKU: SKU ID = {}", id);
|
||||
|
||||
try {
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "SKU ID不能为空");
|
||||
}
|
||||
|
||||
ProductSkus sku = getSkuByIdAndCheckExist(id);
|
||||
if (sku == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "SKU不存在");
|
||||
}
|
||||
|
||||
return ResultUtils.success(sku);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询SKU失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询SKU失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> batchCreateSkus(List<ProductSkus> skus) {
|
||||
logger.info("批量创建SKU: SKU数量 = {}", skus.size());
|
||||
|
||||
try {
|
||||
if (skus == null || skus.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "SKU列表不能为空");
|
||||
}
|
||||
|
||||
// 批量创建SKU
|
||||
for (ProductSkus sku : skus) {
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(sku, "productId", "skuCode", "skuSpecs", "price", "stock");
|
||||
|
||||
// 检查SKU编码是否已存在
|
||||
ProductSkus existingSku = productSkusMapper.selectOne(new QueryWrapper<ProductSkus>().eq("sku_code", sku.getSkuCode()));
|
||||
if (existingSku != null) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "SKU编码已存在: " + sku.getSkuCode());
|
||||
}
|
||||
|
||||
productSkusMapper.insert(sku);
|
||||
}
|
||||
|
||||
logger.info("批量创建SKU成功: SKU数量 = {}", skus.size());
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证SKU信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证SKU信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("批量创建SKU失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量创建SKU失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> deleteSkusByProductId(Long productId) {
|
||||
logger.info("根据商品ID删除所有SKU: {}", productId);
|
||||
|
||||
try {
|
||||
if (productId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品ID不能为空");
|
||||
}
|
||||
|
||||
int result = productSkusMapper.delete(new QueryWrapper<ProductSkus>().eq("product_id", productId));
|
||||
logger.info("删除了 {} 个SKU", result);
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("根据商品ID删除所有SKU失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "根据商品ID删除所有SKU失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> updateSkuStock(Long skuId, Integer quantity) {
|
||||
logger.info("更新SKU库存: SKU ID = {}, 库存数量 = {}", skuId, quantity);
|
||||
|
||||
try {
|
||||
if (skuId == null || quantity == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "SKU ID和库存数量不能为空");
|
||||
}
|
||||
|
||||
// 检查库存数量是否合法
|
||||
if (quantity < 0) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "库存数量不能为负数");
|
||||
}
|
||||
|
||||
// 检查SKU是否存在
|
||||
ProductSkus sku = getSkuByIdAndCheckExist(skuId);
|
||||
if (sku == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "SKU不存在");
|
||||
}
|
||||
|
||||
// 更新库存
|
||||
sku.setStock(quantity);
|
||||
int result = productSkusMapper.updateById(sku);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "更新SKU库存失败");
|
||||
}
|
||||
|
||||
logger.info("SKU库存更新成功: SKU ID = {}, 新库存 = {}", skuId, quantity);
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("更新SKU库存失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新SKU库存失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<ProductSkus>> batchGetSkus(List<Long> skuIds) {
|
||||
logger.info("批量查询SKU: SKU ID列表 = {}", skuIds);
|
||||
|
||||
try {
|
||||
if (skuIds == null || skuIds.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "SKU ID列表不能为空");
|
||||
}
|
||||
|
||||
List<ProductSkus> skus = productSkusMapper.selectBatchIds(skuIds);
|
||||
logger.info("查询到 {} 个SKU", skus.size());
|
||||
return ResultUtils.success(skus);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("批量查询SKU失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量查询SKU失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据SKU ID查询SKU并检查是否存在
|
||||
* @param id SKU ID
|
||||
* @return SKU对象,如果SKU不存在则返回null
|
||||
*/
|
||||
private ProductSkus getSkuByIdAndCheckExist(Long id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
QueryWrapper<ProductSkus> queryWrapper = new QueryWrapper<ProductSkus>().eq("id", id);
|
||||
return productSkusMapper.selectOne(queryWrapper);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
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.plugins.pagination.Page;
|
||||
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.Products;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.ProductsMapper;
|
||||
import com.qf.backend.service.ProductsService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
|
||||
/**
|
||||
* 商品服务实现类
|
||||
*/
|
||||
@Service
|
||||
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements ProductsService {
|
||||
|
||||
@Autowired
|
||||
private ProductsMapper productsMapper;
|
||||
|
||||
@Override
|
||||
public Result<List<Products>> getProductsByName(String productName) {
|
||||
if (ValidateUtil.isEmpty(productName)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品名称不能为空");
|
||||
}
|
||||
try {
|
||||
List<Products> products = productsMapper.selectList(
|
||||
new QueryWrapper<Products>().like("product_name", productName));
|
||||
return ResultUtils.success(products);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询商品失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<Products>> getProductsByCategoryId(Long categoryId) {
|
||||
if (ValidateUtil.isEmpty(categoryId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类ID不能为空");
|
||||
}
|
||||
try {
|
||||
List<Products> products = productsMapper.selectList(
|
||||
new QueryWrapper<Products>().eq("category_id", categoryId));
|
||||
return ResultUtils.success(products);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询商品失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> createProduct(Products products) {
|
||||
if (ValidateUtil.isEmpty(products)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品信息不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(products.getProductName())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品名称不能为空");
|
||||
}
|
||||
if (products.getShopId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "店铺ID不能为空");
|
||||
}
|
||||
if (products.getCategoryId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类ID不能为空");
|
||||
}
|
||||
if (products.getOriginalPrice() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品原价不能为空");
|
||||
}
|
||||
try {
|
||||
int result = productsMapper.insert(products);
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建商品失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> updateProduct(Products products) {
|
||||
if (ValidateUtil.isEmpty(products)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品信息不能为空");
|
||||
}
|
||||
if (products.getId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品ID不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(products.getProductName())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品名称不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查商品是否存在
|
||||
Products existing = productsMapper.selectById(products.getId());
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "商品不存在");
|
||||
}
|
||||
int result = productsMapper.updateById(products);
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新商品失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> deleteProduct(Long id) {
|
||||
if (ValidateUtil.isEmpty(id)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品ID不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查商品是否存在
|
||||
Products existing = productsMapper.selectById(id);
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "商品不存在");
|
||||
}
|
||||
int result = productsMapper.deleteById(id);
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除商品失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Products> getProductById(Long id) {
|
||||
if (ValidateUtil.isEmpty(id)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品ID不能为空");
|
||||
}
|
||||
try {
|
||||
Products product = productsMapper.selectById(id);
|
||||
if (product == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "商品不存在");
|
||||
}
|
||||
return ResultUtils.success(product);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询商品失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<Products>> listProductsByPage(int page, int size) {
|
||||
if (page < 1) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "页码不能小于1");
|
||||
}
|
||||
if (size < 1 || size > 100) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "每页数量必须在1-100之间");
|
||||
}
|
||||
try {
|
||||
Page<Products> productPage = new Page<>(page, size);
|
||||
Page<Products> resultPage = productsMapper.selectPage(productPage, null);
|
||||
return ResultUtils.success(resultPage.getRecords());
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "分页查询商品失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<Products>> getProductsByShopId(Long shopId) {
|
||||
if (ValidateUtil.isEmpty(shopId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "店铺ID不能为空");
|
||||
}
|
||||
try {
|
||||
List<Products> products = productsMapper.selectList(
|
||||
new QueryWrapper<Products>().eq("shop_id", shopId));
|
||||
return ResultUtils.success(products);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询商品失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> batchUpdateProductStatus(List<Long> ids, Integer status) {
|
||||
if (ValidateUtil.isEmpty(ids)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品ID列表不能为空");
|
||||
}
|
||||
if (ids.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品ID列表不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(status)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "商品状态不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查所有商品是否存在
|
||||
for (Long id : ids) {
|
||||
if (ValidateUtil.isEmpty(id)) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "商品ID列表中存在空值");
|
||||
}
|
||||
Products existing = productsMapper.selectById(id);
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "商品ID为" + id + "的商品不存在");
|
||||
}
|
||||
}
|
||||
// 批量更新状态
|
||||
for (Long id : ids) {
|
||||
Products product = new Products();
|
||||
product.setId(id);
|
||||
product.setStatus(status);
|
||||
productsMapper.updateById(product);
|
||||
}
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量更新商品状态失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<Products>> searchProducts(String keyword, int page, int size) {
|
||||
if (ValidateUtil.isEmpty(keyword)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "搜索关键词不能为空");
|
||||
}
|
||||
if (page < 1) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "页码不能小于1");
|
||||
}
|
||||
if (size < 1 || size > 100) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "每页数量必须在1-100之间");
|
||||
}
|
||||
try {
|
||||
Page<Products> productPage = new Page<>(page, size);
|
||||
QueryWrapper<Products> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.like("product_name", keyword)
|
||||
.or().like("description", keyword);
|
||||
Page<Products> resultPage = productsMapper.selectPage(productPage, queryWrapper);
|
||||
return ResultUtils.success(resultPage.getRecords());
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "搜索商品失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
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.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.qf.backend.entity.Refunds;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.RefundsMapper;
|
||||
import com.qf.backend.service.RefundsService;
|
||||
import com.qf.backend.util.RefundNumberFenerator;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
|
||||
/**
|
||||
* 退款服务实现类
|
||||
*/
|
||||
@Service
|
||||
public class RefundsServiceImpl extends ServiceImpl<RefundsMapper, Refunds> implements RefundsService {
|
||||
|
||||
@Autowired
|
||||
private RefundsMapper refundsMapper;
|
||||
|
||||
@Override
|
||||
public List<Refunds> getRefundsByOrderId(Long orderId) {
|
||||
if (ValidateUtil.isEmpty(orderId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单ID不能为空");
|
||||
}
|
||||
try {
|
||||
return refundsMapper.selectList(
|
||||
new QueryWrapper<Refunds>().eq("order_id", orderId));
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询退款记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Refunds getRefundByNumber(String refundNumber) {
|
||||
if (ValidateUtil.isEmpty(refundNumber)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "退款单号不能为空");
|
||||
}
|
||||
try {
|
||||
return refundsMapper.selectOne(
|
||||
new QueryWrapper<Refunds>().eq("refund_number", refundNumber));
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询退款记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean createRefund(Refunds refunds) {
|
||||
if (ValidateUtil.isEmpty(refunds)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "退款信息不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(refunds.getOrderId())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "订单ID不能为空");
|
||||
}
|
||||
if (refunds.getRefundAmount() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "退款金额不能为空");
|
||||
}
|
||||
// 生成退款单号 基于时间戳 + 随机数 / 序列号(最常用)
|
||||
refunds.setRefundNo(RefundNumberFenerator.generateRefundNumber());
|
||||
try {
|
||||
int result = refundsMapper.insert(refunds);
|
||||
return result > 0;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建退款记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateRefund(Refunds refunds) {
|
||||
if (ValidateUtil.isEmpty(refunds)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "退款信息不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(refunds.getId())) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "退款ID不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查退款记录是否存在
|
||||
Refunds existing = refundsMapper.selectById(refunds.getId());
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "退款记录不存在");
|
||||
}
|
||||
int result = refundsMapper.updateById(refunds);
|
||||
return result > 0;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新退款记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteRefund(Long id) {
|
||||
if (ValidateUtil.isEmpty(id)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "退款ID不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查退款记录是否存在
|
||||
Refunds existing = refundsMapper.selectById(id);
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "退款记录不存在");
|
||||
}
|
||||
int result = refundsMapper.deleteById(id);
|
||||
return result > 0;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除退款记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Refunds getRefundById(Long id) {
|
||||
if (ValidateUtil.isEmpty(id)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "退款ID不能为空");
|
||||
}
|
||||
try {
|
||||
Refunds refund = refundsMapper.selectById(id);
|
||||
if (refund == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "退款记录不存在");
|
||||
}
|
||||
return refund;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询退款记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Refunds> getRefundsByUserId(Long userId) {
|
||||
if (ValidateUtil.isEmpty(userId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID不能为空");
|
||||
}
|
||||
try {
|
||||
return refundsMapper.selectList(
|
||||
new QueryWrapper<Refunds>().eq("user_id", userId));
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询退款记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Refunds> getRefundsByStatus(Integer status) {
|
||||
if (ValidateUtil.isEmpty(status)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "退款状态不能为空");
|
||||
}
|
||||
try {
|
||||
return refundsMapper.selectList(
|
||||
new QueryWrapper<Refunds>().eq("status", status));
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询退款记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateRefundStatus(Long refundId, Integer status) {
|
||||
if (ValidateUtil.isEmpty(refundId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "退款ID不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(status)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "退款状态不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查退款记录是否存在
|
||||
Refunds existing = refundsMapper.selectById(refundId);
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "退款记录不存在");
|
||||
}
|
||||
// 更新状态
|
||||
existing.setRefundStatus(status);
|
||||
int result = refundsMapper.updateById(existing);
|
||||
return result > 0;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新退款状态失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Refunds> listRefundsByPage(int page, int size) {
|
||||
if (page < 1) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "页码不能小于1");
|
||||
}
|
||||
if (size < 1 || size > 100) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "每页数量必须在1-100之间");
|
||||
}
|
||||
try {
|
||||
Page<Refunds> refundPage = new Page<>(page, size);
|
||||
Page<Refunds> resultPage = refundsMapper.selectPage(refundPage, null);
|
||||
return resultPage.getRecords();
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "分页查询退款记录失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
package com.qf.backend.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.qf.backend.entity.RolePermissions;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.RolePermissionsMapper;
|
||||
import com.qf.backend.service.RolePermissionsService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 角色权限关联服务实现类
|
||||
*/
|
||||
@Service
|
||||
public class RolePermissionsServiceImpl extends ServiceImpl<RolePermissionsMapper, RolePermissions> implements RolePermissionsService {
|
||||
|
||||
@Autowired
|
||||
private RolePermissionsMapper rolePermissionsMapper;
|
||||
|
||||
@Override
|
||||
public List<RolePermissions> getRolePermissionsByRoleId(Long roleId) {
|
||||
if (ValidateUtil.isEmpty(roleId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "角色ID不能为空");
|
||||
}
|
||||
try {
|
||||
return rolePermissionsMapper.selectList(
|
||||
new QueryWrapper<RolePermissions>().eq("role_id", roleId));
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询角色权限关联失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<RolePermissions> getRolePermissionsByPermissionId(Long permissionId) {
|
||||
if (ValidateUtil.isEmpty(permissionId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "权限ID不能为空");
|
||||
}
|
||||
try {
|
||||
return rolePermissionsMapper.selectList(
|
||||
new QueryWrapper<RolePermissions>().eq("permission_id", permissionId));
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询角色权限关联失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addPermissionToRole(Long roleId, Long permissionId) {
|
||||
if (ValidateUtil.isEmpty(roleId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "角色ID不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(permissionId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "权限ID不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查是否已经存在该关联关系
|
||||
RolePermissions existing = rolePermissionsMapper.selectOne(
|
||||
new QueryWrapper<RolePermissions>()
|
||||
.eq("role_id", roleId)
|
||||
.eq("permission_id", permissionId));
|
||||
if (existing != null) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "该角色已拥有此权限");
|
||||
}
|
||||
RolePermissions rolePermission = new RolePermissions();
|
||||
rolePermission.setRoleId(roleId);
|
||||
rolePermission.setPermissionId(permissionId);
|
||||
int result = rolePermissionsMapper.insert(rolePermission);
|
||||
return result > 0;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "添加角色权限关联失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removePermissionFromRole(Long roleId, Long permissionId) {
|
||||
if (ValidateUtil.isEmpty(roleId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "角色ID不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(permissionId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "权限ID不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查关联关系是否存在
|
||||
RolePermissions existing = rolePermissionsMapper.selectOne(
|
||||
new QueryWrapper<RolePermissions>()
|
||||
.eq("role_id", roleId)
|
||||
.eq("permission_id", permissionId));
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "角色与权限的关联关系不存在");
|
||||
}
|
||||
int result = rolePermissionsMapper.delete(
|
||||
new QueryWrapper<RolePermissions>()
|
||||
.eq("role_id", roleId)
|
||||
.eq("permission_id", permissionId));
|
||||
return result > 0;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "移除角色权限关联失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean batchAddPermissionsToRole(Long roleId, List<Long> permissionIds) {
|
||||
if (ValidateUtil.isEmpty(roleId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "角色ID不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(permissionIds)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "权限ID列表不能为空");
|
||||
}
|
||||
if (permissionIds.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "权限ID列表不能为空");
|
||||
}
|
||||
try {
|
||||
for (Long permissionId : permissionIds) {
|
||||
if (ValidateUtil.isEmpty(permissionId)) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "权限ID列表中存在空值");
|
||||
}
|
||||
// 检查是否已经存在该关联关系
|
||||
RolePermissions existing = rolePermissionsMapper.selectOne(
|
||||
new QueryWrapper<RolePermissions>()
|
||||
.eq("role_id", roleId)
|
||||
.eq("permission_id", permissionId));
|
||||
if (existing == null) {
|
||||
RolePermissions rolePermission = new RolePermissions();
|
||||
rolePermission.setRoleId(roleId);
|
||||
rolePermission.setPermissionId(permissionId);
|
||||
rolePermissionsMapper.insert(rolePermission);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量添加角色权限关联失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean clearRolePermissions(Long roleId) {
|
||||
if (ValidateUtil.isEmpty(roleId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "角色ID不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查角色是否有权限关联
|
||||
List<RolePermissions> existingPermissions = rolePermissionsMapper.selectList(
|
||||
new QueryWrapper<RolePermissions>().eq("role_id", roleId));
|
||||
if (existingPermissions.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "该角色没有关联的权限");
|
||||
}
|
||||
int result = rolePermissionsMapper.delete(
|
||||
new QueryWrapper<RolePermissions>().eq("role_id", roleId));
|
||||
return result > 0;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "清除角色权限关联失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkRoleHasPermission(Long roleId, Long permissionId) {
|
||||
if (ValidateUtil.isEmpty(roleId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "角色ID不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(permissionId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "权限ID不能为空");
|
||||
}
|
||||
try {
|
||||
RolePermissions rolePermission = rolePermissionsMapper.selectOne(
|
||||
new QueryWrapper<RolePermissions>()
|
||||
.eq("role_id", roleId)
|
||||
.eq("permission_id", permissionId));
|
||||
return rolePermission != null;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "检查角色权限关系失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Long> listPermissionIdsByRoleId(Long roleId) {
|
||||
if (ValidateUtil.isEmpty(roleId)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "角色ID不能为空");
|
||||
}
|
||||
try {
|
||||
List<RolePermissions> rolePermissions = rolePermissionsMapper.selectList(
|
||||
new QueryWrapper<RolePermissions>().eq("role_id", roleId));
|
||||
List<Long> permissionIds = new ArrayList<>();
|
||||
for (RolePermissions rp : rolePermissions) {
|
||||
permissionIds.add(rp.getPermissionId());
|
||||
}
|
||||
return permissionIds;
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询角色权限ID列表失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,14 +7,22 @@ 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;
|
||||
|
||||
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.Roles;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.RolesMapper;
|
||||
import com.qf.backend.service.RolesService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -23,66 +31,224 @@ import com.qf.backend.service.RolesService;
|
||||
@Service
|
||||
public class RolesServiceImpl extends ServiceImpl<RolesMapper, Roles> implements RolesService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(RolesServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private RolesMapper rolesMapper;
|
||||
|
||||
// 根据角色名称查询角色
|
||||
@Override
|
||||
public Roles getRoleByName(String roleName) {
|
||||
public Result<Roles> getRoleByName(String roleName) {
|
||||
logger.info("根据角色名称查询角色: {}", roleName);
|
||||
|
||||
// 参数校验
|
||||
if (ValidateUtil.isEmpty(roleName)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "角色名称不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
if (roleName == null) {
|
||||
return null;
|
||||
Roles role = rolesMapper.selectOne(new QueryWrapper<Roles>().eq("role_name", roleName));
|
||||
if (role == null) {
|
||||
logger.warn("角色 {} 不存在", roleName);
|
||||
throw new BusinessException(ErrorCode.ROLE_NOT_FOUND, "角色不存在: " + roleName);
|
||||
}
|
||||
Roles roles = rolesMapper.selectOne(new QueryWrapper<Roles>().eq("role_name", roleName));
|
||||
if (roles == null) {
|
||||
return null;
|
||||
}
|
||||
return roles;
|
||||
return ResultUtils.success(role);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
logger.error("查询角色失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询角色失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 查询所有角色
|
||||
|
||||
@Override
|
||||
public List<Roles> listAllRoles() {
|
||||
|
||||
throw new UnsupportedOperationException("Unimplemented method 'listAllRoles'");
|
||||
public Result<Boolean> createRole(Roles roles) {
|
||||
logger.info("创建角色: {}", roles);
|
||||
|
||||
try {
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(roles, "roleName");
|
||||
|
||||
// 检查角色名称是否已存在
|
||||
Roles existingRole = rolesMapper.selectOne(new QueryWrapper<Roles>().eq("role_name", roles.getRoleName()));
|
||||
if (existingRole != null) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "角色名称已存在: " + roles.getRoleName());
|
||||
}
|
||||
|
||||
int result = rolesMapper.insert(roles);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "创建角色失败");
|
||||
}
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证角色信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证角色信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("创建角色失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建角色失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
// 根据ID查询角色
|
||||
|
||||
@Override
|
||||
public Roles getRoleById(Long id) {
|
||||
|
||||
throw new UnsupportedOperationException("Unimplemented method 'getRoleById'");
|
||||
public Result<Boolean> updateRole(Roles roles) {
|
||||
logger.info("更新角色: {}", roles);
|
||||
|
||||
try {
|
||||
// 检查角色ID是否为空
|
||||
if (roles == null || roles.getId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "角色信息或角色ID不能为空");
|
||||
}
|
||||
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(roles, "roleName");
|
||||
|
||||
// 检查角色是否存在
|
||||
Roles existingRole = rolesMapper.selectById(roles.getId());
|
||||
if (existingRole == null) {
|
||||
throw new BusinessException(ErrorCode.ROLE_NOT_FOUND, "角色不存在");
|
||||
}
|
||||
|
||||
// 检查角色名称是否与其他角色重复
|
||||
Roles nameConflictRole = rolesMapper.selectOne(
|
||||
new QueryWrapper<Roles>().eq("role_name", roles.getRoleName()).ne("id", roles.getId())
|
||||
);
|
||||
if (nameConflictRole != null) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "角色名称已存在: " + roles.getRoleName());
|
||||
}
|
||||
|
||||
rolesMapper.updateInfo(roles, new UpdateWrapper<Roles>().eq("id", roles.getId()));
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证角色信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证角色信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("更新角色失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新角色失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
// 根据用户ID查询角色
|
||||
|
||||
@Override
|
||||
public List<Roles> listRolesByUserId(Long userId) {
|
||||
|
||||
throw new UnsupportedOperationException("Unimplemented method 'listRolesByUserId'");
|
||||
public Result<Boolean> deleteRole(Long id) {
|
||||
logger.info("删除角色: {}", id);
|
||||
|
||||
// 参数校验
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "角色ID不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
// 检查角色是否存在
|
||||
Roles role = rolesMapper.selectById(id);
|
||||
if (role == null) {
|
||||
throw new BusinessException(ErrorCode.ROLE_NOT_FOUND, "角色不存在");
|
||||
}
|
||||
|
||||
// 删除角色
|
||||
int result = rolesMapper.deleteById(id);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "删除角色失败");
|
||||
}
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("删除角色失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除角色失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
// 创建角色
|
||||
|
||||
@Override
|
||||
public boolean createRole(Roles roles) {
|
||||
|
||||
throw new UnsupportedOperationException("Unimplemented method 'createRole'");
|
||||
public Result<List<Roles>> listAllRoles() {
|
||||
logger.info("查询所有角色");
|
||||
|
||||
try {
|
||||
List<Roles> rolesList = rolesMapper.selectList(null);
|
||||
return ResultUtils.success(rolesList);
|
||||
} catch (Exception e) {
|
||||
logger.error("查询所有角色失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询所有角色失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
// 更新角色
|
||||
|
||||
@Override
|
||||
public boolean updateRole(Roles roles) {
|
||||
|
||||
throw new UnsupportedOperationException("Unimplemented method 'updateRole'");
|
||||
public Result<Roles> getRoleById(Long id) {
|
||||
logger.info("根据ID查询角色: {}", id);
|
||||
|
||||
// 参数校验
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "角色ID不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
Roles role = rolesMapper.selectById(id);
|
||||
if (role == null) {
|
||||
throw new BusinessException(ErrorCode.ROLE_NOT_FOUND, "角色不存在");
|
||||
}
|
||||
return ResultUtils.success(role);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询角色失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询角色失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
// 批量删除角色
|
||||
|
||||
@Override
|
||||
public boolean batchDeleteRoles(List<Long> ids) {
|
||||
|
||||
throw new UnsupportedOperationException("Unimplemented method 'batchDeleteRoles'");
|
||||
public Result<Boolean> batchDeleteRoles(List<Long> ids) {
|
||||
logger.info("批量删除角色: {}", ids);
|
||||
|
||||
// 参数校验
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "角色ID列表不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
// 检查所有角色是否存在
|
||||
for (Long id : ids) {
|
||||
Roles role = rolesMapper.selectById(id);
|
||||
if (role == null) {
|
||||
throw new BusinessException(ErrorCode.ROLE_NOT_FOUND, "角色不存在: " + id);
|
||||
}
|
||||
}
|
||||
|
||||
int result = rolesMapper.deleteBatchIds(ids);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "批量删除角色失败");
|
||||
}
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("批量删除角色失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量删除角色失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
// 删除角色
|
||||
|
||||
@Override
|
||||
public boolean deleteRole(Long id) {
|
||||
|
||||
throw new UnsupportedOperationException("Unimplemented method 'deleteRole'");
|
||||
public Result<List<Roles>> listRolesByUserId(Long userId) {
|
||||
logger.info("查询用户角色: 用户ID = {}", userId);
|
||||
|
||||
// 参数校验
|
||||
if (userId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
List<Roles> rolesList = rolesMapper.selectList(new QueryWrapper<Roles>().inSql("id", "select role_id from user_roles where user_id = " + userId));
|
||||
return ResultUtils.success(rolesList);
|
||||
} catch (Exception e) {
|
||||
logger.error("查询用户角色失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询用户角色失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,282 @@
|
||||
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;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
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.ShopCategories;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.ShopCategoriesMapper;
|
||||
import com.qf.backend.service.ShopCategoriesService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
|
||||
@Service
|
||||
public class ShopCategoriesServiceImpl extends ServiceImpl<ShopCategoriesMapper, ShopCategories> implements ShopCategoriesService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ShopCategoriesServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private ShopCategoriesMapper shopCategoriesMapper;
|
||||
|
||||
@Override
|
||||
public Result<ShopCategories> getCategoryByName(String categoryName) {
|
||||
logger.info("根据分类名称查询分类: {}", categoryName);
|
||||
|
||||
try {
|
||||
if (ValidateUtil.isEmpty(categoryName)) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "分类名称不能为空");
|
||||
}
|
||||
|
||||
ShopCategories category = shopCategoriesMapper.selectOne(new QueryWrapper<ShopCategories>().eq("category_name", categoryName));
|
||||
if (category == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "分类不存在: " + categoryName);
|
||||
}
|
||||
return ResultUtils.success(category);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询分类失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询分类失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<ShopCategories>> getSubCategoriesByParentId(Long parentId) {
|
||||
logger.info("根据父分类ID查询子分类: {}", parentId);
|
||||
|
||||
try {
|
||||
if (parentId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "父分类ID不能为空");
|
||||
}
|
||||
|
||||
List<ShopCategories> subCategories = shopCategoriesMapper.selectList(new QueryWrapper<ShopCategories>().eq("parent_id", parentId));
|
||||
return ResultUtils.success(subCategories);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询子分类失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询子分类失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> createCategory(ShopCategories shopCategories) {
|
||||
logger.info("创建分类: 分类对象");
|
||||
|
||||
try {
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(shopCategories, "categoryName");
|
||||
|
||||
int result = shopCategoriesMapper.insert(shopCategories);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "创建分类失败");
|
||||
}
|
||||
|
||||
logger.info("分类创建成功");
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证分类信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证分类信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("创建分类失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建分类失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> updateCategory(ShopCategories shopCategories) {
|
||||
logger.info("更新分类信息: 分类ID = {}", shopCategories.getId());
|
||||
|
||||
try {
|
||||
if (shopCategories == null || shopCategories.getId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类信息或分类ID不能为空");
|
||||
}
|
||||
|
||||
// 检查分类是否存在
|
||||
ShopCategories existingCategory = getCategoryByIdAndCheckExist(shopCategories.getId());
|
||||
if (existingCategory == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "分类不存在");
|
||||
}
|
||||
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(shopCategories, "categoryName");
|
||||
|
||||
// 更新分类信息
|
||||
int result = shopCategoriesMapper.updateById(shopCategories);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "更新分类信息失败");
|
||||
}
|
||||
|
||||
logger.info("分类信息更新成功: 分类ID = {}", shopCategories.getId());
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证分类信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证分类信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("更新分类信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新分类信息失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> deleteCategory(Long id) {
|
||||
logger.info("删除分类: 分类ID = {}", id);
|
||||
|
||||
try {
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类ID不能为空");
|
||||
}
|
||||
// 检查分类是否存在
|
||||
ShopCategories category = getCategoryByIdAndCheckExist(id);
|
||||
if (category == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "分类不存在");
|
||||
}
|
||||
|
||||
// 检查是否有子分类
|
||||
List<ShopCategories> subCategories = shopCategoriesMapper.selectList(new QueryWrapper<ShopCategories>().eq("parent_id", id));
|
||||
if (subCategories != null && !subCategories.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "该分类下存在子分类,无法删除");
|
||||
}
|
||||
|
||||
int result = shopCategoriesMapper.deleteById(id);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "删除分类失败");
|
||||
}
|
||||
|
||||
logger.info("分类删除成功: 分类ID = {}", id);
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("删除分类失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除分类失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<ShopCategories>> listRootCategories() {
|
||||
logger.info("查询所有根分类");
|
||||
|
||||
try {
|
||||
// 查询父分类ID为0或null的分类
|
||||
List<ShopCategories> rootCategories = shopCategoriesMapper.selectList(new QueryWrapper<ShopCategories>().isNull("parent_id").or().eq("parent_id", 0));
|
||||
logger.info("查询到 {} 个根分类", rootCategories.size());
|
||||
return ResultUtils.success(rootCategories);
|
||||
} catch (Exception e) {
|
||||
logger.error("查询根分类失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询根分类失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<ShopCategories> getCategoryById(Long id) {
|
||||
logger.info("根据ID查询分类: 分类ID = {}", id);
|
||||
|
||||
try {
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类ID不能为空");
|
||||
}
|
||||
|
||||
ShopCategories category = getCategoryByIdAndCheckExist(id);
|
||||
if (category == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "分类不存在");
|
||||
}
|
||||
|
||||
return ResultUtils.success(category);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询分类失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询分类失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> batchDeleteCategories(List<Long> ids) {
|
||||
logger.info("批量删除分类: 分类ID列表 = {}", ids);
|
||||
|
||||
try {
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类ID列表不能为空");
|
||||
}
|
||||
|
||||
// 检查所有分类是否存在且没有子分类
|
||||
for (Long id : ids) {
|
||||
ShopCategories category = getCategoryByIdAndCheckExist(id);
|
||||
if (category == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "分类不存在: " + id);
|
||||
}
|
||||
|
||||
// 检查是否有子分类
|
||||
List<ShopCategories> subCategories = shopCategoriesMapper.selectList(new QueryWrapper<ShopCategories>().eq("parent_id", id));
|
||||
if (subCategories != null && !subCategories.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "分类 " + id + " 下存在子分类,无法删除");
|
||||
}
|
||||
}
|
||||
|
||||
int result = shopCategoriesMapper.deleteBatchIds(ids);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "批量删除分类失败");
|
||||
}
|
||||
|
||||
logger.info("批量删除分类成功: 删除了 {} 个分类", result);
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("批量删除分类失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量删除分类失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<ShopCategories>> listAllCategoriesWithTree() {
|
||||
logger.info("查询所有分类(树形结构)");
|
||||
|
||||
try {
|
||||
// 查询所有分类
|
||||
List<ShopCategories> allCategories = shopCategoriesMapper.selectList(null);
|
||||
|
||||
// TODO: 实现树形结构转换逻辑
|
||||
// 这里简单返回所有分类,实际项目中需要将分类转换为树形结构
|
||||
|
||||
logger.info("查询到 {} 个分类", allCategories.size());
|
||||
return ResultUtils.success(allCategories);
|
||||
} catch (Exception e) {
|
||||
logger.error("查询分类树形结构失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询分类树形结构失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据分类ID查询分类并检查是否存在
|
||||
* @param id 分类ID
|
||||
* @return 分类对象,如果分类不存在则返回null
|
||||
*/
|
||||
private ShopCategories getCategoryByIdAndCheckExist(Long id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
QueryWrapper<ShopCategories> queryWrapper = new QueryWrapper<ShopCategories>().eq("id", id);
|
||||
return shopCategoriesMapper.selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,373 @@
|
||||
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;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
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.ShopRatings;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.ShopRatingsMapper;
|
||||
import com.qf.backend.service.ShopRatingsService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
|
||||
@Service
|
||||
public class ShopRatingsServiceImpl extends ServiceImpl<ShopRatingsMapper, ShopRatings> implements ShopRatingsService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ShopRatingsServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private ShopRatingsMapper shopRatingsMapper;
|
||||
|
||||
// 根据店铺ID查询评分
|
||||
@Override
|
||||
public Result<List<ShopRatings>> getRatingsByShopId(Long shopId) {
|
||||
logger.info("根据店铺ID查询评分: {}", shopId);
|
||||
|
||||
try {
|
||||
if (shopId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "店铺ID不能为空");
|
||||
}
|
||||
|
||||
QueryWrapper<ShopRatings> queryWrapper = new QueryWrapper<ShopRatings>().eq("shop_id", shopId);
|
||||
List<ShopRatings> ratingsList = shopRatingsMapper.selectList(queryWrapper);
|
||||
logger.info("查询到 {} 个评分", ratingsList.size());
|
||||
return ResultUtils.success(ratingsList);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询评分失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询评分失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据用户ID查询评分
|
||||
@Override
|
||||
public Result<List<ShopRatings>> getRatingsByUserId(Long userId) {
|
||||
logger.info("根据用户ID查询评分: {}", userId);
|
||||
|
||||
try {
|
||||
if (userId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID不能为空");
|
||||
}
|
||||
|
||||
QueryWrapper<ShopRatings> queryWrapper = new QueryWrapper<ShopRatings>().eq("user_id", userId);
|
||||
List<ShopRatings> ratingsList = shopRatingsMapper.selectList(queryWrapper);
|
||||
logger.info("查询到 {} 个评分", ratingsList.size());
|
||||
return ResultUtils.success(ratingsList);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询评分失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询评分失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建评分
|
||||
@Override
|
||||
public Result<Boolean> createRating(ShopRatings shopRatings) {
|
||||
logger.info("创建评分: 评分对象");
|
||||
|
||||
try {
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(shopRatings, "shopId", "userId", "rating");
|
||||
|
||||
// 检查评分范围
|
||||
if (shopRatings.getRating() < 1 || shopRatings.getRating() > 5) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "评分必须在1-5之间");
|
||||
}
|
||||
|
||||
int result = shopRatingsMapper.insert(shopRatings);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "创建评分失败");
|
||||
}
|
||||
|
||||
logger.info("评分创建成功");
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证评分信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证评分信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("创建评分失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建评分失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新评分信息
|
||||
@Override
|
||||
public Result<Boolean> updateRating(ShopRatings shopRatings) {
|
||||
logger.info("更新评分信息: 评分ID = {}", shopRatings.getId());
|
||||
|
||||
try {
|
||||
if (shopRatings == null || shopRatings.getId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "评分信息或评分ID不能为空");
|
||||
}
|
||||
|
||||
// 检查评分是否存在
|
||||
ShopRatings existingRating = getRatingByIdAndCheckExist(shopRatings.getId());
|
||||
if (existingRating == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "评分不存在");
|
||||
}
|
||||
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(shopRatings, "shopId", "userId", "rating");
|
||||
|
||||
// 检查评分范围
|
||||
if (shopRatings.getRating() < 1 || shopRatings.getRating() > 5) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "评分必须在1-5之间");
|
||||
}
|
||||
|
||||
// 更新评分信息
|
||||
int result = shopRatingsMapper.updateById(shopRatings);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "更新评分信息失败");
|
||||
}
|
||||
|
||||
logger.info("评分信息更新成功: 评分ID = {}", shopRatings.getId());
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证评分信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证评分信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("更新评分信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新评分信息失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除评分
|
||||
@Override
|
||||
public Result<Boolean> deleteRating(Long id) {
|
||||
logger.info("删除评分: 评分ID = {}", id);
|
||||
|
||||
try {
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "评分ID不能为空");
|
||||
}
|
||||
// 检查评分是否存在
|
||||
ShopRatings rating = getRatingByIdAndCheckExist(id);
|
||||
if (rating == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "评分不存在");
|
||||
}
|
||||
|
||||
int result = shopRatingsMapper.deleteById(id);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "删除评分失败");
|
||||
}
|
||||
|
||||
logger.info("评分删除成功: 评分ID = {}", id);
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("删除评分失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除评分失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据评分ID查询评分
|
||||
@Override
|
||||
public Result<ShopRatings> getRatingById(Long id) {
|
||||
logger.info("根据ID查询评分: 评分ID = {}", id);
|
||||
|
||||
try {
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "评分ID不能为空");
|
||||
}
|
||||
|
||||
ShopRatings rating = getRatingByIdAndCheckExist(id);
|
||||
if (rating == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "评分不存在");
|
||||
}
|
||||
|
||||
return ResultUtils.success(rating);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询评分失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询评分失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取店铺平均评分
|
||||
@Override
|
||||
public Result<Double> getAverageRatingByShopId(Long shopId) {
|
||||
logger.info("获取店铺平均评分: 店铺ID = {}", shopId);
|
||||
|
||||
try {
|
||||
if (shopId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "店铺ID不能为空");
|
||||
}
|
||||
|
||||
// 使用QueryWrapper查询所有评分
|
||||
QueryWrapper<ShopRatings> queryWrapper = new QueryWrapper<ShopRatings>().eq("shop_id", shopId);
|
||||
List<ShopRatings> ratingsList = shopRatingsMapper.selectList(queryWrapper);
|
||||
|
||||
// 计算平均评分
|
||||
Double averageRating = 0.0;
|
||||
if (ratingsList != null && !ratingsList.isEmpty()) {
|
||||
int totalRating = 0;
|
||||
for (ShopRatings rating : ratingsList) {
|
||||
totalRating += rating.getRating();
|
||||
}
|
||||
averageRating = (double) totalRating / ratingsList.size();
|
||||
}
|
||||
|
||||
logger.info("店铺平均评分: {}", averageRating);
|
||||
return ResultUtils.success(averageRating);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("获取店铺平均评分失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "获取店铺平均评分失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取店铺评分数量
|
||||
@Override
|
||||
public Result<Integer> getRatingCountByShopId(Long shopId) {
|
||||
logger.info("获取店铺评分数量: 店铺ID = {}", shopId);
|
||||
|
||||
try {
|
||||
if (shopId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "店铺ID不能为空");
|
||||
}
|
||||
|
||||
// 使用QueryWrapper查询评分数量
|
||||
QueryWrapper<ShopRatings> queryWrapper = new QueryWrapper<ShopRatings>().eq("shop_id", shopId);
|
||||
Long ratingCount = shopRatingsMapper.selectCount(queryWrapper);
|
||||
|
||||
logger.info("店铺评分数量: {}", ratingCount);
|
||||
return ResultUtils.success(ratingCount != null ? ratingCount.intValue() : 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("获取店铺评分数量失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "获取店铺评分数量失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据评分星级查询店铺评分
|
||||
@Override
|
||||
public Result<List<ShopRatings>> getRatingsByShopIdAndRating(Long shopId, Integer rating) {
|
||||
logger.info("根据评分星级查询店铺评分: 店铺ID = {}, 评分 = {}", shopId, rating);
|
||||
|
||||
try {
|
||||
if (shopId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "店铺ID不能为空");
|
||||
}
|
||||
if (rating == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "评分不能为空");
|
||||
}
|
||||
|
||||
// 检查评分范围
|
||||
if (rating < 1 || rating > 5) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "评分必须在1-5之间");
|
||||
}
|
||||
|
||||
QueryWrapper<ShopRatings> queryWrapper = new QueryWrapper<ShopRatings>()
|
||||
.eq("shop_id", shopId)
|
||||
.eq("rating", rating);
|
||||
List<ShopRatings> ratingsList = shopRatingsMapper.selectList(queryWrapper);
|
||||
logger.info("查询到 {} 个评分", ratingsList.size());
|
||||
return ResultUtils.success(ratingsList);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("根据评分星级查询店铺评分失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "根据评分星级查询店铺评分失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 检查用户是否已对店铺评分
|
||||
@Override
|
||||
public Result<Boolean> checkUserHasRated(Long shopId, Long userId) {
|
||||
logger.info("检查用户是否已对店铺评分: 店铺ID = {}, 用户ID = {}", shopId, userId);
|
||||
|
||||
try {
|
||||
if (shopId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "店铺ID不能为空");
|
||||
}
|
||||
if (userId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID不能为空");
|
||||
}
|
||||
|
||||
QueryWrapper<ShopRatings> queryWrapper = new QueryWrapper<ShopRatings>()
|
||||
.eq("shop_id", shopId)
|
||||
.eq("user_id", userId);
|
||||
ShopRatings rating = shopRatingsMapper.selectOne(queryWrapper);
|
||||
boolean hasRated = rating != null;
|
||||
logger.info("用户是否已评分: {}", hasRated);
|
||||
return ResultUtils.success(hasRated);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("检查用户是否已对店铺评分失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "检查用户是否已对店铺评分失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 分页查询店铺评分
|
||||
@Override
|
||||
public Result<List<ShopRatings>> listRatingsByShopIdAndPage(Long shopId, int page, int size) {
|
||||
logger.info("分页查询店铺评分: 店铺ID = {}, 页码 = {}, 每页大小 = {}", shopId, page, size);
|
||||
|
||||
try {
|
||||
if (shopId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "店铺ID不能为空");
|
||||
}
|
||||
|
||||
// 参数校验
|
||||
if (page < 1) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "页码不能小于1");
|
||||
}
|
||||
if (size < 1 || size > 100) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "每页大小必须在1-100之间");
|
||||
}
|
||||
|
||||
// 使用MyBatis-Plus的分页功能
|
||||
Page<ShopRatings> ratingPage = new Page<>(page, size);
|
||||
QueryWrapper<ShopRatings> queryWrapper = new QueryWrapper<ShopRatings>().eq("shop_id", shopId);
|
||||
Page<ShopRatings> resultPage = shopRatingsMapper.selectPage(ratingPage, queryWrapper);
|
||||
|
||||
logger.info("分页查询成功: 共 {} 条记录, 第 {} 页", resultPage.getTotal(), page);
|
||||
return ResultUtils.success(resultPage.getRecords());
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("分页查询店铺评分失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "分页查询店铺评分失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据评分ID查询评分并检查是否存在
|
||||
* @param id 评分ID
|
||||
* @return 评分对象,如果评分不存在则返回null
|
||||
*/
|
||||
private ShopRatings getRatingByIdAndCheckExist(Long id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
QueryWrapper<ShopRatings> queryWrapper = new QueryWrapper<ShopRatings>().eq("id", id);
|
||||
return shopRatingsMapper.selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
}
|
||||
335
src/main/java/com/qf/backend/service/impl/ShopsServiceImpl.java
Normal file
335
src/main/java/com/qf/backend/service/impl/ShopsServiceImpl.java
Normal file
@@ -0,0 +1,335 @@
|
||||
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;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
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.Shops;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.ShopsMapper;
|
||||
import com.qf.backend.service.ShopsService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
|
||||
@Service
|
||||
public class ShopsServiceImpl extends ServiceImpl<ShopsMapper, Shops> implements ShopsService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ShopsServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private ShopsMapper shopsMapper;
|
||||
|
||||
// 根据店铺名称查询店铺
|
||||
@Override
|
||||
public Result<List<Shops>> getShopsByName(String shopName) {
|
||||
logger.info("根据店铺名称查询店铺: {}", shopName);
|
||||
|
||||
try {
|
||||
if (ValidateUtil.isEmpty(shopName)) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "店铺名称不能为空");
|
||||
}
|
||||
|
||||
QueryWrapper<Shops> queryWrapper = new QueryWrapper<Shops>().like("shop_name", shopName);
|
||||
List<Shops> shopsList = shopsMapper.selectList(queryWrapper);
|
||||
logger.info("查询到 {} 个店铺", shopsList.size());
|
||||
return ResultUtils.success(shopsList);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询店铺失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询店铺失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据用户ID查询店铺
|
||||
@Override
|
||||
public Result<Shops> getShopByUserId(Long userId) {
|
||||
logger.info("根据用户ID查询店铺: {}", userId);
|
||||
|
||||
try {
|
||||
if (userId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID不能为空");
|
||||
}
|
||||
|
||||
QueryWrapper<Shops> queryWrapper = new QueryWrapper<Shops>().eq("user_id", userId);
|
||||
Shops shop = shopsMapper.selectOne(queryWrapper);
|
||||
if (shop == null) {
|
||||
throw new BusinessException(ErrorCode.SHOP_NOT_FOUND, "店铺不存在: 用户ID = " + userId);
|
||||
}
|
||||
return ResultUtils.success(shop);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询店铺失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询店铺失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建店铺
|
||||
@Override
|
||||
public Result<Boolean> createShop(Shops shops) {
|
||||
logger.info("创建店铺: 店铺对象");
|
||||
|
||||
try {
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(shops, "shopName", "userId", "categoryId");
|
||||
|
||||
int result = shopsMapper.insert(shops);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "创建店铺失败");
|
||||
}
|
||||
|
||||
logger.info("店铺创建成功");
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证店铺信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证店铺信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("创建店铺失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建店铺失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新店铺信息
|
||||
@Override
|
||||
public Result<Boolean> updateShop(Shops shops) {
|
||||
logger.info("更新店铺信息: 店铺ID = {}", shops.getId());
|
||||
|
||||
try {
|
||||
if (shops == null || shops.getId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "店铺信息或店铺ID不能为空");
|
||||
}
|
||||
|
||||
// 检查店铺是否存在
|
||||
Shops existingShop = getShopByIdAndCheckExist(shops.getId());
|
||||
if (existingShop == null) {
|
||||
throw new BusinessException(ErrorCode.SHOP_NOT_FOUND, "店铺不存在");
|
||||
}
|
||||
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(shops, "shopName", "categoryId");
|
||||
|
||||
// 更新店铺信息
|
||||
int result = shopsMapper.updateById(shops);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "更新店铺信息失败");
|
||||
}
|
||||
|
||||
logger.info("店铺信息更新成功: 店铺ID = {}", shops.getId());
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证店铺信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证店铺信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("更新店铺信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新店铺信息失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除店铺
|
||||
@Override
|
||||
public Result<Boolean> deleteShop(Long id) {
|
||||
logger.info("删除店铺: 店铺ID = {}", id);
|
||||
|
||||
try {
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "店铺ID不能为空");
|
||||
}
|
||||
// 检查店铺是否存在
|
||||
Shops shop = getShopByIdAndCheckExist(id);
|
||||
if (shop == null) {
|
||||
throw new BusinessException(ErrorCode.SHOP_NOT_FOUND, "店铺不存在");
|
||||
}
|
||||
|
||||
int result = shopsMapper.deleteById(id);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "删除店铺失败");
|
||||
}
|
||||
|
||||
logger.info("店铺删除成功: 店铺ID = {}", id);
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("删除店铺失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除店铺失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据店铺ID查询店铺
|
||||
@Override
|
||||
public Result<Shops> getShopById(Long id) {
|
||||
logger.info("根据ID查询店铺: 店铺ID = {}", id);
|
||||
|
||||
try {
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "店铺ID不能为空");
|
||||
}
|
||||
|
||||
Shops shop = getShopByIdAndCheckExist(id);
|
||||
if (shop == null) {
|
||||
throw new BusinessException(ErrorCode.SHOP_NOT_FOUND, "店铺不存在");
|
||||
}
|
||||
|
||||
return ResultUtils.success(shop);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询店铺失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询店铺失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 分页查询店铺
|
||||
@Override
|
||||
public Result<List<Shops>> listShopsByPage(int page, int size) {
|
||||
logger.info("分页查询店铺: 页码 = {}, 每页大小 = {}", page, size);
|
||||
|
||||
try {
|
||||
// 参数校验
|
||||
if (page < 1) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "页码不能小于1");
|
||||
}
|
||||
if (size < 1 || size > 100) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "每页大小必须在1-100之间");
|
||||
}
|
||||
|
||||
// 使用MyBatis-Plus的分页功能
|
||||
Page<Shops> shopPage = new Page<>(page, size);
|
||||
Page<Shops> resultPage = shopsMapper.selectPage(shopPage, null);
|
||||
|
||||
logger.info("分页查询成功: 共 {} 条记录, 第 {} 页", resultPage.getTotal(), page);
|
||||
return ResultUtils.success(resultPage.getRecords());
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("分页查询店铺失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "分页查询店铺失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据店铺分类ID查询店铺
|
||||
@Override
|
||||
public Result<List<Shops>> getShopsByCategoryId(Long categoryId) {
|
||||
logger.info("根据店铺分类ID查询店铺: 分类ID = {}", categoryId);
|
||||
|
||||
try {
|
||||
if (categoryId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "分类ID不能为空");
|
||||
}
|
||||
|
||||
QueryWrapper<Shops> queryWrapper = new QueryWrapper<Shops>().eq("category_id", categoryId);
|
||||
List<Shops> shopsList = shopsMapper.selectList(queryWrapper);
|
||||
logger.info("查询到 {} 个店铺", shopsList.size());
|
||||
return ResultUtils.success(shopsList);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("根据分类ID查询店铺失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "根据分类ID查询店铺失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新店铺状态
|
||||
@Override
|
||||
public Result<Boolean> updateShopStatus(Long shopId, Integer status) {
|
||||
logger.info("更新店铺状态: 店铺ID = {}, 状态 = {}", shopId, status);
|
||||
|
||||
try {
|
||||
if (shopId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "店铺ID不能为空");
|
||||
}
|
||||
if (status == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "店铺状态不能为空");
|
||||
}
|
||||
|
||||
// 检查店铺是否存在
|
||||
Shops shop = getShopByIdAndCheckExist(shopId);
|
||||
if (shop == null) {
|
||||
throw new BusinessException(ErrorCode.SHOP_NOT_FOUND, "店铺不存在");
|
||||
}
|
||||
|
||||
// 更新店铺状态
|
||||
shop.setStatus(status);
|
||||
int result = shopsMapper.updateById(shop);
|
||||
if (result <= 0) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "更新店铺状态失败");
|
||||
}
|
||||
|
||||
logger.info("店铺状态更新成功: 店铺ID = {}, 新状态 = {}", shopId, status);
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("更新店铺状态失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新店铺状态失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 搜索店铺
|
||||
@Override
|
||||
public Result<List<Shops>> searchShops(String keyword, int page, int size) {
|
||||
logger.info("搜索店铺: 关键词 = {}, 页码 = {}, 每页大小 = {}", keyword, page, size);
|
||||
|
||||
try {
|
||||
if (ValidateUtil.isEmpty(keyword)) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "搜索关键词不能为空");
|
||||
}
|
||||
|
||||
// 参数校验
|
||||
if (page < 1) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "页码不能小于1");
|
||||
}
|
||||
if (size < 1 || size > 100) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "每页大小必须在1-100之间");
|
||||
}
|
||||
|
||||
// 使用MyBatis-Plus的分页功能
|
||||
Page<Shops> shopPage = new Page<>(page, size);
|
||||
QueryWrapper<Shops> queryWrapper = new QueryWrapper<Shops>()
|
||||
.like("shop_name", keyword)
|
||||
.or().like("description", keyword);
|
||||
Page<Shops> resultPage = shopsMapper.selectPage(shopPage, queryWrapper);
|
||||
|
||||
logger.info("搜索成功: 共 {} 条记录, 第 {} 页", resultPage.getTotal(), page);
|
||||
return ResultUtils.success(resultPage.getRecords());
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("搜索店铺失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "搜索店铺失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据店铺ID查询店铺并检查是否存在
|
||||
* @param id 店铺ID
|
||||
* @return 店铺对象,如果店铺不存在则返回null
|
||||
*/
|
||||
private Shops getShopByIdAndCheckExist(Long id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
QueryWrapper<Shops> queryWrapper = new QueryWrapper<Shops>().eq("id", id);
|
||||
return shopsMapper.selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package com.qf.backend.service.impl;
|
||||
|
||||
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.common.Result;
|
||||
import com.qf.backend.common.ResultUtils;
|
||||
import com.qf.backend.entity.UserDetails;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.UserDetailsMapper;
|
||||
import com.qf.backend.service.UserDetailsService;
|
||||
|
||||
/**
|
||||
* 用户详情服务实现类
|
||||
*/
|
||||
@Service
|
||||
public class UserDetailsServiceImpl extends ServiceImpl<UserDetailsMapper, UserDetails> implements UserDetailsService {
|
||||
|
||||
@Autowired
|
||||
private UserDetailsMapper userDetailsMapper;
|
||||
|
||||
@Override
|
||||
public Result<UserDetails> getUserDetailsByUserId(Long userId) {
|
||||
if (userId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID不能为空");
|
||||
}
|
||||
try {
|
||||
UserDetails userDetails = userDetailsMapper.selectOne(
|
||||
new QueryWrapper<UserDetails>().eq("user_id", userId));
|
||||
return ResultUtils.success(userDetails);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询用户详情失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> createUserDetails(UserDetails userDetails) {
|
||||
if (userDetails == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户详情信息不能为空");
|
||||
}
|
||||
if (userDetails.getUserId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查是否已存在该用户的详情
|
||||
UserDetails existing = userDetailsMapper.selectOne(
|
||||
new QueryWrapper<UserDetails>().eq("user_id", userDetails.getUserId()));
|
||||
if (existing != null) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "该用户已存在详情信息");
|
||||
}
|
||||
int result = userDetailsMapper.insert(userDetails);
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建用户详情失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> updateUserDetails(UserDetails userDetails) {
|
||||
if (userDetails == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户详情信息不能为空");
|
||||
}
|
||||
if (userDetails.getId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户详情ID不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查用户详情是否存在
|
||||
UserDetails existing = userDetailsMapper.selectById(userDetails.getId());
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "用户详情不存在");
|
||||
}
|
||||
int result = userDetailsMapper.updateById(userDetails);
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新用户详情失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> deleteUserDetailsByUserId(Long userId) {
|
||||
if (userId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查用户详情是否存在
|
||||
UserDetails existing = userDetailsMapper.selectOne(
|
||||
new QueryWrapper<UserDetails>().eq("user_id", userId));
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "用户详情不存在");
|
||||
}
|
||||
int result = userDetailsMapper.delete(
|
||||
new QueryWrapper<UserDetails>().eq("user_id", userId));
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除用户详情失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<UserDetails> getUserDetailsById(Long id) {
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户详情ID不能为空");
|
||||
}
|
||||
try {
|
||||
UserDetails userDetails = userDetailsMapper.selectById(id);
|
||||
if (userDetails == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, "用户详情不存在");
|
||||
}
|
||||
return ResultUtils.success(userDetails);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询用户详情失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public Result<Boolean> updateContactInfo(Long userId, String phone, String email) {
|
||||
// if (userId == null) {
|
||||
// throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID不能为空");
|
||||
// }
|
||||
// if (ValidateUtil.isEmpty(phone) && ValidateUtil.isEmpty(email)) {
|
||||
// throw new BusinessException(ErrorCode.MISSING_PARAM, "手机号和邮箱不能同时为空");
|
||||
// }
|
||||
// try {
|
||||
// // 检查用户详情是否存在
|
||||
// UserDetails existing = userDetailsMapper.selectOne(
|
||||
// new QueryWrapper<UserDetails>().eq("user_id", userId));
|
||||
// if (existing == null) {
|
||||
// throw new BusinessException(ErrorCode.NOT_FOUND, "用户详情不存在");
|
||||
// }
|
||||
// // 更新联系方式
|
||||
// if (phone != null) {
|
||||
// existing.setPhone(phone);
|
||||
// }
|
||||
// if (email != null) {
|
||||
// existing.setEmail(email);
|
||||
// }
|
||||
// int result = userDetailsMapper.updateById(existing);
|
||||
// return ResultUtils.success(result > 0);
|
||||
// } catch (BusinessException e) {
|
||||
// throw e;
|
||||
// } catch (Exception e) {
|
||||
// throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新用户联系方式失败", e);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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 org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.qf.backend.common.Result;
|
||||
import com.qf.backend.common.ResultUtils;
|
||||
import com.qf.backend.dto.LoginUser;
|
||||
import com.qf.backend.entity.Users;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.service.UserLoginService;
|
||||
import com.qf.backend.service.UsersService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 30803
|
||||
*/
|
||||
@Service
|
||||
public class UserLoginServiceImpl implements UserLoginService {
|
||||
private Logger logger = LoggerFactory.getLogger(UserLoginServiceImpl.class);
|
||||
@Autowired
|
||||
private UsersService usersServiceImpl;
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
* @param username 用户名
|
||||
* @param password 密码
|
||||
* @return 登录结果
|
||||
*/
|
||||
@Override
|
||||
public Result<LoginUser> login(String username, String password) {
|
||||
logger.info("用户登录,用户名:{}", username);
|
||||
// 1. 校验用户名和密码是否为空
|
||||
if (ValidateUtil.isEmpty(username) || ValidateUtil.isEmpty(password)) {
|
||||
return ResultUtils.fail(ErrorCode.PARAM_ERROR, "用户名或密码不能为空");
|
||||
}
|
||||
// 2. 校验用户名是否存在
|
||||
Users user = usersServiceImpl.getUserByUsername(username).getData();
|
||||
if (user == null) {
|
||||
return ResultUtils.fail(ErrorCode.USER_NOT_FOUND, "用户名不存在");
|
||||
}
|
||||
// 3. 校验密码是否正确
|
||||
// 加密密码
|
||||
String encryptedPassword = ValidateUtil.encryptPassword(password);
|
||||
if (!user.getPassword().equals(encryptedPassword)) {
|
||||
return ResultUtils.fail(ErrorCode.PASSWORD_ERROR, "密码错误");
|
||||
}
|
||||
// 4. 登录成功,返回登录用户信息
|
||||
LoginUser loginUser = new LoginUser();
|
||||
// loginUser.setUserId(user.getUserId());
|
||||
loginUser.setUsername(user.getUsername());
|
||||
// loginUser.setRoles(userRolesServiceImpl.findRolesByUserId(user.getUserId()));
|
||||
return ResultUtils.success(loginUser);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
/*
|
||||
* 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 com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
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.UserRoles;
|
||||
import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.UserRolesMapper;
|
||||
import com.qf.backend.service.UserRolesService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 30803
|
||||
*/
|
||||
public class UsersRolesServiceImpl extends ServiceImpl<UserRolesMapper, UserRoles> implements UserRolesService {
|
||||
@Autowired
|
||||
private UserRolesMapper userRolesMapper;
|
||||
|
||||
@Override
|
||||
public Result<List<UserRoles>> getUserRolesByUserId(Long userId) {
|
||||
if (userId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID不能为空");
|
||||
}
|
||||
try {
|
||||
List<UserRoles> userRoles = userRolesMapper.selectList(new QueryWrapper<UserRoles>().eq("user_id", userId));
|
||||
return ResultUtils.success(userRoles);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询用户角色信息失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<UserRoles>> getUserRolesByRoleId(Long roleId) {
|
||||
if (roleId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "角色ID不能为空");
|
||||
}
|
||||
try {
|
||||
List<UserRoles> userRoles = userRolesMapper.selectList(new QueryWrapper<UserRoles>().eq("role_id", roleId));
|
||||
return ResultUtils.success(userRoles);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询角色用户信息失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> addRoleToUser(Long userId, Long roleId) {
|
||||
if (userId == null || roleId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID和角色ID不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查是否已经存在该关联关系
|
||||
UserRoles existing = userRolesMapper.selectOne(new QueryWrapper<UserRoles>().eq("user_id", userId).eq("role_id", roleId));
|
||||
if (existing != null) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "用户已拥有该角色");
|
||||
}
|
||||
UserRoles userRoles = new UserRoles();
|
||||
userRoles.setUserId(userId);
|
||||
userRoles.setRoleId(roleId);
|
||||
int result = userRolesMapper.insert(userRoles);
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "添加用户角色关联失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> removeRoleFromUser(Long userId, Long roleId) {
|
||||
if (userId == null || roleId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID和角色ID不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查关联关系是否存在
|
||||
UserRoles existing = userRolesMapper.selectOne(new QueryWrapper<UserRoles>().eq("user_id", userId).eq("role_id", roleId));
|
||||
if (existing == null) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "用户与角色的关联关系不存在");
|
||||
}
|
||||
int result = userRolesMapper.delete(new QueryWrapper<UserRoles>().eq("user_id", userId).eq("role_id", roleId));
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "移除用户角色关联失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> batchAddRolesToUser(Long userId, List<Long> roleIds) {
|
||||
if (userId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID不能为空");
|
||||
}
|
||||
if (roleIds == null || roleIds.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "角色ID列表不能为空");
|
||||
}
|
||||
try {
|
||||
for (Long roleId : roleIds) {
|
||||
if (roleId == null) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "角色ID列表中存在空值");
|
||||
}
|
||||
// 检查是否已经存在该关联关系
|
||||
UserRoles existing = userRolesMapper.selectOne(new QueryWrapper<UserRoles>().eq("user_id", userId).eq("role_id", roleId));
|
||||
if (existing == null) {
|
||||
UserRoles userRoles = new UserRoles();
|
||||
userRoles.setUserId(userId);
|
||||
userRoles.setRoleId(roleId);
|
||||
userRolesMapper.insert(userRoles);
|
||||
}
|
||||
}
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "批量添加用户角色关联失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> clearUserRoles(Long userId) {
|
||||
if (userId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID不能为空");
|
||||
}
|
||||
try {
|
||||
// 检查用户是否有角色关联
|
||||
List<UserRoles> existingRoles = userRolesMapper.selectList(new QueryWrapper<UserRoles>().eq("user_id", userId));
|
||||
if (existingRoles.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "该用户没有关联的角色");
|
||||
}
|
||||
int result = userRolesMapper.delete(new QueryWrapper<UserRoles>().eq("user_id", userId));
|
||||
return ResultUtils.success(result > 0);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "清除用户角色关联失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> checkUserHasRole(Long userId, Long roleId) {
|
||||
if (userId == null || roleId == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID和角色ID不能为空");
|
||||
}
|
||||
try {
|
||||
// 修改selectInfo为selectOne,因为selectInfo方法可能不存在
|
||||
UserRoles userRoles = userRolesMapper.selectOne(new QueryWrapper<UserRoles>().eq("user_id", userId).eq("role_id", roleId));
|
||||
return ResultUtils.success(userRoles != null);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "检查用户角色关系失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,190 +2,303 @@ 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.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.plugins.pagination.Page;
|
||||
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.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.UsersMapper;
|
||||
import com.qf.backend.service.UsersService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
|
||||
@Service
|
||||
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(UsersServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private UsersMapper usersMapper;
|
||||
|
||||
// 根据用户名查询用户
|
||||
@Override
|
||||
public Result<Users> getUserByUsername(String username) {
|
||||
logger.info("根据用户名查询用户: {}", username);
|
||||
|
||||
try {
|
||||
if (username == null || username.isEmpty()) {
|
||||
return ResultUtils.fail(ErrorCode.INVALID_PARAM);
|
||||
if (ValidateUtil.isEmpty(username)) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "用户名不能为空");
|
||||
}
|
||||
|
||||
Users users = usersMapper.selectByUsername(username);
|
||||
if (users == null) {
|
||||
return ResultUtils.fail(ErrorCode.USER_NOT_FOUND);
|
||||
throw new BusinessException(ErrorCode.USER_NOT_FOUND, "用户不存在: " + username);
|
||||
}
|
||||
return ResultUtils.success(users);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
logger.error("查询用户失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询用户失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据邮箱查询用户
|
||||
@Override
|
||||
public Result<Users> getUserByEmail(String email) {
|
||||
logger.info("根据邮箱查询用户: {}", email);
|
||||
|
||||
try {
|
||||
if (email == null || email.isEmpty()) {
|
||||
return ResultUtils.fail(ErrorCode.INVALID_PARAM);
|
||||
if (ValidateUtil.isEmpty(email)) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "邮箱不能为空");
|
||||
}
|
||||
|
||||
// 使用ValidateUtil进行邮箱格式校验
|
||||
if (!ValidateUtil.isValidEmail(email)) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "邮箱格式不正确");
|
||||
}
|
||||
|
||||
Users users = usersMapper.selectByEmail(email);
|
||||
if (users == null) {
|
||||
return ResultUtils.fail(ErrorCode.USER_NOT_FOUND);
|
||||
throw new BusinessException(ErrorCode.USER_NOT_FOUND, "用户不存在: " + email);
|
||||
}
|
||||
return ResultUtils.success(users);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
logger.error("查询用户失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询用户失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
//创建用户
|
||||
@Override
|
||||
public Result<Boolean> createUser(Users users) {
|
||||
logger.info("创建用户: 用户对象");
|
||||
|
||||
try {
|
||||
// 调用封装的验证方法
|
||||
Result<Boolean> validationResult = validateUserBeforeCreate(users);
|
||||
if (validationResult != null) {
|
||||
return validationResult;
|
||||
}
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(users, "username", "email", "password");
|
||||
|
||||
// 加密密码
|
||||
users.setPassword(new BCryptPasswordEncoder().encode(users.getPassword()));
|
||||
|
||||
int result = usersMapper.insert(users);
|
||||
if (result <= 0) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "创建用户失败");
|
||||
}
|
||||
return ResultUtils.success();
|
||||
|
||||
logger.info("用户创建成功");
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证用户信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证用户信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
logger.error("创建用户失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "创建用户失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新用户信息
|
||||
@Override
|
||||
public Result<Boolean> updateUser(Users users) {
|
||||
|
||||
logger.info("更新用户信息: 用户ID = {}", users.getId());
|
||||
|
||||
try {
|
||||
// 调用封装的验证方法
|
||||
Result<Boolean> validationResult = validateUserBeforeCreate(users);
|
||||
if (validationResult != null) {
|
||||
return validationResult;
|
||||
if (users == null || users.getId() == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户信息或用户ID不能为空");
|
||||
}
|
||||
// 更新用户信息
|
||||
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()));
|
||||
|
||||
// 检查用户是否存在
|
||||
Users existingUser = getUserByIdAndCheckExist(users.getId());
|
||||
if (existingUser == null) {
|
||||
throw new BusinessException(ErrorCode.USER_NOT_FOUND, "用户不存在");
|
||||
}
|
||||
|
||||
// 使用ValidateUtil进行实体验证
|
||||
ValidateUtil.validateEntity(users, "username", "email");
|
||||
|
||||
// 更新用户信息,不包含密码更新
|
||||
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);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "更新用户信息失败");
|
||||
}
|
||||
return ResultUtils.success();
|
||||
|
||||
logger.info("用户信息更新成功: 用户ID = {}", users.getId());
|
||||
return ResultUtils.success(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 转换为业务异常
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, e.getMessage());
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("验证用户信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "验证用户信息失败: " + e.getMessage(), e);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
logger.error("更新用户信息失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新用户信息失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除用户
|
||||
@Override
|
||||
public Result<Boolean> deleteUser(Long id) {
|
||||
logger.info("删除用户: 用户ID = {}", id);
|
||||
|
||||
try {
|
||||
if (id == null) {
|
||||
return ResultUtils.fail(ErrorCode.MISSING_PARAM);
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID不能为空");
|
||||
}
|
||||
// 检查用户是否存在
|
||||
Users users = getUserByIdAndCheckExist(id);
|
||||
if (users == null) {
|
||||
return ResultUtils.fail(ErrorCode.USER_NOT_FOUND);
|
||||
throw new BusinessException(ErrorCode.USER_NOT_FOUND, "用户不存在");
|
||||
}
|
||||
|
||||
int result = usersMapper.deleteById(id);
|
||||
if (result <= 0) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "删除用户失败");
|
||||
}
|
||||
return ResultUtils.success();
|
||||
|
||||
logger.info("用户删除成功: 用户ID = {}", id);
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
logger.error("删除用户失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "删除用户失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 查询所有用户
|
||||
@Override
|
||||
public Result<List<Users>> listAllUsers() {
|
||||
logger.info("查询所有用户列表");
|
||||
|
||||
try {
|
||||
List<Users> usersList = usersMapper.selectList(null);
|
||||
if (usersList == null || usersList.isEmpty()) {
|
||||
return ResultUtils.fail(ErrorCode.NOT_FOUND);
|
||||
}
|
||||
logger.info("查询到 {} 个用户", usersList.size());
|
||||
return ResultUtils.success(usersList);
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
logger.error("查询用户列表失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询用户列表失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 分页查询用户
|
||||
@Override
|
||||
public Result<List<Users>> listUsersByPage(int page, int size) {
|
||||
logger.info("分页查询用户: 页码 = {}, 每页大小 = {}", page, size);
|
||||
|
||||
throw new UnsupportedOperationException("Unimplemented method 'listUsersByPage'");
|
||||
try {
|
||||
// 参数校验
|
||||
if (page < 1) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "页码不能小于1");
|
||||
}
|
||||
if (size < 1 || size > 100) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "每页大小必须在1-100之间");
|
||||
}
|
||||
|
||||
// 使用MyBatis-Plus的分页功能
|
||||
Page<Users> userPage = new Page<>(page, size);
|
||||
Page<Users> resultPage = usersMapper.selectPage(userPage, null);
|
||||
|
||||
logger.info("分页查询成功: 共 {} 条记录, 第 {} 页", resultPage.getTotal(), page);
|
||||
return ResultUtils.success(resultPage.getRecords());
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("分页查询用户失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "分页查询用户失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据ID查询用户
|
||||
@Override
|
||||
public Result<Users> getUserById(Long id) {
|
||||
logger.info("根据ID查询用户: 用户ID = {}", id);
|
||||
|
||||
try {
|
||||
if (id == null) {
|
||||
return ResultUtils.fail(ErrorCode.MISSING_PARAM);
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID不能为空");
|
||||
}
|
||||
|
||||
Users users = getUserByIdAndCheckExist(id);
|
||||
if (users == null) {
|
||||
return ResultUtils.fail(ErrorCode.USER_NOT_FOUND);
|
||||
throw new BusinessException(ErrorCode.USER_NOT_FOUND, "用户不存在");
|
||||
}
|
||||
|
||||
return ResultUtils.success(users);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
logger.error("查询用户失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询用户失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新用户密码
|
||||
@Override
|
||||
public Result<Boolean> updatePassword(Long id, String newPassword) {
|
||||
logger.info("更新用户密码: 用户ID = {}", id);
|
||||
|
||||
try {
|
||||
if (id == null || newPassword == null || newPassword.isEmpty()) {
|
||||
return ResultUtils.fail(ErrorCode.MISSING_PARAM);
|
||||
if (id == null) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "用户ID不能为空");
|
||||
}
|
||||
if (ValidateUtil.isEmpty(newPassword)) {
|
||||
throw new BusinessException(ErrorCode.MISSING_PARAM, "新密码不能为空");
|
||||
}
|
||||
|
||||
// 密码强度校验
|
||||
if (newPassword.length() < 6) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "密码长度不能少于6个字符");
|
||||
}
|
||||
|
||||
Users users = getUserByIdAndCheckExist(id);
|
||||
if (users == null) {
|
||||
return ResultUtils.fail(ErrorCode.USER_NOT_FOUND);
|
||||
throw new BusinessException(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));
|
||||
int result = usersMapper.updateInfo(users, new UpdateWrapper<Users>()
|
||||
.set("password", users.getPassword())
|
||||
.eq("id", id));
|
||||
|
||||
if (result <= 0) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
throw new BusinessException(ErrorCode.BUSINESS_ERROR, "更新密码失败");
|
||||
}
|
||||
return ResultUtils.success();
|
||||
|
||||
logger.info("用户密码更新成功: 用户ID = {}", id);
|
||||
return ResultUtils.success(true);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
return ResultUtils.fail(ErrorCode.DATABASE_ERROR);
|
||||
logger.error("更新用户密码失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "更新用户密码失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,43 +315,4 @@ public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements
|
||||
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