CmsGroupsRelationServiceImpl.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package com.cyksj.service.relation.impl;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUtil;
  4. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  5. import com.cyksj.common.constant.Constant;
  6. import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
  7. import com.cyksj.mapper.*;
  8. import com.cyksj.model.entity.*;
  9. import com.cyksj.redis.RedisService;
  10. import com.cyksj.service.relation.CmsGroupsRelationService;
  11. import com.cyksj.service.relation.GroupRelationClearService;
  12. import com.cyksj.service.user.UserBindRelationService;
  13. import lombok.RequiredArgsConstructor;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.transaction.annotation.Transactional;
  17. import java.util.List;
  18. /**
  19. * 项目名: yhlxj11111111
  20. * 文件名: CmsGroupsRelationServiceImpl
  21. * 创建者: JavaZou
  22. * 创建时间:2025/11/11 17:45
  23. */
  24. @Service
  25. @RequiredArgsConstructor
  26. @Slf4j
  27. public class CmsGroupsRelationServiceImpl implements CmsGroupsRelationService {
  28. private final NetflixUserAccountSubmitRecoverRecordMapper recoverRecordMapper;
  29. private final GroupsRelationMapper groupsRelationMapper;
  30. private final RedisService redisService;
  31. private final GroupsMapper groupsMapper;
  32. private final GoodsDonSkuMapper goodsDonSkuMapper;
  33. private final UserBindRelationService userBindRelationService;
  34. private final GroupRelationClearService clearService;
  35. private final OrderDonMapper orderDonMapper;
  36. private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
  37. @Override
  38. @Transactional(rollbackFor = Throwable.class)
  39. public void recoverAvailableParkingSpace() {
  40. List<NetflixUserAccountSubmitRecoverRecord> netflixUserAccountSubmitRecoverRecords = recoverRecordMapper.selectList(Wrappers.lambdaQuery(NetflixUserAccountSubmitRecoverRecord.class).eq(NetflixUserAccountSubmitRecoverRecord::getStatus, 0));
  41. if (netflixUserAccountSubmitRecoverRecords.isEmpty()) {
  42. return;
  43. }
  44. for (NetflixUserAccountSubmitRecoverRecord recoverRecord : netflixUserAccountSubmitRecoverRecords) {
  45. Long recoverRecordId = recoverRecord.getId();
  46. int update = recoverRecordMapper.update(null, Wrappers.lambdaUpdate(NetflixUserAccountSubmitRecoverRecord.class)
  47. .set(NetflixUserAccountSubmitRecoverRecord::getStatus, 1)
  48. .eq(NetflixUserAccountSubmitRecoverRecord::getId,recoverRecordId)
  49. .eq(NetflixUserAccountSubmitRecoverRecord::getStatus, 0));
  50. if (update > 0) {
  51. //获取同规格的车票
  52. Long skuId = recoverRecord.getSkuId();
  53. GoodsDonSku sku = goodsDonSkuMapper.selectById(skuId);
  54. DateTime newExpiryTime = DateUtil.offsetDay(DateTime.now(), recoverRecord.getRemainDays().intValue());
  55. GroupsRelation newRelation = getRelation(sku, newExpiryTime);
  56. if (newRelation == null) {
  57. //恢复 待恢复状态
  58. recoverStatusReset(recoverRecordId);
  59. continue;
  60. }
  61. //清除车票
  62. List<Long> userIdList;
  63. try {
  64. userIdList = userBindRelationService.getRelationUserIdList(recoverRecord.getUserId(), null);
  65. } catch (Exception e) {
  66. userIdList = List.of(recoverRecord.getUserId());
  67. }
  68. GroupsRelation oldRelation = groupsRelationMapper.selectOne(Wrappers.lambdaUpdate(GroupsRelation.class).eq(GroupsRelation::getId, recoverRecord.getRelationId()).in(GroupsRelation::getUserId, userIdList).last("limit 1"));
  69. if (oldRelation != null) {
  70. newRelation.setStartTime(oldRelation.getStartTime());
  71. newRelation.setExpiryTime(newExpiryTime);
  72. if (!resetUserTicket(oldRelation, newRelation, userIdList)) {
  73. //恢复 待恢复状态
  74. recoverStatusReset(recoverRecordId);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. public GroupsRelation getRelation(GoodsDonSku sku, DateTime expiryTime) {
  81. GroupsRelation relation = null;
  82. Integer maxNum = sku.getNum();
  83. GroupsTrips groups = null;
  84. if (groups == null) {
  85. //寻找差不多过期时间规格的车位
  86. relation = getSimilarExpiredGroupRelation(sku, expiryTime);
  87. if (relation == null) {
  88. DateTime filterAccountTime = null;
  89. groups = groupsMapper.selectTicketGroups(sku.getId(), GroupsTrips.Status.validity, maxNum, null, filterAccountTime);
  90. if (groups == null) {
  91. return null;
  92. }
  93. //获取车位
  94. if (relation == null) {
  95. //寻找快满编车队进行占座
  96. relation = groupsRelationMapper.selectRandomOneRelationByGroupsId(groups.getId());
  97. if (relation == null) {
  98. Long groupsId = groups.getId();
  99. TASK_EXECUTOR.execute(() -> groupsMapper.updateAvailableNum(groupsId));
  100. return null;
  101. }
  102. }
  103. }
  104. }
  105. if (groups == null) {
  106. groups = groupsMapper.selectById(relation.getGroupsId());
  107. }
  108. //车队目前可用停车数
  109. relation.setGroupsAvailParkingNum(groups.getAvailableParkingNum());
  110. relation.setMaxNum(maxNum);
  111. return relation;
  112. }
  113. public GroupsRelation getSimilarExpiredGroupRelation(GoodsDonSku sku, DateTime expiryTime) {
  114. Long skuId = sku.getId();
  115. Integer maxNum = sku.getNum();
  116. DateTime minExpiryTime = DateUtil.offsetDay(expiryTime, -5);
  117. Long groupsId = groupsRelationMapper.selectSimilarExpiredRelation(skuId, minExpiryTime, maxNum, null);
  118. if (groupsId == null) {
  119. log.info("暂无相似过期时间车队");
  120. return null;
  121. }
  122. GroupsRelation relation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
  123. .eq(GroupsRelation::getGroupsId, groupsId)
  124. .eq(GroupsRelation::getStatus, GroupsRelation.Status.none)
  125. .eq(GroupsRelation::getUserId, 0)
  126. .orderByAsc(GroupsRelation::getNum)
  127. .last("limit 1"));
  128. if (relation != null) {
  129. log.info("获取相似车队的车位relationId:{}", relation.getId());
  130. }
  131. return relation;
  132. }
  133. private Boolean resetUserTicket(GroupsRelation oldRelation, GroupsRelation newRelation, List<Long> userIdList) {
  134. Long userId = oldRelation.getUserId();
  135. Long oldRelationId = oldRelation.getId();
  136. newRelation.setUserId(userId);
  137. //校验座位
  138. int update = groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
  139. .set(GroupsRelation::getUserId, userId)
  140. .eq(GroupsRelation::getUserId, 0)
  141. .eq(GroupsRelation::getId, newRelation.getId()));
  142. if (update == 0) {
  143. return Boolean.FALSE;
  144. }
  145. groupsMapper.decrAvailableNum(newRelation.getGroupsId());
  146. oldRelation.setNewRelationId(newRelation.getId());
  147. clearService.clearTicket(oldRelation, UserTicketClearedRecord.Source.recover_replace);
  148. //恢复状态
  149. newRelation.setStatus(GroupsRelation.Status.validity);
  150. groupsRelationMapper.updateById(newRelation);
  151. //原订单座位id更新
  152. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  153. .in(OrderDon::getUserId, userIdList)
  154. .eq(OrderDon::getRelationId, oldRelationId)
  155. .notIn(OrderDon::getStatus, Constant.noOrderStatus)
  156. .orderByDesc(OrderDon::getId)
  157. .last("limit 1"));
  158. if (orderDon != null) {
  159. orderDon.setRelationId(newRelation.getId());
  160. orderDonMapper.updateById(orderDon);
  161. }
  162. return Boolean.TRUE;
  163. }
  164. public void recoverStatusReset(Long recoverId) {
  165. recoverRecordMapper.update(null, Wrappers.lambdaUpdate(NetflixUserAccountSubmitRecoverRecord.class)
  166. .set(NetflixUserAccountSubmitRecoverRecord::getStatus, 0)
  167. .eq(NetflixUserAccountSubmitRecoverRecord::getId, recoverId)
  168. .eq(NetflixUserAccountSubmitRecoverRecord::getStatus, 1));
  169. }
  170. }