소스 검색

Merge branch 'master' into pre

zoujiajian 1 개월 전
부모
커밋
9ada62b656
1개의 변경된 파일과 23개의 추가작업 그리고 38개의 파일을 삭제
  1. 23 38
      netflix-service/src/main/java/com/cyksj/service/codex/impl/CodexServiceImpl.java

+ 23 - 38
netflix-service/src/main/java/com/cyksj/service/codex/impl/CodexServiceImpl.java

@@ -858,56 +858,41 @@ public class CodexServiceImpl implements CodexService {
 					.notIn(OrderDon::getStatus, Constant.noOrderStatus)
 					.orderByDesc(OrderDon::getId));
 		}
-		//原车票订单总实付金额
-		BigDecimal totalOrderMoney = BigDecimal.ZERO;
-		//原车票订单总天数
-		Integer totalDays = 0;
+		// 多笔订单的有效期是依次追加的,必须从最终到期时间向前逐笔划分,
+		// 不能把不同价格订单合并后用平均日价折算。
+		BigDecimal deductMoney = BigDecimal.ZERO;
+		Date segmentEndTime = relationExpiryTime;
 		for (OrderDon orderDon : orderDonList) {
-			GoodsDonSku sku = skuMapper.selectById(skuId);
+			GoodsDonSku sku = skuMapper.selectById(orderDon.getSkuId());
+			long orderDays = sku.getDays() != null ? sku.getDays() : sku.getMonths() * 30L;
+			Date segmentStartTime = DateUtil.offsetDay(segmentEndTime, -(int) orderDays);
+			// 当前订单段已经全部使用,排在它前面的订单段也必然已使用完。
+			if (!segmentEndTime.after(now)) {
+				break;
+			}
 			BigDecimal thisOrderMoney = orderDon.getMoney().add(Optional.ofNullable(orderDon.getBalance()).orElse(BigDecimal.ZERO)).subtract(Optional.ofNullable(orderDon.getRefundMoney()).orElse(BigDecimal.ZERO)).add(Optional.ofNullable(orderDon.getCouponMoney()).orElse(BigDecimal.ZERO));
 			OrderDonGalaxyCoinRecord orderDonGalaxyCoinRecord = orderDonGalaxyCoinRecordMapper.selectOne(Wrappers.lambdaQuery(OrderDonGalaxyCoinRecord.class).eq(OrderDonGalaxyCoinRecord::getOrderId, orderDon.getId()).last("limit 1"));
 			if (orderDonGalaxyCoinRecord != null) {
 				thisOrderMoney = thisOrderMoney.add(orderDonGalaxyCoinRecord.getDeductMoney());
 			}
+            CodexDeductRecord codexDeductRecord = codexDeductRecordMapper.selectOne(Wrappers.lambdaQuery(CodexDeductRecord.class).eq(CodexDeductRecord::getOrderId, orderDon.getId()).last("limit 1"));
+            if (codexDeductRecord != null) {
+				thisOrderMoney = thisOrderMoney.add(Optional.ofNullable(codexDeductRecord.getDeductMoney()).orElse(BigDecimal.ZERO));
+            }
 			if (thisOrderMoney.compareTo(BigDecimal.ZERO) == 0) {
 				thisOrderMoney = sku.getPrice();
 			}
-			totalOrderMoney = totalOrderMoney.add(thisOrderMoney);
-			//总时间
-			if (sku.getDays() != null) {
-				totalDays += sku.getDays();
+
+			if (!now.after(segmentStartTime)) {
+				// 该订单追加的时段尚未开始,完整价值参与抵扣。
+				deductMoney = deductMoney.add(thisOrderMoney);
 			} else {
-				totalDays += sku.getMonths() * 30;
-			}
-            CodexDeductRecord codexDeductRecord = codexDeductRecordMapper.selectOne(Wrappers.lambdaQuery(CodexDeductRecord.class).eq(CodexDeductRecord::getOrderId, orderDon.getId()).last("limit 1"));
-            if (codexDeductRecord != null) {
-                totalOrderMoney = totalOrderMoney.add(Optional.ofNullable(codexDeductRecord.getDeductMoney()).orElse(BigDecimal.ZERO));
-            }
-			//原订单数
-			if (orderDonList.size() == 1) {
-				//此次订单过期时间
-				Date thisOrderExpiryTime;
-				if (sku.getDays() != null) {
-					thisOrderExpiryTime = DateUtil.offsetDay(orderDon.getCreatedTime(), sku.getDays());
-				} else {
-					thisOrderExpiryTime = DateUtil.offsetMonth(orderDon.getCreatedTime(), sku.getMonths());
-				}
-				//当天购买的按规格原价抵扣
-				if (now.toDateStr().equals(DateUtil.format(orderDon.getCreatedTime(), "yyyy-MM-dd")) && (DateUtil.betweenDay(thisOrderExpiryTime, relationExpiryTime, true) == 0)) {
-                    if (codexDeductRecord != null) {
-                        thisOrderMoney = thisOrderMoney.add(Optional.ofNullable(codexDeductRecord.getDeductMoney()).orElse(BigDecimal.ZERO));
-                    }
-					return thisOrderMoney;
-				}
+				long remainingDays = Math.min(orderDays, DateUtil.betweenDay(now, segmentEndTime, false) + 1);
+				BigDecimal single = thisOrderMoney.divide(BigDecimal.valueOf(orderDays), 2, RoundingMode.HALF_UP);
+				deductMoney = deductMoney.add(single.multiply(BigDecimal.valueOf(remainingDays)));
 			}
+			segmentEndTime = segmentStartTime;
 		}
-		if (totalDays == 0) {
-			return BigDecimal.ZERO;
-		}
-		Long thisDeductBetweenDay = DateUtil.betweenDay(now, relationExpiryTime, false) + 1;
-		BigDecimal single = totalOrderMoney.divide(BigDecimal.valueOf(totalDays), 0, RoundingMode.HALF_UP);
-		//该车票抵扣金额
-		BigDecimal deductMoney = single.multiply(BigDecimal.valueOf(thisDeductBetweenDay));
 		return deductMoney;
 	}