VipFrontServiceImpl.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package com.cyksj.service.vip.impl;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.cyksj.common.constant.Constant;
  7. import com.cyksj.common.exception.BusinessRuntimeException;
  8. import com.cyksj.mapper.ChatgptUserMapper;
  9. import com.cyksj.mapper.ClaudeUserMapper;
  10. import com.cyksj.mapper.GoodsDonSkuMapper;
  11. import com.cyksj.mapper.OrderDonRenewRecordMapper;
  12. import com.cyksj.mapper.sys.SysConfigMapper;
  13. import com.cyksj.mapper.vip.VipGoodsSkuMapper;
  14. import com.cyksj.model.dto.VipDeductDto;
  15. import com.cyksj.model.entity.*;
  16. import com.cyksj.model.manage.views.GroupsRelationView;
  17. import com.cyksj.model.response.VipUserPageResp;
  18. import com.cyksj.service.user.UserBindRelationService;
  19. import com.cyksj.service.vip.VipFrontService;
  20. import com.ejlchina.searcher.BeanSearcher;
  21. import com.ejlchina.searcher.param.Operator;
  22. import com.ejlchina.searcher.util.MapUtils;
  23. import lombok.RequiredArgsConstructor;
  24. import lombok.extern.slf4j.Slf4j;
  25. import org.springframework.stereotype.Service;
  26. import java.math.BigDecimal;
  27. import java.math.RoundingMode;
  28. import java.util.ArrayList;
  29. import java.util.Date;
  30. import java.util.List;
  31. import java.util.stream.Collectors;
  32. /**
  33. * 项目名: yhlxj2
  34. * 文件名: VipFrontServiceImpl
  35. * 创建者: JavaZou
  36. * 创建时间:2025/5/15 16:00
  37. */
  38. @Service
  39. @RequiredArgsConstructor
  40. @Slf4j
  41. public class VipFrontServiceImpl implements VipFrontService {
  42. private final SysConfigMapper sysConfigMapper;
  43. private final VipGoodsSkuMapper vipGoodsSkuMapper;
  44. private final UserBindRelationService userBindRelationService;
  45. private final BeanSearcher beanSearcher;
  46. private final GoodsDonSkuMapper skuMapper;
  47. private final ChatgptUserMapper chatgptUserMapper;
  48. private final OrderDonRenewRecordMapper orderDonRenewRecordMapper;
  49. private final ClaudeUserMapper claudeUserMapper;
  50. @Override
  51. public void fillVipInfo(long userId, VipUserPageResp vipUserPageResp) {
  52. SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class).eq(SysConfig::getSysKey, Constant.VIP_FEE_KEY).last("limit 1"));
  53. if (sysConfig != null && StrUtil.isNotBlank(sysConfig.getSysValue())) {
  54. vipUserPageResp.setVipFee(BigDecimal.valueOf(Integer.parseInt(sysConfig.getSysValue())));
  55. }
  56. //可抵扣价格
  57. BigDecimal deductMoney = BigDecimal.ZERO;
  58. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  59. List<Long> vipSkuIds = vipGoodsSkuMapper.selectList(null).stream().map(VipGoodsSku::getSkuId).collect(Collectors.toList());
  60. //可有抵扣车票
  61. DateTime now = DateTime.now();
  62. Number count = beanSearcher.searchCount(GroupsRelationView.class, MapUtils.builder().field(GroupsRelationView::getUserId, userIdList).op(Operator.InList).field(GroupsRelationView::getSkuId, vipSkuIds).op(Operator.InList).field(GroupsRelationView::getExpiryTime, now).op(Operator.GreaterThan).build());
  63. //可抵扣车票
  64. if (count.intValue() > 0) {
  65. //处理vip抵扣金额
  66. VipDeductDto vipDeductDto = commonHandleVipDeductMoney(userIdList, vipSkuIds);
  67. deductMoney = vipDeductDto.getDeductMoney();
  68. }
  69. vipUserPageResp.setDeductMoney(deductMoney);
  70. }
  71. /**
  72. * 检查用户并获取订单抵扣金额
  73. */
  74. @Override
  75. public VipDeductDto checkAndGetOrderDeductMoney(Long userId) {
  76. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  77. List<Long> vipSkuIds = vipGoodsSkuMapper.selectList(null).stream().map(VipGoodsSku::getSkuId).collect(Collectors.toList());
  78. Number count = beanSearcher.searchCount(GroupsRelationView.class, MapUtils.builder().field(GroupsRelationView::getUserId, userIdList).op(Operator.InList).field(GroupsRelationView::getSkuId, vipSkuIds).op(Operator.InList).field(GroupsRelationView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan).build());
  79. if (count.intValue() == 0) {
  80. throw BusinessRuntimeException.getInstance("暂无可抵扣AI车票");
  81. }
  82. VipDeductDto vipDeductDto = commonHandleVipDeductMoney(userIdList, vipSkuIds);
  83. return vipDeductDto;
  84. }
  85. private BigDecimal getVipMoney() {
  86. SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class).eq(SysConfig::getSysKey, Constant.VIP_FEE_KEY).last("limit 1"));
  87. if (sysConfig == null || StrUtil.isEmpty(sysConfig.getSysValue())) {
  88. throw BusinessRuntimeException.getInstance("系统异常");
  89. }
  90. BigDecimal vipFee = BigDecimal.valueOf(Long.valueOf(sysConfig.getSysValue()));
  91. return vipFee;
  92. }
  93. /**
  94. * 统一处理vip抵扣金额
  95. */
  96. public VipDeductDto commonHandleVipDeductMoney(List<Long> userIdList, List<Long> vipSkuIds) {
  97. VipDeductDto vipDeductDto = new VipDeductDto();
  98. DateTime now = DateTime.now();
  99. List<Long> deductRelationIds = new ArrayList<>();
  100. BigDecimal deductMoney = BigDecimal.ZERO;
  101. BigDecimal vipMoney = getVipMoney();
  102. for (Long vipSkuId : vipSkuIds) {
  103. GroupsRelationView relation = beanSearcher.searchFirst(GroupsRelationView.class, MapUtils.builder().field(GroupsRelationView::getSkuId, vipSkuId).field(GroupsRelationView::getUserId, userIdList).op(Operator.InList).orderBy(GroupsRelationView::getExpiryTime).desc().build());
  104. if (relation != null) {
  105. Long goodsId = relation.getGoodsId();
  106. Long relationId = relation.getId();
  107. Date relationExpiryTime = relation.getExpiryTime();
  108. //GPT
  109. if (goodsId == Constant.GPT_PLUS_GID) {
  110. ChatgptUser chatgptUser = chatgptUserMapper.selectOne(Wrappers.lambdaQuery(ChatgptUser.class).eq(ChatgptUser::getRelationId, relationId).last("limit 1"));
  111. if (chatgptUser != null) {
  112. //获取抵扣金额
  113. deductMoney = deductMoney.add(getFinalDeductMoney(relationId, relation.getSkuId(), chatgptUser.getRenewSkuId(), chatgptUser.getRenewOrderId(), chatgptUser.getRenewExpiryTime(), relationExpiryTime, now, userIdList));
  114. }
  115. } else if (goodsId == Constant.CLAUDE_PRO_GID) {
  116. ClaudeUser claudeUser = claudeUserMapper.selectOne(Wrappers.lambdaQuery(ClaudeUser.class).eq(ClaudeUser::getRelationId, relationId).last("limit 1"));
  117. if (claudeUser != null) {
  118. //获取抵扣金额
  119. deductMoney = deductMoney.add(getFinalDeductMoney(relationId, relation.getSkuId(), claudeUser.getRenewSkuId(), claudeUser.getRenewOrderId(), claudeUser.getRenewExpiryTime(), relationExpiryTime, now, userIdList));
  120. }
  121. } else {
  122. //该车票抵扣金额
  123. deductMoney = deductMoney.add(getDeductMoneyFromTicket(vipSkuId, now, relationExpiryTime));
  124. }
  125. deductRelationIds.add(relationId);
  126. //超出可抵扣的金额 直接返回
  127. if (deductMoney.compareTo(vipMoney) >= 0) {
  128. deductMoney = vipMoney;
  129. break;
  130. }
  131. }
  132. }
  133. vipDeductDto.setDeductMoney(deductMoney);
  134. vipDeductDto.setDeductRelationIds(deductRelationIds);
  135. return vipDeductDto;
  136. }
  137. /**
  138. * 获取最终抵扣金额
  139. */
  140. public BigDecimal getFinalDeductMoney(Long relationId, Long relationSkuId, Long renewSkuId, Long renewOrderId, Date renewExpiryTime, Date relationExpiryTime, DateTime now, List<Long> userIdList) {
  141. BigDecimal deductMoney = BigDecimal.ZERO;
  142. //一个月当30天算单价 往上取整
  143. //续费规格
  144. if (renewSkuId != null) {
  145. GoodsDonSku sku = skuMapper.selectById(renewSkuId);
  146. BigDecimal single = sku.getPrice().divide(BigDecimal.valueOf(sku.getMonths()).multiply(BigDecimal.valueOf(30)), 0, RoundingMode.UP);
  147. Long betweenDay = DateUtil.betweenDay(now, renewExpiryTime, false);
  148. //该车票抵扣金额
  149. BigDecimal renewDeductMoney = single.multiply(BigDecimal.valueOf(betweenDay));
  150. log.info("车票id:{}目前续费升级规格可抵扣的金额为:{}", relationId, renewDeductMoney);
  151. deductMoney = deductMoney.add(renewDeductMoney);
  152. relationExpiryTime = DateUtil.offsetDay(relationExpiryTime, betweenDay.intValue());
  153. //上一个续费升级规格
  154. List<OrderDon> orderDOns = orderDonRenewRecordMapper.getOtherRenewSkus(renewOrderId, relationId, userIdList);
  155. for (OrderDon orderDOn : orderDOns) {
  156. Long skuId = orderDOn.getSkuId();
  157. sku = skuMapper.selectById(skuId);
  158. BigDecimal upDeductMoney = orderDOn.getMoney().add(orderDOn.getBalance());
  159. log.info("车票id:{}目前次一级规格:{}可抵扣的金额为:{}", relationId, sku.getSpecVal(), upDeductMoney);
  160. deductMoney = deductMoney.add(upDeductMoney);
  161. if (sku.getDays() != null) {
  162. relationExpiryTime = DateUtil.offsetDay(relationExpiryTime, -sku.getDays());
  163. } else {
  164. relationExpiryTime = DateUtil.offsetMonth(relationExpiryTime, -sku.getMonths());
  165. }
  166. }
  167. }
  168. if (relationExpiryTime.after(DateTime.now())) {
  169. //该车票抵扣金额
  170. BigDecimal deductMoneyFromTicket = getDeductMoneyFromTicket(relationSkuId, now, relationExpiryTime);
  171. log.info("车票id:{}最终到期时间为:{}车票可返回 抵扣金额:{}", relationId, relationExpiryTime, deductMoney);
  172. deductMoney = deductMoney.add(deductMoneyFromTicket);
  173. }
  174. return deductMoney;
  175. }
  176. /**
  177. * 获取车票可抵扣金额
  178. */
  179. public BigDecimal getDeductMoneyFromTicket(Long skuId, DateTime now, Date relationExpiryTime) {
  180. GoodsDonSku sku = skuMapper.selectById(skuId);
  181. BigDecimal single = sku.getPrice().divide(BigDecimal.valueOf(sku.getMonths()).multiply(BigDecimal.valueOf(30)), 0, RoundingMode.UP);
  182. Long betweenDay = DateUtil.betweenDay(now, relationExpiryTime, false);
  183. //该车票抵扣金额
  184. BigDecimal deductMoney = single.multiply(BigDecimal.valueOf(betweenDay));
  185. return deductMoney;
  186. }
  187. }