VipFrontServiceImpl.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. package com.cyksj.service.vip.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateTime;
  4. import cn.hutool.core.date.DateUtil;
  5. import cn.hutool.core.util.ObjectUtil;
  6. import cn.hutool.core.util.StrUtil;
  7. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  8. import com.cyksj.common.constant.Constant;
  9. import com.cyksj.common.exception.BusinessRuntimeException;
  10. import com.cyksj.mapper.*;
  11. import com.cyksj.mapper.sys.SysConfigMapper;
  12. import com.cyksj.mapper.vip.VipGoodsSkuMapper;
  13. import com.cyksj.model.dto.VipDeductDto;
  14. import com.cyksj.model.entity.*;
  15. import com.cyksj.model.manage.views.GroupsRelationView;
  16. import com.cyksj.model.response.VipUserPageResp;
  17. import com.cyksj.model.views.RenewalView;
  18. import com.cyksj.model.views.VipGoodsSkuFrontView;
  19. import com.cyksj.service.chatgpt.ChatGptAccountService;
  20. import com.cyksj.service.claude.ClaudeService;
  21. import com.cyksj.service.user.UserBindRelationService;
  22. import com.cyksj.service.vip.VipFrontService;
  23. import com.ejlchina.searcher.BeanSearcher;
  24. import com.ejlchina.searcher.param.Operator;
  25. import com.ejlchina.searcher.util.MapUtils;
  26. import lombok.RequiredArgsConstructor;
  27. import lombok.extern.slf4j.Slf4j;
  28. import org.springframework.stereotype.Service;
  29. import java.math.BigDecimal;
  30. import java.math.RoundingMode;
  31. import java.util.*;
  32. import java.util.stream.Collectors;
  33. /**
  34. * 项目名: yhlxj2
  35. * 文件名: VipFrontServiceImpl
  36. * 创建者: JavaZou
  37. * 创建时间:2025/5/15 16:00
  38. */
  39. @Service
  40. @RequiredArgsConstructor
  41. @Slf4j
  42. public class VipFrontServiceImpl implements VipFrontService {
  43. private final SysConfigMapper sysConfigMapper;
  44. private final VipGoodsSkuMapper vipGoodsSkuMapper;
  45. private final UserBindRelationService userBindRelationService;
  46. private final BeanSearcher beanSearcher;
  47. private final GoodsDonSkuMapper skuMapper;
  48. private final ChatgptUserMapper chatgptUserMapper;
  49. private final OrderDonRenewRecordMapper orderDonRenewRecordMapper;
  50. private final ClaudeUserMapper claudeUserMapper;
  51. private final OrderDonMapper orderDonMapper;
  52. private final GoodsDonSkuMapper goodsDonSkuMapper;
  53. private final ChatGptAccountService chatGptAccountService;
  54. private final ClaudeService claudeService;
  55. private final LumaUserMapper lumaUserMapper;
  56. private final UpgradeOrderDonMapper upgradeOrderDonMapper;
  57. private final NanoUserQuotaMapper nanoUserQuotaMapper;
  58. private final MidjourneyUserMapper midjourneyUserMapper;
  59. @Override
  60. public void fillVipInfo(Long userId, VipUserPageResp vipUserPageResp) {
  61. SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class).eq(SysConfig::getSysKey, Constant.VIP_FEE_KEY).last("limit 1"));
  62. if (sysConfig != null && StrUtil.isNotBlank(sysConfig.getSysValue())) {
  63. vipUserPageResp.setVipFee(BigDecimal.valueOf(Integer.parseInt(sysConfig.getSysValue())));
  64. }
  65. if (userId == null) {
  66. return;
  67. }
  68. //可抵扣价格
  69. BigDecimal deductMoney = BigDecimal.ZERO;
  70. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  71. List<Long> vipSkuIds = beanSearcher.searchAll(VipGoodsSkuFrontView.class, MapUtils.builder().build()).stream().map(VipGoodsSkuFrontView::getSkuId).collect(Collectors.toList());
  72. //可有抵扣车票
  73. DateTime now = DateTime.now();
  74. //会员规格
  75. Set<Long> goodsIds = goodsDonSkuMapper.selectList(Wrappers.lambdaQuery(GoodsDonSku.class)
  76. .in(GoodsDonSku::getId, vipSkuIds)).stream().map(GoodsDonSku::getGoodsId).collect(Collectors.toSet());
  77. 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());
  78. //可抵扣车票
  79. if (count.intValue() > 0) {
  80. Integer months = vipUserPageResp.getMonths();
  81. //处理vip抵扣金额
  82. VipDeductDto vipDeductDto = commonHandleVipDeductMoney(userIdList, goodsIds, months);
  83. vipUserPageResp.setRelationUserViews(vipDeductDto.getRelationUserViews());
  84. vipUserPageResp.setExtendsDays(vipDeductDto.getExtendsDays());
  85. if (months != 12) {
  86. //取出最大可抵扣info
  87. vipDeductDto = commonHandleVipDeductMoney(userIdList, goodsIds, 12);
  88. }
  89. deductMoney = vipDeductDto.getDeductMoney();
  90. }
  91. vipUserPageResp.setDeductMoney(deductMoney);
  92. }
  93. /**
  94. * 检查用户并获取订单抵扣金额
  95. */
  96. @Override
  97. public VipDeductDto checkAndGetOrderDeductMoney(Long userId, Integer months) {
  98. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  99. List<Long> vipSkuIds = vipGoodsSkuMapper.selectUpSkus().stream().map(VipGoodsSku::getSkuId).collect(Collectors.toList());
  100. //会员规格
  101. Set<Long> goodsIds = goodsDonSkuMapper.selectList(Wrappers.lambdaQuery(GoodsDonSku.class)
  102. .in(GoodsDonSku::getId, vipSkuIds)).stream().map(GoodsDonSku::getGoodsId).collect(Collectors.toSet());
  103. Number count = beanSearcher.searchCount(GroupsRelationView.class, MapUtils.builder()
  104. .field(GroupsRelationView::getUserId, userIdList).op(Operator.InList)
  105. .field(GroupsRelationView::getGoodsId, goodsIds).op(Operator.InList)
  106. .field(GroupsRelationView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
  107. .field(GroupsRelationView::getIsVip, false)
  108. .build());
  109. if (count.intValue() == 0) {
  110. throw BusinessRuntimeException.getInstance("暂无可抵扣AI车票");
  111. }
  112. VipDeductDto vipDeductDto = commonHandleVipDeductMoney(userIdList, goodsIds, months);
  113. return vipDeductDto;
  114. }
  115. private BigDecimal getVipMoney(Integer months) {
  116. SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class).eq(SysConfig::getSysKey, Constant.VIP_FEE_KEY).last("limit 1"));
  117. if (sysConfig == null || StrUtil.isEmpty(sysConfig.getSysValue())) {
  118. throw BusinessRuntimeException.getInstance("系统异常");
  119. }
  120. BigDecimal vipFee = BigDecimal.valueOf(Long.valueOf(sysConfig.getSysValue()));
  121. return vipFee.multiply(BigDecimal.valueOf(months));
  122. }
  123. /**
  124. * 统一处理vip抵扣金额
  125. */
  126. public VipDeductDto commonHandleVipDeductMoney(List<Long> userIdList, Set<Long> goodsIds, Integer months) {
  127. VipDeductDto vipDeductDto = new VipDeductDto();
  128. DateTime now = DateTime.now();
  129. List<Long> deductRelationIds = new ArrayList<>();
  130. BigDecimal deductMoney = BigDecimal.ZERO;
  131. BigDecimal vipMoney = getVipMoney(months);
  132. List<GroupsRelationView> relations = beanSearcher.searchAll(GroupsRelationView.class, MapUtils.builder()
  133. .field(GroupsRelationView::getGoodsId, goodsIds).op(Operator.InList)
  134. .field(GroupsRelationView::getIsMirror, Boolean.TRUE)
  135. .field(GroupsRelationView::getUserId, userIdList).op(Operator.InList)
  136. .field(GroupsRelationView::getIsVip, false)
  137. .field(GroupsRelationView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
  138. .field(GroupsRelationView::getAqType, 1)
  139. .orderBy(GroupsRelationView::getExpiryTime).desc()
  140. .build());
  141. Set<Long> deductGoodsIds = null;
  142. List<GroupsRelationView> deductRelations = null;
  143. for (GroupsRelationView relation : relations) {
  144. if (deductGoodsIds == null) {
  145. deductGoodsIds = new HashSet<>();
  146. }
  147. if (deductRelations == null) {
  148. deductRelations = new ArrayList<>();
  149. }
  150. Long goodsId = relation.getGoodsId();
  151. if (deductGoodsIds.contains(goodsId)) {
  152. continue;
  153. }
  154. if (goodsId == Constant.NANO_GOODS_ID) {
  155. NanoUserQuota nanoUserQuota = nanoUserQuotaMapper.selectOne(Wrappers.lambdaQuery(NanoUserQuota.class).in(NanoUserQuota::getUid, userIdList).orderByDesc(NanoUserQuota::getRemainingQuota).last("limit 1"));
  156. if (nanoUserQuota != null) {
  157. if (!ObjectUtil.equal(nanoUserQuota.getRelationId(), relation.getId())) {
  158. continue;
  159. }
  160. } else {
  161. //规格最高
  162. RenewalView renewalView = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder()
  163. .field(RenewalView::getGoodsId, goodsId)
  164. .field(RenewalView::getUserId, userIdList).op(Operator.InList)
  165. .orderBy(RenewalView::getSkuNanoQuota).desc()
  166. .onlySelect(RenewalView::getRelationId)
  167. .build());
  168. if (renewalView != null && !ObjectUtil.equal(renewalView.getRelationId(), relation.getId())) {
  169. continue;
  170. }
  171. }
  172. }
  173. //订单是否存在
  174. Integer selectCount = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class).eq(OrderDon::getRelationId, relation.getId()).in(OrderDon::getUserId, userIdList).notIn(OrderDon::getStatus, Constant.noOrderAllStatus));
  175. if (selectCount == 0) {
  176. continue;
  177. }
  178. Long skuId = relation.getSkuId();
  179. deductGoodsIds.add(goodsId);
  180. Long relationId = relation.getId();
  181. Date relationExpiryTime = relation.getExpiryTime();
  182. //GPT
  183. if (goodsId == Constant.GPT_PLUS_GID) {
  184. ChatgptUser chatgptUser = chatgptUserMapper.selectOne(Wrappers.lambdaQuery(ChatgptUser.class).eq(ChatgptUser::getRelationId, relationId).last("limit 1"));
  185. if (chatgptUser == null) {
  186. chatGptAccountService.getUserInfo(relation.getUserId(), relation.getId());
  187. chatgptUser = chatgptUserMapper.selectOne(Wrappers.lambdaQuery(ChatgptUser.class).eq(ChatgptUser::getRelationId, relationId).last("limit 1"));
  188. }
  189. if (chatgptUser != null) {
  190. //获取抵扣金额
  191. deductMoney = deductMoney.add(getFinalDeductMoney(relationId, relation.getSkuId(), chatgptUser.getRenewSkuId(), chatgptUser.getRenewOrderId(), chatgptUser.getRenewExpiryTime(), relation.getStartTime(), relationExpiryTime, now, userIdList));
  192. }
  193. } else if (goodsId == Constant.CLAUDE_PRO_GID) {
  194. ClaudeUser claudeUser = claudeUserMapper.selectOne(Wrappers.lambdaQuery(ClaudeUser.class).eq(ClaudeUser::getRelationId, relationId).last("limit 1"));
  195. if (claudeUser == null) {
  196. claudeService.getUserInfo(relation.getUserId(), relation.getId());
  197. claudeUser = claudeUserMapper.selectOne(Wrappers.lambdaQuery(ClaudeUser.class).eq(ClaudeUser::getRelationId, relationId).last("limit 1"));
  198. }
  199. if (claudeUser != null) {
  200. //获取抵扣金额
  201. deductMoney = deductMoney.add(getFinalDeductMoney(relationId, relation.getSkuId(), claudeUser.getRenewSkuId(), claudeUser.getRenewOrderId(), claudeUser.getRenewExpiryTime(), relation.getStartTime(), relationExpiryTime, now, userIdList));
  202. }
  203. } else if (goodsId == Constant.LUMA_GOODS_ID || goodsId == Constant.NANO_GOODS_ID) {
  204. //luma、nano
  205. deductMoney = deductMoney.add(getGeneralRemainNumDeductMoney(relationId, goodsId, skuId, userIdList, relation.getStartTime()));
  206. } else if (goodsId == Constant.MIDJOURNEY_GOODS_ID) {
  207. //mj
  208. deductMoney = deductMoney.add(getMjRemainNumDeductMoney(relationId, skuId, userIdList, relation.getStartTime(), relation.getExpiryTime()));
  209. } else {
  210. //该车票抵扣金额
  211. deductMoney = deductMoney.add(getDeductMoneyFromTicket(relationId, userIdList, skuId, now, relation.getStartTime(), relationExpiryTime));
  212. }
  213. deductRelationIds.add(relationId);
  214. //可抵扣车票
  215. deductRelations.add(relation);
  216. }
  217. //实际可抵扣金额
  218. vipDeductDto.setActuallyDeDuctMoney(deductMoney);
  219. vipDeductDto.setDeductMoney(deductMoney);
  220. //超出可抵扣的金额 直接返回
  221. if (deductMoney.compareTo(vipMoney) >= 0) {
  222. BigDecimal overMoney = deductMoney.subtract(vipMoney);
  223. //超过抵扣金额可换延长会员多少天
  224. //每天单价
  225. BigDecimal daySingle = vipMoney.divide(BigDecimal.valueOf(months)).divide(BigDecimal.valueOf(30), 0, RoundingMode.DOWN);
  226. //超出部分可换取多少天 低于每天单价为0天
  227. BigDecimal extendsDays = overMoney.divide(daySingle, 0, RoundingMode.UP);
  228. vipDeductDto.setExtendsDays(extendsDays.intValue());
  229. vipDeductDto.setDeductMoney(vipMoney);
  230. }
  231. vipDeductDto.setDeductRelationIds(deductRelationIds);
  232. vipDeductDto.setRelationUserViews(deductRelations);
  233. return vipDeductDto;
  234. }
  235. /**
  236. * 获取最终抵扣金额
  237. */
  238. public BigDecimal getFinalDeductMoney(Long relationId, Long relationSkuId, Long renewSkuId, Long renewOrderId, Date renewExpiryTime, Date relationStartTime, Date relationExpiryTime, DateTime now, List<Long> userIdList) {
  239. BigDecimal deductMoney = BigDecimal.ZERO;
  240. //一个月当30天算单价 往上取整
  241. //续费规格
  242. if (renewSkuId != null && renewExpiryTime.compareTo(now) > 0) {
  243. OrderDon renewOrderDon = orderDonMapper.selectById(renewOrderId);
  244. BigDecimal orderMoney = renewOrderDon.getMoney().add(Optional.ofNullable(renewOrderDon.getBalance()).orElse(BigDecimal.ZERO));
  245. GoodsDonSku presentSku = skuMapper.selectById(renewSkuId);
  246. if (orderMoney.compareTo(BigDecimal.ZERO) == 0) {
  247. orderMoney = presentSku.getPrice();
  248. }
  249. Long betweenDay = DateUtil.betweenDay(now, renewExpiryTime, false) + 1;
  250. //当天购买的按规格原价抵扣
  251. BigDecimal renewDeductMoney;
  252. if (now.toDateStr().equals(DateUtil.format(renewOrderDon.getCreatedTime(), "yyyy-MM-dd"))) {
  253. renewDeductMoney = orderMoney;
  254. deductMoney = deductMoney.add(renewDeductMoney);
  255. if (presentSku.getDays() != null) {
  256. relationExpiryTime = DateUtil.offsetDay(relationExpiryTime, -presentSku.getDays());
  257. } else {
  258. relationExpiryTime = DateUtil.offsetMonth(relationExpiryTime, -presentSku.getMonths());
  259. }
  260. } else {
  261. //此次续费规格单价
  262. BigDecimal single = getPayOrderSingle(orderMoney, presentSku);
  263. //该车票抵扣金额
  264. renewDeductMoney = single.multiply(BigDecimal.valueOf(betweenDay));
  265. log.info("车票id:{}目前续费升级规格可抵扣的金额为:{}", relationId, renewDeductMoney);
  266. deductMoney = deductMoney.add(renewDeductMoney);
  267. relationExpiryTime = DateUtil.offsetDay(relationExpiryTime, -betweenDay.intValue());
  268. }
  269. //上一个续费升级规格
  270. List<OrderDon> orderDOns = orderDonRenewRecordMapper.getOtherRenewSkus(renewOrderId, relationId, userIdList);
  271. OrderDon presentRenewOrderDon = renewOrderDon;
  272. for (OrderDon thisRenewOrderDon : orderDOns) {
  273. Long skuId = thisRenewOrderDon.getSkuId();
  274. GoodsDonSku thisRenewSku = skuMapper.selectById(skuId);
  275. BigDecimal thisOrderMoney = thisRenewOrderDon.getMoney().add(Optional.ofNullable(thisRenewOrderDon.getBalance()).orElse(BigDecimal.ZERO));
  276. if (thisOrderMoney.compareTo(BigDecimal.ZERO) == 0) {
  277. thisOrderMoney = thisRenewSku.getPrice();
  278. }
  279. //当天购买的按规格原价抵扣
  280. BigDecimal thisRenewDeductMoney = BigDecimal.ZERO;
  281. if (now.toDateStr().equals(DateUtil.format(thisRenewOrderDon.getCreatedTime(), "yyyy-MM-dd"))) {
  282. thisRenewDeductMoney = thisOrderMoney;
  283. deductMoney = deductMoney.add(thisRenewDeductMoney);
  284. if (thisRenewSku.getDays() != null) {
  285. relationExpiryTime = DateUtil.offsetDay(relationExpiryTime, -thisRenewSku.getDays());
  286. } else {
  287. relationExpiryTime = DateUtil.offsetMonth(relationExpiryTime, -thisRenewSku.getMonths());
  288. }
  289. } else {
  290. //此次续费订单过期时间
  291. Date thisOrderExpiryTime;
  292. if (thisRenewSku.getDays() != null) {
  293. thisOrderExpiryTime = DateUtil.offsetDay(thisRenewOrderDon.getCreatedTime(), thisRenewSku.getDays());
  294. } else {
  295. thisOrderExpiryTime = DateUtil.offsetMonth(thisRenewOrderDon.getCreatedTime(), thisRenewSku.getMonths());
  296. }
  297. //续费同规格不处理
  298. if (!ObjectUtil.equal(thisRenewOrderDon.getSkuId(), presentRenewOrderDon.getSkuId())) {
  299. //先扣除已使用的天数
  300. //相差多少天
  301. Long thisRenewBetDay = DateUtil.betweenDay(thisRenewOrderDon.getCreatedTime(), presentRenewOrderDon.getCreatedTime(), false);
  302. //扣除已使用的
  303. thisOrderExpiryTime = DateUtil.offsetDay(thisOrderExpiryTime, -thisRenewBetDay.intValue());
  304. }
  305. //此订单过期时间
  306. if (thisOrderExpiryTime.compareTo(now) > 0) {
  307. //距离过期还剩多少天
  308. Long thisDeductBetweenDay = DateUtil.betweenDay(now, thisOrderExpiryTime, false) + 1;
  309. if (thisDeductBetweenDay != 0) {
  310. //此次续费规格单价
  311. BigDecimal single = getPayOrderSingle(thisOrderMoney, thisRenewSku);
  312. //该车票抵扣金额
  313. thisRenewDeductMoney = single.multiply(BigDecimal.valueOf(thisDeductBetweenDay));
  314. deductMoney = deductMoney.add(thisRenewDeductMoney);
  315. }
  316. //目前级别续费订单
  317. presentRenewOrderDon = thisRenewOrderDon;
  318. relationExpiryTime = DateUtil.offsetDay(relationExpiryTime, -thisDeductBetweenDay.intValue());
  319. }
  320. }
  321. log.info("车票id:{}目前次一级规格:{}可抵扣的金额为:{}", relationId, thisRenewSku.getSpecVal(), thisRenewDeductMoney);
  322. }
  323. }
  324. if (relationExpiryTime.after(now)) {
  325. //该车票抵扣金额
  326. BigDecimal deductMoneyFromTicket = getDeductMoneyFromTicket(relationId, userIdList, relationSkuId, now, relationStartTime, relationExpiryTime);
  327. deductMoney = deductMoney.add(deductMoneyFromTicket);
  328. }
  329. log.info("车票id:{}最终到期时间为:{}车票可返回 抵扣金额:{}", relationId, DateUtil.format(relationExpiryTime, "yyyy-MM-dd HH:mm:ss"), deductMoney);
  330. return deductMoney;
  331. }
  332. /**
  333. * 获取车票可抵扣金额
  334. */
  335. public BigDecimal getDeductMoneyFromTicket(Long relationId, List<Long> userIdList, Long skuId, DateTime now, Date relationStartTime, Date relationExpiryTime) {
  336. OrderDon oriOrderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class).eq(OrderDon::getRelationId, relationId).in(OrderDon::getUserId, userIdList).notIn(OrderDon::getStatus, Constant.noOrderAllStatus).eq(OrderDon::getOrderType, 1).orderByDesc(OrderDon::getId).last("limit 1"));
  337. List<OrderDon> orderDonList = orderDonMapper.selectList(Wrappers.lambdaQuery(OrderDon.class)
  338. .eq(OrderDon::getRelationId, relationId)
  339. .in(OrderDon::getUserId, userIdList)
  340. .eq(OrderDon::getSkuId, skuId)
  341. .ge(oriOrderDon != null, OrderDon::getCreatedTime, oriOrderDon.getCreatedTime())
  342. .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
  343. .orderByDesc(OrderDon::getId));
  344. //部分退款
  345. if (orderDonList.isEmpty()) {
  346. orderDonList = orderDonMapper.selectList(Wrappers.lambdaQuery(OrderDon.class)
  347. .eq(OrderDon::getRelationId, relationId)
  348. .in(OrderDon::getUserId, userIdList)
  349. .eq(OrderDon::getSkuId, skuId)
  350. .notIn(OrderDon::getStatus, Constant.noOrderStatus)
  351. .orderByDesc(OrderDon::getId));
  352. }
  353. //原车票订单总实付金额
  354. BigDecimal totalOrderMoney = BigDecimal.ZERO;
  355. //原车票订单总天数
  356. Integer totalDays = 0;
  357. for (OrderDon orderDon : orderDonList) {
  358. GoodsDonSku sku = skuMapper.selectById(skuId);
  359. BigDecimal thisOrderMoney = orderDon.getMoney().add(Optional.ofNullable(orderDon.getBalance()).orElse(BigDecimal.ZERO)).subtract(Optional.ofNullable(orderDon.getRefundMoney()).orElse(BigDecimal.ZERO));
  360. if (thisOrderMoney.compareTo(BigDecimal.ZERO) == 0) {
  361. thisOrderMoney = sku.getPrice();
  362. }
  363. totalOrderMoney = totalOrderMoney.add(thisOrderMoney);
  364. //总时间
  365. if (sku.getDays() != null) {
  366. totalDays += sku.getDays();
  367. } else {
  368. totalDays += sku.getMonths() * 30;
  369. }
  370. //原订单数
  371. if (orderDonList.size() == 1) {
  372. //此次订单过期时间
  373. Date thisOrderExpiryTime;
  374. if (sku.getDays() != null) {
  375. thisOrderExpiryTime = DateUtil.offsetDay(orderDon.getCreatedTime(), sku.getDays());
  376. } else {
  377. thisOrderExpiryTime = DateUtil.offsetMonth(orderDon.getCreatedTime(), sku.getMonths());
  378. }
  379. //当天购买的按规格原价抵扣
  380. if (now.toDateStr().equals(DateUtil.format(relationStartTime, "yyyy-MM-dd")) && (DateUtil.betweenDay(thisOrderExpiryTime, relationExpiryTime, true) == 0)) {
  381. return thisOrderMoney;
  382. }
  383. }
  384. }
  385. Long thisDeductBetweenDay = DateUtil.betweenDay(now, relationExpiryTime, false) + 1;
  386. BigDecimal single = totalOrderMoney.divide(BigDecimal.valueOf(totalDays), 0, RoundingMode.HALF_UP);
  387. //该车票抵扣金额
  388. BigDecimal deductMoney = single.multiply(BigDecimal.valueOf(thisDeductBetweenDay));
  389. return deductMoney;
  390. }
  391. /**
  392. * 获取订单单价
  393. */
  394. public BigDecimal getPayOrderSingle(BigDecimal orderMoney, GoodsDonSku sku) {
  395. //此次续费规格单价
  396. BigDecimal single;
  397. //区分天/月
  398. if (sku.getDays() != null) {
  399. single = orderMoney.divide(BigDecimal.valueOf(sku.getDays()), 0, RoundingMode.HALF_UP);
  400. } else {
  401. single = orderMoney.divide(BigDecimal.valueOf(sku.getMonths()).multiply(BigDecimal.valueOf(30)), 0, RoundingMode.HALF_UP);
  402. }
  403. return single;
  404. }
  405. /**
  406. * 获取普通剩余次数可抵扣金额
  407. */
  408. private BigDecimal getGeneralRemainNumDeductMoney(Long relationId, Long goodsId, Long skuId, List<Long> userIdList, Date relationStartTime) {
  409. BigDecimal deductMoney = BigDecimal.ZERO;
  410. BigDecimal orderTotalMoney = BigDecimal.ZERO;
  411. //总次数
  412. Integer totalNum = 0;
  413. DateTime now = DateTime.now();
  414. List<OrderDon> orderDonList = orderDonMapper.selectList(Wrappers.lambdaQuery(OrderDon.class).eq(OrderDon::getRelationId, relationId).in(OrderDon::getUserId, userIdList).notIn(OrderDon::getStatus, Constant.noOrderAllStatus));
  415. for (OrderDon orderDon : orderDonList) {
  416. GoodsDonSku sku = skuMapper.selectById(orderDon.getSkuId());
  417. Date orderCreatedTime = orderDon.getCreatedTime();
  418. Integer months = sku.getMonths();
  419. Integer days = sku.getDays();
  420. Date orderExpiryTime;
  421. if (days != null) {
  422. orderExpiryTime = DateUtil.offsetDay(orderCreatedTime, days);
  423. } else {
  424. orderExpiryTime = DateUtil.offsetMonth(orderCreatedTime, months);
  425. }
  426. //过期的订单不算入
  427. if (orderExpiryTime.before(now)) {
  428. continue;
  429. }
  430. if (orderDon.getMoney().compareTo(BigDecimal.ZERO) > 0) {
  431. orderTotalMoney = orderTotalMoney.add(orderDon.getMoney().add(Optional.ofNullable(orderDon.getBalance()).orElse(BigDecimal.ZERO)));
  432. } else {
  433. //兑换码订单使用规格价格
  434. orderTotalMoney = orderTotalMoney.add(sku.getPrice());
  435. }
  436. if (goodsId == Constant.LUMA_GOODS_ID) {
  437. totalNum += sku.getLumaNum();
  438. } else {
  439. totalNum += sku.getNanoQuota();
  440. }
  441. }
  442. //加量包订单
  443. List<UpgradeOrderDon> upgradeOrderDons = upgradeOrderDonMapper.selectList(Wrappers.lambdaQuery(UpgradeOrderDon.class).eq(UpgradeOrderDon::getRelationId, relationId).in(UpgradeOrderDon::getUserId, userIdList).notIn(UpgradeOrderDon::getStatus, Constant.noOrderAllStatus).gt(UpgradeOrderDon::getCreatedTime, relationStartTime));
  444. for (UpgradeOrderDon upgradeOrderDon : upgradeOrderDons) {
  445. orderTotalMoney = orderTotalMoney.add(upgradeOrderDon.getMoney().add(Optional.ofNullable(upgradeOrderDon.getBalance()).orElse(BigDecimal.ZERO)));
  446. totalNum = totalNum + upgradeOrderDon.getNum();
  447. }
  448. Integer remainNum = null;
  449. if (goodsId == Constant.LUMA_GOODS_ID) {
  450. LumaUser lumaUser = lumaUserMapper.selectOne(Wrappers.lambdaQuery(LumaUser.class).eq(LumaUser::getRelationId, relationId).last("limit 1"));
  451. if (lumaUser != null) {
  452. remainNum = lumaUser.getNum();
  453. }
  454. } else {
  455. NanoUserQuota nanoUserQuota = nanoUserQuotaMapper.selectOne(Wrappers.lambdaQuery(NanoUserQuota.class).eq(NanoUserQuota::getRelationId, relationId).last("limit 1"));
  456. if (nanoUserQuota != null) {
  457. remainNum = nanoUserQuota.getRemainingQuota();
  458. }
  459. }
  460. //若未使用
  461. if (remainNum == null) {
  462. deductMoney = deductMoney.add(orderTotalMoney);
  463. } else {
  464. BigDecimal remainMoney = totalNum == 0 ? BigDecimal.ZERO : BigDecimal.valueOf(remainNum).divide(BigDecimal.valueOf(totalNum), 2, RoundingMode.DOWN).multiply(orderTotalMoney);
  465. deductMoney = deductMoney.add(remainMoney);
  466. }
  467. return deductMoney;
  468. }
  469. /**
  470. * 获取mj剩余次数可抵扣金额
  471. */
  472. private BigDecimal getMjRemainNumDeductMoney(Long relationId, Long skuId, List<Long> userIdList, Date relationStartTime, Date relationExpiryTime) {
  473. BigDecimal deductMoney = BigDecimal.ZERO;
  474. BigDecimal orderTotalMoney = BigDecimal.ZERO;
  475. //总次数
  476. Integer totalNum = null;
  477. //原车票订单总天数
  478. Integer totalDays = 0;
  479. DateTime now = DateTime.now();
  480. List<OrderDon> orderDonList = orderDonMapper.selectList(Wrappers.lambdaQuery(OrderDon.class).eq(OrderDon::getRelationId, relationId).in(OrderDon::getUserId, userIdList).notIn(OrderDon::getStatus, Constant.noOrderAllStatus));
  481. for (OrderDon orderDon : orderDonList) {
  482. Date orderCreatedTime = orderDon.getCreatedTime();
  483. GoodsDonSku sku = skuMapper.selectById(orderDon.getSkuId());
  484. Integer months = sku.getMonths();
  485. Integer days = sku.getDays();
  486. //订单过期时间
  487. Date orderExpiryTime;
  488. if (days != null) {
  489. orderExpiryTime = DateUtil.offsetDay(orderCreatedTime, days);
  490. } else {
  491. orderExpiryTime = DateUtil.offsetMonth(orderCreatedTime, months);
  492. }
  493. //总时间
  494. totalDays += sku.getMonths() * 30;
  495. //过期的订单不算入
  496. if (orderExpiryTime.before(now)) {
  497. continue;
  498. }
  499. if (orderDon.getMoney().compareTo(BigDecimal.ZERO) > 0) {
  500. orderTotalMoney = orderTotalMoney.add(orderDon.getMoney().add(Optional.ofNullable(orderDon.getBalance()).orElse(BigDecimal.ZERO)));
  501. } else {
  502. //兑换码订单使用规格价格
  503. orderTotalMoney = orderTotalMoney.add(sku.getPrice());
  504. }
  505. if (totalNum == null) {
  506. totalNum = sku.getMjFastNum() + Optional.ofNullable(sku.getMjRelaxNum()).orElse(0);
  507. }
  508. }
  509. if (totalNum == null) {
  510. GoodsDonSku sku = skuMapper.selectById(skuId);
  511. totalNum = sku.getMjFastNum() + Optional.ofNullable(sku.getMjRelaxNum()).orElse(0);
  512. }
  513. Integer upgradeNum = 0;
  514. BigDecimal upgradeOrderMoney = BigDecimal.ZERO;
  515. //加量包订单
  516. List<UpgradeOrderDon> upgradeOrderDons = upgradeOrderDonMapper.selectList(Wrappers.lambdaQuery(UpgradeOrderDon.class).eq(UpgradeOrderDon::getRelationId, relationId).in(UpgradeOrderDon::getUserId, userIdList).notIn(UpgradeOrderDon::getStatus, Constant.noOrderAllStatus).gt(UpgradeOrderDon::getCreatedTime, relationStartTime));
  517. for (UpgradeOrderDon upgradeOrderDon : upgradeOrderDons) {
  518. upgradeNum += upgradeOrderDon.getNum();
  519. upgradeOrderMoney = upgradeOrderMoney.add(upgradeOrderDon.getMoney().add(Optional.ofNullable(upgradeOrderDon.getBalance()).orElse(BigDecimal.ZERO)));
  520. }
  521. MidjourneyUser midjourneyUser = midjourneyUserMapper.selectOne(Wrappers.lambdaQuery(MidjourneyUser.class).eq(MidjourneyUser::getRelationId, relationId).last("limit 1"));
  522. if (midjourneyUser == null) {
  523. deductMoney = deductMoney.add(orderTotalMoney).add(upgradeOrderMoney);
  524. } else {
  525. if (CollUtil.isNotEmpty(orderDonList)) {
  526. Integer remainFastNum = midjourneyUser.getMjFastNum();
  527. Integer remainRelaxNum = Optional.ofNullable(midjourneyUser.getMjRelaxNum()).orElse(0);
  528. Integer usedNum = totalNum - remainFastNum - remainRelaxNum;
  529. if (usedNum == 0 && now.toDateStr().equals(DateUtil.format(relationStartTime, "yyyy-MM-dd"))) {
  530. deductMoney = deductMoney.add(orderTotalMoney);
  531. } else {
  532. //每天单价
  533. BigDecimal single = orderTotalMoney.divide(BigDecimal.valueOf(totalDays), 0, RoundingMode.HALF_UP);
  534. BigDecimal consumeRate = BigDecimal.valueOf(usedNum).divide(BigDecimal.valueOf(totalNum), 2, RoundingMode.DOWN);
  535. //消耗的次数当月 值多少钱
  536. BigDecimal consumeMoney = consumeRate.multiply(single.multiply(BigDecimal.valueOf(30)));
  537. //该车票多长时间
  538. Long thisDeductBetweenDay = DateUtil.betweenDay(now, relationExpiryTime, false) + 1;
  539. //该车票抵扣金额
  540. BigDecimal orderRemainMoney = single.multiply(BigDecimal.valueOf(thisDeductBetweenDay));
  541. //最终可抵扣
  542. deductMoney = deductMoney.add(orderRemainMoney.subtract(consumeMoney));
  543. }
  544. }
  545. //加量包
  546. Integer remainUpgradeNum = midjourneyUser.getMjUpgradeNum();
  547. if (CollUtil.isNotEmpty(upgradeOrderDons)) {
  548. BigDecimal upgradeMoney = BigDecimal.valueOf(remainUpgradeNum).divide(BigDecimal.valueOf(upgradeNum), 2, RoundingMode.DOWN).multiply(upgradeOrderMoney);
  549. deductMoney = deductMoney.add(upgradeMoney);
  550. }
  551. }
  552. return deductMoney;
  553. }
  554. }