diff --git a/src/main/java/com/cultural/heritage/controller/good/CartExperienceController.java b/src/main/java/com/cultural/heritage/controller/good/CartExperienceController.java index 7ee676e..6a7acbd 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CartExperienceController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CartExperienceController.java @@ -10,14 +10,17 @@ import com.cultural.heritage.exception.ThrowUtils; import com.cultural.heritage.model.dto.CommonBatchRequest; import com.cultural.heritage.model.dto.cartService.CartExperienceAddRequest; import com.cultural.heritage.model.dto.cartService.CartExperienceUpdateRequest; +import com.cultural.heritage.model.dto.order.BookingOrderQueryRequest; import com.cultural.heritage.model.dto.order.BuySingleServiceGoodVerifyRequest; import com.cultural.heritage.model.entity.*; import com.cultural.heritage.model.vo.cartService.CartExperienceGoodVO; import com.cultural.heritage.model.vo.cartService.CartExperienceOrderVO; import com.cultural.heritage.model.vo.cartService.CartExperienceVO; +import com.cultural.heritage.model.vo.cartService.CartExperienceVOPlus; import com.cultural.heritage.service.common.CommonService; import com.cultural.heritage.service.good.CartExperienceService; import com.cultural.heritage.service.good.GoodService; +import com.cultural.heritage.service.order.PendingServiceGoodService; import com.cultural.heritage.service.order.PendingServiceOrderService; import com.cultural.heritage.service.user.UserService; import io.swagger.v3.oas.annotations.Operation; @@ -62,6 +65,10 @@ public class CartExperienceController { private PendingServiceOrderService pendingServiceOrderService; + @Resource + private PendingServiceGoodService pendingServiceGoodService; + + /** @@ -171,7 +178,7 @@ public class CartExperienceController { */ @PostMapping("/list") @Operation(summary = "小程序端用户查询服务类购物车中的商品", description = "参数:无,权限:所有人,方法名:listUserCartRecord") - public BaseResponse> listUserCartRecord(HttpServletRequest request) { + public BaseResponse> listUserCartRecord(HttpServletRequest request) { // 根据用户id查询购物车记录 User loginUser = userService.getLoginUser(request); Long userId = loginUser.getId(); @@ -181,7 +188,45 @@ public class CartExperienceController { List cartExperienceList = cartExperienceService.list(queryWrapper); // 封装用户购物车中的商品信息 List cartExperienceVOList = cartExperienceService.transformToCartRecordVOList(cartExperienceList); - return ResultUtils.success(cartExperienceVOList); + + List pendingServiceGoodList = commonService.findByFieldInTargetField(cartExperienceList, pendingServiceGoodService, CartExperience::getGoodId, "goodId"); + + List pendingServiceOrderList = commonService.findByFieldInTargetField(pendingServiceGoodList, pendingServiceOrderService, PendingServiceGood::getId, "pendingId"); + + // 封装Map集合(键:服务类商品待处理id,值:预约人数) + Map countMap = new HashMap<>(); + for (PendingServiceOrder pendingServiceOrder : pendingServiceOrderList) { + Long pendingId = pendingServiceOrder.getPendingId(); + Integer quantity = pendingServiceOrder.getQuantity(); + Integer currentNumber = countMap.get(pendingId); + countMap.put(pendingId, currentNumber == null ? quantity : currentNumber + quantity); + } + // 封装Map集合(键:预约情况,值:剩余可预约人数) + Map bookingNumberMap = new HashMap<>(); + for (PendingServiceGood pendingServiceGood : pendingServiceGoodList) { + Long pendingId = pendingServiceGood.getId(); + Integer count = countMap.get(pendingId); + Long goodId = pendingServiceGood.getGoodId(); + String reservationDate = pendingServiceGood.getReservationDate(); + String timeSlot = pendingServiceGood.getTimeSlot(); + BookingOrderQueryRequest bookingOrderQueryRequest = new BookingOrderQueryRequest(goodId, reservationDate, timeSlot); + Integer maxNumber = pendingServiceGood.getMaxNumber(); + bookingNumberMap.put(bookingOrderQueryRequest.toString(), count == null ? maxNumber : maxNumber - count); + } + + List cartExperienceVOPluses = cartExperienceVOList.stream().map(cartExperienceVO -> { + CartExperienceVOPlus cartExperienceVOPlus = new CartExperienceVOPlus(); + BeanUtils.copyProperties(cartExperienceVO, cartExperienceVOPlus); + Long goodId = cartExperienceVO.getGoodId(); + String reservationDate = cartExperienceVO.getReservationDate(); + String timeSlot = cartExperienceVO.getTimeSlot(); + BookingOrderQueryRequest bookingOrderQueryRequest = new BookingOrderQueryRequest(goodId, reservationDate, timeSlot); + Integer restNumber = bookingNumberMap.get(bookingOrderQueryRequest.toString()); + cartExperienceVOPlus.setRestNumber(restNumber); + return cartExperienceVOPlus; + }).toList(); + + return ResultUtils.success(cartExperienceVOPluses); } diff --git a/src/main/java/com/cultural/heritage/model/vo/cartService/CartExperienceVOPlus.java b/src/main/java/com/cultural/heritage/model/vo/cartService/CartExperienceVOPlus.java new file mode 100644 index 0000000..acf0e2b --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/vo/cartService/CartExperienceVOPlus.java @@ -0,0 +1,55 @@ +package com.cultural.heritage.model.vo.cartService; + +import lombok.Data; + +import java.math.BigDecimal; + +@Data +public class CartExperienceVOPlus { + + + /** + * 购物车记录id + */ + private Long id; + + /** + * 商品id + */ + private Long goodId; + + /** + * 数量 + */ + private Integer quantity; + + /** + * 小计 + */ + private BigDecimal subtotal; + + /** + * 预约日期 + */ + private String reservationDate; + + + /** + * 预约时间段 + */ + private String timeSlot; + + + /** + * 服务类购物车商品详情信息 + */ + private CartExperienceGoodVO cartExperienceGoodVO; + + + /** + * 剩余预约人数 + */ + private Integer restNumber; + + +} diff --git a/src/main/java/com/cultural/heritage/service/good/impl/CartExperienceServiceImpl.java b/src/main/java/com/cultural/heritage/service/good/impl/CartExperienceServiceImpl.java index 3872eae..19e419f 100644 --- a/src/main/java/com/cultural/heritage/service/good/impl/CartExperienceServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/good/impl/CartExperienceServiceImpl.java @@ -109,11 +109,8 @@ public class CartExperienceServiceImpl extends ServiceImpl transformToCartRecordVOList(List cartExperienceList) { - // 获取所有商品id - List goodIds = cartExperienceList.stream().map(CartExperience::getGoodId).toList(); - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.in("id", goodIds); - List goodList = goodService.list(queryWrapper); + // 获取商品列表 + List goodList = commonService.findByFieldInTargetField(cartExperienceList, goodService, CartExperience::getGoodId, "id"); // 封装map集合(键:商品id, 值:商品详情信息) Map map = new HashMap<>(); for (Good good : goodList) { diff --git a/src/main/java/com/cultural/heritage/service/good/impl/CartRecordServiceImpl.java b/src/main/java/com/cultural/heritage/service/good/impl/CartRecordServiceImpl.java index ca012f9..21391f3 100644 --- a/src/main/java/com/cultural/heritage/service/good/impl/CartRecordServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/good/impl/CartRecordServiceImpl.java @@ -95,11 +95,8 @@ public class CartRecordServiceImpl extends ServiceImpl transformToCartRecordVOList(List list) { - // 获取所有商品id - List goodIds = list.stream().map(CartRecord::getGoodId).toList(); - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.in("id", goodIds); - List goodList = goodService.list(queryWrapper); + // 获取所有商品列表 + List goodList = commonService.findByFieldInTargetField(list, goodService, CartRecord::getGoodId, "id"); // 封装map集合(键:商品id, 值:商品详情信息) Map map = new HashMap<>(); for (Good good : goodList) {