zoujiajian 11 місяців тому
батько
коміт
ef5117392c

+ 36 - 20
netflix-service/src/main/java/com/cyksj/service/vip/impl/VipFrontServiceImpl.java

@@ -222,7 +222,7 @@ public class VipFrontServiceImpl implements VipFrontService {
 				deductMoney = deductMoney.add(getGeneralRemainNumDeductMoney(relationId, goodsId, skuId, userIdList));
 			} else if (goodsId == Constant.MIDJOURNEY_GOODS_ID) {
 				//mj
-				deductMoney = deductMoney.add(getMjRemainNumDeductMoney(relationId, skuId, userIdList));
+				deductMoney = deductMoney.add(getMjRemainNumDeductMoney(relationId, skuId, userIdList, relationExpiryTime));
 			} else {
 				//该车票抵扣金额
 				deductMoney = deductMoney.add(getDeductMoneyFromTicket(relationId, userIdList, skuId, now, relation.getStartTime(), relationExpiryTime));
@@ -448,8 +448,8 @@ public class VipFrontServiceImpl implements VipFrontService {
 			if (orderExpiryTime.before(now)) {
 				continue;
 			}
-			if (orderDon.getMoney().compareTo(BigDecimal.ZERO) >= 0) {
-				orderTotalMoney = orderTotalMoney.add(orderDon.getMoney());
+			if (orderDon.getMoney().compareTo(BigDecimal.ZERO) > 0) {
+				orderTotalMoney = orderTotalMoney.add(orderDon.getMoney().add(Optional.ofNullable(orderDon.getBalance()).orElse(BigDecimal.ZERO)));
 			} else {
 				//兑换码订单使用规格价格
 				orderTotalMoney = orderTotalMoney.add(sku.getPrice());
@@ -463,7 +463,7 @@ public class VipFrontServiceImpl implements VipFrontService {
 		//加量包订单
 		List<UpgradeOrderDon> upgradeOrderDons = upgradeOrderDonMapper.selectList(Wrappers.lambdaQuery(UpgradeOrderDon.class).eq(UpgradeOrderDon::getRelationId, relationId).in(UpgradeOrderDon::getUserId, userIdList).notIn(UpgradeOrderDon::getStatus, Constant.noOrderAllStatus));
 		for (UpgradeOrderDon upgradeOrderDon : upgradeOrderDons) {
-			orderTotalMoney = orderTotalMoney.add(upgradeOrderDon.getMoney());
+			orderTotalMoney = orderTotalMoney.add(upgradeOrderDon.getMoney().add(Optional.ofNullable(upgradeOrderDon.getBalance()).orElse(BigDecimal.ZERO)));
 			totalNum = totalNum + upgradeOrderDon.getNum();
 		}
 		Integer remainNum = null;
@@ -482,7 +482,7 @@ public class VipFrontServiceImpl implements VipFrontService {
 		if (remainNum == null) {
 			deductMoney = deductMoney.add(orderTotalMoney);
 		} else {
-			BigDecimal remainMoney = BigDecimal.valueOf(remainNum).divide(BigDecimal.valueOf(totalNum), 2, RoundingMode.UP).multiply(orderTotalMoney);
+			BigDecimal remainMoney = BigDecimal.valueOf(remainNum).divide(BigDecimal.valueOf(totalNum), 2, RoundingMode.DOWN).multiply(orderTotalMoney);
 			deductMoney = deductMoney.add(remainMoney);
 		}
 		return deductMoney;
@@ -491,12 +491,13 @@ public class VipFrontServiceImpl implements VipFrontService {
 	/**
 	 * 获取mj剩余次数可抵扣金额
 	 */
-	private BigDecimal getMjRemainNumDeductMoney(Long relationId, Long skuId, List<Long> userIdList) {
+	private BigDecimal getMjRemainNumDeductMoney(Long relationId, Long skuId, List<Long> userIdList, Date relationExpiryTime) {
 		BigDecimal deductMoney = BigDecimal.ZERO;
 		BigDecimal orderTotalMoney = BigDecimal.ZERO;
 		//总次数
-		Integer fastTotalNum = 0;
-		Integer relaxTotalNum = 0;
+		Integer totalNum = null;
+		//原车票订单总天数
+		Integer totalDays = 0;
 		DateTime now = DateTime.now();
 
 		List<OrderDon> orderDonList = orderDonMapper.selectList(Wrappers.lambdaQuery(OrderDon.class).eq(OrderDon::getRelationId, relationId).in(OrderDon::getUserId, userIdList).notIn(OrderDon::getStatus, Constant.noOrderAllStatus));
@@ -512,18 +513,21 @@ public class VipFrontServiceImpl implements VipFrontService {
 			} else {
 				orderExpiryTime = DateUtil.offsetMonth(orderCreatedTime, months);
 			}
+			//总时间
+			totalDays += sku.getMonths() * 30;
 			//过期的订单不算入
 			if (orderExpiryTime.before(now)) {
 				continue;
 			}
-			if (orderDon.getMoney().compareTo(BigDecimal.ZERO) >= 0) {
-				orderTotalMoney = orderTotalMoney.add(orderDon.getMoney());
+			if (orderDon.getMoney().compareTo(BigDecimal.ZERO) > 0) {
+				orderTotalMoney = orderTotalMoney.add(orderDon.getMoney().add(Optional.ofNullable(orderDon.getBalance()).orElse(BigDecimal.ZERO)));
 			} else {
 				//兑换码订单使用规格价格
 				orderTotalMoney = orderTotalMoney.add(sku.getPrice());
 			}
-			fastTotalNum += sku.getMjFastNum() * sku.getMonths();
-			relaxTotalNum += sku.getMjRelaxNum() * sku.getMonths();
+			if (totalNum == null) {
+				totalNum = sku.getMjFastNum() + sku.getMjRelaxNum();
+			}
 		}
 		Integer upgradeNum = 0;
 		BigDecimal upgradeOrderMoney = BigDecimal.ZERO;
@@ -531,24 +535,36 @@ public class VipFrontServiceImpl implements VipFrontService {
 		List<UpgradeOrderDon> upgradeOrderDons = upgradeOrderDonMapper.selectList(Wrappers.lambdaQuery(UpgradeOrderDon.class).eq(UpgradeOrderDon::getRelationId, relationId).in(UpgradeOrderDon::getUserId, userIdList).notIn(UpgradeOrderDon::getStatus, Constant.noOrderAllStatus));
 		for (UpgradeOrderDon upgradeOrderDon : upgradeOrderDons) {
 			upgradeNum += upgradeOrderDon.getNum();
-			upgradeOrderMoney = upgradeOrderMoney.add(upgradeOrderDon.getMoney());
+			upgradeOrderMoney = upgradeOrderMoney.add(upgradeOrderDon.getMoney().add(Optional.ofNullable(upgradeOrderDon.getBalance()).orElse(BigDecimal.ZERO)));
 		}
 		MidjourneyUser midjourneyUser = midjourneyUserMapper.selectOne(Wrappers.lambdaQuery(MidjourneyUser.class).eq(MidjourneyUser::getRelationId, relationId).last("limit 1"));
 		if (midjourneyUser == null) {
 			deductMoney = deductMoney.add(orderTotalMoney).add(upgradeOrderMoney);
 		} else {
-			Integer remainFastNum = midjourneyUser.getMjFastNum();
-			Integer remainRelaxNum = midjourneyUser.getMjRelaxNum();
 			if (CollUtil.isNotEmpty(orderDonList)) {
-				BigDecimal fastMoney = BigDecimal.valueOf(remainFastNum).divide(BigDecimal.valueOf(fastTotalNum), 2, RoundingMode.UP).multiply(orderTotalMoney);
-				BigDecimal relaxMoney = BigDecimal.valueOf(remainRelaxNum).divide(BigDecimal.valueOf(relaxTotalNum), 2, RoundingMode.UP).multiply(orderTotalMoney);
-				deductMoney = deductMoney.add(fastMoney).add(relaxMoney);
+				Integer remainFastNum = midjourneyUser.getMjFastNum();
+				Integer remainRelaxNum = midjourneyUser.getMjRelaxNum();
+				Integer usedNum = totalNum - remainFastNum - remainRelaxNum;
+				if (usedNum == 0 && DateUtil.isSameDay(now, relationExpiryTime)) {
+					deductMoney = deductMoney.add(orderTotalMoney);
+				} else {
+					//每天单价
+					BigDecimal single = orderTotalMoney.divide(BigDecimal.valueOf(totalDays), 0, RoundingMode.HALF_UP);
+					BigDecimal consumeRate = BigDecimal.valueOf(usedNum).divide(BigDecimal.valueOf(totalNum), 2, RoundingMode.DOWN);
+					//消耗的次数当月 值多少钱
+					BigDecimal consumeMoney = consumeRate.multiply(single.multiply(BigDecimal.valueOf(30)));
+					//该车票多长时间
+					Long thisDeductBetweenDay = DateUtil.betweenDay(now, relationExpiryTime, false) + 1;
+					//该车票抵扣金额
+					BigDecimal orderRemainMoney = single.multiply(BigDecimal.valueOf(thisDeductBetweenDay));
+					//最终可抵扣
+					deductMoney = deductMoney.add(orderRemainMoney.subtract(consumeMoney));
+				}
 			}
-
 			//加量包
 			Integer remainUpgradeNum = midjourneyUser.getMjUpgradeNum();
 			if (CollUtil.isNotEmpty(upgradeOrderDons)) {
-				BigDecimal upgradeMoney = BigDecimal.valueOf(remainUpgradeNum).divide(BigDecimal.valueOf(upgradeNum), 2, RoundingMode.UP).multiply(upgradeOrderMoney);
+				BigDecimal upgradeMoney = BigDecimal.valueOf(remainUpgradeNum).divide(BigDecimal.valueOf(upgradeNum), 2, RoundingMode.DOWN).multiply(upgradeOrderMoney);
 				deductMoney = deductMoney.add(upgradeMoney);
 			}
 		}