更新了商品管理模块

This commit is contained in:
chen-xin-zhi 2024-12-17 15:36:50 +08:00
parent d95a82c2a9
commit c008edf059
11 changed files with 85 additions and 80 deletions

View File

@ -14,7 +14,7 @@ import com.cultural.heritage.model.entity.User;
import com.cultural.heritage.model.vo.cart.CartOrderVO;
import com.cultural.heritage.model.vo.cart.CartRecordQueryVO;
import com.cultural.heritage.model.vo.cart.CartRecordVO;
import com.cultural.heritage.model.vo.good.GoodVO;
import com.cultural.heritage.model.vo.cart.CartGoodVO;
import com.cultural.heritage.service.good.CartRecordService;
import com.cultural.heritage.service.good.GoodService;
import com.cultural.heritage.service.user.UserService;
@ -61,8 +61,9 @@ public class CartRecordController {
if (cartRecordAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
boolean result = cartRecordService.dealAlreadyExistGood(cartRecordAddRequest, request);
return ResultUtils.success(result);
// 更新购物车的商品属性
cartRecordService.updateCartGoodAttribute(cartRecordAddRequest, request);
return ResultUtils.success(true);
}
@ -112,8 +113,8 @@ public class CartRecordController {
/**
* 展示用户购物车中的商品
* @return
* 小程序端用户查询购物车中的商品
* @return 商品列表信息
*/
@PostMapping("/list")
@Operation(summary = "小程序端用户查询购物车中的商品", description = "参数权限所有人方法名listUserCartRecord")
@ -129,6 +130,7 @@ public class CartRecordController {
}
/**
* 展示用户批量购买后的订单信息
* @param cartRecordQueryVOList 商品id和数量的集合
@ -145,9 +147,9 @@ public class CartRecordController {
Long goodId = cartRecordQueryVO.getGoodId();
Integer quantity = cartRecordQueryVO.getQuantity();
Good good = goodService.getById(goodId);
ThrowUtils.throwIf(good == null, ErrorCode.OPERATION_ERROR, "找不到该商品");
ThrowUtils.throwIf(good == null || good.getIsShelves() == 0, ErrorCode.OPERATION_ERROR, "找不到该商品或者该商品已下架");
CartOrderVO cartOrderVO = new CartOrderVO();
GoodVO goodVO = new GoodVO();
CartGoodVO goodVO = new CartGoodVO();
BeanUtils.copyProperties(good, goodVO);
cartOrderVO.setGoodVO(goodVO);

View File

@ -176,16 +176,40 @@ public class CategoryController {
}
/**
* Web端用户查询商品类别
* @return 商品类别列表
*/
@PostMapping("/list/web")
@Operation(summary = "Web端用户查询商品类别", description = "参数权限所有人方法名listCategoryOnWeb")
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<List<Category>> listCategoryOnWeb(HttpServletRequest request) {
userService.getLoginUser(request);
List<Category> list = categoryService.list();
return ResultUtils.success(list);
}
/**
* 查询所有商品类别
* @return 商品类别列表
*/
@PostMapping("/list")
@Operation(summary = "(小程序端和Web端)用户查询商品类别", description = "参数权限所有人方法名listCategory")
@Operation(summary = "小程序端用户查询商品类别", description = "参数权限所有人方法名listCategory")
public BaseResponse<List<Category>> listCategory(HttpServletRequest request) {
userService.getLoginUser(request);
List<Category> list = categoryService.list();
return ResultUtils.success(list);
List<Category> categoryList = list.stream().filter(category -> {
String typeName = category.getTypeName();
QueryWrapper<Good> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("type", typeName);
long count = goodService.count(queryWrapper);
return count != 0;
}).toList();
return ResultUtils.success(categoryList);
}

View File

@ -32,7 +32,10 @@ import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@ -220,4 +223,31 @@ public class OrderController {
}
/**
* 小程序端用户取消订单
* @param commonRequest 订单id
* @return 是否取消成功
*/
@PostMapping ("/cancel/id")
@Operation(summary = "小程序端用户取消订单", description = "参数订单id权限所有人方法名cancelOrderById")
public BaseResponse<Boolean> cancelOrderById(@RequestBody CommonRequest commonRequest, HttpServletRequest request) {
if (commonRequest == null || commonRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Long id = commonRequest.getId();
userService.getLoginUser(request);
Order order = orderService.getById(id);
ThrowUtils.throwIf(order == null, ErrorCode.OPERATION_ERROR, "订单不存在");
order.setOrderStatus(OrderStatusConstant.TRANSACTION_CLOSED);
boolean update = orderService.updateById(order);
ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR, "订单状态更新失败");
return ResultUtils.success(true);
}
}

View File

@ -8,7 +8,7 @@ import java.io.Serializable;
import java.math.BigDecimal;
@Data
@Schema(description = "购物车记录添加请求体", requiredProperties = {"userId", "goodId", "quantity", "subtotal", "isGoodType"})
@Schema(description = "购物车记录添加请求体", requiredProperties = {"userId", "goodId", "quantity", "subtotal"})
public class CartRecordAddRequest implements Serializable {
/**
@ -29,12 +29,6 @@ public class CartRecordAddRequest implements Serializable {
@Schema(description = "小计(商品单价 * 数量)", example = "60")
private BigDecimal subtotal;
/**
* 是否是常规类商品
*/
@Schema(description = "是否是常规类商品", example = "1")
private Integer isGoodType;
@Serial

View File

@ -14,13 +14,6 @@ import java.util.List;
public class OrderMainInfoAddRequest implements Serializable {
// /**
// * 用户id
// */
// @Schema(description = "用户id(id > 0)", example = "2")
// private Long userId;
/**
* 订单类别
*/
@ -34,13 +27,6 @@ public class OrderMainInfoAddRequest implements Serializable {
@Schema(description = "用户昵称", example = "Hello")
private String userName;
//
// /**
// * 订单编号
// */
// @Schema(description = "订单编号", example = "1432442845453453")
// private String orderNumber;
/**
* 地址id
@ -69,11 +55,6 @@ public class OrderMainInfoAddRequest implements Serializable {
@Schema(description = "订单总金额", example = "560")
private BigDecimal totalAmount;
// /**
// * 订单状态
// */
// @Schema(description = "订单状态", example = "已支付")
// private String orderStatus;
/**
* 订单备注

View File

@ -11,7 +11,7 @@ import java.math.BigDecimal;
import java.util.Date;
/**
* 购物车添加记录
* 常规类购物车添加商品
* @TableName cart_record
*/
@Data
@ -57,9 +57,10 @@ public class CartRecord implements Serializable {
private Date updateTime;
/**
* 是否为商品类型
* 是否删除
*/
private Integer isGoodType;
private Integer isDelete;
@Serial

View File

@ -1,4 +1,4 @@
package com.cultural.heritage.model.vo.good;
package com.cultural.heritage.model.vo.cart;
import lombok.Data;
@ -7,7 +7,7 @@ import java.io.Serializable;
import java.math.BigDecimal;
@Data
public class GoodVO implements Serializable {
public class CartGoodVO implements Serializable {
/**
* 商品编号
@ -41,17 +41,6 @@ public class GoodVO implements Serializable {
private Integer inventory;
/**
* 商品标签
*/
private String label;
/**
* 是否是常规类商品
*/
private Integer isGoodType;
@Serial
private static final long serialVersionUID = 1L;

View File

@ -1,6 +1,5 @@
package com.cultural.heritage.model.vo.cart;
import com.cultural.heritage.model.vo.good.GoodVO;
import lombok.Data;
import java.io.Serial;
@ -13,7 +12,7 @@ public class CartOrderVO implements Serializable {
/**
* 商品详细信息
*/
private GoodVO goodVO;
private CartGoodVO goodVO;
/**

View File

@ -1,6 +1,5 @@
package com.cultural.heritage.model.vo.cart;
import com.cultural.heritage.model.vo.good.GoodVO;
import lombok.Data;
import java.io.Serial;
@ -16,11 +15,6 @@ public class CartRecordVO implements Serializable {
*/
private Long id;
/**
* 用户id
*/
private Long userId;
/**
* 商品id
*/
@ -40,13 +34,7 @@ public class CartRecordVO implements Serializable {
/**
* 购物车商品详情信息
*/
private GoodVO goodVO;
/**
* 是否是常规类商品
*/
private Integer isGoodType;
private CartGoodVO cartGoodVO;
@Serial

View File

@ -11,9 +11,9 @@ import java.util.List;
public interface CartRecordService extends IService<CartRecord> {
/**
* 处理用户购物车已存在该商品
* 更新购物车的商品属性
*/
boolean dealAlreadyExistGood(CartRecordAddRequest cartRecordAddRequest, HttpServletRequest request);
boolean updateCartGoodAttribute(CartRecordAddRequest cartRecordAddRequest, HttpServletRequest request);
/**

View File

@ -12,7 +12,7 @@ import com.cultural.heritage.model.entity.CartRecord;
import com.cultural.heritage.model.entity.Good;
import com.cultural.heritage.model.entity.User;
import com.cultural.heritage.model.vo.cart.CartRecordVO;
import com.cultural.heritage.model.vo.good.GoodVO;
import com.cultural.heritage.model.vo.cart.CartGoodVO;
import com.cultural.heritage.service.good.CartRecordService;
import com.cultural.heritage.service.good.GoodService;
import com.cultural.heritage.service.user.UserService;
@ -41,10 +41,10 @@ public class CartRecordServiceImpl extends ServiceImpl<CartRecordMapper, CartRec
private UserService userService;
/**
* 处理用户购物车已存在该商品
* 更新购物车的商品属性
*/
@Override
public boolean dealAlreadyExistGood(CartRecordAddRequest cartRecordAddRequest, HttpServletRequest request) {
public boolean updateCartGoodAttribute(CartRecordAddRequest cartRecordAddRequest, HttpServletRequest request) {
User loginUser = userService.getLoginUser(request);
Long userId = loginUser.getId();
Long goodId = cartRecordAddRequest.getGoodId();
@ -53,11 +53,13 @@ public class CartRecordServiceImpl extends ServiceImpl<CartRecordMapper, CartRec
queryWrapper.eq("goodId", goodId);
CartRecord cartRecord = this.baseMapper.selectOne(queryWrapper);
if (cartRecord != null) {
// 如果购物车中已存在该商品, 就叠加数量
cartRecord.setQuantity(cartRecord.getQuantity() + cartRecordAddRequest.getQuantity());
cartRecord.setSubtotal(cartRecord.getSubtotal().add(cartRecordAddRequest.getSubtotal()));
boolean result = this.updateById(cartRecord);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "更新购物车商品数量失败");
} else {
// 如果购物车不存在该商品就向购物车中添加一项
CartRecord cartGood = new CartRecord();
BeanUtils.copyProperties(cartRecordAddRequest, cartGood);
cartGood.setUserId(userId);
@ -88,9 +90,9 @@ public class CartRecordServiceImpl extends ServiceImpl<CartRecordMapper, CartRec
QueryWrapper<Good> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id", goodId);
Good good = goodMapper.selectOne(queryWrapper);
GoodVO goodVO = new GoodVO();
CartGoodVO goodVO = new CartGoodVO();
BeanUtils.copyProperties(good, goodVO);
cartRecordVO.setGoodVO(goodVO);
cartRecordVO.setCartGoodVO(goodVO);
return cartRecordVO;
}).toList();
}
@ -106,7 +108,6 @@ public class CartRecordServiceImpl extends ServiceImpl<CartRecordMapper, CartRec
Long goodId = cartRecord.getGoodId();
Integer quantity = cartRecord.getQuantity();
BigDecimal subtotal = cartRecord.getSubtotal();
Integer isGoodType = cartRecord.getIsGoodType();
if (update) {
if (userId == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
@ -122,9 +123,5 @@ public class CartRecordServiceImpl extends ServiceImpl<CartRecordMapper, CartRec
if (ObjectUtils.isEmpty(subtotal) || subtotal.compareTo(BigDecimal.ZERO) <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "价格错误");
}
if (ObjectUtils.isEmpty(isGoodType) || isGoodType != 1 && isGoodType != 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "请传递正确的商品类别");
}
}
}