|
@@ -0,0 +1,178 @@
|
|
|
|
|
+package com.ylx.massage.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
+import com.ylx.common.exception.ServiceException;
|
|
|
|
|
+import com.ylx.massage.domain.Product;
|
|
|
|
|
+import com.ylx.massage.domain.ProductOrderInfo;
|
|
|
|
|
+import com.ylx.massage.domain.ProductOrderItem;
|
|
|
|
|
+import com.ylx.massage.domain.dto.ProductOrderCreateRequest;
|
|
|
|
|
+import com.ylx.massage.mapper.ProductMapper;
|
|
|
|
|
+import com.ylx.massage.mapper.ProductOrderInfoMapper;
|
|
|
|
|
+import com.ylx.massage.mapper.ProductOrderItemMapper;
|
|
|
|
|
+import com.ylx.massage.service.IProductOrderInfoService;
|
|
|
|
|
+import com.ylx.massage.utils.OrderNumberGenerator;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 商品订单信息Service实现类
|
|
|
|
|
+ */
|
|
|
|
|
+@Service
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class ProductOrderInfoServiceImpl extends ServiceImpl<ProductOrderInfoMapper, ProductOrderInfo> implements IProductOrderInfoService {
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private ProductOrderInfoMapper productOrderInfoMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private ProductOrderItemMapper productOrderItemMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private OrderNumberGenerator orderNumberGenerator;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private ProductMapper productMapper;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 创建商品订单
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param request 订单创建请求
|
|
|
|
|
+ * @return String 订单编号
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public String createProductOrder(ProductOrderCreateRequest request) {
|
|
|
|
|
+ // 1. 参数校验
|
|
|
|
|
+ validateRequest(request);
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 生成订单编号
|
|
|
|
|
+ String orderNo = orderNumberGenerator.generateNextOrderNumber(OrderNumberGenerator.KEY_PREFIX_PRODUCTORDER);
|
|
|
|
|
+ log.info("创建商品订单,订单编号:{}", orderNo);
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 构建订单主表信息
|
|
|
|
|
+ ProductOrderInfo orderInfo = buildOrderInfo(request, orderNo);
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 保存订单主表
|
|
|
|
|
+ int insertResult = productOrderInfoMapper.insert(orderInfo);
|
|
|
|
|
+ if (insertResult <= 0) {
|
|
|
|
|
+ throw new ServiceException("订单创建失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("订单主表保存成功,订单编号:{}", orderNo);
|
|
|
|
|
+
|
|
|
|
|
+ // 5. 构建订单商品明细
|
|
|
|
|
+ ProductOrderItem orderItem = buildOrderItem(request, orderInfo);
|
|
|
|
|
+
|
|
|
|
|
+ // 6. 保存订单商品明细
|
|
|
|
|
+ int itemInsertResult = productOrderItemMapper.insert(orderItem);
|
|
|
|
|
+ if (itemInsertResult <= 0) {
|
|
|
|
|
+ throw new ServiceException("订单商品明细创建失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("订单商品明细保存成功,订单编号:{}", orderNo);
|
|
|
|
|
+ return orderNo;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 参数校验
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param request 订单创建请求
|
|
|
|
|
+ */
|
|
|
|
|
+ private void validateRequest(ProductOrderCreateRequest request) {
|
|
|
|
|
+ if (request == null) {
|
|
|
|
|
+ throw new ServiceException("订单请求不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (request.getOpenId() == null || request.getOpenId().trim().isEmpty()) {
|
|
|
|
|
+ throw new ServiceException("用户ID不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (request.getAddressId() == null) {
|
|
|
|
|
+ throw new ServiceException("地址ID不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (request.getProductId() == null) {
|
|
|
|
|
+ throw new ServiceException("商品ID不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ // 根据商品ID查询商品信息
|
|
|
|
|
+ Product product = productMapper.selectById(request.getProductId());
|
|
|
|
|
+ if (product == null) {
|
|
|
|
|
+ throw new ServiceException("商品不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (request.getSkuId() == null) {
|
|
|
|
|
+ throw new ServiceException("SKU ID不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (request.getQuantity() == null || request.getQuantity() <= 0) {
|
|
|
|
|
+ throw new ServiceException("购买数量必须大于0");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (request.getPayType() == null || (request.getPayType() != 1 && request.getPayType() != 2)) {
|
|
|
|
|
+ throw new ServiceException("支付方式不正确");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (request.getTotalAmount() == null || request.getTotalAmount() < 0) {
|
|
|
|
|
+ throw new ServiceException("总金额不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (request.getPayAmount() == null || request.getPayAmount() < 0) {
|
|
|
|
|
+ throw new ServiceException("支付金额不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 构建订单主表信息
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param request 订单创建请求
|
|
|
|
|
+ * @param orderNo 订单编号
|
|
|
|
|
+ * @return ProductOrderInfo 订单主表信息
|
|
|
|
|
+ */
|
|
|
|
|
+ private ProductOrderInfo buildOrderInfo(ProductOrderCreateRequest request, String orderNo) {
|
|
|
|
|
+ ProductOrderInfo orderInfo = new ProductOrderInfo();
|
|
|
|
|
+ orderInfo.setOrderNo(orderNo);
|
|
|
|
|
+ orderInfo.setOpenId(request.getOpenId());
|
|
|
|
|
+ orderInfo.setOrderStatus(0); // 待付款
|
|
|
|
|
+ orderInfo.setPayStatus(0); // 未支付
|
|
|
|
|
+ orderInfo.setPayType(request.getPayType());
|
|
|
|
|
+ orderInfo.setTotalAmount(request.getTotalAmount());
|
|
|
|
|
+ orderInfo.setDiscountAmount(0); // 优惠金额暂无
|
|
|
|
|
+ orderInfo.setFreightAmount(request.getFreightAmount() != null ? request.getFreightAmount() : 0);
|
|
|
|
|
+ orderInfo.setPayAmount(request.getPayAmount());
|
|
|
|
|
+ orderInfo.setPointsUsed(request.getPointsUsed() != null ? request.getPointsUsed() : 0);
|
|
|
|
|
+ orderInfo.setAddressId(request.getAddressId());
|
|
|
|
|
+ // 快递
|
|
|
|
|
+ orderInfo.setDeliveryType(1);
|
|
|
|
|
+ orderInfo.setBuyerRemark(request.getBuyerRemark());
|
|
|
|
|
+ // 支付超时时间15分钟
|
|
|
|
|
+ orderInfo.setPayExpireTime(LocalDateTime.now().plusMinutes(15));
|
|
|
|
|
+ return orderInfo;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 构建订单商品明细
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param request 订单创建请求
|
|
|
|
|
+ * @param orderInfo 订单主表信息
|
|
|
|
|
+ * @return ProductOrderItem 订单商品明细
|
|
|
|
|
+ */
|
|
|
|
|
+ private ProductOrderItem buildOrderItem(ProductOrderCreateRequest request, ProductOrderInfo orderInfo) {
|
|
|
|
|
+ ProductOrderItem orderItem = new ProductOrderItem();
|
|
|
|
|
+ orderItem.setOrderId(orderInfo.getId());
|
|
|
|
|
+ orderItem.setOrderNo(orderInfo.getOrderNo());
|
|
|
|
|
+ orderItem.setProductId(request.getProductId());
|
|
|
|
|
+ orderItem.setProductName(request.getName());
|
|
|
|
|
+ orderItem.setSkuId(request.getSkuId());
|
|
|
|
|
+ // 将SKU规格信息转为JSON
|
|
|
|
|
+ if (request.getSpecNameValueList() != null && !request.getSpecNameValueList().isEmpty()) {
|
|
|
|
|
+ orderItem.setSkuSpec(JSONUtil.toJsonStr(request.getSpecNameValueList()));
|
|
|
|
|
+ }
|
|
|
|
|
+ orderItem.setSkuImage(request.getProductMainImage());
|
|
|
|
|
+ // 单价 = 总金额 / 数量
|
|
|
|
|
+ orderItem.setPrice(request.getTotalAmount() / request.getQuantity());
|
|
|
|
|
+ orderItem.setQuantity(request.getQuantity());
|
|
|
|
|
+ orderItem.setTotalAmount(request.getTotalAmount());
|
|
|
|
|
+ orderItem.setDiscountAmount(0); // 优惠金额暂无
|
|
|
|
|
+ orderItem.setActualAmount(request.getPayAmount());
|
|
|
|
|
+ orderItem.setRefundStatus(0); // 无退款
|
|
|
|
|
+ return orderItem;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|