SchedulerServiceImpl.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package com.cyksj.service.scheduler.impl;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUnit;
  4. import cn.hutool.core.date.DateUtil;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.cyksj.common.constant.Constant;
  7. import com.cyksj.common.util.StringUtil;
  8. import com.cyksj.mapper.*;
  9. import com.cyksj.model.entity.*;
  10. import com.cyksj.model.manage.views.GroupsRelationView;
  11. import com.cyksj.model.request.ChangeRelationReq;
  12. import com.cyksj.service.error.UserRelationChangeErrorRecordService;
  13. import com.cyksj.service.groups.GroupsFuncService;
  14. import com.cyksj.service.mange.CmsOrderDonService;
  15. import com.cyksj.service.relation.GroupRelationClearService;
  16. import com.cyksj.service.scheduler.SchedulerService;
  17. import com.cyksj.service.user.UserBenefitsService;
  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.Date;
  27. import java.util.List;
  28. /*
  29. *项目名: netflix
  30. *文件名: SchedulerServiceImpl
  31. *创建者: JavaZou
  32. *创建时间:2023/6/8 16:48
  33. */
  34. @Service
  35. @RequiredArgsConstructor
  36. @Slf4j
  37. public class SchedulerServiceImpl implements SchedulerService {
  38. private final GoodsDonSkuMapper skuMapper;
  39. private final GroupsMapper groupsMapper;
  40. private final BeanSearcher beanSearcher;
  41. private final OrderDonMapper orderDonMapper;
  42. private final GroupsRelationMapper groupsRelationMapper;
  43. private final GroupRelationClearService groupRelationClearService;
  44. private final UserBenefitsService userBenefitsService;
  45. private final GroupsFuncService groupsFuncService;
  46. private final CmsOrderDonService cmsOrderDonService;
  47. private final UserRelationChangeErrorRecordService changeErrorRecordService;
  48. private final UserRelationChangeErrorRecordMapper userRelationChangeErrorRecordMapper;
  49. @Override
  50. public void transExpiryAccountValidRelation() {
  51. DateTime now = new DateTime();
  52. DateTime nextBeginDay = DateUtil.offsetDay(DateUtil.beginOfDay(now), 1);
  53. //针对AI类 CHAT PLUS、MidJourney 月付
  54. List<Long> aiSkuIds = skuMapper.selectAIMonthSkuIds(Constant.AI_goodsIds);
  55. List<GroupsTrips> expiryAccountGroups = groupsMapper.getExpiryAccountGroups(nextBeginDay, aiSkuIds);
  56. expiryAccountGroups.forEach(expiry_groups -> {
  57. if (expiry_groups.getStatus() != GroupsTrips.Status.down) {
  58. expiry_groups.setStatus(GroupsTrips.Status.down);
  59. groupsMapper.updateById(expiry_groups);
  60. }
  61. Long g_groupsId = expiry_groups.getId();
  62. Date a_expiryTime = expiry_groups.getExpiryTime();
  63. //未过期用户 只分配有效的用户车票 outside状态过滤
  64. List<GroupsRelationView> groupsRelations = beanSearcher.searchAll(GroupsRelationView.class, MapUtils.builder()
  65. .field(GroupsRelationView::getGroupsId, g_groupsId)
  66. .field(GroupsRelationView::getStatus, GroupsRelation.Status.validity.name())
  67. .field(GroupsRelationView::getExpiryTime, nextBeginDay).op(Operator.GreaterEqual)
  68. .build());
  69. log.info("迁移groupsId:{}未过期用户数量:{}", g_groupsId, groupsRelations.size());
  70. groupsRelations.forEach(relationView -> {
  71. Long change_relationId = relationView.getId();
  72. Long userId = relationView.getUserId();
  73. UserRelationChangeErrorRecord exists = userRelationChangeErrorRecordMapper.selectOne(Wrappers.lambdaQuery(UserRelationChangeErrorRecord.class)
  74. .eq(UserRelationChangeErrorRecord::getRelationId, change_relationId)
  75. .eq(UserRelationChangeErrorRecord::getUserId, userId)
  76. .eq(UserRelationChangeErrorRecord::getDeleted, true)
  77. .last("limit 1"));
  78. if (exists != null) {
  79. log.info("存在userId:{}无法转移的车票relationId:{}", userId, change_relationId);
  80. return;
  81. }
  82. Date expiryTime = relationView.getExpiryTime();
  83. Long skuId = relationView.getSkuId();
  84. //仅针对chatGPT
  85. if (relationView.getGoodsId() == 18) {
  86. Long bet_day = DateUtil.between(now, expiryTime, DateUnit.DAY);
  87. //10天以内
  88. if (bet_day <= 10) {
  89. //分配到过期账号5天内误差的相同规格车队里
  90. DateTime five_day = DateUtil.offsetDay(expiryTime, 5);
  91. Long item_groupsId = groupsMapper.selectSameSpecItemGroupsByTime(g_groupsId, skuId, expiryTime, five_day, false);
  92. if (item_groupsId == null) {
  93. //无对应车队 退款
  94. //退款至余额
  95. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  96. .eq(OrderDon::getRelationId, change_relationId)
  97. .eq(OrderDon::getUserId, userId)
  98. .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
  99. .orderByDesc(OrderDon::getId)
  100. .last("limit 1"));
  101. if (orderDon == null) {
  102. //更换过车票
  103. orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  104. .eq(OrderDon::getSkuId, skuId)
  105. .eq(OrderDon::getUserId, userId)
  106. .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
  107. .orderByDesc(OrderDon::getId)
  108. .last("limit 1"));
  109. }
  110. if (orderDon == null) {
  111. log.error("用户userId:{}的车票relationId:{}账号过期后转移错误", userId, change_relationId);
  112. changeErrorRecordService.changeTicketErrorRecord(relationView, UserRelationChangeErrorRecord.Source.change);
  113. return;
  114. }
  115. //清除车票
  116. GroupsRelation relation = groupsRelationMapper.selectById(change_relationId);
  117. groupRelationClearService.clearTicket(relation, UserTicketClearedRecord.Source.timing_change_ticket);
  118. //退款至余额
  119. //实付金额 + 余额
  120. BigDecimal money = orderDon.getMoney().add(orderDon.getBalance());
  121. //当月天数
  122. Date payTime = orderDon.getPayTime();
  123. if (payTime == null) {
  124. payTime = orderDon.getCreatedTime();
  125. }
  126. if (money.compareTo(BigDecimal.ZERO) == 0) {
  127. GoodsDonSku sku = skuMapper.selectById(skuId);
  128. if (sku != null) {
  129. money = sku.getPrice();
  130. }
  131. }
  132. int dayNum = DateUtil.dayOfMonth(DateUtil.endOfMonth(payTime));
  133. BigDecimal re_balance = money.divide(BigDecimal.valueOf(dayNum), 0, RoundingMode.DOWN).multiply(BigDecimal.valueOf(bet_day));
  134. if (re_balance.compareTo(BigDecimal.ZERO) <= 0) {
  135. log.info("user_id:{},relationId:{}返回余额为0", userId, change_relationId);
  136. return;
  137. }
  138. userBenefitsService.addUserBalance(userId, re_balance);
  139. log.info("车票未到期清除,退款至用户user_id:{}余额:{}成功", userId, re_balance);
  140. userBenefitsService.recordBalanceBySource(userId, re_balance, UserBalanceSourceRecord.Source.clear_valid);
  141. return;
  142. }
  143. changeTickerRelation(item_groupsId, relationView, UserTicketClearedRecord.Source.timing_change_ticket);
  144. return;
  145. }
  146. //大于10天
  147. //分配到过期账号10天内误差的相同规格车队里
  148. DateTime ten_day = DateUtil.offsetDay(expiryTime, 10);
  149. Long item_groupsId = groupsMapper.selectSameSpecItemGroupsByTime(g_groupsId, skuId, expiryTime, ten_day, true);
  150. if (item_groupsId == null) {
  151. //无对应车队 生成空车队
  152. //寻找空车队
  153. item_groupsId = groupsMapper.selectSameSpecEmptyGroups(skuId);
  154. if (item_groupsId == null) {
  155. //新增空车队
  156. GoodsDonSku sku = skuMapper.selectById(skuId);
  157. GroupsRelation new_relation = groupsFuncService.createNewGroupsTrips(sku);
  158. item_groupsId = new_relation.getGroupsId();
  159. }
  160. //新车队
  161. changeTickerRelation(item_groupsId, relationView, UserTicketClearedRecord.Source.new_groups_ticket);
  162. return;
  163. }
  164. changeTickerRelation(item_groupsId, relationView, UserTicketClearedRecord.Source.timing_change_ticket);
  165. return;
  166. }
  167. if (relationView.getGoodsId() == 26) {
  168. //移入合适车队
  169. Long item_groupsId = groupsMapper.selectMidJourneySameSpecItemGroups(g_groupsId, skuId, expiryTime);
  170. if (item_groupsId == null) {
  171. //midJourney 无合适车队
  172. log.error("midJourney账号过期groupsId:{},用户车票relation_id:{}", g_groupsId, change_relationId);
  173. changeErrorRecordService.changeTicketErrorRecord(relationView, UserRelationChangeErrorRecord.Source.change);
  174. return;
  175. }
  176. changeTickerRelation(item_groupsId, relationView, UserTicketClearedRecord.Source.timing_change_ticket);
  177. }
  178. });
  179. });
  180. }
  181. public void changeTickerRelation(Long item_groupsId, GroupsRelationView relationView, UserTicketClearedRecord.Source source) {
  182. Long change_relationId = relationView.getId();
  183. try {
  184. ChangeRelationReq req = new ChangeRelationReq();
  185. req.setGroupsId(item_groupsId);
  186. req.setRelationId(change_relationId);
  187. req.setSource(source);
  188. cmsOrderDonService.changeRelation(req);
  189. } catch (Exception e) {
  190. log.error("更换用户relationId:{}错误:{}", change_relationId, StringUtil.getErrorText(e));
  191. relationView.setErrorMsg(StringUtil.getErrorMsg(e));
  192. changeErrorRecordService.changeTicketErrorRecord(relationView, UserRelationChangeErrorRecord.Source.change);
  193. }
  194. }
  195. }