From c5565b97ac2ac644466135db02aa80faf02b4027 Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Thu, 19 Dec 2024 20:10:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E5=95=86=E5=93=81?= =?UTF-8?q?=E7=B1=BB=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/order/OrderController.java | 61 ++++++++++++++++--- .../cultural/heritage/mapper/OrderMapper.java | 4 ++ .../model/dto/cart/CartOrderAddRequest.java | 7 --- .../model/dto/snapshot/GoodSnapshot.java | 9 ++- .../heritage/service/order/OrderService.java | 4 +- .../service/order/impl/OrderServiceImpl.java | 41 ++++++++++++- 6 files changed, 107 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/cultural/heritage/controller/order/OrderController.java b/src/main/java/com/cultural/heritage/controller/order/OrderController.java index 6c35992..20a1719 100644 --- a/src/main/java/com/cultural/heritage/controller/order/OrderController.java +++ b/src/main/java/com/cultural/heritage/controller/order/OrderController.java @@ -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 cartOrderItemAddRequestList = cartOrderAddRequest.getCartOrderItemAddRequestList(); boolean isAccurate = cartRecordService.validIsConsistent(cartOrderItemAddRequestList); ThrowUtils.throwIf(!isAccurate, ErrorCode.SYSTEM_ERROR, "当前购物车中的某些商品已下架或者库存不足,无法生成订单"); + // 获取购物车id列表 List cartIds = cartOrderItemAddRequestList.stream().map(CartOrderItemAddRequest::getCartRecordId).toList(); // 根据id列表获取购物车商品信息 @@ -101,6 +106,13 @@ public class OrderController { queryWrapper.in("id", cartIds); List cartRecordList = cartRecordService.list(queryWrapper); + // 计算订单总金额 + BigDecimal totalAmount = BigDecimal.ZERO; + for (CartRecord cartRecord : cartRecordList) { + totalAmount = totalAmount.add(cartRecord.getSubtotal()); + } + orderMainInfoAddRequest.setTotalAmount(totalAmount); + // 封装成一个map集合(键:购物车id, 值:购物车商品数量) Map 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 queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("orderId", id); + List orderItemsList = orderItemService.list(queryWrapper); + // 获取商品id列表 + List goodIds = orderItemsList.stream().map(orderItems -> { + GoodSnapshot goodSnapshot = orderItems.getGoodSnapshot(); + return goodSnapshot.getId(); + }).toList(); + QueryWrapper goodQueryWrapper = new QueryWrapper<>(); + goodQueryWrapper.in("id", goodIds); + // 获取商品列表 + List goodList = goodService.list(goodQueryWrapper); + // 封装为map集合(键:商品id, 值:库存量),用来存储购买的商品id对应的购买数量 + Map 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); } - - - } diff --git a/src/main/java/com/cultural/heritage/mapper/OrderMapper.java b/src/main/java/com/cultural/heritage/mapper/OrderMapper.java index b11fa30..b18a96b 100644 --- a/src/main/java/com/cultural/heritage/mapper/OrderMapper.java +++ b/src/main/java/com/cultural/heritage/mapper/OrderMapper.java @@ -4,4 +4,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.cultural.heritage.model.entity.Order; public interface OrderMapper extends BaseMapper { + + + + } diff --git a/src/main/java/com/cultural/heritage/model/dto/cart/CartOrderAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/cart/CartOrderAddRequest.java index f4933cc..3e97d7a 100644 --- a/src/main/java/com/cultural/heritage/model/dto/cart/CartOrderAddRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/cart/CartOrderAddRequest.java @@ -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; - /** * 订单备注 diff --git a/src/main/java/com/cultural/heritage/model/dto/snapshot/GoodSnapshot.java b/src/main/java/com/cultural/heritage/model/dto/snapshot/GoodSnapshot.java index a5b25b7..25a75f1 100644 --- a/src/main/java/com/cultural/heritage/model/dto/snapshot/GoodSnapshot.java +++ b/src/main/java/com/cultural/heritage/model/dto/snapshot/GoodSnapshot.java @@ -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; + + /** * 商品名 */ diff --git a/src/main/java/com/cultural/heritage/service/order/OrderService.java b/src/main/java/com/cultural/heritage/service/order/OrderService.java index 93e9556..921d4c5 100644 --- a/src/main/java/com/cultural/heritage/service/order/OrderService.java +++ b/src/main/java/com/cultural/heritage/service/order/OrderService.java @@ -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 { @@ -32,5 +34,5 @@ public interface OrderService extends IService { /** * 创建通用订单(常规类和服务类商品) */ - Long createCommonOrder(OrderMainInfoAddRequest orderMainInfoAddRequest, Long userId); + Long createCommonOrder(OrderMainInfoAddRequest orderMainInfoAddRequest, Long userId, boolean isCartOrder, List cartIds); } diff --git a/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java b/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java index 9fdd00b..f7bad09 100644 --- a/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java @@ -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 implements OrderService { @@ -55,11 +60,18 @@ public class OrderServiceImpl extends ServiceImpl 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 implements * 创建通用订单(常规类和服务类商品) */ @Override - public Long createCommonOrder(OrderMainInfoAddRequest orderMainInfoAddRequest, Long userId) { + public Long createCommonOrder(OrderMainInfoAddRequest orderMainInfoAddRequest, Long userId, boolean isCartOrder, List cartIds) { // 封装订单详细信息请求体 OrderAddRequest orderAddRequest = this.initOrderMainInfo(orderMainInfoAddRequest); if (orderAddRequest == null) { @@ -223,6 +235,33 @@ public class OrderServiceImpl extends ServiceImpl implements }).toList(); boolean save = orderItemService.saveBatch(newOrderItemsList); ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR); + + // 扣减商品库存 + List orderItemMainInfoAddRequestList = orderMainInfoAddRequest.getOrderItemMainInfoAddRequestList(); + // 封装map集合(键:商品id, 值:购买数量) + Map map = new HashMap<>(); + for (OrderItemMainInfoAddRequest orderItemMainInfoAddRequest : orderItemMainInfoAddRequestList) { + Long goodId = orderItemMainInfoAddRequest.getGoodId(); + Integer quantity = orderItemMainInfoAddRequest.getQuantity(); + map.put(goodId, quantity); + } + // 批量修改商品库存信息 + List goodIds = orderItemMainInfoAddRequestList.stream().map(OrderItemMainInfoAddRequest::getGoodId).toList(); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.in("id", goodIds); + List 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; }