|
|
@@ -3,6 +3,7 @@ package com.etotem.cfc.service;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.etotem.cfc.common.Result;
|
|
|
import com.etotem.cfc.dto.CreateProductOrderDTO;
|
|
|
+import com.etotem.cfc.dto.OrderItemVO;
|
|
|
import com.etotem.cfc.dto.ProductOrderDTO;
|
|
|
import com.etotem.cfc.entity.Product;
|
|
|
import com.etotem.cfc.entity.ProductOrder;
|
|
|
@@ -14,8 +15,11 @@ import com.etotem.cfc.service.CommissionService;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
@@ -36,10 +40,18 @@ public class ProductOrderService {
|
|
|
@Resource
|
|
|
private CommissionService commissionService;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private PaymentService paymentService;
|
|
|
+
|
|
|
public Result<ProductOrderDTO> create(CreateProductOrderDTO dto, Long buyerId) {
|
|
|
if (buyerId == null) {
|
|
|
return Result.error("请先登录");
|
|
|
}
|
|
|
+
|
|
|
+ if (dto.getItems() != null && !dto.getItems().isEmpty()) {
|
|
|
+ return createMultiItem(dto, buyerId);
|
|
|
+ }
|
|
|
+
|
|
|
Product product = productMapper.selectById(dto.getProductId());
|
|
|
if (product == null) {
|
|
|
return Result.error("商品不存在");
|
|
|
@@ -65,13 +77,67 @@ public class ProductOrderService {
|
|
|
order.setStatus("pending");
|
|
|
order.setPaymentMethod(dto.getPaymentMethod() != null ? dto.getPaymentMethod() : "wechat");
|
|
|
order.setRemark(dto.getRemark());
|
|
|
+ order.setCoverImage(product.getCoverImage());
|
|
|
+ order.setDiscountAmount(dto.getDiscountAmount());
|
|
|
+ order.setCouponId(dto.getCouponId());
|
|
|
+ order.setAddressSnapshot(dto.getAddressSnapshot());
|
|
|
+ order.setCancelAt(new Date(System.currentTimeMillis() + 30 * 60 * 1000));
|
|
|
order.setCreatedAt(new Date());
|
|
|
order.setUpdatedAt(new Date());
|
|
|
orderMapper.insert(order);
|
|
|
return Result.success(ProductOrderDTO.from(order));
|
|
|
}
|
|
|
|
|
|
- public Result<ProductOrderDTO> pay(String orderNo, Long userId) {
|
|
|
+ private Result<ProductOrderDTO> createMultiItem(CreateProductOrderDTO dto, Long buyerId) {
|
|
|
+ int totalAmount = 0;
|
|
|
+ int totalQuantity = 0;
|
|
|
+ OrderItemVO firstItem = dto.getItems().get(0);
|
|
|
+ Product firstProduct = null;
|
|
|
+
|
|
|
+ for (OrderItemVO item : dto.getItems()) {
|
|
|
+ Product product = productMapper.selectById(item.getProductId());
|
|
|
+ if (product == null) {
|
|
|
+ return Result.error("商品不存在: " + item.getProductId());
|
|
|
+ }
|
|
|
+ if (!"on_shelf".equals(product.getStatus())) {
|
|
|
+ return Result.error("商品已下架: " + product.getName());
|
|
|
+ }
|
|
|
+ if (product.getStock() != null && product.getStock() < item.getQuantity()) {
|
|
|
+ return Result.error("库存不足: " + product.getName());
|
|
|
+ }
|
|
|
+ totalAmount += product.getPrice() * item.getQuantity();
|
|
|
+ totalQuantity += item.getQuantity();
|
|
|
+ if (firstProduct == null) {
|
|
|
+ firstProduct = product;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ User buyer = userMapper.selectById(buyerId);
|
|
|
+ ProductOrder order = new ProductOrder();
|
|
|
+ order.setOrderNo(generateOrderNo());
|
|
|
+ order.setProductId(firstProduct.getId());
|
|
|
+ order.setProductName(firstProduct.getName() + "等多件");
|
|
|
+ order.setProductType(firstProduct.getProductType());
|
|
|
+ order.setBuyerId(buyerId);
|
|
|
+ order.setFamilyId(buyer != null ? buyer.getFamilyId() : null);
|
|
|
+ order.setQuantity(totalQuantity);
|
|
|
+ order.setUnitPrice(firstProduct.getPrice());
|
|
|
+ order.setTotalAmount(totalAmount);
|
|
|
+ order.setStatus("pending");
|
|
|
+ order.setPaymentMethod(dto.getPaymentMethod() != null ? dto.getPaymentMethod() : "wechat");
|
|
|
+ order.setRemark(dto.getRemark());
|
|
|
+ order.setCoverImage(firstItem.getCoverImage() != null ? firstItem.getCoverImage() : firstProduct.getCoverImage());
|
|
|
+ order.setDiscountAmount(dto.getDiscountAmount());
|
|
|
+ order.setCouponId(dto.getCouponId());
|
|
|
+ order.setAddressSnapshot(dto.getAddressSnapshot());
|
|
|
+ order.setCancelAt(new Date(System.currentTimeMillis() + 30 * 60 * 1000));
|
|
|
+ order.setCreatedAt(new Date());
|
|
|
+ order.setUpdatedAt(new Date());
|
|
|
+ orderMapper.insert(order);
|
|
|
+ return Result.success(ProductOrderDTO.from(order));
|
|
|
+ }
|
|
|
+
|
|
|
+ public Result<Map<String, Object>> pay(String orderNo, Long userId) {
|
|
|
ProductOrder order = orderMapper.selectOne(
|
|
|
new LambdaQueryWrapper<ProductOrder>().eq(ProductOrder::getOrderNo, orderNo));
|
|
|
if (order == null) {
|
|
|
@@ -87,13 +153,37 @@ public class ProductOrderService {
|
|
|
if (!stockOk) {
|
|
|
return Result.error("库存不足");
|
|
|
}
|
|
|
+ Map<String, Object> wechatPayParams = paymentService.createWechatPrepay(
|
|
|
+ orderNo, order.getProductName(), order.getTotalAmount());
|
|
|
order.setStatus("paid");
|
|
|
order.setPaidAt(new Date());
|
|
|
order.setUpdatedAt(new Date());
|
|
|
orderMapper.updateById(order);
|
|
|
commissionService.settle(order.getId(), "product", order.getBuyerId(),
|
|
|
order.getTotalAmount(), order.getProductId());
|
|
|
- return Result.success(ProductOrderDTO.from(order));
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ result.put("order", ProductOrderDTO.from(order));
|
|
|
+ result.put("wechatPayParams", wechatPayParams);
|
|
|
+ return Result.success(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Result<String> handlePaymentSuccess(String orderNo, String transactionId) {
|
|
|
+ ProductOrder order = orderMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<ProductOrder>().eq(ProductOrder::getOrderNo, orderNo));
|
|
|
+ if (order == null) {
|
|
|
+ return Result.error("订单不存在");
|
|
|
+ }
|
|
|
+ if (!"pending".equals(order.getStatus())) {
|
|
|
+ return Result.success("订单状态非待支付,跳过");
|
|
|
+ }
|
|
|
+ order.setStatus("paid");
|
|
|
+ order.setTransactionId(transactionId);
|
|
|
+ order.setPaidAt(new Date());
|
|
|
+ order.setUpdatedAt(new Date());
|
|
|
+ orderMapper.updateById(order);
|
|
|
+ commissionService.settle(order.getId(), "product", order.getBuyerId(),
|
|
|
+ order.getTotalAmount(), order.getProductId());
|
|
|
+ return Result.success("支付成功");
|
|
|
}
|
|
|
|
|
|
public Result<String> cancel(String orderNo, Long userId) {
|
|
|
@@ -186,6 +276,25 @@ public class ProductOrderService {
|
|
|
return Result.success("订单已完成");
|
|
|
}
|
|
|
|
|
|
+ public Result<Map<String, Object>> queryLogistics(Long orderId, Long userId) {
|
|
|
+ ProductOrder order = orderMapper.selectById(orderId);
|
|
|
+ if (order == null) {
|
|
|
+ return Result.error("订单不存在");
|
|
|
+ }
|
|
|
+ Product product = productMapper.selectById(order.getProductId());
|
|
|
+ boolean isBuyer = order.getBuyerId().equals(userId);
|
|
|
+ boolean isVendor = product != null && product.getVendorId() != null && product.getVendorId().equals(userId);
|
|
|
+ if (!isBuyer && !isVendor) {
|
|
|
+ return Result.error("无权查看");
|
|
|
+ }
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ result.put("logisticsNo", order.getLogisticsNo());
|
|
|
+ result.put("logisticsCompany", order.getLogisticsCompany());
|
|
|
+ result.put("status", order.getStatus());
|
|
|
+ result.put("trackingData", new ArrayList<>());
|
|
|
+ return Result.success(result);
|
|
|
+ }
|
|
|
+
|
|
|
private String generateOrderNo() {
|
|
|
return "PO" + System.currentTimeMillis();
|
|
|
}
|