Parcourir la source

超出部分延长天数

zoujiajian il y a 10 mois
Parent
commit
cd7219a206

+ 23 - 0
netflix-dao/src/main/java/com/cyksj/model/dto/ClaudeCodeDeductDto.java

@@ -0,0 +1,23 @@
+package com.cyksj.model.dto;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.math.BigDecimal;
+
+/**
+ * 项目名: yhlxj11111111
+ * 文件名: ClaudeCodeDeductDto
+ * 创建者: JavaZou
+ * 创建时间:2025/10/16 16:53
+ */
+@Getter
+@Setter
+public class ClaudeCodeDeductDto {
+	private BigDecimal deductMoney;
+
+	/**
+	 * 超出部分可延长天数
+	 */
+	private Integer extendsDays;
+}

+ 5 - 0
netflix-dao/src/main/java/com/cyksj/model/entity/ClaudeCodeDeductOrderRelate.java

@@ -23,4 +23,9 @@ public class ClaudeCodeDeductOrderRelate extends BaseEntity{
 	 * 新规格车票id
 	 */
 	private Long newRelationId;
+
+	/**
+	 * 延长天数days
+	 */
+	private Integer extendsDays;
 }

+ 3 - 3
netflix-service/src/main/java/com/cyksj/service/claude/ClaudeCodeService.java

@@ -1,6 +1,7 @@
 package com.cyksj.service.claude;
 
 import cn.hutool.core.date.DateTime;
+import com.cyksj.model.dto.ClaudeCodeDeductDto;
 import com.cyksj.model.entity.ClaudeCodeUser;
 import com.cyksj.model.entity.GoodsDonSku;
 import com.cyksj.model.entity.GroupsRelation;
@@ -18,7 +19,6 @@ import com.cyksj.model.views.ClaudeCodeUserInfoView;
 import com.cyksj.model.views.ClaudeCodeUserResetRecordView;
 import com.ejlchina.searcher.SearchResult;
 
-import java.math.BigDecimal;
 import java.util.Date;
 import java.util.List;
 
@@ -130,7 +130,7 @@ public interface ClaudeCodeService {
 	/**
 	 * cc抵扣升级
 	 */
-	BigDecimal getDeductMoney(Long relationId, Long relationSkuId, Long renewSkuId, Long renewOrderId, Date renewExpiryTime, Date relationStartTime, Date relationExpiryTime, DateTime now, List<Long> userIdList);
+	ClaudeCodeDeductDto getDeductMoney(Long relationId, Long relationSkuId, Long renewSkuId, Long renewOrderId, Date renewExpiryTime, Date relationStartTime, Date relationExpiryTime, DateTime now, List<Long> userIdList, GoodsDonSku upgradeSku);
 
-	BigDecimal getDeductSkuMoney(Long userId, Long relationId, Long upgradeSkuId);
+	ClaudeCodeDeductDto getDeductSkuMoney(Long userId, Long relationId, Long upgradeSkuId);
 }

+ 29 - 10
netflix-service/src/main/java/com/cyksj/service/claude/impl/ClaudeCodeServiceImpl.java

@@ -21,6 +21,7 @@ import com.cyksj.mapper.claudecode.ClaudeCodeUserCreditCardRecordMapper;
 import com.cyksj.mapper.claudecode.ClaudeCodeUserResetRecordMapper;
 import com.cyksj.mapper.manage.distribute.DistributePointsExchangeMoneyMapper;
 import com.cyksj.mapper.manage.distribute.DistributeWaitingSendPointsMapper;
+import com.cyksj.model.dto.ClaudeCodeDeductDto;
 import com.cyksj.model.entity.*;
 import com.cyksj.model.manage.views.GroupsRelationView;
 import com.cyksj.model.request.ClaudeCodeApiKeysReq;
@@ -1026,7 +1027,7 @@ public class ClaudeCodeServiceImpl implements ClaudeCodeService {
 	}
 
 	@Override
-	public BigDecimal getDeductSkuMoney(Long userId, Long relationId, Long skuId) {
+	public ClaudeCodeDeductDto getDeductSkuMoney(Long userId, Long relationId, Long skuId) {
 		List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
 		GroupsRelationView relationView = beanSearcher.searchFirst(GroupsRelationView.class, MapUtils.builder()
 				.field(GroupsRelationView::getGoodsId, Constant.CLAUDE_CODE_GOODS_ID)
@@ -1034,13 +1035,16 @@ public class ClaudeCodeServiceImpl implements ClaudeCodeService {
 				.field(GroupsRelationView::getUserId, userIdList).op(Operator.InList)
 				.field(GroupsRelationView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
 				.build());
+		ClaudeCodeDeductDto claudeCodeDeductDto = new ClaudeCodeDeductDto();
 		if (relationView == null) {
-			return BigDecimal.ZERO;
+			claudeCodeDeductDto.setDeductMoney(BigDecimal.ZERO);
+			return claudeCodeDeductDto;
 		}
 		ClaudeCodeUser claudeCodeUser = claudeCodeUserMapper.selectOne(Wrappers.lambdaQuery(ClaudeCodeUser.class).eq(ClaudeCodeUser::getRelationId, relationId).last("limit 1"));
 		GoodsDonSku upgradeSku = skuMapper.selectById(skuId);
 		if (upgradeSku.getGoodsId() != Constant.CLAUDE_CODE_GOODS_ID) {
-			return BigDecimal.ZERO;
+			claudeCodeDeductDto.setDeductMoney(BigDecimal.ZERO);
+			return claudeCodeDeductDto;
 		}
 		GoodsDonSku relationPresentSku = null;
 		Long renewSkuId = claudeCodeUser.getRenewSkuId();
@@ -1051,24 +1055,27 @@ public class ClaudeCodeServiceImpl implements ClaudeCodeService {
 			relationPresentSku = skuMapper.selectById(relationView.getSkuId());
 		}
 		if (upgradeSku.getClaudeQuota().intValue() < relationPresentSku.getClaudeQuota().intValue()) {
-			return BigDecimal.ZERO;
+			claudeCodeDeductDto.setDeductMoney(BigDecimal.ZERO);
+			return claudeCodeDeductDto;
 		}
-		BigDecimal deductMoney = getDeductMoney(relationId, relationView.getSkuId(), claudeCodeUser.getRenewSkuId(), claudeCodeUser.getRenewOrderId(), claudeCodeUser.getRenewExpiryTime(), relationView.getStartTime(), relationView.getExpiryTime(), DateTime.now(), userIdList);
-		return deductMoney;
+		claudeCodeDeductDto = getDeductMoney(relationId, relationView.getSkuId(), claudeCodeUser.getRenewSkuId(), claudeCodeUser.getRenewOrderId(), claudeCodeUser.getRenewExpiryTime(), relationView.getStartTime(), relationView.getExpiryTime(), DateTime.now(), userIdList, upgradeSku);
+		return claudeCodeDeductDto;
 	}
 
 	@Override
-	public BigDecimal getDeductMoney(Long relationId, Long relationSkuId, Long renewSkuId, Long renewOrderId, Date renewExpiryTime, Date relationStartTime, Date relationExpiryTime, DateTime now, List<Long> userIdList) {
+	public ClaudeCodeDeductDto getDeductMoney(Long relationId, Long relationSkuId, Long renewSkuId, Long renewOrderId, Date renewExpiryTime, Date relationStartTime, Date relationExpiryTime, DateTime now, List<Long> userIdList, GoodsDonSku upgradeSku) {
 		BigDecimal deductMoney = BigDecimal.ZERO;
 		//一个月当30天算单价 往上取整
 		//续费规格
+		ClaudeCodeDeductDto claudeCodeDeductDto = new ClaudeCodeDeductDto();
 		if (renewSkuId != null && renewExpiryTime.compareTo(now) > 0) {
 			OrderDon renewOrderDon = orderDonMapper.selectById(renewOrderId);
 			BigDecimal orderMoney = renewOrderDon.getMoney().add(Optional.ofNullable(renewOrderDon.getBalance()).orElse(BigDecimal.ZERO));
 			GoodsDonSku presentSku = skuMapper.selectById(renewSkuId);
 			if (orderMoney.compareTo(BigDecimal.ZERO) == 0) {
 				//存在升级后的0元订单 即兑换码订单 不让抵扣
-				return BigDecimal.ZERO;
+				claudeCodeDeductDto.setDeductMoney(BigDecimal.ZERO);
+				return claudeCodeDeductDto;
 			} else {
 				Long betweenDay = DateUtil.betweenDay(now, renewExpiryTime, false) + 1;
 				//当天购买的按规格原价抵扣
@@ -1100,7 +1107,8 @@ public class ClaudeCodeServiceImpl implements ClaudeCodeService {
 				BigDecimal thisOrderMoney = thisRenewOrderDon.getMoney().add(Optional.ofNullable(thisRenewOrderDon.getBalance()).orElse(BigDecimal.ZERO));
 				if (thisOrderMoney.compareTo(BigDecimal.ZERO) == 0) {
 					//存在升级后的0元订单 即兑换码订单 不让抵扣
-					return BigDecimal.ZERO;
+					claudeCodeDeductDto.setDeductMoney(BigDecimal.ZERO);
+					return claudeCodeDeductDto;
 				}
 				//当天购买的按规格原价抵扣
 				BigDecimal thisRenewDeductMoney = BigDecimal.ZERO;
@@ -1154,7 +1162,18 @@ public class ClaudeCodeServiceImpl implements ClaudeCodeService {
 			deductMoney = deductMoney.add(deductMoneyFromTicket);
 		}
 		log.info("车票id:{}最终到期时间为:{}车票可返回 抵扣金额:{}", relationId, DateUtil.format(relationExpiryTime, "yyyy-MM-dd HH:mm:ss"), deductMoney);
-		return deductMoney;
+		//超出可抵扣的金额 直接返回
+		if (deductMoney.compareTo(upgradeSku.getPrice()) >= 0) {
+			BigDecimal overMoney = deductMoney.subtract(upgradeSku.getPrice());
+			//超过抵扣金额可换延长会员多少天
+			//每天单价
+			BigDecimal daySingle = upgradeSku.getPrice().divide(BigDecimal.valueOf(upgradeSku.getMonths())).divide(BigDecimal.valueOf(30), 0, RoundingMode.DOWN);
+			//超出部分可换取多少天 低于每天单价为0天
+			BigDecimal extendsDays = overMoney.divide(daySingle, 0, RoundingMode.UP);
+			claudeCodeDeductDto.setExtendsDays(extendsDays.intValue());
+		}
+		claudeCodeDeductDto.setDeductMoney(deductMoney);
+		return claudeCodeDeductDto;
 	}
 
 	private void adjClaudeCodeUserPoints(Long userId, Integer points, String type, String description) throws Exception {

+ 12 - 4
netflix-service/src/main/java/com/cyksj/service/order/impl/OrderDonServiceImpl.java

@@ -3355,6 +3355,10 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 				}
 				claudeCodeDeductOrderRelate.setNewRelationId(newRelationId);
 				claudeCodeDeductOrderRelateMapper.updateById(claudeCodeDeductOrderRelate);
+				if (claudeCodeDeductOrderRelate.getExtendsDays() != null && claudeCodeDeductOrderRelate.getExtendsDays() > 0) {
+					newRelation.setExpiryTime(DateUtil.offsetDay(newRelation.getExpiryTime(), claudeCodeDeductOrderRelate.getExtendsDays()));
+					groupsRelationMapper.updateById(newRelation);
+				}
 				//将订单车票id同步成新规格车票id
 				orderDon.setRelationId(newRelationId);
 				orderDonMapper.updateById(orderDon);
@@ -4792,7 +4796,9 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 		if (upgradeSku.getClaudeQuota().intValue() < relationPresentSku.getClaudeQuota().intValue()) {
 			throw BusinessRuntimeException.getInstance("无法抵扣升级该规格");
 		}
-		BigDecimal deductMoney = claudeCodeService.getDeductMoney(relationId, relationView.getSkuId(), claudeCodeUser.getRenewSkuId(), claudeCodeUser.getRenewOrderId(), claudeCodeUser.getRenewExpiryTime(), relationView.getStartTime(), relationView.getExpiryTime(), DateTime.now(), userIdList);
+		ClaudeCodeDeductDto claudeCodeDeductDto = claudeCodeService.getDeductMoney(relationId, relationView.getSkuId(), claudeCodeUser.getRenewSkuId(), claudeCodeUser.getRenewOrderId(), claudeCodeUser.getRenewExpiryTime(), relationView.getStartTime(), relationView.getExpiryTime(), DateTime.now(), userIdList, upgradeSku);
+		BigDecimal deductMoney = claudeCodeDeductDto.getDeductMoney();
+		Integer extendsDays = claudeCodeDeductDto.getExtendsDays();
 		//订单
 		OrderDon orderDon = new OrderDon();
 		orderDon.setUserId(userId);
@@ -4806,7 +4812,8 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 		orderDon.setRealMoney(price);
 		//抵扣后的金额
 		BigDecimal discountPrice = price.subtract(deductMoney);
-		orderDon.setMoney(discountPrice);
+		//0元订单
+		orderDon.setMoney(discountPrice.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : discountPrice);
 
 		StringBuilder payTitle = new StringBuilder();
 		payTitle.append(goodsDon.getTitle()).append(upgradeSku.getSpecVal());
@@ -4822,7 +4829,7 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 		deductBalancePayOrderMoney(orderDon, payRequest.getBalance(), user);
 		orderDonMapper.insert(orderDon);
 		//记录抵扣升级的规格
-		recordCcUpgradeOrder(orderDon, upgradeSku);
+		recordCcUpgradeOrder(orderDon, extendsDays);
 		Long orderId = orderDon.getId();
 		//使用银河币记录
 		userGalaxyCoinService.handleUserGalaxyCoinOrderNotify(orderDon.getId(), orderDon.getUserId(), orderDon.getOrderNo(), OrderDonGalaxyCoinRecord.Source.ordinary);
@@ -5595,10 +5602,11 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 		}
 	}
 
-	private void recordCcUpgradeOrder(OrderDon orderDon, GoodsDonSku upgradeSku) {
+	private void recordCcUpgradeOrder(OrderDon orderDon, Integer extendsDays) {
 		ClaudeCodeDeductOrderRelate claudeCodeDeductOrderRelate = new ClaudeCodeDeductOrderRelate();
 		claudeCodeDeductOrderRelate.setOrderId(orderDon.getId());
 		claudeCodeDeductOrderRelate.setRelationId(orderDon.getRelationId());
+		claudeCodeDeductOrderRelate.setExtendsDays(extendsDays);
 		claudeCodeDeductOrderRelateMapper.insert(claudeCodeDeductOrderRelate);
 	}
 }

+ 4 - 4
netflix-web/src/main/java/com/cyksj/web/controller/claude/ClaudeCodeController.java

@@ -15,6 +15,7 @@ import com.cyksj.mapper.GroupsMapper;
 import com.cyksj.mapper.GroupsRelationMapper;
 import com.cyksj.mapper.manage.coupon.*;
 import com.cyksj.mapper.manage.distribute.UserDistributeMapper;
+import com.cyksj.model.dto.ClaudeCodeDeductDto;
 import com.cyksj.model.entity.*;
 import com.cyksj.model.manage.views.OrderDonView;
 import com.cyksj.model.request.ClaudeCodeApiKeysReq;
@@ -42,7 +43,6 @@ import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
-import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -575,9 +575,9 @@ public class ClaudeCodeController {
 	 * 车票可抵扣升级金额
 	 */
 	@GetMapping("/get/deduct/money/{relationId}/{upgradeSkuId}")
-	public Result<BigDecimal> getDeductMoney(@PathVariable Long relationId, @PathVariable Long upgradeSkuId) {
+	public Result<ClaudeCodeDeductDto> getDeductMoney(@PathVariable Long relationId, @PathVariable Long upgradeSkuId) {
 		long userId = StpUserUtil.getLoginIdAsLong();
-		BigDecimal deductMoney = claudeCodeService.getDeductSkuMoney(userId, relationId, upgradeSkuId);
-		return GatewayResponse.SUCCESS.newBuilder().toResult(deductMoney);
+		ClaudeCodeDeductDto claudeCodeDeductDto = claudeCodeService.getDeductSkuMoney(userId, relationId, upgradeSkuId);
+		return GatewayResponse.SUCCESS.newBuilder().toResult(claudeCodeDeductDto);
 	}
 }