更新了商品类别

This commit is contained in:
chen-xin-zhi 2024-12-19 20:10:04 +08:00
parent f951a9b652
commit c5565b97ac
6 changed files with 107 additions and 19 deletions

View File

@ -18,12 +18,11 @@ import com.cultural.heritage.model.dto.order.OrderQueryRequest;
import com.cultural.heritage.model.dto.order.OrderUpdateRequest;
import com.cultural.heritage.model.dto.order.capital.OrderItemMainInfoAddRequest;
import com.cultural.heritage.model.dto.order.capital.OrderMainInfoAddRequest;
import com.cultural.heritage.model.entity.CartRecord;
import com.cultural.heritage.model.entity.Order;
import com.cultural.heritage.model.entity.OrderItems;
import com.cultural.heritage.model.entity.User;
import com.cultural.heritage.model.dto.snapshot.GoodSnapshot;
import com.cultural.heritage.model.entity.*;
import com.cultural.heritage.model.vo.order.OrderVO;
import com.cultural.heritage.service.good.CartRecordService;
import com.cultural.heritage.service.good.GoodService;
import com.cultural.heritage.service.order.OrderItemService;
import com.cultural.heritage.service.order.OrderService;
import com.cultural.heritage.service.user.UserService;
@ -39,6 +38,7 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -70,6 +70,10 @@ public class OrderController {
private CartRecordService cartRecordService;
@Resource
private GoodService goodService;
/**
* 用户通过购物车创建订单
@ -94,6 +98,7 @@ public class OrderController {
List<CartOrderItemAddRequest> cartOrderItemAddRequestList = cartOrderAddRequest.getCartOrderItemAddRequestList();
boolean isAccurate = cartRecordService.validIsConsistent(cartOrderItemAddRequestList);
ThrowUtils.throwIf(!isAccurate, ErrorCode.SYSTEM_ERROR, "当前购物车中的某些商品已下架或者库存不足,无法生成订单");
// 获取购物车id列表
List<Long> cartIds = cartOrderItemAddRequestList.stream().map(CartOrderItemAddRequest::getCartRecordId).toList();
// 根据id列表获取购物车商品信息
@ -101,6 +106,13 @@ public class OrderController {
queryWrapper.in("id", cartIds);
List<CartRecord> cartRecordList = cartRecordService.list(queryWrapper);
// 计算订单总金额
BigDecimal totalAmount = BigDecimal.ZERO;
for (CartRecord cartRecord : cartRecordList) {
totalAmount = totalAmount.add(cartRecord.getSubtotal());
}
orderMainInfoAddRequest.setTotalAmount(totalAmount);
// 封装成一个map集合购物车id, 购物车商品数量
Map<Long, Integer> map = new HashMap<>();
for (CartOrderItemAddRequest cartOrderItemAddRequest : cartOrderItemAddRequestList) {
@ -121,7 +133,7 @@ public class OrderController {
// 填充到订单主要信息请求体
orderMainInfoAddRequest.setOrderItemMainInfoAddRequestList(orderItemMainInfoAddRequestList);
Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId);
Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId, true, cartIds);
return ResultUtils.success(orderId);
}
@ -143,7 +155,7 @@ public class OrderController {
User loginUser = userService.getLoginUser(request);
Long userId = loginUser.getId();
// 创建通用订单(常规类和服务类商品)
Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId);
Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId, false, null);
return ResultUtils.success(orderId);
}
@ -283,11 +295,42 @@ public class OrderController {
order.setOrderStatus(OrderStatusConstant.TRANSACTION_CLOSED);
boolean update = orderService.updateById(order);
ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR, "订单状态更新失败");
// 恢复商品库存
QueryWrapper<OrderItems> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("orderId", id);
List<OrderItems> orderItemsList = orderItemService.list(queryWrapper);
// 获取商品id列表
List<Long> goodIds = orderItemsList.stream().map(orderItems -> {
GoodSnapshot goodSnapshot = orderItems.getGoodSnapshot();
return goodSnapshot.getId();
}).toList();
QueryWrapper<Good> goodQueryWrapper = new QueryWrapper<>();
goodQueryWrapper.in("id", goodIds);
// 获取商品列表
List<Good> goodList = goodService.list(goodQueryWrapper);
// 封装为map集合商品id, 库存量,用来存储购买的商品id对应的购买数量
Map<Long, Integer> map = new HashMap<>();
for (OrderItems orderItems : orderItemsList) {
GoodSnapshot goodSnapshot = orderItems.getGoodSnapshot();
Long goodId = goodSnapshot.getId();
Integer quantity = orderItems.getQuantity();
map.put(goodId, quantity);
}
// 批量更新商品库存
for (Good good : goodList) {
Long goodId = good.getId();
// 已经下单购买的数量
Integer quantity = map.get(goodId);
// 原有的库存
Integer inventory = good.getInventory();
good.setInventory(quantity + inventory);
}
boolean updateBatch = goodService.updateBatchById(goodList);
ThrowUtils.throwIf(!updateBatch, ErrorCode.SYSTEM_ERROR, "商品库存恢复失败");
return ResultUtils.success(true);
}
}

View File

@ -4,4 +4,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cultural.heritage.model.entity.Order;
public interface OrderMapper extends BaseMapper<Order> {
}

View File

@ -5,7 +5,6 @@ import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
@Data
@ -42,12 +41,6 @@ public class CartOrderAddRequest implements Serializable {
private Long couponId;
/**
* 订单总金额
*/
@Schema(description = "订单总金额", example = "500")
private BigDecimal totalAmount;
/**
* 订单备注

View File

@ -8,10 +8,17 @@ import java.io.Serializable;
import java.math.BigDecimal;
@Data
@Schema(description = "订单商品信息", requiredProperties = {"name", "type", "price", "goodImg", "festivalName", "reserveDate"})
@Schema(description = "订单商品信息", requiredProperties = {"id", "name", "type", "price", "goodImg", "festivalName", "reserveDate"})
public class GoodSnapshot implements Serializable {
/**
* 商品id
*/
@Schema(description = "商品id(id > 0)", example = "332")
private Long id;
/**
* 商品名
*/

View File

@ -7,6 +7,8 @@ import com.cultural.heritage.model.dto.order.OrderQueryRequest;
import com.cultural.heritage.model.dto.order.capital.OrderMainInfoAddRequest;
import com.cultural.heritage.model.entity.Order;
import java.util.List;
public interface OrderService extends IService<Order> {
@ -32,5 +34,5 @@ public interface OrderService extends IService<Order> {
/**
* 创建通用订单(常规类和服务类商品)
*/
Long createCommonOrder(OrderMainInfoAddRequest orderMainInfoAddRequest, Long userId);
Long createCommonOrder(OrderMainInfoAddRequest orderMainInfoAddRequest, Long userId, boolean isCartOrder, List<Long> cartIds);
}

View File

@ -18,9 +18,12 @@ import com.cultural.heritage.model.dto.snapshot.AddressSnapshot;
import com.cultural.heritage.model.dto.snapshot.ContactsSnapshot;
import com.cultural.heritage.model.dto.snapshot.CouponSnapshot;
import com.cultural.heritage.model.dto.snapshot.GoodSnapshot;
import com.cultural.heritage.model.entity.Good;
import com.cultural.heritage.model.entity.Order;
import com.cultural.heritage.model.entity.OrderItems;
import com.cultural.heritage.model.enums.GoodTypeEnum;
import com.cultural.heritage.service.good.CartRecordService;
import com.cultural.heritage.service.good.GoodService;
import com.cultural.heritage.service.order.OrderItemService;
import com.cultural.heritage.service.order.OrderService;
import com.cultural.heritage.utils.OrderNumberUtils;
@ -33,7 +36,9 @@ import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
@ -55,11 +60,18 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
private GoodMapper goodMapper;
@Resource
private GoodService goodService;
@Resource
private OrderItemService orderItemService;
@Resource
private CartRecordService cartRecordService;
/**
* 获取查询条件
*/
@ -191,7 +203,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
* 创建通用订单(常规类和服务类商品)
*/
@Override
public Long createCommonOrder(OrderMainInfoAddRequest orderMainInfoAddRequest, Long userId) {
public Long createCommonOrder(OrderMainInfoAddRequest orderMainInfoAddRequest, Long userId, boolean isCartOrder, List<Long> cartIds) {
// 封装订单详细信息请求体
OrderAddRequest orderAddRequest = this.initOrderMainInfo(orderMainInfoAddRequest);
if (orderAddRequest == null) {
@ -223,6 +235,33 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
}).toList();
boolean save = orderItemService.saveBatch(newOrderItemsList);
ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);
// 扣减商品库存
List<OrderItemMainInfoAddRequest> orderItemMainInfoAddRequestList = orderMainInfoAddRequest.getOrderItemMainInfoAddRequestList();
// 封装map集合商品id, 购买数量
Map<Long, Integer> map = new HashMap<>();
for (OrderItemMainInfoAddRequest orderItemMainInfoAddRequest : orderItemMainInfoAddRequestList) {
Long goodId = orderItemMainInfoAddRequest.getGoodId();
Integer quantity = orderItemMainInfoAddRequest.getQuantity();
map.put(goodId, quantity);
}
// 批量修改商品库存信息
List<Long> goodIds = orderItemMainInfoAddRequestList.stream().map(OrderItemMainInfoAddRequest::getGoodId).toList();
QueryWrapper<Good> queryWrapper = new QueryWrapper<>();
queryWrapper.in("id", goodIds);
List<Good> goodList = goodService.list(queryWrapper);
for (Good good : goodList) {
Integer inventory = map.get(good.getId());
good.setInventory(good.getInventory() - inventory);
}
boolean update = goodService.updateBatchById(goodList);
ThrowUtils.throwIf(!update, ErrorCode.SYSTEM_ERROR, "商品库存更新失败");
// 清空购物车
if (isCartOrder) {
boolean remove = cartRecordService.removeBatchByIds(cartIds);
ThrowUtils.throwIf(!remove, ErrorCode.SYSTEM_ERROR, "清空购物车失败");
}
return id;
}