|
|
@@ -22,6 +22,7 @@ import com.cyksj.mapper.claudecode.ClaudeCodeUserResetRecordMapper;
|
|
|
import com.cyksj.mapper.manage.distribute.DistributePointsExchangeMoneyMapper;
|
|
|
import com.cyksj.mapper.manage.distribute.DistributeWaitingSendPointsMapper;
|
|
|
import com.cyksj.model.entity.*;
|
|
|
+import com.cyksj.model.manage.views.GroupsRelationView;
|
|
|
import com.cyksj.model.request.ClaudeCodeApiKeysReq;
|
|
|
import com.cyksj.model.request.ClaudeCodeCreditCardUseReq;
|
|
|
import com.cyksj.model.request.ClaudeCodeDelReq;
|
|
|
@@ -1024,6 +1025,136 @@ public class ClaudeCodeServiceImpl implements ClaudeCodeService {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public BigDecimal 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)
|
|
|
+ .field(GroupsRelationView::getId, relationId)
|
|
|
+ .field(GroupsRelationView::getUserId, userIdList).op(Operator.InList)
|
|
|
+ .field(GroupsRelationView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
|
|
|
+ .build());
|
|
|
+ if (relationView == null) {
|
|
|
+ return BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ GoodsDon goodsDon = goodsDonMapper.selectById(upgradeSku.getGoodsId());
|
|
|
+ GoodsDonSku relationPresentSku = null;
|
|
|
+ Long renewSkuId = claudeCodeUser.getRenewSkuId();
|
|
|
+ if (renewSkuId != null) {
|
|
|
+ relationPresentSku = skuMapper.selectById(renewSkuId);
|
|
|
+ }
|
|
|
+ if (relationPresentSku == null) {
|
|
|
+ relationPresentSku = skuMapper.selectById(relationView.getSkuId());
|
|
|
+ }
|
|
|
+ if (upgradeSku.getClaudeQuota().intValue() < relationPresentSku.getClaudeQuota().intValue()) {
|
|
|
+ return BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ BigDecimal deductMoney = getDeductMoney(relationId, relationView.getSkuId(), claudeCodeUser.getRenewOrderId(), claudeCodeUser.getRenewOrderId(), claudeCodeUser.getRenewExpiryTime(), relationView.getStartTime(), relationView.getExpiryTime(), DateTime.now(), userIdList);
|
|
|
+ return deductMoney;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public BigDecimal getDeductMoney(Long relationId, Long relationSkuId, Long renewSkuId, Long renewOrderId, Date renewExpiryTime, Date relationStartTime, Date relationExpiryTime, DateTime now, List<Long> userIdList) {
|
|
|
+ BigDecimal deductMoney = BigDecimal.ZERO;
|
|
|
+ //一个月当30天算单价 往上取整
|
|
|
+ //续费规格
|
|
|
+ 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) {
|
|
|
+ orderMoney = presentSku.getPrice();
|
|
|
+ }
|
|
|
+ Long betweenDay = DateUtil.betweenDay(now, renewExpiryTime, false) + 1;
|
|
|
+ //当天购买的按规格原价抵扣
|
|
|
+ BigDecimal renewDeductMoney;
|
|
|
+ if (now.toDateStr().equals(DateUtil.format(renewOrderDon.getCreatedTime(), "yyyy-MM-dd"))) {
|
|
|
+ renewDeductMoney = orderMoney;
|
|
|
+ deductMoney = deductMoney.add(renewDeductMoney);
|
|
|
+ if (presentSku.getDays() != null) {
|
|
|
+ relationExpiryTime = DateUtil.offsetDay(relationExpiryTime, -presentSku.getDays());
|
|
|
+ } else {
|
|
|
+ relationExpiryTime = DateUtil.offsetMonth(relationExpiryTime, -presentSku.getMonths());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //此次续费规格单价
|
|
|
+ BigDecimal single = getPayOrderSingle(orderMoney, presentSku);
|
|
|
+ //该车票抵扣金额
|
|
|
+ renewDeductMoney = single.multiply(BigDecimal.valueOf(betweenDay));
|
|
|
+ log.info("车票id:{}目前续费升级规格可抵扣的金额为:{}", relationId, renewDeductMoney);
|
|
|
+ deductMoney = deductMoney.add(renewDeductMoney);
|
|
|
+ relationExpiryTime = DateUtil.offsetDay(relationExpiryTime, -betweenDay.intValue());
|
|
|
+ }
|
|
|
+ //上一个续费升级规格
|
|
|
+ List<OrderDon> orderDOns = claudeCodeUserRenewExpiryRecordMapper.getOtherRenewSkuOrders(renewOrderId, relationId, userIdList);
|
|
|
+ OrderDon presentRenewOrderDon = renewOrderDon;
|
|
|
+ for (OrderDon thisRenewOrderDon : orderDOns) {
|
|
|
+ Long skuId = thisRenewOrderDon.getSkuId();
|
|
|
+ GoodsDonSku thisRenewSku = skuMapper.selectById(skuId);
|
|
|
+ BigDecimal thisOrderMoney = thisRenewOrderDon.getMoney().add(Optional.ofNullable(thisRenewOrderDon.getBalance()).orElse(BigDecimal.ZERO));
|
|
|
+ if (thisOrderMoney.compareTo(BigDecimal.ZERO) == 0) {
|
|
|
+ thisOrderMoney = thisRenewSku.getPrice();
|
|
|
+ }
|
|
|
+ //当天购买的按规格原价抵扣
|
|
|
+ BigDecimal thisRenewDeductMoney = BigDecimal.ZERO;
|
|
|
+ if (now.toDateStr().equals(DateUtil.format(thisRenewOrderDon.getCreatedTime(), "yyyy-MM-dd"))) {
|
|
|
+ thisRenewDeductMoney = thisOrderMoney;
|
|
|
+ deductMoney = deductMoney.add(thisRenewDeductMoney);
|
|
|
+ if (thisRenewSku.getDays() != null) {
|
|
|
+ relationExpiryTime = DateUtil.offsetDay(relationExpiryTime, -thisRenewSku.getDays());
|
|
|
+ } else {
|
|
|
+ relationExpiryTime = DateUtil.offsetMonth(relationExpiryTime, -thisRenewSku.getMonths());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //此次续费订单过期时间
|
|
|
+ Date thisOrderExpiryTime;
|
|
|
+ if (thisRenewSku.getDays() != null) {
|
|
|
+ thisOrderExpiryTime = DateUtil.offsetDay(thisRenewOrderDon.getCreatedTime(), thisRenewSku.getDays());
|
|
|
+ } else {
|
|
|
+ thisOrderExpiryTime = DateUtil.offsetMonth(thisRenewOrderDon.getCreatedTime(), thisRenewSku.getMonths());
|
|
|
+ }
|
|
|
+ //续费同规格不处理
|
|
|
+ if (!ObjectUtil.equal(thisRenewOrderDon.getSkuId(), presentRenewOrderDon.getSkuId())) {
|
|
|
+ //先扣除已使用的天数
|
|
|
+ //相差多少天
|
|
|
+ Long thisRenewBetDay = DateUtil.betweenDay(thisRenewOrderDon.getCreatedTime(), presentRenewOrderDon.getCreatedTime(), false);
|
|
|
+ //扣除已使用的
|
|
|
+ thisOrderExpiryTime = DateUtil.offsetDay(thisOrderExpiryTime, -thisRenewBetDay.intValue());
|
|
|
+ }
|
|
|
+ //此订单过期时间
|
|
|
+ if (thisOrderExpiryTime.compareTo(now) > 0) {
|
|
|
+ //距离过期还剩多少天
|
|
|
+ Long thisDeductBetweenDay = DateUtil.betweenDay(now, thisOrderExpiryTime, false) + 1;
|
|
|
+ if (thisDeductBetweenDay != 0) {
|
|
|
+ //此次续费规格单价
|
|
|
+ BigDecimal single = getPayOrderSingle(thisOrderMoney, thisRenewSku);
|
|
|
+ //该车票抵扣金额
|
|
|
+ thisRenewDeductMoney = single.multiply(BigDecimal.valueOf(thisDeductBetweenDay));
|
|
|
+ deductMoney = deductMoney.add(thisRenewDeductMoney);
|
|
|
+ }
|
|
|
+ //目前级别续费订单
|
|
|
+ presentRenewOrderDon = thisRenewOrderDon;
|
|
|
+ relationExpiryTime = DateUtil.offsetDay(relationExpiryTime, -thisDeductBetweenDay.intValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("车票id:{}目前次一级规格:{}可抵扣的金额为:{}", relationId, thisRenewSku.getSpecVal(), thisRenewDeductMoney);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (relationExpiryTime.after(now)) {
|
|
|
+ //该车票抵扣金额
|
|
|
+ BigDecimal deductMoneyFromTicket = getDeductMoneyFromTicket(relationId, userIdList, relationSkuId, now, relationStartTime, relationExpiryTime);
|
|
|
+ deductMoney = deductMoney.add(deductMoneyFromTicket);
|
|
|
+ }
|
|
|
+ log.info("车票id:{}最终到期时间为:{}车票可返回 抵扣金额:{}", relationId, DateUtil.format(relationExpiryTime, "yyyy-MM-dd HH:mm:ss"), deductMoney);
|
|
|
+ return deductMoney;
|
|
|
+ }
|
|
|
+
|
|
|
private void adjClaudeCodeUserPoints(Long userId, Integer points, String type, String description) throws Exception {
|
|
|
String apiKeyUrl = String.format(Constant.CLAUDE_CODE_API_PREFIX + "api/claude/users/%s/limit", userId);
|
|
|
Map<String, Object> parmas = new HashMap<>();
|
|
|
@@ -1035,4 +1166,81 @@ public class ClaudeCodeServiceImpl implements ClaudeCodeService {
|
|
|
throw BusinessRuntimeException.getInstance("使用补充卡失败");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取订单单价
|
|
|
+ */
|
|
|
+ public BigDecimal getPayOrderSingle(BigDecimal orderMoney, GoodsDonSku sku) {
|
|
|
+ //此次续费规格单价
|
|
|
+ BigDecimal single;
|
|
|
+ //区分天/月
|
|
|
+ if (sku.getDays() != null) {
|
|
|
+ single = orderMoney.divide(BigDecimal.valueOf(sku.getDays()), 0, RoundingMode.HALF_UP);
|
|
|
+ } else {
|
|
|
+ single = orderMoney.divide(BigDecimal.valueOf(sku.getMonths()).multiply(BigDecimal.valueOf(30)), 0, RoundingMode.HALF_UP);
|
|
|
+ }
|
|
|
+ return single;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取车票可抵扣金额
|
|
|
+ */
|
|
|
+ public BigDecimal getDeductMoneyFromTicket(Long relationId, List<Long> userIdList, Long skuId, DateTime now, Date relationStartTime, Date relationExpiryTime) {
|
|
|
+ List<OrderDon> orderDonList = orderDonMapper.selectList(Wrappers.lambdaQuery(OrderDon.class)
|
|
|
+ .eq(OrderDon::getRelationId, relationId)
|
|
|
+ .in(OrderDon::getUserId, userIdList)
|
|
|
+ .eq(OrderDon::getSkuId, skuId)
|
|
|
+ .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
|
|
|
+ .orderByDesc(OrderDon::getId));
|
|
|
+ //部分退款
|
|
|
+ if (orderDonList.isEmpty()) {
|
|
|
+ orderDonList = orderDonMapper.selectList(Wrappers.lambdaQuery(OrderDon.class)
|
|
|
+ .eq(OrderDon::getRelationId, relationId)
|
|
|
+ .in(OrderDon::getUserId, userIdList)
|
|
|
+ .eq(OrderDon::getSkuId, skuId)
|
|
|
+ .notIn(OrderDon::getStatus, Constant.noOrderStatus)
|
|
|
+ .orderByDesc(OrderDon::getId));
|
|
|
+ }
|
|
|
+ //未有订单 返回抵扣金额0
|
|
|
+ if (orderDonList.isEmpty()) {
|
|
|
+ return BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ //原车票订单总实付金额
|
|
|
+ BigDecimal totalOrderMoney = BigDecimal.ZERO;
|
|
|
+ //原车票订单总天数
|
|
|
+ Integer totalDays = 0;
|
|
|
+ for (OrderDon orderDon : orderDonList) {
|
|
|
+ GoodsDonSku sku = skuMapper.selectById(skuId);
|
|
|
+ BigDecimal thisOrderMoney = orderDon.getMoney().add(Optional.ofNullable(orderDon.getBalance()).orElse(BigDecimal.ZERO)).subtract(Optional.ofNullable(orderDon.getRefundMoney()).orElse(BigDecimal.ZERO));
|
|
|
+ if (thisOrderMoney.compareTo(BigDecimal.ZERO) == 0) {
|
|
|
+ thisOrderMoney = sku.getPrice();
|
|
|
+ }
|
|
|
+ totalOrderMoney = totalOrderMoney.add(thisOrderMoney);
|
|
|
+ //总时间
|
|
|
+ if (sku.getDays() != null) {
|
|
|
+ totalDays += sku.getDays();
|
|
|
+ } else {
|
|
|
+ totalDays += sku.getMonths() * 30;
|
|
|
+ }
|
|
|
+ //原订单数
|
|
|
+ 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(relationStartTime, "yyyy-MM-dd")) && (DateUtil.betweenDay(thisOrderExpiryTime, relationExpiryTime, true) == 0)) {
|
|
|
+ return thisOrderMoney;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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;
|
|
|
+ }
|
|
|
}
|