diff --git a/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java b/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java index a6384a1..479b87d 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java @@ -31,10 +31,7 @@ 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; -import java.util.Map; +import java.util.*; @RestController @RequestMapping("/cart") @@ -106,6 +103,7 @@ public class CartRecordController { } + /** * 用户删除购物车的物品 * @param idList 删除的商品id列表 @@ -124,6 +122,7 @@ public class CartRecordController { } + /** * 小程序端用户查询购物车中的商品 * @return 商品列表信息 @@ -136,6 +135,7 @@ public class CartRecordController { Long userId = loginUser.getId(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("userId", userId); + queryWrapper.orderByDesc("id"); List list = cartRecordService.list(queryWrapper); // 封装用户购物车中的商品信息 List cartRecordVOList = cartRecordService.transformToCartRecordVOList(list); @@ -215,6 +215,7 @@ public class CartRecordController { cartOrderVO.setQuantity(quantity); cartOrderVOList.add(cartOrderVO); } + Collections.reverse(cartOrderVOList); return ResultUtils.success(cartOrderVOList); } diff --git a/src/main/java/com/cultural/heritage/controller/good/CategoryController.java b/src/main/java/com/cultural/heritage/controller/good/CategoryController.java index 3fac68d..9f97b98 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CategoryController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CategoryController.java @@ -233,7 +233,10 @@ public class CategoryController { if (StringUtils.isBlank(typeName)) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } - List goodList = goodService.getGoodListByTypeName(typeName); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("type", typeName); + queryWrapper.orderByDesc("id"); + List goodList = goodService.list(queryWrapper); Map> map = new HashMap<>(); map.put(id, goodList); return ResultUtils.success(map); 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 1deeb98..910cc63 100644 --- a/src/main/java/com/cultural/heritage/controller/order/OrderController.java +++ b/src/main/java/com/cultural/heritage/controller/order/OrderController.java @@ -39,10 +39,7 @@ 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; -import java.util.Map; +import java.util.*; @RestController @RequestMapping("/order") @@ -106,13 +103,15 @@ 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()); + List goodIds = cartRecordList.stream().map(CartRecord::getGoodId).toList(); + QueryWrapper goodQueryWrapper = new QueryWrapper<>(); + goodQueryWrapper.in("id", goodIds); + List goodList = goodService.list(goodQueryWrapper); + // 封装map集合(键:商品id, 值:商品信息) + Map goodMap = new HashMap<>(); + for (Good good : goodList) { + goodMap.put(good.getId(), good); } - orderMainInfoAddRequest.setTotalAmount(totalAmount); - // 封装成一个map集合(键:购物车id, 值:购物车商品数量) Map map = new HashMap<>(); for (CartOrderItemAddRequest cartOrderItemAddRequest : cartOrderItemAddRequestList) { @@ -120,6 +119,18 @@ public class OrderController { Integer quantity = cartOrderItemAddRequest.getQuantity(); map.put(cartRecordId, quantity); } + + // 计算订单总金额 + BigDecimal totalAmount = BigDecimal.ZERO; + for (CartRecord cartRecord : cartRecordList) { + Long goodId = cartRecord.getGoodId(); + Good good = goodMap.get(goodId); + Long cartRecordId = cartRecord.getId(); + Integer quantity = map.get(cartRecordId); + totalAmount = totalAmount.add(good.getPrice().multiply(BigDecimal.valueOf(quantity))); + } + + orderMainInfoAddRequest.setTotalAmount(totalAmount); // 封装成订单明细主要信息请求体列表 List orderItemMainInfoAddRequestList = new ArrayList<>(); OrderItemMainInfoAddRequest orderItemMainInfoAddRequest; @@ -156,6 +167,9 @@ public class OrderController { Long userId = loginUser.getId(); // 校验单独购买的商品数据是否准确 orderService.validSingleGoodOrder(orderMainInfoAddRequest); + // 计算单个商品购买的总金额 + BigDecimal totalAmount = orderService.calculateTotalAmount(orderMainInfoAddRequest); + orderMainInfoAddRequest.setTotalAmount(totalAmount); // 创建通用订单(常规类和服务类商品) Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId, false, null); return ResultUtils.success(orderId); @@ -214,6 +228,7 @@ public class OrderController { Long id = loginUser.getId(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("userId", id); + queryWrapper.orderByDesc("id"); List orders = orderService.list(queryWrapper); List orderVOS = orders.stream().map(order -> { Long orderId = order.getId(); 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 6cd7a50..a8091dd 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,7 @@ 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.math.BigDecimal; import java.util.List; public interface OrderService extends IService { @@ -41,4 +42,11 @@ public interface OrderService extends IService { * 校验单独购买的商品数据是否准确 */ void validSingleGoodOrder(OrderMainInfoAddRequest orderMainInfoAddRequest); + + + + /** + * 计算单个商品购买的总金额 + */ + BigDecimal calculateTotalAmount(OrderMainInfoAddRequest orderMainInfoAddRequest); } 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 2953634..d8cbffe 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 @@ -286,6 +286,19 @@ public class OrderServiceImpl extends ServiceImpl implements + /** + * 计算单个商品购买的总金额 + */ + @Override + public BigDecimal calculateTotalAmount(OrderMainInfoAddRequest orderMainInfoAddRequest) { + List orderItemMainInfoAddRequestList = orderMainInfoAddRequest.getOrderItemMainInfoAddRequestList(); + OrderItemMainInfoAddRequest orderItemMainInfoAddRequest = orderItemMainInfoAddRequestList.get(0); + Integer quantity = orderItemMainInfoAddRequest.getQuantity(); + Long goodId = orderItemMainInfoAddRequest.getGoodId(); + Good good = goodService.getById(goodId); + return good.getPrice().multiply(BigDecimal.valueOf(quantity)); + } + /** * 根据id获取订单(地址,联系人,优惠券,商品)信息