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 9ae83b0..3fac68d 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CategoryController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CategoryController.java @@ -10,7 +10,7 @@ import com.cultural.heritage.common.ResultUtils; import com.cultural.heritage.constant.UserConstant; import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.ThrowUtils; -import com.cultural.heritage.model.dto.CommonDelBatchRequest; +import com.cultural.heritage.model.dto.CommonBatchRequest; import com.cultural.heritage.model.dto.CommonRequest; import com.cultural.heritage.model.dto.category.CategoryAddRequest; import com.cultural.heritage.model.dto.category.CategoryUpdateRequest; @@ -113,7 +113,7 @@ public class CategoryController { @PostMapping("/delBatch") @Operation(summary = "Web端管理员批量删除商品类别", description = "参数:类别删除请求体,权限:管理员(admin, boss),方法名:deleteCategory") @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) - public BaseResponse delBatchCategory(@RequestBody CommonDelBatchRequest commonDelBatchRequest) { + public BaseResponse delBatchCategory(@RequestBody CommonBatchRequest commonDelBatchRequest) { if (commonDelBatchRequest == null || CollectionUtils.isEmpty(commonDelBatchRequest.getIdList())) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } @@ -206,6 +206,7 @@ public class CategoryController { String typeName = category.getTypeName(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("type", typeName); + queryWrapper.eq("isShelves", 1); long count = goodService.count(queryWrapper); return count != 0; }).toList(); 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 6408079..347da08 100644 --- a/src/main/java/com/cultural/heritage/controller/good/GoodController.java +++ b/src/main/java/com/cultural/heritage/controller/good/GoodController.java @@ -14,7 +14,7 @@ import com.cultural.heritage.common.ResultUtils; import com.cultural.heritage.constant.UserConstant; import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.ThrowUtils; -import com.cultural.heritage.model.dto.CommonDelBatchRequest; +import com.cultural.heritage.model.dto.CommonBatchRequest; import com.cultural.heritage.model.dto.CommonRequest; import com.cultural.heritage.model.dto.appointment.AppointmentDateAddRequest; import com.cultural.heritage.model.dto.good.GoodAddRequest; @@ -26,6 +26,7 @@ import com.cultural.heritage.model.dto.good.service.ServiceGoodSingleUpdateReque import com.cultural.heritage.model.dto.good.service.ServiceGoodUpdateRequest; import com.cultural.heritage.model.dto.timeperiod.TimePeriodAddRequest; import com.cultural.heritage.model.entity.AppointmentDate; +import com.cultural.heritage.model.entity.CartRecord; import com.cultural.heritage.model.entity.Good; import com.cultural.heritage.model.entity.TimePeriod; import com.cultural.heritage.model.vo.appointment.AppointmentDateTimePeriodVO; @@ -35,6 +36,7 @@ import com.cultural.heritage.model.vo.good.ServiceGoodCardVO; import com.cultural.heritage.model.vo.good.ServiceGoodVO; import com.cultural.heritage.model.vo.timeperiod.TimePeriodVO; import com.cultural.heritage.service.good.AppointmentDateService; +import com.cultural.heritage.service.good.CartRecordService; import com.cultural.heritage.service.good.GoodService; import com.cultural.heritage.service.good.TimePeriodService; import com.cultural.heritage.service.user.UserService; @@ -47,6 +49,7 @@ import org.springframework.beans.BeanUtils; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; +import java.math.BigDecimal; import java.util.*; @@ -76,6 +79,11 @@ public class GoodController { private TimePeriodService timePeriodService; + @Resource + private CartRecordService cartRecordService; + + + /** * Web端管理员添加常规类商品 @@ -271,12 +279,27 @@ public class GoodController { if (goodUpdateRequest == null || goodUpdateRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } + + // 获取原来的商品价格 + Long sourceId = goodUpdateRequest.getId(); + Good resourceGood = goodService.getById(sourceId); + BigDecimal resourcePrice = resourceGood.getPrice(); Good good = new Good(); BeanUtils.copyProperties(goodUpdateRequest, good); // 校验 goodService.validGood(good, true); boolean result = goodService.updateById(good); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); + // 如果更新了商品的价格,就需要更新购物车中的小计 + Long id = good.getId(); + BigDecimal targetPrice = good.getPrice(); + + if (resourcePrice.compareTo(targetPrice) != 0) { + // 更新购物车中的小计 + List cartRecordList = goodService.updateCartGoodPrice(id, targetPrice); + boolean update = cartRecordService.updateBatchById(cartRecordList); + ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR, "购物车商品价格更新失败"); + } return ResultUtils.success(true); } @@ -323,7 +346,7 @@ public class GoodController { @PostMapping("/delBatch") @Operation(summary = "Web端管理员批量删除常规类商品", description = "参数:商品批量删除请求体,权限:管理员(admin, boss),方法名:delBatchGoods") @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) - public BaseResponse delBatchGoods(@RequestBody CommonDelBatchRequest commonDelBatchRequest) { + public BaseResponse delBatchGoods(@RequestBody CommonBatchRequest commonDelBatchRequest) { List idList = commonDelBatchRequest.getIdList(); boolean result = goodService.removeBatchByIds(idList); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); @@ -681,17 +704,6 @@ public class GoodController { -// /** -// * (小程序端)在支付订单前找出不合理的商品项 -// * @return 存在问题的购物车id列表 -// */ -// @PostMapping("/cart/pay/error") -// @Operation(summary = "(小程序端)在支付订单前找出不合理的商品项", description = "参数:无,权限:所有人,方法名:listInvalidCartIds") -// public BaseResponse> listInvalidCartIdsBeforePay(@RequestBody List cartPayRequests, HttpServletRequest request) { -// User loginUser = userService.getLoginUser(request); -// -// } - } 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 378d4df..55a044d 100644 --- a/src/main/java/com/cultural/heritage/controller/order/OrderController.java +++ b/src/main/java/com/cultural/heritage/controller/order/OrderController.java @@ -12,19 +12,21 @@ import com.cultural.heritage.constant.UserConstant; import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.ThrowUtils; import com.cultural.heritage.model.dto.CommonRequest; -import com.cultural.heritage.model.dto.order.OrderAddRequest; -import com.cultural.heritage.model.dto.order.OrderItemAddRequest; +import com.cultural.heritage.model.dto.cart.CartOrderAddRequest; +import com.cultural.heritage.model.dto.cart.CartOrderItemAddRequest; 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.vo.order.OrderVO; +import com.cultural.heritage.service.good.CartRecordService; import com.cultural.heritage.service.order.OrderItemService; import com.cultural.heritage.service.order.OrderService; import com.cultural.heritage.service.user.UserService; -import com.cultural.heritage.utils.OrderNumberUtils; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; @@ -37,7 +39,10 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; @RestController @RequestMapping("/order") @@ -60,6 +65,69 @@ public class OrderController { private UserService userService; + + @Resource + private CartRecordService cartRecordService; + + + + /** + * 用户通过购物车创建订单 + */ + @PostMapping("/add/cart") + @Transactional(rollbackFor = Exception.class) + @Operation(summary = "用户通过购物车创建订单", description = "参数:") + 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(); + + + + + // 封装成订单创建请求体 + OrderMainInfoAddRequest orderMainInfoAddRequest = new OrderMainInfoAddRequest(); + BeanUtils.copyProperties(cartOrderAddRequest, orderMainInfoAddRequest); + + // 校验购物车数据是否准确 + List cartOrderItemAddRequestList = cartOrderAddRequest.getCartOrderItemAddRequestList(); + + // 获取购物车id列表 + List cartIds = cartOrderItemAddRequestList.stream().map(CartOrderItemAddRequest::getCartRecordId).toList(); + // 根据id列表获取购物车商品信息 + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.in("id", cartIds); + List cartRecordList = cartRecordService.list(queryWrapper); + + // 封装成一个map集合(键:购物车id, 值:购物车商品数量) + Map map = new HashMap<>(); + for (CartOrderItemAddRequest cartOrderItemAddRequest : cartOrderItemAddRequestList) { + Long cartRecordId = cartOrderItemAddRequest.getCartRecordId(); + Integer quantity = cartOrderItemAddRequest.getQuantity(); + map.put(cartRecordId, quantity); + } + // 封装成订单明细主要信息请求体列表 + List orderItemMainInfoAddRequestList = new ArrayList<>(); + OrderItemMainInfoAddRequest orderItemMainInfoAddRequest; + for (CartRecord cartRecord : cartRecordList) { + Long goodId = cartRecord.getGoodId(); + Long id = cartRecord.getId(); + Integer quantity = map.get(id); + orderItemMainInfoAddRequest = new OrderItemMainInfoAddRequest(goodId, quantity, null, null); + orderItemMainInfoAddRequestList.add(orderItemMainInfoAddRequest); + } + // 填充到订单主要信息请求体 + orderMainInfoAddRequest.setOrderItemMainInfoAddRequestList(orderItemMainInfoAddRequestList); + + Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId); + return ResultUtils.success(orderId); + } + + + /** * 用户创建订单 * @param orderMainInfoAddRequest 订单创建请求体 @@ -67,43 +135,17 @@ public class OrderController { */ @PostMapping("/add") @Transactional(rollbackFor = Exception.class) - @Operation(summary = "小程序端创建用户订单", description = "参数:订单创建请求体,排序字段 sortField: totalAmount id createTime,权限:所有人,方法名:addOrder") + @Operation(summary = "小程序端创建用户订单", description = "参数:订单创建请求体,权限:所有人,方法名:addOrder") public BaseResponse addOrder(@RequestBody OrderMainInfoAddRequest orderMainInfoAddRequest, HttpServletRequest request) { if (orderMainInfoAddRequest == null) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } // 校验用户当前是否处于登录态 User loginUser = userService.getLoginUser(request); - // 封装订单详细信息请求体 - OrderAddRequest orderAddRequest = orderService.initOrderMainInfo(orderMainInfoAddRequest); - if (orderAddRequest == null) { - throw new BusinessException(ErrorCode.PARAMS_ERROR); - } - // 创建一个总订单 - Order order = new Order(); - BeanUtils.copyProperties(orderAddRequest, order); - order.setUserId(loginUser.getId()); - // 生成订单编号 - String orderNumber = OrderNumberUtils.generateOrderId(); - order.setOrderNumber(orderNumber); - // 设置订单状态 - order.setOrderStatus(OrderStatusConstant.PENDING_PAYMENT); - // 校验订单 - orderService.validOrder(order); - boolean result = orderService.save(order); - ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); - // 创建订单明细 - Long id = order.getId(); - List orderItemList = orderAddRequest.getOrderItemList(); - List newOrderItemsList = orderItemList.stream().map(orderItem -> { - OrderItems orderItems = new OrderItems(); - BeanUtils.copyProperties(orderItem, orderItems); - orderItems.setOrderId(id); - return orderItems; - }).toList(); - boolean save = orderItemService.saveBatch(newOrderItemsList); - ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR); - return ResultUtils.success(id); + Long userId = loginUser.getId(); + // 创建通用订单(常规类和服务类商品) + Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId); + return ResultUtils.success(orderId); } @@ -249,5 +291,4 @@ public class OrderController { - } diff --git a/src/main/java/com/cultural/heritage/model/dto/CommonDelBatchRequest.java b/src/main/java/com/cultural/heritage/model/dto/CommonBatchRequest.java similarity index 88% rename from src/main/java/com/cultural/heritage/model/dto/CommonDelBatchRequest.java rename to src/main/java/com/cultural/heritage/model/dto/CommonBatchRequest.java index 5c98cc1..d2356d4 100644 --- a/src/main/java/com/cultural/heritage/model/dto/CommonDelBatchRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/CommonBatchRequest.java @@ -9,7 +9,7 @@ import java.util.List; @Data @Schema(description = "商品批量删除请求体", requiredProperties = {"idList"}) -public class CommonDelBatchRequest implements Serializable { +public class CommonBatchRequest implements Serializable { @Schema(description = "批量删除的商品id列表(id > 0)", example = "[8, 9, 17]") 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 new file mode 100644 index 0000000..f4933cc --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/cart/CartOrderAddRequest.java @@ -0,0 +1,69 @@ +package com.cultural.heritage.model.dto.cart; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; +import java.math.BigDecimal; +import java.util.List; + +@Data +@Schema(description = "购物车订单创建请求体", requiredProperties = {"orderType", "userName", "addressId", + "couponId", "totalAmount", "note", "cartPayRequestList"}) +public class CartOrderAddRequest 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 = "500") + private BigDecimal totalAmount; + + + /** + * 订单备注 + */ + @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/cart/CartPayRequest.java b/src/main/java/com/cultural/heritage/model/dto/cart/CartOrderItemAddRequest.java similarity index 60% rename from src/main/java/com/cultural/heritage/model/dto/cart/CartPayRequest.java rename to src/main/java/com/cultural/heritage/model/dto/cart/CartOrderItemAddRequest.java index 092121b..a33e3d8 100644 --- a/src/main/java/com/cultural/heritage/model/dto/cart/CartPayRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/cart/CartOrderItemAddRequest.java @@ -4,15 +4,19 @@ package com.cultural.heritage.model.dto.cart; 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 CartPayRequest { +@Schema(description = "购物车订单明细创建请求体", requiredProperties = {"cartRecordId", "quantity"}) +public class CartOrderItemAddRequest implements Serializable { + /** * 购物车id */ @Schema(description = "购物车id(id > 0)", example = "20") - private Long id; + private Long cartRecordId; /** * 数量 @@ -21,5 +25,6 @@ public class CartPayRequest { private Integer quantity; + @Serial private static final long serialVersionUID = 1L; } diff --git a/src/main/java/com/cultural/heritage/model/dto/order/OrderAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/order/OrderAddRequest.java index 660063f..0e47816 100644 --- a/src/main/java/com/cultural/heritage/model/dto/order/OrderAddRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/order/OrderAddRequest.java @@ -12,7 +12,7 @@ import java.math.BigDecimal; import java.util.List; @Data -@Schema(description = "订单添加请求体", requiredProperties = {"userId", "orderNumber", "addressSnapshot", "contactsSnapshot", +@Schema(description = "订单添加请求体", requiredProperties = {"userId", "addressSnapshot", "contactsSnapshot", "couponSnapshot", "totalAmount", "orderStatus", "orderItemList"}) public class OrderAddRequest implements Serializable { @@ -30,12 +30,6 @@ public class OrderAddRequest implements Serializable { @Schema(description = "用户昵称", example = "张三") private String userName; - /** - * 订单编号 - */ - @Schema(description = "订单编号", example = "20241105342838428324834") - private String orderNumber; - /** * 地址信息快照 diff --git a/src/main/java/com/cultural/heritage/model/dto/order/capital/OrderItemMainInfoAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/order/capital/OrderItemMainInfoAddRequest.java index 8d781d8..41df2dd 100644 --- a/src/main/java/com/cultural/heritage/model/dto/order/capital/OrderItemMainInfoAddRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/order/capital/OrderItemMainInfoAddRequest.java @@ -1,12 +1,14 @@ package com.cultural.heritage.model.dto.order.capital; import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; import lombok.Data; import java.io.Serial; import java.io.Serializable; @Data +@AllArgsConstructor @Schema(description = "订单明细主要信息请求体", requiredProperties = {"goodId", "quantity"}) public class OrderItemMainInfoAddRequest implements Serializable { @@ -41,4 +43,5 @@ public class OrderItemMainInfoAddRequest implements Serializable { @Serial private static final long serialVersionUID = 1L; + } diff --git a/src/main/java/com/cultural/heritage/model/entity/Order.java b/src/main/java/com/cultural/heritage/model/entity/Order.java index df57b34..9871f1e 100644 --- a/src/main/java/com/cultural/heritage/model/entity/Order.java +++ b/src/main/java/com/cultural/heritage/model/entity/Order.java @@ -95,6 +95,10 @@ public class Order implements Serializable { */ private String note; + /** + * 物流单号 + */ + private String trackingNumber; /** * 创建时间 diff --git a/src/main/java/com/cultural/heritage/model/vo/order/OrderVO.java b/src/main/java/com/cultural/heritage/model/vo/order/OrderVO.java index 3429cb6..599ce09 100644 --- a/src/main/java/com/cultural/heritage/model/vo/order/OrderVO.java +++ b/src/main/java/com/cultural/heritage/model/vo/order/OrderVO.java @@ -77,6 +77,11 @@ public class OrderVO implements Serializable { */ private String note; + /** + * 物流单号 + */ + private String trackingNumber; + /** * 订单明细信息 diff --git a/src/main/java/com/cultural/heritage/service/good/CartRecordService.java b/src/main/java/com/cultural/heritage/service/good/CartRecordService.java index a7ba1a1..69024c6 100644 --- a/src/main/java/com/cultural/heritage/service/good/CartRecordService.java +++ b/src/main/java/com/cultural/heritage/service/good/CartRecordService.java @@ -1,6 +1,7 @@ package com.cultural.heritage.service.good; import com.baomidou.mybatisplus.extension.service.IService; +import com.cultural.heritage.model.dto.cart.CartOrderItemAddRequest; import com.cultural.heritage.model.dto.cart.CartRecordAddRequest; import com.cultural.heritage.model.entity.CartRecord; import com.cultural.heritage.model.vo.cart.CartRecordVO; @@ -28,10 +29,11 @@ public interface CartRecordService extends IService { void validCart(CartRecord cartRecord, boolean update); + /** - * 校验购物车商品项的准确性 + * 根据购物车订单明细列表校验购物车商品项的准确性 */ - boolean validIsConsistent(CartRecord cartRecord); + boolean validIsConsistent(List cartOrderItemAddRequestList); 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 a110880..73df18e 100644 --- a/src/main/java/com/cultural/heritage/service/good/GoodService.java +++ b/src/main/java/com/cultural/heritage/service/good/GoodService.java @@ -3,8 +3,10 @@ package com.cultural.heritage.service.good; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.IService; import com.cultural.heritage.model.dto.good.GoodQueryRequest; +import com.cultural.heritage.model.entity.CartRecord; import com.cultural.heritage.model.entity.Good; +import java.math.BigDecimal; import java.util.List; public interface GoodService extends IService { @@ -26,4 +28,9 @@ public interface GoodService extends IService { */ void validGood(Good good, boolean update); + + /** + * 更新购物车中的小计 + */ + List updateCartGoodPrice(Long goodId, BigDecimal targetPrice); } 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 c612dcc..cf16812 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 @@ -7,12 +7,13 @@ 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; import com.cultural.heritage.model.entity.Good; import com.cultural.heritage.model.entity.User; -import com.cultural.heritage.model.vo.cart.CartRecordVO; import com.cultural.heritage.model.vo.cart.CartGoodVO; +import com.cultural.heritage.model.vo.cart.CartRecordVO; import com.cultural.heritage.service.good.CartRecordService; import com.cultural.heritage.service.good.GoodService; import com.cultural.heritage.service.user.UserService; @@ -24,7 +25,9 @@ import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import java.math.BigDecimal; +import java.util.HashMap; import java.util.List; +import java.util.Map; @Service @Slf4j @@ -57,12 +60,22 @@ public class CartRecordServiceImpl extends ServiceImpl good.getInventory()) { + throw new BusinessException(ErrorCode.SYSTEM_ERROR, "商品库存不足, 无法添加"); + } // 如果购物车中已存在该商品, 就叠加数量 - cartRecord.setQuantity(cartRecord.getQuantity() + cartRecordAddRequest.getQuantity()); - cartRecord.setSubtotal(good.getPrice().multiply(BigDecimal.valueOf(cartRecord.getQuantity()))); + cartRecord.setQuantity(currentQuantity); + cartRecord.setSubtotal(good.getPrice().multiply(BigDecimal.valueOf(currentQuantity))); boolean result = this.updateById(cartRecord); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "更新购物车商品数量失败"); } else { + // 判断数量是否超出库存 + Integer currentQuantity = cartRecordAddRequest.getQuantity(); + if (currentQuantity > good.getInventory()) { + throw new BusinessException(ErrorCode.SYSTEM_ERROR, "商品库存不足, 无法添加"); + } // 如果购物车不存在该商品,就向购物车中添加一项 CartRecord cartGood = new CartRecord(); BeanUtils.copyProperties(cartRecordAddRequest, cartGood); @@ -85,6 +98,18 @@ 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); + // 封装map集合(键:商品id, 值:商品详情信息) + Map map = new HashMap<>(); + for (Good good : goodList) { + map.put(good.getId(), good); + } + return list.stream().map(cartRecord -> { // 封装购物车记录VO CartRecordVO cartRecordVO = new CartRecordVO(); @@ -92,9 +117,7 @@ public class CartRecordServiceImpl extends ServiceImpl queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("id", goodId); - Good good = goodMapper.selectOne(queryWrapper); + Good good = map.get(goodId); CartGoodVO goodVO = new CartGoodVO(); BeanUtils.copyProperties(good, goodVO); cartRecordVO.setCartGoodVO(goodVO); @@ -134,11 +157,8 @@ public class CartRecordServiceImpl extends ServiceImpl cartOrderItemAddRequestList) { + return false; + } + /** * 找出存在问题的购物车id列表 */ @Override public List getInvalidCartIds(List cartRecords) { - List cartRecordList = cartRecords.stream().filter(cartRecord -> !validIsConsistent(cartRecord)).toList(); + // 获取购物车的商品id列表 + List goodIds = cartRecords.stream().map(CartRecord::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 cartRecordList = cartRecords.stream().filter(cartRecord -> { + Long goodId = cartRecord.getGoodId(); + Good good = map.get(goodId); + return !validIsConsistent(cartRecord, good); + }).toList(); return cartRecordList.stream().map(CartRecord::getId).toList(); } } diff --git a/src/main/java/com/cultural/heritage/service/good/impl/GoodServiceImpl.java b/src/main/java/com/cultural/heritage/service/good/impl/GoodServiceImpl.java index f8191c5..bc5c872 100644 --- a/src/main/java/com/cultural/heritage/service/good/impl/GoodServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/good/impl/GoodServiceImpl.java @@ -5,11 +5,14 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.cultural.heritage.common.ErrorCode; import com.cultural.heritage.constant.CommonConstant; import com.cultural.heritage.exception.BusinessException; +import com.cultural.heritage.mapper.CartRecordMapper; import com.cultural.heritage.mapper.GoodMapper; import com.cultural.heritage.model.dto.good.GoodQueryRequest; +import com.cultural.heritage.model.entity.CartRecord; import com.cultural.heritage.model.entity.Good; import com.cultural.heritage.service.good.GoodService; import com.cultural.heritage.utils.SqlUtils; +import jakarta.annotation.Resource; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; @@ -22,6 +25,11 @@ import java.util.List; @Service public class GoodServiceImpl extends ServiceImpl implements GoodService { + + @Resource + private CartRecordMapper cartRecordMapper; + + /** * 商品查询条件 */ @@ -107,5 +115,21 @@ public class GoodServiceImpl extends ServiceImpl implements Go } + /** + * 更新购物车中的小计 + */ + @Override + public List updateCartGoodPrice(Long goodId, BigDecimal targetPrice) { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("goodId", goodId); + List cartRecordList = cartRecordMapper.selectList(queryWrapper); + // 更新购物车中的小计 + for (CartRecord cartRecord : cartRecordList) { + Integer quantity = cartRecord.getQuantity(); + cartRecord.setSubtotal(targetPrice.multiply(BigDecimal.valueOf(quantity))); + } + return cartRecordList; + } + } 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 329b80b..93e9556 100644 --- a/src/main/java/com/cultural/heritage/service/order/OrderService.java +++ b/src/main/java/com/cultural/heritage/service/order/OrderService.java @@ -26,4 +26,11 @@ public interface OrderService extends IService { * 校验订单 */ void validOrder(Order order); + + + + /** + * 创建通用订单(常规类和服务类商品) + */ + Long createCommonOrder(OrderMainInfoAddRequest orderMainInfoAddRequest, Long userId); } 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 ae0df72..9fdd00b 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 @@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.cultural.heritage.common.ErrorCode; import com.cultural.heritage.constant.CommonConstant; +import com.cultural.heritage.constant.OrderStatusConstant; import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.ThrowUtils; import com.cultural.heritage.mapper.*; @@ -18,8 +19,11 @@ 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.Order; +import com.cultural.heritage.model.entity.OrderItems; import com.cultural.heritage.model.enums.GoodTypeEnum; +import com.cultural.heritage.service.order.OrderItemService; import com.cultural.heritage.service.order.OrderService; +import com.cultural.heritage.utils.OrderNumberUtils; import com.cultural.heritage.utils.SqlUtils; import jakarta.annotation.Resource; import org.apache.commons.lang3.ObjectUtils; @@ -52,6 +56,10 @@ public class OrderServiceImpl extends ServiceImpl implements + @Resource + private OrderItemService orderItemService; + + /** * 获取查询条件 */ @@ -178,6 +186,47 @@ public class OrderServiceImpl extends ServiceImpl implements + + /** + * 创建通用订单(常规类和服务类商品) + */ + @Override + public Long createCommonOrder(OrderMainInfoAddRequest orderMainInfoAddRequest, Long userId) { + // 封装订单详细信息请求体 + OrderAddRequest orderAddRequest = this.initOrderMainInfo(orderMainInfoAddRequest); + if (orderAddRequest == null) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + // 创建一个总订单 + Order order = new Order(); + BeanUtils.copyProperties(orderAddRequest, order); + order.setUserId(userId); + // 生成订单编号 + String orderNumber = OrderNumberUtils.generateOrderId(); + order.setOrderNumber(orderNumber); + // 设置物流单号 + order.setTrackingNumber("无"); + // 设置订单状态 + order.setOrderStatus(OrderStatusConstant.PENDING_PAYMENT); + // 校验订单 + this.validOrder(order); + boolean result = this.save(order); + ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); + // 创建订单明细 + Long id = order.getId(); + List orderItemList = orderAddRequest.getOrderItemList(); + List newOrderItemsList = orderItemList.stream().map(orderItem -> { + OrderItems orderItems = new OrderItems(); + BeanUtils.copyProperties(orderItem, orderItems); + orderItems.setOrderId(id); + return orderItems; + }).toList(); + boolean save = orderItemService.saveBatch(newOrderItemsList); + ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR); + return id; + } + + /** * 根据id获取订单(地址,联系人,优惠券,商品)信息 */ @@ -185,20 +234,15 @@ public class OrderServiceImpl extends ServiceImpl implements // 创建查询条件 QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("id", id); - // 查询实体对象 T entity = mapper.selectOne(queryWrapper); - // 判断实体对象是否存在,不存在则抛出异常 ThrowUtils.throwIf(entity == null, ErrorCode.NOT_FOUND_ERROR); - try { // 创建快照对象实例 S snapshot = snapshotClass.getDeclaredConstructor().newInstance(); - // 复制属性到快照对象 BeanUtils.copyProperties(entity, snapshot); - return snapshot; } catch (Exception e) { throw new BusinessException(ErrorCode.SYSTEM_ERROR);