CmsGroupsRelationServiceImpl.java 7.7 KB

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