|
|
@@ -1007,6 +1007,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|
|
|
|
|
/**
|
|
|
* 修复缺失商品记录的订单:解析 orders.items (sku_list) 补写 t_order_goods
|
|
|
+ * 优先使用订单 paidAmount,其次 totalAmount 作为商品金额基准
|
|
|
*
|
|
|
* @param activityId 指定 activityId 修复单个订单,为 null 则批量修复所有缺失商品的订单
|
|
|
* @return 修复的订单数
|
|
|
@@ -1023,12 +1024,6 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|
|
int fixed = 0;
|
|
|
|
|
|
for (Order order : orders) {
|
|
|
- Long goodsCount = orderGoodsMapper.selectCount(
|
|
|
- new LambdaQueryWrapper<OrderGoods>().eq(OrderGoods::getOrderId, order.getId()));
|
|
|
- if (goodsCount != null && goodsCount > 0) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
String items = order.getItems();
|
|
|
if (items == null || items.isEmpty()) {
|
|
|
continue;
|
|
|
@@ -1040,6 +1035,28 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
+ // 先删除已有商品记录(可能是之前占位写入的零售价数据),以最新金额重写
|
|
|
+ orderGoodsMapper.delete(
|
|
|
+ new LambdaQueryWrapper<OrderGoods>().eq(OrderGoods::getOrderId, order.getId()));
|
|
|
+
|
|
|
+ // 计算每个商品的单价:用订单实付金额(或订单金额)均摊
|
|
|
+ BigDecimal orderAmount = order.getPaidAmount() != null
|
|
|
+ && order.getPaidAmount().compareTo(BigDecimal.ZERO) > 0
|
|
|
+ ? order.getPaidAmount()
|
|
|
+ : order.getTotalAmount() != null ? order.getTotalAmount() : BigDecimal.ZERO;
|
|
|
+
|
|
|
+ // 计算总商品数量,按数量比例分配金额
|
|
|
+ int totalQty = 0;
|
|
|
+ for (int i = 0; i < skuList.size(); i++) {
|
|
|
+ JSONObject sku = skuList.getJSONObject(i);
|
|
|
+ Integer number = sku.getInteger("number");
|
|
|
+ totalQty += (number != null ? Math.abs(number) : 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ BigDecimal unitPrice = totalQty > 0
|
|
|
+ ? orderAmount.divide(BigDecimal.valueOf(totalQty), 2, RoundingMode.HALF_UP)
|
|
|
+ : orderAmount;
|
|
|
+
|
|
|
for (int i = 0; i < skuList.size(); i++) {
|
|
|
JSONObject sku = skuList.getJSONObject(i);
|
|
|
String code = sku.getString("code");
|
|
|
@@ -1048,6 +1065,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
+ int qty = number != null ? Math.abs(number) : 1;
|
|
|
com.haha.entity.Product product = productService.getProductByCode(code);
|
|
|
|
|
|
OrderGoods goods = new OrderGoods();
|
|
|
@@ -1057,25 +1075,24 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|
|
goods.setDeviceId(order.getDeviceId());
|
|
|
goods.setUserId(order.getUserId());
|
|
|
goods.setCode(code);
|
|
|
- goods.setProductNum(number != null ? Math.abs(number) : 1);
|
|
|
+ goods.setProductNum(qty);
|
|
|
goods.setCreateTime(LocalDateTime.now());
|
|
|
+ goods.setPrice(unitPrice);
|
|
|
+ goods.setMoney(unitPrice.multiply(BigDecimal.valueOf(qty)));
|
|
|
|
|
|
if (product != null) {
|
|
|
goods.setProductId(product.getId());
|
|
|
goods.setBarCode(product.getBarcode());
|
|
|
goods.setProductName(product.getName());
|
|
|
goods.setPic(product.getPic());
|
|
|
- if (product.getRetailPrice() != null) {
|
|
|
- goods.setPrice(BigDecimal.valueOf(product.getRetailPrice()));
|
|
|
- goods.setMoney(BigDecimal.valueOf(product.getRetailPrice()));
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
orderGoodsMapper.insert(goods);
|
|
|
}
|
|
|
|
|
|
fixed++;
|
|
|
- log.info("补写订单商品成功 - orderId: {}, activityId: {}", order.getId(), order.getActivityId());
|
|
|
+ log.info("补写订单商品成功 - orderId: {}, activityId: {}, 金额基准: {}",
|
|
|
+ order.getId(), order.getActivityId(), orderAmount);
|
|
|
} catch (Exception e) {
|
|
|
log.error("补写订单商品失败 - orderId: {}", order.getId(), e);
|
|
|
}
|