From 7bd07866ef1d0f7e22f095a3e46cf4cdef952b28 Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Sun, 9 Feb 2025 00:59:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E4=BC=98=E6=83=A0?= =?UTF-8?q?=E5=88=B8=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../good/CartExperienceController.java | 285 ++++++++++++++++++ .../controller/good/CartRecordController.java | 111 ++++--- .../controller/good/GoodController.java | 8 +- .../controller/order/OrderController.java | 73 ++--- .../heritage/mapper/CartExperienceMapper.java | 7 + .../heritage/mapper/RefundRecordMapper.java | 2 + .../cartService/CartExperienceAddRequest.java | 40 +++ .../CartExperienceOrderAddRequest.java | 62 ++++ .../CartExperienceOrderItemAddRequest.java | 30 ++ .../CartExperienceUpdateRequest.java | 43 +++ .../heritage/model/entity/CartExperience.java | 75 +++++ .../heritage/model/entity/CartRecord.java | 6 +- .../heritage/model/entity/RefundRecord.java | 2 - .../heritage/model/vo/cart/CartOrderVO.java | 1 - .../heritage/model/vo/cart/CartRecordVO.java | 2 +- .../vo/cartService/CartExperienceGoodVO.java | 41 +++ .../vo/cartService/CartExperienceOrderVO.java | 38 +++ .../vo/cartService/CartExperienceVO.java | 54 ++++ .../service/good/CartExperienceService.java | 45 +++ .../heritage/service/good/GoodService.java | 2 + .../good/impl/CartExperienceServiceImpl.java | 249 +++++++++++++++ .../good/impl/CartRecordServiceImpl.java | 4 - .../resources/mapper/CartExperienceMapper.xml | 7 + .../resources/mapper/RefundRecordMapper.xml | 2 +- 24 files changed, 1091 insertions(+), 98 deletions(-) create mode 100644 src/main/java/com/cultural/heritage/controller/good/CartExperienceController.java create mode 100644 src/main/java/com/cultural/heritage/mapper/CartExperienceMapper.java create mode 100644 src/main/java/com/cultural/heritage/model/dto/cartService/CartExperienceAddRequest.java create mode 100644 src/main/java/com/cultural/heritage/model/dto/cartService/CartExperienceOrderAddRequest.java create mode 100644 src/main/java/com/cultural/heritage/model/dto/cartService/CartExperienceOrderItemAddRequest.java create mode 100644 src/main/java/com/cultural/heritage/model/dto/cartService/CartExperienceUpdateRequest.java create mode 100644 src/main/java/com/cultural/heritage/model/entity/CartExperience.java create mode 100644 src/main/java/com/cultural/heritage/model/vo/cartService/CartExperienceGoodVO.java create mode 100644 src/main/java/com/cultural/heritage/model/vo/cartService/CartExperienceOrderVO.java create mode 100644 src/main/java/com/cultural/heritage/model/vo/cartService/CartExperienceVO.java create mode 100644 src/main/java/com/cultural/heritage/service/good/CartExperienceService.java create mode 100644 src/main/java/com/cultural/heritage/service/good/impl/CartExperienceServiceImpl.java create mode 100644 src/main/resources/mapper/CartExperienceMapper.xml diff --git a/src/main/java/com/cultural/heritage/controller/good/CartExperienceController.java b/src/main/java/com/cultural/heritage/controller/good/CartExperienceController.java new file mode 100644 index 0000000..33ed3b4 --- /dev/null +++ b/src/main/java/com/cultural/heritage/controller/good/CartExperienceController.java @@ -0,0 +1,285 @@ +package com.cultural.heritage.controller.good; + + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.cultural.heritage.common.BaseResponse; +import com.cultural.heritage.common.ErrorCode; +import com.cultural.heritage.common.ResultUtils; +import com.cultural.heritage.exception.BusinessException; +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.BuySingleGoodVerifyRequest; +import com.cultural.heritage.model.entity.CartExperience; +import com.cultural.heritage.model.entity.Good; +import com.cultural.heritage.model.entity.User; +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.service.good.CartExperienceService; +import com.cultural.heritage.service.good.GoodService; +import com.cultural.heritage.service.user.UserService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.Resource; +import jakarta.servlet.http.HttpServletRequest; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeanUtils; +import org.springframework.util.CollectionUtils; +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.math.BigDecimal; +import java.util.*; + +@RestController +@RequestMapping("/cartExperience") +@Slf4j +@Tag(name = "服务类购物车模块") +public class CartExperienceController { + + + @Resource + private CartExperienceService cartExperienceService; + + + @Resource + private GoodService goodService; + + + @Resource + private UserService userService; + + + + /** + * 小程序端用户添加服务类商品至购物车 + * @param cartExperienceAddRequest 服务类购物车商品添加请求体 + * @return 是否添加成功 + */ + @PostMapping("/add") + @Operation(summary = "小程序端用户添加服务类商品至购物车", description = "参数:服务类购物车商品添加请求体,权限:所有人,方法名:addCartRecord") + public BaseResponse addCartRecord(@RequestBody CartExperienceAddRequest cartExperienceAddRequest, HttpServletRequest request) { + if (cartExperienceAddRequest == null) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + // 更新购物车的商品属性 + cartExperienceService.updateCartGoodAttribute(cartExperienceAddRequest, request); + return ResultUtils.success(true); + } + + + /** + * 小程序端用户更新服务类购物车中的商品数量 + * @param cartExperienceUpdateRequestList 服务类购物车更新请求体 + * @return 是否更新成功 + */ + @PostMapping("/update") + @Operation(summary = "小程序端用户更新服务类购物车中的商品数量", description = "参数:服务类购物车更新请求体,权限:所有人,方法名:updateCartRecord") + public BaseResponse updateCartRecord(@RequestBody List cartExperienceUpdateRequestList, HttpServletRequest request) { + if (CollectionUtils.isEmpty(cartExperienceUpdateRequestList)) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + userService.getLoginUser(request); + + // 获取购物车id列表 + List cartIds = cartExperienceUpdateRequestList.stream().map(CartExperienceUpdateRequest::getId).toList(); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.in("id", cartIds); + List cartExperienceList = cartExperienceService.list(queryWrapper); + + // 封装map集合(键:购物车id, 值:商品数量) + Map QuantityMap = new HashMap<>(); + // 封装map集合(键:购物车id, 值:预约时间) + Map reservationDateMap = new HashMap<>(); + // 封装map集合(键:购物车id, 值:预约时间段) + Map timeSlotMap = new HashMap<>(); + for (CartExperienceUpdateRequest cartExperienceUpdateRequest : cartExperienceUpdateRequestList) { + Long id = cartExperienceUpdateRequest.getId(); + Integer quantity = cartExperienceUpdateRequest.getQuantity(); + String reservationDate = cartExperienceUpdateRequest.getReservationDate(); + String timeSlot = cartExperienceUpdateRequest.getTimeSlot(); + QuantityMap.put(id, quantity); + reservationDateMap.put(id, reservationDate); + timeSlotMap.put(id, timeSlot); + } + + // 获取商品id列表 + List goodIds = cartExperienceList.stream().map(CartExperience::getGoodId).toList(); + QueryWrapper goodQueryWrapper = new QueryWrapper<>(); + goodQueryWrapper.in("id", goodIds); + List goodList = goodService.list(goodQueryWrapper); + + // 封装map集合(键:商品id, 值:商品价格) + Map priceMap = new HashMap<>(); + for (Good good : goodList) { + priceMap.put(good.getId(), good.getPrice()); + } + + // 批量更新购物车数量、小计、预约时间、预约时间段 + for (CartExperience cartExperience : cartExperienceList) { + Long cartRecordId = cartExperience.getId(); + Integer quantity = QuantityMap.get(cartRecordId); + cartExperience.setQuantity(quantity); + + Long goodId = cartExperience.getGoodId(); + BigDecimal price = priceMap.get(goodId); + cartExperience.setSubtotal(price.multiply(BigDecimal.valueOf(quantity))); + + String reservationDate = reservationDateMap.get(cartRecordId); + cartExperience.setReservationDate(reservationDate); + String timeSlot = timeSlotMap.get(cartRecordId); + cartExperience.setTimeSlot(timeSlot); + } + + boolean result = cartExperienceService.updateBatchById(cartExperienceList); + ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "购物车信息更新失败"); + return ResultUtils.success(true); + } + + + + /** + * 小程序端用户删除服务类购物车中的物品 + * @param commonBatchRequest 删除的商品id列表 + * @return 是否删除成功 + */ + @PostMapping("/delete") + @Operation(summary = "小程序端用户删除服务类购物车中的物品", description = "参数:购物车记录删除请求体(商品id列表,如[2,3,4]),权限:所有人,方法名:deleteCart") + public BaseResponse deleteCart(@RequestBody CommonBatchRequest commonBatchRequest, HttpServletRequest request) { + if (commonBatchRequest == null || commonBatchRequest.getIdList().isEmpty()) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + userService.getLoginUser(request); + List idList = commonBatchRequest.getIdList(); + boolean result = cartExperienceService.removeBatchByIds(idList); + ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); + return ResultUtils.success(true); + } + + + + /** + * 小程序端用户查询服务类购物车中的商品 + * @return 商品列表信息 + */ + @PostMapping("/list") + @Operation(summary = "小程序端用户查询服务类购物车中的商品", description = "参数:无,权限:所有人,方法名:listUserCartRecord") + public BaseResponse> listUserCartRecord(HttpServletRequest request) { + // 根据用户id查询购物车记录 + User loginUser = userService.getLoginUser(request); + Long userId = loginUser.getId(); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("userId", userId); + queryWrapper.orderByDesc("id"); + List cartExperienceList = cartExperienceService.list(queryWrapper); + // 封装用户购物车中的商品信息 + List cartExperienceVOList = cartExperienceService.transformToCartRecordVOList(cartExperienceList); + return ResultUtils.success(cartExperienceVOList); + } + + + + + /** + * 小程序端显示服务类购物车中不合理的商品项 + * @return 存在问题的购物车id列表 + */ + @PostMapping("/submit/error") + @Operation(summary = "小程序端显示服务类购物车中不合理的商品项", description = "参数:无,权限:所有人,方法名:listInvalidCartIds") + public BaseResponse> listInvalidCartIds (HttpServletRequest request) { + User loginUser = userService.getLoginUser(request); + Long userId = loginUser.getId(); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("userId", userId); + List cartExperienceList = cartExperienceService.list(queryWrapper); + // 找出存在问题的购物车id列表 + List invalidCartIds = cartExperienceService.getInvalidCartIds(cartExperienceList); + return ResultUtils.success(invalidCartIds); + } + + + + + + /** + * 小程序端显示服务类购物车提交订单页的商品信息 + * @param commonBatchRequest 购物车id列表 + * @return 订单商品信息列表 + */ + @PostMapping("/submit/list") + @Operation(summary = "小程序端显示服务类购物车提交订单页的商品信息", description = "参数:商品id和购买数量信息,权限:所有人,方法名:listCartRecord") + public BaseResponse> listCartRecord(@RequestBody CommonBatchRequest commonBatchRequest, HttpServletRequest request) { + if (commonBatchRequest == null || commonBatchRequest.getIdList().isEmpty()) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + userService.getLoginUser(request); + List cartExperienceIds = commonBatchRequest.getIdList(); + List cartExperienceList = cartExperienceService.list(); + // 获取购物车列表信息 + List cartExperiences = cartExperienceList.stream().filter(cartExperience -> { + Long id = cartExperience.getId(); + return cartExperienceIds.contains(id); + }).toList(); + + // 提取出购物车中的商品id列表 + List goodIds = cartExperiences.stream().map(CartExperience::getGoodId).toList(); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.in("id", goodIds); + List goodList = goodService.list(queryWrapper); + + // 封装goodMap(根据商品id查询商品) + Map goodMap = new HashMap<>(); + for (Good good : goodList) { + goodMap.put(good.getId(), good); + } + + // 封装提交订单页的商品信息列表 + List cartOrderVOList = new ArrayList<>(); + for (CartExperience cartExperience : cartExperiences) { + Integer quantity = cartExperience.getQuantity(); + Long goodId = cartExperience.getGoodId(); + Good good = goodMap.get(goodId); + + CartExperienceGoodVO cartExperienceGoodVO = new CartExperienceGoodVO(); + BeanUtils.copyProperties(good, cartExperienceGoodVO); + + CartExperienceOrderVO cartExperienceOrderVO = new CartExperienceOrderVO(); + cartExperienceOrderVO.setCartExperienceGoodVO(cartExperienceGoodVO); + cartExperienceOrderVO.setQuantity(quantity); + cartExperienceOrderVO.setReservationDate(cartExperience.getReservationDate()); + cartExperienceOrderVO.setTimeSlot(cartExperience.getTimeSlot()); + cartOrderVOList.add(cartExperienceOrderVO); + } + Collections.reverse(cartOrderVOList); + return ResultUtils.success(cartOrderVOList); + } + + + + + + /** + * 小程序端用户点击立即购买触发的校验 + * @param buySingleGoodVerifyRequest 商品id和购买数量 + * @return 是否通过校验 + */ + @PostMapping("/verify/good") + @Operation(summary = "小程序端用户点击立即购买触发的校验", description = "参数:商品id和购买数量,权限:所有人,方法名:verifyPurchaseInfo") + public BaseResponse verifyPurchaseInfo(@RequestBody BuySingleGoodVerifyRequest buySingleGoodVerifyRequest, HttpServletRequest request) { + if (buySingleGoodVerifyRequest == null || buySingleGoodVerifyRequest.getGoodId() <= 0) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + userService.getLoginUser(request); + Long goodId = buySingleGoodVerifyRequest.getGoodId(); + Integer quantity = buySingleGoodVerifyRequest.getQuantity(); + Good good = goodService.getById(goodId); + ThrowUtils.throwIf(good == null || good.getIsShelves() == 0, ErrorCode.SYSTEM_ERROR, "商品已下架或不存在"); + ThrowUtils.throwIf(quantity > good.getInventory(), ErrorCode.SYSTEM_ERROR, "商品库存不足"); + return ResultUtils.success(true); + } + +} 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 479b87d..467d922 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java @@ -6,6 +6,7 @@ import com.cultural.heritage.common.ErrorCode; import com.cultural.heritage.common.ResultUtils; import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.ThrowUtils; +import com.cultural.heritage.model.dto.CommonBatchRequest; import com.cultural.heritage.model.dto.cart.CartRecordAddRequest; import com.cultural.heritage.model.dto.cart.CartRecordUpdateRequest; import com.cultural.heritage.model.dto.order.BuySingleGoodVerifyRequest; @@ -36,7 +37,7 @@ import java.util.*; @RestController @RequestMapping("/cart") @Slf4j -@Tag(name = "购物车信息模块") +@Tag(name = "常规类购物车模块") public class CartRecordController { @@ -52,12 +53,12 @@ public class CartRecordController { private UserService userService; /** - * 用户添加商品至购物车 - * @param cartRecordAddRequest 购物车记录添加请求体 + * 小程序端用户添加常规类商品至购物车 + * @param cartRecordAddRequest 常规类购物车商品添加请求体 * @return 是否添加成功 */ @PostMapping("/add") - @Operation(summary = "小程序端用户添加商品至购物车", description = "参数:购物车记录添加请求体,权限:所有人,方法名:addCartRecord") + @Operation(summary = "小程序端用户添加常规类商品至购物车", description = "参数:常规类购物车商品添加请求体,权限:所有人,方法名:addCartRecord") public BaseResponse addCartRecord(@RequestBody CartRecordAddRequest cartRecordAddRequest, HttpServletRequest request) { if (cartRecordAddRequest == null) { throw new BusinessException(ErrorCode.PARAMS_ERROR); @@ -69,34 +70,55 @@ public class CartRecordController { /** - * 小程序端用户更新购物车中的商品数量 - * @param cartRecordUpdateRequestList 购物车记录列表更新请求体 + * 小程序端用户更新常规类购物车中的商品数量 + * @param cartRecordUpdateRequestList 常规类购物车更新请求体 * @return 是否更新成功 */ @PostMapping("/update") - @Operation(summary = "小程序端用户更新购物车中的商品数量", description = "参数:购物车记录更新请求体,权限:所有人,方法名:updateCartRecord") + @Operation(summary = "小程序端用户更新常规类购物车中的商品数量", description = "参数:常规类购物车更新请求体,权限:所有人,方法名:updateCartRecord") public BaseResponse updateCartRecord(@RequestBody List cartRecordUpdateRequestList, HttpServletRequest request) { if (CollectionUtils.isEmpty(cartRecordUpdateRequestList)) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } - User loginUser = userService.getLoginUser(request); - Long userId = loginUser.getId(); - List cartRecordList = cartRecordUpdateRequestList.stream().map(cartRecordUpdateRequest -> { + userService.getLoginUser(request); + + // 获取购物车id列表 + List cartIds = cartRecordUpdateRequestList.stream().map(CartRecordUpdateRequest::getId).toList(); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.in("id", cartIds); + List cartRecordList = cartRecordService.list(queryWrapper); + + // 封装map集合(键:购物车id, 值:商品数量) + Map QuantityMap = new HashMap<>(); + for (CartRecordUpdateRequest cartRecordUpdateRequest : cartRecordUpdateRequestList) { Long id = cartRecordUpdateRequest.getId(); - CartRecord cart = cartRecordService.getById(id); - ThrowUtils.throwIf(cart == null, ErrorCode.OPERATION_ERROR, "该项商品已被移除"); - CartRecord cartRecord = new CartRecord(); - BeanUtils.copyProperties(cartRecordUpdateRequest, cartRecord); - Long goodId = cart.getGoodId(); - Good good = goodService.getById(goodId); - ThrowUtils.throwIf(good == null || good.getIsShelves() == 0, ErrorCode.OPERATION_ERROR, "购物车商品更新失败"); - cartRecord.setSubtotal(good.getPrice().multiply(BigDecimal.valueOf(cartRecord.getQuantity()))); - cartRecord.setUserId(userId); - cartRecord.setGoodId(goodId); - // 校验 - cartRecordService.validCart(cartRecord, true); - return cartRecord; - }).toList(); + Integer quantity = cartRecordUpdateRequest.getQuantity(); + QuantityMap.put(id, quantity); + } + + // 获取商品id列表 + List goodIds = cartRecordList.stream().map(CartRecord::getGoodId).toList(); + QueryWrapper goodQueryWrapper = new QueryWrapper<>(); + goodQueryWrapper.in("id", goodIds); + List goodList = goodService.list(goodQueryWrapper); + + // 封装map集合(键:商品id, 值:商品价格) + Map priceMap = new HashMap<>(); + for (Good good : goodList) { + priceMap.put(good.getId(), good.getPrice()); + } + + // 批量更新购物车数量和小计 + for (CartRecord cartRecord : cartRecordList) { + Long cartRecordId = cartRecord.getId(); + Integer quantity = QuantityMap.get(cartRecordId); + cartRecord.setQuantity(quantity); + + Long goodId = cartRecord.getGoodId(); + BigDecimal price = priceMap.get(goodId); + cartRecord.setSubtotal(price.multiply(BigDecimal.valueOf(quantity))); + } + boolean result = cartRecordService.updateBatchById(cartRecordList); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); return ResultUtils.success(true); @@ -105,17 +127,18 @@ public class CartRecordController { /** - * 用户删除购物车的物品 - * @param idList 删除的商品id列表 + * 小程序端用户删除常规类购物车中的物品 + * @param commonBatchRequest 删除的商品id列表 * @return 是否删除成功 */ @PostMapping("/delete") - @Operation(summary = "小程序端用户删除购物车的物品", description = "参数:购物车记录删除请求体(商品id列表,如[2,3,4]),权限:所有人,方法名:deleteCart") - public BaseResponse deleteCart(@RequestBody List idList, HttpServletRequest request) { - if (CollectionUtils.isEmpty(idList)) { + @Operation(summary = "小程序端用户删除常规类购物车中的物品", description = "参数:购物车记录删除请求体(商品id列表,如[2,3,4]),权限:所有人,方法名:deleteCart") + public BaseResponse deleteCart(@RequestBody CommonBatchRequest commonBatchRequest, HttpServletRequest request) { + if (commonBatchRequest == null || commonBatchRequest.getIdList().isEmpty()) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } userService.getLoginUser(request); + List idList = commonBatchRequest.getIdList(); boolean result = cartRecordService.removeBatchByIds(idList); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); return ResultUtils.success(true); @@ -124,11 +147,11 @@ public class CartRecordController { /** - * 小程序端用户查询购物车中的商品 + * 小程序端用户查询常规类购物车中的商品 * @return 商品列表信息 */ @PostMapping("/list") - @Operation(summary = "小程序端用户查询购物车中的商品", description = "参数:无,权限:所有人,方法名:listUserCartRecord") + @Operation(summary = "小程序端用户查询常规类购物车中的商品", description = "参数:无,权限:所有人,方法名:listUserCartRecord") public BaseResponse> listUserCartRecord(HttpServletRequest request) { // 根据用户id查询购物车记录 User loginUser = userService.getLoginUser(request); @@ -146,11 +169,11 @@ public class CartRecordController { /** - * (小程序端)从购物车中找出不合理的商品项 + * 小程序端显示常规类购物车中不合理的商品项 * @return 存在问题的购物车id列表 */ @PostMapping("/submit/error") - @Operation(summary = "(小程序端)从购物车中找出不合理的商品项", description = "参数:无,权限:所有人,方法名:listInvalidCartIds") + @Operation(summary = "小程序端显示常规类购物车中不合理的商品项", description = "参数:无,权限:所有人,方法名:listInvalidCartIds") public BaseResponse> listInvalidCartIds (HttpServletRequest request) { User loginUser = userService.getLoginUser(request); Long userId = loginUser.getId(); @@ -167,17 +190,18 @@ public class CartRecordController { /** - * (小程序端)展示购物车提交订单页的商品信息 - * @param cartRecordIds 购物车id列表 + * 小程序端显示常规类购物车提交订单页的商品信息 + * @param commonBatchRequest 购物车id列表 * @return 订单商品信息列表 */ @PostMapping("/submit/list") - @Operation(summary = "(小程序端)展示购物车提交订单页的商品信息", description = "参数:商品id和购买数量信息,权限:所有人,方法名:listCartRecord") - public BaseResponse> listCartRecord(@RequestBody List cartRecordIds, HttpServletRequest request) { - if (CollectionUtils.isEmpty(cartRecordIds)) { + @Operation(summary = "小程序端显示常规类购物车提交订单页的商品信息", description = "参数:购物车id列表,权限:所有人,方法名:listCartRecord") + public BaseResponse> listCartRecord(@RequestBody CommonBatchRequest commonBatchRequest, HttpServletRequest request) { + if (commonBatchRequest == null || commonBatchRequest.getIdList().isEmpty()) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } userService.getLoginUser(request); + List cartRecordIds = commonBatchRequest.getIdList(); List cartRecordList = cartRecordService.list(); // 获取购物车列表信息 List cartRecords = cartRecordList.stream().filter(cartRecord -> { @@ -185,11 +209,6 @@ public class CartRecordController { return cartRecordIds.contains(id); }).toList(); - // 判断是否存在无效的购物车id - if (cartRecords.size() < cartRecordIds.size()) { - throw new BusinessException(ErrorCode.PARAMS_ERROR, "存在无效的购物车id"); - } - // 提取出购物车中的商品id列表 List goodIds = cartRecords.stream().map(CartRecord::getGoodId).toList(); QueryWrapper queryWrapper = new QueryWrapper<>(); @@ -208,8 +227,10 @@ public class CartRecordController { Integer quantity = cartRecord.getQuantity(); Long goodId = cartRecord.getGoodId(); Good good = goodMap.get(goodId); + CartGoodVO cartGoodVO = new CartGoodVO(); BeanUtils.copyProperties(good, cartGoodVO); + CartOrderVO cartOrderVO = new CartOrderVO(); cartOrderVO.setCartGoodVO(cartGoodVO); cartOrderVO.setQuantity(quantity); @@ -224,12 +245,12 @@ public class CartRecordController { /** - * (小程序端)用户点击立即购买或者支付订单触发的校验 + * 小程序端用户点击立即购买触发的校验 * @param buySingleGoodVerifyRequest 商品id和购买数量 * @return 是否通过校验 */ @PostMapping("/verify/good") - @Operation(summary = "(小程序端)用户点击立即购买或者支付订单触发的校验", description = "参数:商品id和购买数量,权限:所有人,方法名:verifyPurchaseInfo") + @Operation(summary = "小程序端用户点击立即购买触发的校验", description = "参数:商品id和购买数量,权限:所有人,方法名:verifyPurchaseInfo") public BaseResponse verifyPurchaseInfo(@RequestBody BuySingleGoodVerifyRequest buySingleGoodVerifyRequest, HttpServletRequest request) { if (buySingleGoodVerifyRequest == null || buySingleGoodVerifyRequest.getGoodId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); diff --git a/src/main/java/com/cultural/heritage/controller/good/GoodController.java b/src/main/java/com/cultural/heritage/controller/good/GoodController.java index de6d864..53fb992 100644 --- a/src/main/java/com/cultural/heritage/controller/good/GoodController.java +++ b/src/main/java/com/cultural/heritage/controller/good/GoodController.java @@ -519,11 +519,11 @@ public class GoodController { List timePeriodVOList = timeMap.get(dateId); if (result == 0) { timePeriodVOList = timePeriodVOList.stream().filter(timePeriodVO -> { - String startTime = timePeriodVO.getTimeSlot().split("-")[0]; - String currentTime = DateUtil.format(DateUtil.date(), "HH:mm"); + String startTime = timePeriodVO.getTimeSlot().split("-")[0] + ":00"; + String currentTime = DateUtil.format(DateUtil.date(), "HH:mm:ss"); - Date startDate = DateUtil.parse(startTime, "HH:mm"); - Date currentDate = DateUtil.parse(currentTime, "HH:mm"); + Date startDate = DateUtil.parse(startTime, "HH:mm:ss"); + Date currentDate = DateUtil.parse(currentTime, "HH:mm:ss"); return startDate.after(currentDate); }).toList(); if (timePeriodVOList.isEmpty()) continue; 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 ba02e81..bc9156b 100644 --- a/src/main/java/com/cultural/heritage/controller/order/OrderController.java +++ b/src/main/java/com/cultural/heritage/controller/order/OrderController.java @@ -81,41 +81,6 @@ public class OrderController { - - /** - * 小程序端用户创建常规类商品购物车订单 - */ - @PostMapping("/add/cart") - @Transactional(rollbackFor = Exception.class) - @Operation(summary = "小程序端用户创建常规类商品购物车订单", description = "参数:购物车订单创建请求体,权限:所有人,方法名:addCartOrder") - public BaseResponse addCartOrder(@RequestBody CartOrderAddRequest cartOrderAddRequest, HttpServletRequest request) { - if (cartOrderAddRequest == null) { - throw new BusinessException(ErrorCode.PARAMS_ERROR); - } - // 校验用户当前是否处于登录态 - User loginUser = userService.getLoginUser(request); - Long userId = loginUser.getId(); - - // 校验购物车数据是否准确 - List cartOrderItemAddRequestList = cartOrderAddRequest.getCartOrderItemAddRequestList(); - boolean isAccurate = cartRecordService.validIsConsistent(cartOrderItemAddRequestList); - ThrowUtils.throwIf(!isAccurate, ErrorCode.OPERATION_ERROR, "当前购物车中的某些商品已下架或者库存不足,无法生成订单"); - - // 封装成订单主要信息请求体 - OrderMainInfoAddRequest orderMainInfoAddRequest = orderService.packagedOrderMainInfoAddRequest(cartOrderAddRequest); - - // 创建通用订单(常规类和服务类商品) - List cartIds = cartOrderItemAddRequestList.stream().map(CartOrderItemAddRequest::getCartRecordId).toList(); - Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId, true, cartIds, false); - - // 向消息队列中发送订单创建的消息 - orderService.sendCreateOrderMessage(orderId); - - return ResultUtils.success(orderId); - } - - - /** * 小程序端用户创建常规类(服务类)商品订单 * @param generalGoodSingleBuyAddRequest 常规类商品单独购买创建请求体 @@ -156,6 +121,44 @@ public class OrderController { + + + /** + * 小程序端用户创建常规类商品购物车订单 + */ + @PostMapping("/add/cart") + @Transactional(rollbackFor = Exception.class) + @Operation(summary = "小程序端用户创建常规类商品购物车订单", description = "参数:购物车订单创建请求体,权限:所有人,方法名:addCartOrder") + public BaseResponse addCartOrder(@RequestBody CartOrderAddRequest cartOrderAddRequest, HttpServletRequest request) { + if (cartOrderAddRequest == null) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + // 校验用户当前是否处于登录态 + User loginUser = userService.getLoginUser(request); + Long userId = loginUser.getId(); + + // 校验购物车数据是否准确 + List cartOrderItemAddRequestList = cartOrderAddRequest.getCartOrderItemAddRequestList(); + boolean isAccurate = cartRecordService.validIsConsistent(cartOrderItemAddRequestList); + ThrowUtils.throwIf(!isAccurate, ErrorCode.OPERATION_ERROR, "当前购物车中的某些商品已下架或者库存不足,无法生成订单"); + + // 封装成订单主要信息请求体 + OrderMainInfoAddRequest orderMainInfoAddRequest = orderService.packagedOrderMainInfoAddRequest(cartOrderAddRequest); + + // 创建通用订单(常规类和服务类商品) + List cartIds = cartOrderItemAddRequestList.stream().map(CartOrderItemAddRequest::getCartRecordId).toList(); + Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId, true, cartIds, false); + + // 向消息队列中发送订单创建的消息 + orderService.sendCreateOrderMessage(orderId); + + return ResultUtils.success(orderId); + } + + + + + /** * Web端管理员分页查询订单 * @param orderQueryRequest 订单查询请求体 diff --git a/src/main/java/com/cultural/heritage/mapper/CartExperienceMapper.java b/src/main/java/com/cultural/heritage/mapper/CartExperienceMapper.java new file mode 100644 index 0000000..8404bd1 --- /dev/null +++ b/src/main/java/com/cultural/heritage/mapper/CartExperienceMapper.java @@ -0,0 +1,7 @@ +package com.cultural.heritage.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.cultural.heritage.model.entity.CartExperience; + +public interface CartExperienceMapper extends BaseMapper { +} diff --git a/src/main/java/com/cultural/heritage/mapper/RefundRecordMapper.java b/src/main/java/com/cultural/heritage/mapper/RefundRecordMapper.java index 49ee451..2d35f6c 100644 --- a/src/main/java/com/cultural/heritage/mapper/RefundRecordMapper.java +++ b/src/main/java/com/cultural/heritage/mapper/RefundRecordMapper.java @@ -4,4 +4,6 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.cultural.heritage.model.entity.RefundRecord; public interface RefundRecordMapper extends BaseMapper { + + } diff --git a/src/main/java/com/cultural/heritage/model/dto/cartService/CartExperienceAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/cartService/CartExperienceAddRequest.java new file mode 100644 index 0000000..10d37b4 --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/cartService/CartExperienceAddRequest.java @@ -0,0 +1,40 @@ +package com.cultural.heritage.model.dto.cartService; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + +@Data +@Schema(description = "购物车记录添加请求体", requiredProperties = {"goodId", "quantity"}) +public class CartExperienceAddRequest implements Serializable { + + /** + * 商品id + */ + @Schema(description = "商品id(id > 0)", example = "20") + private Long goodId; + + /** + * 数量 + */ + @Schema(description = "商品数量(quantity > 0)", example = "3") + private Integer quantity; + + /** + * 预约日期 + */ + @Schema(description = "预约日期", example = "2025-02-08") + private String reservationDate; + + + /** + * 预约时间段 + */ + @Schema(description = "预约时间段", example = "8:00-11:00") + private String timeSlot; + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/model/dto/cartService/CartExperienceOrderAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/cartService/CartExperienceOrderAddRequest.java new file mode 100644 index 0000000..76757bb --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/cartService/CartExperienceOrderAddRequest.java @@ -0,0 +1,62 @@ +package com.cultural.heritage.model.dto.cartService; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; +import java.util.List; + +@Data +@Schema(description = "购物车订单创建请求体", requiredProperties = {"orderType", "userName", "addressId", + "couponId", "totalAmount", "note", "cartPayRequestList"}) +public class CartExperienceOrderAddRequest implements Serializable { + + + /** + * 订单类别 + */ + @Schema(description = "订单类别", example = "product") + private String orderType; + + + /** + * 用户昵称 + */ + @Schema(description = "用户昵称", example = "张三") + private String userName; + + + /** + * 地址id + */ + @Schema(description = "地址id(id > 0)", example = "12") + private Long addressId; + + + /** + * 优惠券id + */ + @Schema(description = "优惠券id(id > 0)", example = "2") + private Long couponId; + + + + /** + * 订单备注 + */ + @Schema(description = "订单备注", example = "希望能完整体验非遗文化,保持产品原貌,妥善包装") + private String note; + + + /** + * 购物车订单明细创建请求体 + */ + @Schema(description = "购物车订单明细创建请求体") + private List cartOrderItemAddRequestList; + + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/model/dto/cartService/CartExperienceOrderItemAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/cartService/CartExperienceOrderItemAddRequest.java new file mode 100644 index 0000000..b97d60a --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/cartService/CartExperienceOrderItemAddRequest.java @@ -0,0 +1,30 @@ +package com.cultural.heritage.model.dto.cartService; + + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + +@Data +@Schema(description = "购物车订单明细创建请求体", requiredProperties = {"cartRecordId", "quantity"}) +public class CartExperienceOrderItemAddRequest implements Serializable { + + + /** + * 购物车id + */ + @Schema(description = "购物车id(id > 0)", example = "20") + private Long cartRecordId; + + /** + * 数量 + */ + @Schema(description = "商品数量(quantity > 0)", example = "3") + private Integer quantity; + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/model/dto/cartService/CartExperienceUpdateRequest.java b/src/main/java/com/cultural/heritage/model/dto/cartService/CartExperienceUpdateRequest.java new file mode 100644 index 0000000..0136cbc --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/cartService/CartExperienceUpdateRequest.java @@ -0,0 +1,43 @@ +package com.cultural.heritage.model.dto.cartService; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + +@Data +@Schema(description = "购物车记录更新请求体", requiredProperties = {"id", "quantity"}) +public class CartExperienceUpdateRequest implements Serializable { + + /** + * 购物车记录id + */ + @Schema(description = "购物车记录Id(id > 0)", example = "5") + private Long id; + + + /** + * 数量 + */ + @Schema(description = "商品数量(quantity > 0)", example = "3") + private Integer quantity; + + + /** + * 预约日期 + */ + @Schema(description = "预约日期", example = "2025-02-08") + private String reservationDate; + + + /** + * 预约时间段 + */ + @Schema(description = "预约时间段", example = "10:00-12:00") + private String timeSlot; + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/model/entity/CartExperience.java b/src/main/java/com/cultural/heritage/model/entity/CartExperience.java new file mode 100644 index 0000000..17ff243 --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/entity/CartExperience.java @@ -0,0 +1,75 @@ +package com.cultural.heritage.model.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; +import java.math.BigDecimal; +import java.util.Date; + +/** + * 服务类商品购物车 + * @TableName cart_experience + */ +@Data +@TableName("cart_experience") +public class CartExperience implements Serializable { + + + /** + * 购物车记录id + */ + @TableId(type = IdType.AUTO) + private Long id; + + /** + * 用户id + */ + private Long userId; + + /** + * 商品id + */ + private Long goodId; + + /** + * 数量 + */ + private Integer quantity; + + /** + * 小计 + */ + private BigDecimal subtotal; + + + /** + * 预约日期 + */ + private String reservationDate; + + + /** + * 预约时间段 + */ + private String timeSlot; + + + /** + * 创建时间 + */ + private Date createTime; + + /** + * 更新时间 + */ + private Date updateTime; + + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/model/entity/CartRecord.java b/src/main/java/com/cultural/heritage/model/entity/CartRecord.java index 10a16c2..91d2285 100644 --- a/src/main/java/com/cultural/heritage/model/entity/CartRecord.java +++ b/src/main/java/com/cultural/heritage/model/entity/CartRecord.java @@ -11,7 +11,7 @@ import java.math.BigDecimal; import java.util.Date; /** - * 常规类购物车添加商品 + * 常规类商品购物车 * @TableName cart_record */ @Data @@ -56,10 +56,6 @@ public class CartRecord implements Serializable { */ private Date updateTime; - /** - * 是否删除 - */ - private Integer isDelete; diff --git a/src/main/java/com/cultural/heritage/model/entity/RefundRecord.java b/src/main/java/com/cultural/heritage/model/entity/RefundRecord.java index e34776d..e0904a4 100644 --- a/src/main/java/com/cultural/heritage/model/entity/RefundRecord.java +++ b/src/main/java/com/cultural/heritage/model/entity/RefundRecord.java @@ -3,7 +3,6 @@ package com.cultural.heritage.model.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; -import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; import java.io.Serial; @@ -18,7 +17,6 @@ import java.util.Date; */ @Data @TableName(value = "refund_record") -@Schema(description = "退款记录", requiredProperties = {"id", "outTradeNo", "outRefundNo", "refundAmount"}) public class RefundRecord implements Serializable { diff --git a/src/main/java/com/cultural/heritage/model/vo/cart/CartOrderVO.java b/src/main/java/com/cultural/heritage/model/vo/cart/CartOrderVO.java index 8b13ac5..318a2e9 100644 --- a/src/main/java/com/cultural/heritage/model/vo/cart/CartOrderVO.java +++ b/src/main/java/com/cultural/heritage/model/vo/cart/CartOrderVO.java @@ -21,7 +21,6 @@ public class CartOrderVO implements Serializable { private Integer quantity; - @Serial private static final long serialVersionUID = 1L; } diff --git a/src/main/java/com/cultural/heritage/model/vo/cart/CartRecordVO.java b/src/main/java/com/cultural/heritage/model/vo/cart/CartRecordVO.java index 6913a32..b2d42c1 100644 --- a/src/main/java/com/cultural/heritage/model/vo/cart/CartRecordVO.java +++ b/src/main/java/com/cultural/heritage/model/vo/cart/CartRecordVO.java @@ -32,7 +32,7 @@ public class CartRecordVO implements Serializable { /** - * 购物车商品详情信息 + * 常规类购物车商品详情信息 */ private CartGoodVO cartGoodVO; diff --git a/src/main/java/com/cultural/heritage/model/vo/cartService/CartExperienceGoodVO.java b/src/main/java/com/cultural/heritage/model/vo/cartService/CartExperienceGoodVO.java new file mode 100644 index 0000000..d21c171 --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/vo/cartService/CartExperienceGoodVO.java @@ -0,0 +1,41 @@ +package com.cultural.heritage.model.vo.cartService; + +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; +import java.math.BigDecimal; + +@Data +public class CartExperienceGoodVO implements Serializable { + + /** + * 商品编号 + */ + private Long id; + + /** + * 商品名 + */ + private String name; + + /** + * 商品类型 + */ + private String type; + + /** + * 商品价格 + */ + private BigDecimal price; + + /** + * 商品图片 + */ + private String goodImg; + + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/model/vo/cartService/CartExperienceOrderVO.java b/src/main/java/com/cultural/heritage/model/vo/cartService/CartExperienceOrderVO.java new file mode 100644 index 0000000..4aa6190 --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/vo/cartService/CartExperienceOrderVO.java @@ -0,0 +1,38 @@ +package com.cultural.heritage.model.vo.cartService; + +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + +@Data +public class CartExperienceOrderVO implements Serializable { + + + /** + * 商品详细信息 + */ + private CartExperienceGoodVO cartExperienceGoodVO; + + + /** + * 商品购买数量 + */ + private Integer quantity; + + + /** + * 预约日期 + */ + private String reservationDate; + + + /** + * 预约时间段 + */ + private String timeSlot; + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/model/vo/cartService/CartExperienceVO.java b/src/main/java/com/cultural/heritage/model/vo/cartService/CartExperienceVO.java new file mode 100644 index 0000000..f2eaa13 --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/vo/cartService/CartExperienceVO.java @@ -0,0 +1,54 @@ +package com.cultural.heritage.model.vo.cartService; + +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; +import java.math.BigDecimal; + +@Data +public class CartExperienceVO implements Serializable { + + + /** + * 购物车记录id + */ + private Long id; + + /** + * 商品id + */ + private Long goodId; + + /** + * 数量 + */ + private Integer quantity; + + /** + * 小计 + */ + private BigDecimal subtotal; + + /** + * 预约日期 + */ + private String reservationDate; + + + /** + * 预约时间段 + */ + private String timeSlot; + + + /** + * 服务类购物车商品详情信息 + */ + private CartExperienceGoodVO cartExperienceGoodVO; + + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/service/good/CartExperienceService.java b/src/main/java/com/cultural/heritage/service/good/CartExperienceService.java new file mode 100644 index 0000000..fe8ce50 --- /dev/null +++ b/src/main/java/com/cultural/heritage/service/good/CartExperienceService.java @@ -0,0 +1,45 @@ +package com.cultural.heritage.service.good; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.cultural.heritage.model.dto.cartService.CartExperienceAddRequest; +import com.cultural.heritage.model.entity.CartExperience; +import com.cultural.heritage.model.vo.cartService.CartExperienceVO; +import jakarta.servlet.http.HttpServletRequest; + +import java.util.List; + +public interface CartExperienceService extends IService { + + + /** + * 更新购物车的商品属性 + */ + boolean updateCartGoodAttribute(CartExperienceAddRequest cartExperienceAddRequest, HttpServletRequest request); + + + /** + * 封装用户购物车中的商品信息 + */ + List transformToCartRecordVOList(List cartExperienceList); + + + /** + * 校验购物车中的商品信息 + */ + void validCart(CartExperience cartExperience, boolean update); + + + +// /** +// * 在提交订单页校验购物车商品项的准确性 +// */ +// boolean validIsConsistent(List cartOrderItemAddRequestList); + + + + /** + * 找出存在问题的购物车id列表 + */ + List getInvalidCartIds(List cartExperienceList); + +} diff --git a/src/main/java/com/cultural/heritage/service/good/GoodService.java b/src/main/java/com/cultural/heritage/service/good/GoodService.java index 73df18e..277bdff 100644 --- a/src/main/java/com/cultural/heritage/service/good/GoodService.java +++ b/src/main/java/com/cultural/heritage/service/good/GoodService.java @@ -33,4 +33,6 @@ public interface GoodService extends IService { * 更新购物车中的小计 */ List updateCartGoodPrice(Long goodId, BigDecimal targetPrice); + + } 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 new file mode 100644 index 0000000..637a03e --- /dev/null +++ b/src/main/java/com/cultural/heritage/service/good/impl/CartExperienceServiceImpl.java @@ -0,0 +1,249 @@ +package com.cultural.heritage.service.good.impl; + +import cn.hutool.core.date.DateUtil; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.cultural.heritage.common.ErrorCode; +import com.cultural.heritage.exception.BusinessException; +import com.cultural.heritage.exception.ThrowUtils; +import com.cultural.heritage.mapper.CartExperienceMapper; +import com.cultural.heritage.model.dto.cartService.CartExperienceAddRequest; +import com.cultural.heritage.model.entity.CartExperience; +import com.cultural.heritage.model.entity.Good; +import com.cultural.heritage.model.entity.User; +import com.cultural.heritage.model.vo.cartService.CartExperienceGoodVO; +import com.cultural.heritage.model.vo.cartService.CartExperienceVO; +import com.cultural.heritage.service.good.CartExperienceService; +import com.cultural.heritage.service.good.GoodService; +import com.cultural.heritage.service.user.UserService; +import jakarta.annotation.Resource; +import jakarta.servlet.http.HttpServletRequest; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.BeanUtils; +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 +@Slf4j +public class CartExperienceServiceImpl extends ServiceImpl implements CartExperienceService { + + + + + @Resource + private GoodService goodService; + + + @Resource + private UserService userService; + + + /** + * 更新购物车的商品属性 + */ + @Override + public boolean updateCartGoodAttribute(CartExperienceAddRequest cartExperienceAddRequest, HttpServletRequest request) { + User loginUser = userService.getLoginUser(request); + Long userId = loginUser.getId(); + Long goodId = cartExperienceAddRequest.getGoodId(); + Good good = goodService.getById(goodId); + ThrowUtils.throwIf(good == null || good.getIsShelves() == 0, ErrorCode.OPERATION_ERROR, "商品不存在或者已下架"); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("userId", userId); + queryWrapper.eq("goodId", goodId); + CartExperience cartExperience = this.baseMapper.selectOne(queryWrapper); + if (cartExperience != null) { + // 判断数量是否超出库存 + int currentQuantity = cartExperience.getQuantity() + cartExperienceAddRequest.getQuantity(); + if (currentQuantity > good.getInventory()) { + throw new BusinessException(ErrorCode.SYSTEM_ERROR, "商品库存不足, 无法添加"); + } + // 如果购物车中已存在该商品, 就叠加数量 + cartExperience.setQuantity(currentQuantity); + cartExperience.setSubtotal(good.getPrice().multiply(BigDecimal.valueOf(currentQuantity))); + boolean result = this.updateById(cartExperience); + ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "更新购物车商品数量失败"); + } else { + // 判断数量是否超出库存 + Integer currentQuantity = cartExperienceAddRequest.getQuantity(); + if (currentQuantity > good.getInventory()) { + throw new BusinessException(ErrorCode.SYSTEM_ERROR, "商品库存不足, 无法添加"); + } + // 如果购物车不存在该商品,就向购物车中添加一项 + CartExperience cartExperienceRecord = new CartExperience(); + BeanUtils.copyProperties(cartExperienceAddRequest, cartExperienceRecord); + cartExperienceRecord.setUserId(userId); + cartExperienceRecord.setSubtotal(good.getPrice().multiply(BigDecimal.valueOf(cartExperienceRecord.getQuantity()))); + // 校验 + this.validCart(cartExperienceRecord, false); + boolean result = this.save(cartExperienceRecord); + ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); + } + return true; + } + + + + + + /** + * 封装用户购物车中的商品信息 + */ + @Override + public List 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); + // 封装map集合(键:商品id, 值:商品详情信息) + Map map = new HashMap<>(); + for (Good good : goodList) { + map.put(good.getId(), good); + } + + return cartExperienceList.stream().map(cartExperience -> { + CartExperienceVO cartExperienceVO = new CartExperienceVO(); + BeanUtils.copyProperties(cartExperience, cartExperienceVO); + + CartExperienceGoodVO cartExperienceGoodVO = new CartExperienceGoodVO(); + Long goodId = cartExperience.getGoodId(); + Good good = map.get(goodId); + BeanUtils.copyProperties(good, cartExperienceGoodVO); + + cartExperienceVO.setCartExperienceGoodVO(cartExperienceGoodVO); + return cartExperienceVO; + }).toList(); + } + + + + /** + * 校验购物车中的商品信息 + */ + @Override + public void validCart(CartExperience cartExperience, boolean update) { + Long userId = cartExperience.getUserId(); + Long goodId = cartExperience.getGoodId(); + Integer quantity = cartExperience.getQuantity(); + BigDecimal subtotal = cartExperience.getSubtotal(); + String reservationDate = cartExperience.getReservationDate(); + String timeSlot = cartExperience.getTimeSlot(); + if (update) { + if (userId == null) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + } + + Good good = goodService.getById(goodId); + if (good == null || good.getIsShelves() == 0) { + throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "不存在该商品或该商品已下架"); + } + if (StringUtils.isAnyBlank(reservationDate, timeSlot)) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "预约日期或者预约时间段参数为空"); + } + + String startTime = timeSlot.split("-")[0]; + String advanceDateTimeStr = reservationDate + " " + startTime + ":00"; + String currentDateTimeStr = DateUtil.now(); + + Date advanceDateTime = DateUtil.parse(advanceDateTimeStr, "yyyy-MM-dd HH:mm:ss"); + Date currentDateTime = DateUtil.parse(currentDateTimeStr, "yyyy-MM-dd HH:mm:ss"); + + int result = DateUtil.compare(advanceDateTime, currentDateTime); + ThrowUtils.throwIf(result <= 0, ErrorCode.OPERATION_ERROR, "当前时间段已过期"); + + if (ObjectUtils.isEmpty(quantity) || quantity < 1) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "请传递正确的商品数量"); + } + if (ObjectUtils.isEmpty(subtotal) || subtotal.compareTo(BigDecimal.ZERO) <= 0) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "价格错误"); + } + } + + + /** + * 校验购物车商品项的准确性 + */ + private boolean validIsConsistent(CartExperience cartExperience, Good good) { + // 校验购物车中的商品是否存在 + if (good == null || good.getIsShelves() == 0) { + log.info("商品不存在或者已下架, 购物车id为" + cartExperience.getId()); + return false; + } + // 判断当前时间是否晚于预约时间 + String reservationDate = cartExperience.getReservationDate(); + String startTime = cartExperience.getTimeSlot().split("-")[0]; + String advanceDateTimeStr = reservationDate + " " + startTime + ":00"; + String currentDateTimeStr = DateUtil.now(); + + Date advanceDateTime = DateUtil.parse(advanceDateTimeStr, "yyyy-MM-dd HH:mm:ss"); + Date currentDateTime = DateUtil.parse(currentDateTimeStr, "yyyy-MM-dd HH:mm:ss"); + + int result = DateUtil.compare(advanceDateTime, currentDateTime); + return result > 0; + } + + +// /** +// * 在提交订单页校验购物车商品项的准确性 +// */ +// @Override +// public boolean validIsConsistent(List cartOrderItemAddRequestList) { +// if (cartOrderItemAddRequestList.size() == 0) { +// throw new BusinessException(ErrorCode.PARAMS_ERROR, "购物车中没有商品"); +// } +// // 封装map集合(键:购物车id, 值:购买数量) +// Map map = new HashMap<>(); +// for (CartOrderItemAddRequest cartOrderItemAddRequest : cartOrderItemAddRequestList) { +// Long cartRecordId = cartOrderItemAddRequest.getCartRecordId(); +// Integer quantity = cartOrderItemAddRequest.getQuantity(); +// map.put(cartRecordId, quantity); +// } +// // 修改购物车商品项的购买数量 +// List cartRecordIds = cartOrderItemAddRequestList.stream().map(CartOrderItemAddRequest::getCartRecordId).toList(); +// QueryWrapper queryWrapper = new QueryWrapper<>(); +// queryWrapper.in("id", cartRecordIds); +// List cartRecordList = this.list(queryWrapper); +// for (CartRecord cartRecord : cartRecordList) { +// Long id = cartRecord.getId(); +// Integer quantity = map.get(id); +// cartRecord.setQuantity(quantity); +// } +// List cartIds = getInvalidCartIds(cartRecordList); +// return cartIds.isEmpty(); +// } + + + /** + * 找出存在问题的购物车id列表 + */ + @Override + public List getInvalidCartIds(List cartExperienceList) { + // 获取购物车的商品id列表 + List goodIds = cartExperienceList.stream().map(CartExperience::getGoodId).toList(); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.in("id", goodIds); + List goodList = goodService.list(queryWrapper); + // 封装map集合(键:商品id, 值:商品详情信息) + Map map = new HashMap<>(); + for (Good good : goodList) { + map.put(good.getId(), good); + } + List cartExperiences = cartExperienceList.stream().filter(cartExperience -> { + Long goodId = cartExperience.getGoodId(); + Good good = map.get(goodId); + return !validIsConsistent(cartExperience, good); + }).toList(); + return cartExperiences.stream().map(CartExperience::getId).toList(); + } + +} 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 334c6bb..ff5e528 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 @@ -6,7 +6,6 @@ import com.cultural.heritage.common.ErrorCode; import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.ThrowUtils; import com.cultural.heritage.mapper.CartRecordMapper; -import com.cultural.heritage.mapper.GoodMapper; import com.cultural.heritage.model.dto.cart.CartOrderItemAddRequest; import com.cultural.heritage.model.dto.cart.CartRecordAddRequest; import com.cultural.heritage.model.entity.CartRecord; @@ -34,9 +33,6 @@ import java.util.Map; public class CartRecordServiceImpl extends ServiceImpl implements CartRecordService { - @Resource - private GoodMapper goodMapper; - @Resource private GoodService goodService; diff --git a/src/main/resources/mapper/CartExperienceMapper.xml b/src/main/resources/mapper/CartExperienceMapper.xml new file mode 100644 index 0000000..f18c440 --- /dev/null +++ b/src/main/resources/mapper/CartExperienceMapper.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/src/main/resources/mapper/RefundRecordMapper.xml b/src/main/resources/mapper/RefundRecordMapper.xml index 6057893..15fc747 100644 --- a/src/main/resources/mapper/RefundRecordMapper.xml +++ b/src/main/resources/mapper/RefundRecordMapper.xml @@ -2,6 +2,6 @@ - + \ No newline at end of file