VipFrontServiceImpl.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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.ObjectUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  7. import com.cyksj.common.constant.Constant;
  8. import com.cyksj.common.exception.BusinessRuntimeException;
  9. import com.cyksj.mapper.*;
  10. import com.cyksj.mapper.sys.SysConfigMapper;
  11. import com.cyksj.mapper.vip.VipGoodsSkuMapper;
  12. import com.cyksj.model.dto.VipDeductDto;
  13. import com.cyksj.model.entity.*;
  14. import com.cyksj.model.manage.views.GroupsRelationView;
  15. import com.cyksj.model.response.VipUserPageResp;
  16. import com.cyksj.service.user.UserBindRelationService;
  17. import com.cyksj.service.vip.VipFrontService;
  18. import com.ejlchina.searcher.BeanSearcher;
  19. import com.ejlchina.searcher.param.Operator;
  20. import com.ejlchina.searcher.util.MapUtils;
  21. import lombok.RequiredArgsConstructor;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.springframework.stereotype.Service;
  24. import java.math.BigDecimal;
  25. import java.math.RoundingMode;
  26. import java.util.*;
  27. import java.util.stream.Collectors;
  28. /**
  29. * 项目名: yhlxj2
  30. * 文件名: VipFrontServiceImpl
  31. * 创建者: JavaZou
  32. * 创建时间:2025/5/15 16:00
  33. */
  34. @Service
  35. @RequiredArgsConstructor
  36. @Slf4j
  37. public class VipFrontServiceImpl implements VipFrontService {
  38. private final SysConfigMapper sysConfigMapper;
  39. private final VipGoodsSkuMapper vipGoodsSkuMapper;
  40. private final UserBindRelationService userBindRelationService;
  41. private final BeanSearcher beanSearcher;
  42. private final GoodsDonSkuMapper skuMapper;
  43. private final ChatgptUserMapper chatgptUserMapper;
  44. private final OrderDonRenewRecordMapper orderDonRenewRecordMapper;
  45. private final ClaudeUserMapper claudeUserMapper;
  46. private final OrderDonMapper orderDonMapper;
  47. private final GoodsDonSkuMapper goodsDonSkuMapper;
  48. @Override
  49. public void fillVipInfo(Long userId, VipUserPageResp vipUserPageResp) {
  50. SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class).eq(SysConfig::getSysKey, Constant.VIP_FEE_KEY).last("limit 1"));
  51. if (sysConfig != null && StrUtil.isNotBlank(sysConfig.getSysValue())) {
  52. vipUserPageResp.setVipFee(BigDecimal.valueOf(Integer.parseInt(sysConfig.getSysValue())));
  53. }
  54. if (userId == null) {
  55. return;
  56. }
  57. //可抵扣价格
  58. BigDecimal deductMoney = BigDecimal.ZERO;
  59. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  60. List<Long> vipSkuIds = vipGoodsSkuMapper.selectList(null).stream().map(VipGoodsSku::getSkuId).collect(Collectors.toList());
  61. //可有抵扣车票
  62. DateTime now = DateTime.now();
  63. //会员规格
  64. Set<Long> goodsIds = goodsDonSkuMapper.selectList(Wrappers.lambdaQuery(GoodsDonSku.class)
  65. .in(GoodsDonSku::getId, vipSkuIds)).stream().map(GoodsDonSku::getGoodsId).collect(Collectors.toSet());
  66. Number count = beanSearcher.searchCount(GroupsRelationView.class, MapUtils.builder().field(GroupsRelationView::getUserId, userIdList).op(Operator.InList).field(GroupsRelationView::getGoodsId, goodsIds).op(Operator.InList).field(GroupsRelationView::getExpiryTime, now).op(Operator.GreaterThan).build());
  67. //可抵扣车票
  68. if (count.intValue() > 0) {
  69. //处理vip抵扣金额
  70. VipDeductDto vipDeductDto = commonHandleVipDeductMoney(userIdList, goodsIds, 12);
  71. deductMoney = vipDeductDto.getDeductMoney();
  72. vipUserPageResp.setRelationUserViews(vipDeductDto.getRelationUserViews());
  73. }
  74. vipUserPageResp.setDeductMoney(deductMoney);
  75. }
  76. /**
  77. * 检查用户并获取订单抵扣金额
  78. */
  79. @Override
  80. public VipDeductDto checkAndGetOrderDeductMoney(Long userId, Integer months) {
  81. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  82. List<Long> vipSkuIds = vipGoodsSkuMapper.selectList(null).stream().map(VipGoodsSku::getSkuId).collect(Collectors.toList());
  83. //会员规格
  84. Set<Long> goodsIds = goodsDonSkuMapper.selectList(Wrappers.lambdaQuery(GoodsDonSku.class)
  85. .in(GoodsDonSku::getId, vipSkuIds)).stream().map(GoodsDonSku::getGoodsId).collect(Collectors.toSet());
  86. Number count = beanSearcher.searchCount(GroupsRelationView.class, MapUtils.builder()
  87. .field(GroupsRelationView::getUserId, userIdList).op(Operator.InList)
  88. .field(GroupsRelationView::getGoodsId, goodsIds).op(Operator.InList)
  89. .field(GroupsRelationView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
  90. .field(GroupsRelationView::getIsVip, false)
  91. .build());
  92. if (count.intValue() == 0) {
  93. throw BusinessRuntimeException.getInstance("暂无可抵扣AI车票");
  94. }
  95. VipDeductDto vipDeductDto = commonHandleVipDeductMoney(userIdList, goodsIds, months);
  96. return vipDeductDto;
  97. }
  98. private BigDecimal getVipMoney(Integer months) {
  99. SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class).eq(SysConfig::getSysKey, Constant.VIP_FEE_KEY).last("limit 1"));
  100. if (sysConfig == null || StrUtil.isEmpty(sysConfig.getSysValue())) {
  101. throw BusinessRuntimeException.getInstance("系统异常");
  102. }
  103. BigDecimal vipFee = BigDecimal.valueOf(Long.valueOf(sysConfig.getSysValue()));
  104. return vipFee.multiply(BigDecimal.valueOf(months));
  105. }
  106. /**
  107. * 统一处理vip抵扣金额
  108. */
  109. public VipDeductDto commonHandleVipDeductMoney(List<Long> userIdList, Set<Long> goodsIds, Integer months) {
  110. VipDeductDto vipDeductDto = new VipDeductDto();
  111. DateTime now = DateTime.now();
  112. List<Long> deductRelationIds = new ArrayList<>();
  113. BigDecimal deductMoney = BigDecimal.ZERO;
  114. BigDecimal vipMoney = getVipMoney(months);
  115. List<GroupsRelationView> relations = beanSearcher.searchAll(GroupsRelationView.class, MapUtils.builder()
  116. .field(GroupsRelationView::getGoodsId, goodsIds).op(Operator.InList)
  117. .field(GroupsRelationView::getUserId, userIdList).op(Operator.InList)
  118. .field(GroupsRelationView::getIsVip, false)
  119. .field(GroupsRelationView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
  120. .field(GroupsRelationView::getAqType, 1)
  121. .orderBy(GroupsRelationView::getExpiryTime).desc()
  122. .build());
  123. Set<Long> deductGoodsIds = null;
  124. List<GroupsRelationView> deductRelations = null;
  125. for (GroupsRelationView relation : relations) {
  126. if (deductGoodsIds == null) {
  127. deductGoodsIds = new HashSet<>();
  128. }
  129. if (deductRelations == null) {
  130. deductRelations = new ArrayList<>();
  131. }
  132. Long goodsId = relation.getGoodsId();
  133. if (deductGoodsIds.contains(goodsId)) {
  134. continue;
  135. }
  136. Long skuId = relation.getSkuId();
  137. deductGoodsIds.add(goodsId);
  138. Long relationId = relation.getId();
  139. Date relationExpiryTime = relation.getExpiryTime();
  140. //GPT
  141. if (goodsId == Constant.GPT_PLUS_GID) {
  142. ChatgptUser chatgptUser = chatgptUserMapper.selectOne(Wrappers.lambdaQuery(ChatgptUser.class).eq(ChatgptUser::getRelationId, relationId).last("limit 1"));
  143. if (chatgptUser != null) {
  144. //获取抵扣金额
  145. deductMoney = deductMoney.add(getFinalDeductMoney(relationId, relation.getSkuId(), chatgptUser.getRenewSkuId(), chatgptUser.getRenewOrderId(), chatgptUser.getRenewExpiryTime(), relation.getStartTime(), relationExpiryTime, now, userIdList));
  146. }
  147. } else if (goodsId == Constant.CLAUDE_PRO_GID) {
  148. ClaudeUser claudeUser = claudeUserMapper.selectOne(Wrappers.lambdaQuery(ClaudeUser.class).eq(ClaudeUser::getRelationId, relationId).last("limit 1"));
  149. if (claudeUser != null) {
  150. //获取抵扣金额
  151. deductMoney = deductMoney.add(getFinalDeductMoney(relationId, relation.getSkuId(), claudeUser.getRenewSkuId(), claudeUser.getRenewOrderId(), claudeUser.getRenewExpiryTime(), relation.getStartTime(), relationExpiryTime, now, userIdList));
  152. }
  153. } else {
  154. //该车票抵扣金额
  155. deductMoney = deductMoney.add(getDeductMoneyFromTicket(relationId, userIdList, skuId, now, relation.getStartTime(), relationExpiryTime));
  156. }
  157. deductRelationIds.add(relationId);
  158. //可抵扣车票
  159. deductRelations.add(relation);
  160. //超出可抵扣的金额 直接返回
  161. if (deductMoney.compareTo(vipMoney) >= 0) {
  162. deductMoney = vipMoney;
  163. break;
  164. }
  165. }
  166. vipDeductDto.setDeductMoney(deductMoney);
  167. vipDeductDto.setDeductRelationIds(deductRelationIds);
  168. vipDeductDto.setRelationUserViews(deductRelations);
  169. return vipDeductDto;
  170. }
  171. /**
  172. * 获取最终抵扣金额
  173. */
  174. public BigDecimal getFinalDeductMoney(Long relationId, Long relationSkuId, Long renewSkuId, Long renewOrderId, Date renewExpiryTime, Date relationStartTime, Date relationExpiryTime, DateTime now, List<Long> userIdList) {
  175. BigDecimal deductMoney = BigDecimal.ZERO;
  176. //一个月当30天算单价 往上取整
  177. //续费规格
  178. if (renewSkuId != null && renewExpiryTime.compareTo(now) > 0) {
  179. OrderDon renewOrderDon = orderDonMapper.selectById(renewOrderId);
  180. BigDecimal orderMoney = renewOrderDon.getMoney().add(Optional.ofNullable(renewOrderDon.getBalance()).orElse(BigDecimal.ZERO));
  181. GoodsDonSku presentSku = skuMapper.selectById(renewSkuId);
  182. if (orderMoney.compareTo(BigDecimal.ZERO) == 0) {
  183. orderMoney = presentSku.getPrice();
  184. }
  185. Long betweenDay = DateUtil.betweenDay(now, renewExpiryTime, false) + 1;
  186. //当天购买的按规格原价抵扣
  187. BigDecimal renewDeductMoney;
  188. if (now.toDateStr().equals(DateUtil.format(renewOrderDon.getCreatedTime(), "yyyy-MM-dd"))) {
  189. renewDeductMoney = orderMoney;
  190. deductMoney = deductMoney.add(renewDeductMoney);
  191. if (presentSku.getDays() != null) {
  192. relationExpiryTime = DateUtil.offsetDay(relationExpiryTime, -presentSku.getDays());
  193. } else {
  194. relationExpiryTime = DateUtil.offsetMonth(relationExpiryTime, -presentSku.getMonths());
  195. }
  196. } else {
  197. //此次续费规格单价
  198. BigDecimal single = getPayOrderSingle(orderMoney, presentSku);
  199. //该车票抵扣金额
  200. renewDeductMoney = single.multiply(BigDecimal.valueOf(betweenDay));
  201. log.info("车票id:{}目前续费升级规格可抵扣的金额为:{}", relationId, renewDeductMoney);
  202. deductMoney = deductMoney.add(renewDeductMoney);
  203. relationExpiryTime = DateUtil.offsetDay(relationExpiryTime, -betweenDay.intValue());
  204. }
  205. //上一个续费升级规格
  206. List<OrderDon> orderDOns = orderDonRenewRecordMapper.getOtherRenewSkus(renewOrderId, relationId, userIdList);
  207. OrderDon presentRenewOrderDon = renewOrderDon;
  208. for (OrderDon thisRenewOrderDon : orderDOns) {
  209. Long skuId = thisRenewOrderDon.getSkuId();
  210. GoodsDonSku thisRenewSku = skuMapper.selectById(skuId);
  211. BigDecimal thisOrderMoney = thisRenewOrderDon.getMoney().add(Optional.ofNullable(thisRenewOrderDon.getBalance()).orElse(BigDecimal.ZERO));
  212. if (thisOrderMoney.compareTo(BigDecimal.ZERO) == 0) {
  213. thisOrderMoney = thisRenewSku.getPrice();
  214. }
  215. //当天购买的按规格原价抵扣
  216. BigDecimal thisRenewDeductMoney = BigDecimal.ZERO;
  217. if (now.toDateStr().equals(DateUtil.format(thisRenewOrderDon.getCreatedTime(), "yyyy-MM-dd"))) {
  218. thisRenewDeductMoney = thisOrderMoney;
  219. deductMoney = deductMoney.add(thisRenewDeductMoney);
  220. if (thisRenewSku.getDays() != null) {
  221. relationExpiryTime = DateUtil.offsetDay(relationExpiryTime, -thisRenewSku.getDays());
  222. } else {
  223. relationExpiryTime = DateUtil.offsetMonth(relationExpiryTime, -thisRenewSku.getMonths());
  224. }
  225. } else {
  226. //此次续费订单过期时间
  227. Date thisOrderExpiryTime;
  228. if (thisRenewSku.getDays() != null) {
  229. thisOrderExpiryTime = DateUtil.offsetDay(thisRenewOrderDon.getCreatedTime(), thisRenewSku.getDays());
  230. } else {
  231. thisOrderExpiryTime = DateUtil.offsetMonth(thisRenewOrderDon.getCreatedTime(), thisRenewSku.getMonths());
  232. }
  233. //续费同规格不处理
  234. if (!ObjectUtil.equal(thisRenewOrderDon.getSkuId(), presentRenewOrderDon.getSkuId())) {
  235. //先扣除已使用的天数
  236. //相差多少天
  237. Long thisRenewBetDay = DateUtil.betweenDay(thisRenewOrderDon.getCreatedTime(), presentRenewOrderDon.getCreatedTime(), false);
  238. //扣除已使用的
  239. thisOrderExpiryTime = DateUtil.offsetDay(thisOrderExpiryTime, -thisRenewBetDay.intValue());
  240. }
  241. //此订单过期时间
  242. if (thisOrderExpiryTime.compareTo(now) > 0) {
  243. //距离过期还剩多少天
  244. Long thisDeductBetweenDay = DateUtil.betweenDay(now, thisOrderExpiryTime, false) + 1;
  245. if (thisDeductBetweenDay != 0) {
  246. //此次续费规格单价
  247. BigDecimal single = getPayOrderSingle(thisOrderMoney, thisRenewSku);
  248. //该车票抵扣金额
  249. thisRenewDeductMoney = single.multiply(BigDecimal.valueOf(thisDeductBetweenDay));
  250. deductMoney = deductMoney.add(thisRenewDeductMoney);
  251. }
  252. //目前级别续费订单
  253. presentRenewOrderDon = thisRenewOrderDon;
  254. relationExpiryTime = DateUtil.offsetDay(relationExpiryTime, -thisDeductBetweenDay.intValue());
  255. }
  256. }
  257. log.info("车票id:{}目前次一级规格:{}可抵扣的金额为:{}", relationId, thisRenewSku.getSpecVal(), thisRenewDeductMoney);
  258. }
  259. }
  260. if (relationExpiryTime.after(now)) {
  261. //该车票抵扣金额
  262. BigDecimal deductMoneyFromTicket = getDeductMoneyFromTicket(relationId, userIdList, relationSkuId, now, relationStartTime, relationExpiryTime);
  263. deductMoney = deductMoney.add(deductMoneyFromTicket);
  264. }
  265. log.info("车票id:{}最终到期时间为:{}车票可返回 抵扣金额:{}", relationId, DateUtil.format(relationExpiryTime, "yyyy-MM-dd HH:mm:ss"), deductMoney);
  266. return deductMoney;
  267. }
  268. /**
  269. * 获取车票可抵扣金额
  270. */
  271. public BigDecimal getDeductMoneyFromTicket(Long relationId, List<Long> userIdList, Long skuId, DateTime now, Date relationStartTime, Date relationExpiryTime) {
  272. List<OrderDon> orderDonList = orderDonMapper.selectList(Wrappers.lambdaQuery(OrderDon.class)
  273. .eq(OrderDon::getRelationId, relationId)
  274. .in(OrderDon::getUserId, userIdList)
  275. .eq(OrderDon::getSkuId, skuId)
  276. .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
  277. .orderByDesc(OrderDon::getId));
  278. //原车票订单总实付金额
  279. BigDecimal totalOrderMoney = BigDecimal.ZERO;
  280. //原车票订单总天数
  281. Integer totalDays = 0;
  282. for (OrderDon orderDon : orderDonList) {
  283. GoodsDonSku sku = skuMapper.selectById(skuId);
  284. BigDecimal thisOrderMoney = orderDon.getMoney().add(Optional.ofNullable(orderDon.getBalance()).orElse(BigDecimal.ZERO));
  285. if (thisOrderMoney.compareTo(BigDecimal.ZERO) == 0) {
  286. thisOrderMoney = sku.getPrice();
  287. }
  288. totalOrderMoney = totalOrderMoney.add(thisOrderMoney);
  289. //总时间
  290. if (sku.getDays() != null) {
  291. totalDays += sku.getDays();
  292. } else {
  293. totalDays += sku.getMonths() * 30;
  294. }
  295. //原订单数
  296. if (orderDonList.size() == 1) {
  297. //此次订单过期时间
  298. Date thisOrderExpiryTime;
  299. if (sku.getDays() != null) {
  300. thisOrderExpiryTime = DateUtil.offsetDay(orderDon.getCreatedTime(), sku.getDays());
  301. } else {
  302. thisOrderExpiryTime = DateUtil.offsetMonth(orderDon.getCreatedTime(), sku.getMonths());
  303. }
  304. //当天购买的按规格原价抵扣
  305. if (now.toDateStr().equals(DateUtil.format(relationStartTime, "yyyy-MM-dd")) && (DateUtil.betweenDay(thisOrderExpiryTime, relationExpiryTime, true) == 0)) {
  306. return thisOrderMoney;
  307. }
  308. }
  309. }
  310. Long thisDeductBetweenDay = DateUtil.betweenDay(now, relationExpiryTime, false) + 1;
  311. BigDecimal single = totalOrderMoney.divide(BigDecimal.valueOf(totalDays), 0, RoundingMode.HALF_UP);
  312. //该车票抵扣金额
  313. BigDecimal deductMoney = single.multiply(BigDecimal.valueOf(thisDeductBetweenDay));
  314. return deductMoney;
  315. }
  316. /**
  317. * 获取订单单价
  318. */
  319. public BigDecimal getPayOrderSingle(BigDecimal orderMoney, GoodsDonSku sku) {
  320. //此次续费规格单价
  321. BigDecimal single;
  322. //区分天/月
  323. if (sku.getDays() != null) {
  324. single = orderMoney.divide(BigDecimal.valueOf(sku.getDays()), 0, RoundingMode.HALF_UP);
  325. } else {
  326. single = orderMoney.divide(BigDecimal.valueOf(sku.getMonths()).multiply(BigDecimal.valueOf(30)), 0, RoundingMode.HALF_UP);
  327. }
  328. return single;
  329. }
  330. }