VipFrontServiceImpl.java 18 KB

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