GroupRelationFrontServiceImpl.java 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105
  1. package com.cyksj.service.relation.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.collection.CollectionUtil;
  4. import cn.hutool.core.date.BetweenFormater;
  5. import cn.hutool.core.date.DateTime;
  6. import cn.hutool.core.date.DateUtil;
  7. import cn.hutool.core.lang.Assert;
  8. import cn.hutool.core.lang.Validator;
  9. import cn.hutool.core.util.ObjectUtil;
  10. import cn.hutool.core.util.StrUtil;
  11. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  12. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  13. import com.cyksj.common.constant.Constant;
  14. import com.cyksj.common.exception.BusinessRuntimeException;
  15. import com.cyksj.common.snowflake.Sequence;
  16. import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
  17. import com.cyksj.common.util.Jsons;
  18. import com.cyksj.common.util.StringUtil;
  19. import com.cyksj.enums.GatewayApiCode;
  20. import com.cyksj.mapper.*;
  21. import com.cyksj.mapper.manage.distribute.DistributeGeneralSkuRateMapper;
  22. import com.cyksj.mapper.manage.distribute.UserDistributeMapper;
  23. import com.cyksj.mapper.manage.distribute.UserPlatDistributeRateMapper;
  24. import com.cyksj.mapper.manage.distribute.UserPlatSkuDistributeRateMapper;
  25. import com.cyksj.mapper.mirrorshop.UserMirrorShopMapper;
  26. import com.cyksj.mapper.renew.RenewUpgradePackageMapper;
  27. import com.cyksj.mapper.sys.SysConfigMapper;
  28. import com.cyksj.mapper.sys.SysEmailMapper;
  29. import com.cyksj.mapper.vip.VipGoodsSkuMapper;
  30. import com.cyksj.mapper.vip.VipUserMapper;
  31. import com.cyksj.model.dto.ErrorNetflixChangeOtherSkuTicketDto;
  32. import com.cyksj.model.dto.TicketChangeDto;
  33. import com.cyksj.model.entity.*;
  34. import com.cyksj.model.manage.views.AccountView;
  35. import com.cyksj.model.manage.views.GroupsRelationView;
  36. import com.cyksj.model.request.ErrorNetflixChangeReq;
  37. import com.cyksj.model.request.OrderRefundReq;
  38. import com.cyksj.model.request.SpecialGoodsTicketReq;
  39. import com.cyksj.model.request.TicketLoginReq;
  40. import com.cyksj.model.response.NetflixErrorAccountStatus;
  41. import com.cyksj.model.response.NetflixRecoverResp;
  42. import com.cyksj.model.response.NetflixRefundInfoResp;
  43. import com.cyksj.model.views.*;
  44. import com.cyksj.redis.RedisService;
  45. import com.cyksj.service.chatgpt.ChatGptAccountService;
  46. import com.cyksj.service.chatgpt.ChatGptAuthService;
  47. import com.cyksj.service.claude.ClaudeService;
  48. import com.cyksj.service.groups.GroupsFuncService;
  49. import com.cyksj.service.luma.LumaService;
  50. import com.cyksj.service.mail.CommonMailService;
  51. import com.cyksj.service.mail.MailService;
  52. import com.cyksj.service.mange.coupon.CouponCommonService;
  53. import com.cyksj.service.midjourney.MidjourneyAccountService;
  54. import com.cyksj.service.order.OrderRefundService;
  55. import com.cyksj.service.relation.GroupRelationClearService;
  56. import com.cyksj.service.relation.GroupRelationFrontService;
  57. import com.cyksj.service.relation.GroupsRelationChangeService;
  58. import com.cyksj.service.relation.GroupsRelationNetflixService;
  59. import com.cyksj.service.user.UserBenefitsService;
  60. import com.cyksj.service.user.UserBindRelationService;
  61. import com.cyksj.service.user.UserLoginRecordService;
  62. import com.ejlchina.searcher.BeanSearcher;
  63. import com.ejlchina.searcher.SearchResult;
  64. import com.ejlchina.searcher.param.Operator;
  65. import com.ejlchina.searcher.util.MapBuilder;
  66. import com.ejlchina.searcher.util.MapUtils;
  67. import com.github.rholder.retry.*;
  68. import lombok.RequiredArgsConstructor;
  69. import lombok.extern.slf4j.Slf4j;
  70. import org.springframework.dao.DuplicateKeyException;
  71. import org.springframework.stereotype.Service;
  72. import org.springframework.transaction.annotation.Transactional;
  73. import javax.servlet.http.HttpServletRequest;
  74. import java.math.BigDecimal;
  75. import java.math.RoundingMode;
  76. import java.util.*;
  77. import java.util.concurrent.ExecutionException;
  78. import java.util.concurrent.TimeUnit;
  79. import java.util.concurrent.atomic.AtomicInteger;
  80. import java.util.stream.Collectors;
  81. import static com.cyksj.enums.GatewayApiCode.GROUP_INDEX_ALREADY_BE_USER;
  82. import static com.cyksj.enums.GatewayApiCode.GROUP_INDEX_NOT_FIND;
  83. /*
  84. *项目名: netflix
  85. *文件名: GroupRelationFrontServiceImpl
  86. *创建者: JavaZou
  87. *创建时间:2022/12/29 15:42
  88. */
  89. @Service
  90. @RequiredArgsConstructor
  91. @Slf4j
  92. public class GroupRelationFrontServiceImpl implements GroupRelationFrontService {
  93. private final UserBindRelationService userBindRelationService;
  94. private final UserMapper userMapper;
  95. private final BeanSearcher beanSearcher;
  96. private final CouponCommonService couponCommonService;
  97. private final GroupsRelationMapper groupsRelationMapper;
  98. private final GroupsMapper groupsMapper;
  99. private final ChatGptAuthService chatGptAuthService;
  100. private final RedisService redisService;
  101. private final MailService mailService;
  102. private final OrderDonMapper orderDonMapper;
  103. private final GroupsRelationExpiryNotifyRecordMapper groupsRelationExpiryNotifyRecordMapper;
  104. private final HttpServletRequest request;
  105. private final GroupsFuncService groupsFuncService;
  106. private final OrderDiscountPlusPurchaseSkuRelationMapper orderDiscountPlusPurchaseSkuRelationMapper;
  107. private final GroupsRelationLoginCodeRecordMapper groupsRelationLoginCodeRecordMapper;
  108. private final SysEmailMapper sysEmailMapper;
  109. private final GoodsDonSkuMapper goodsDonSkuMapper;
  110. private final UserFuncPropertyMapper userFuncPropertyMapper;
  111. private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
  112. private final UserLoginRecordService userLoginRecordService;
  113. private final UserCertRecordMapper userCertRecordMapper;
  114. private final MidjourneyAccountService midjourneyAccountService;
  115. private final ChatGptAccountService chatGptAccountService;
  116. private final UpgradePackageMapper upgradePackageMapper;
  117. private final LumaService lumaService;
  118. private final GroupsRelationNetflixService groupsRelationNetflixService;
  119. private final UserDistributeMapper userDistributeMapper;
  120. private final UserPlatDistributeRateMapper userPlatDistributeRateMapper;
  121. private final GoodsDonMapper goodsDonMapper;
  122. private final UserPlatSkuDistributeRateMapper userPlatSkuDistributeRateMapper;
  123. private final DistributeGeneralSkuRateMapper distributeGeneralSkuRateMapper;
  124. private final RenewUpgradePackageMapper renewUpgradePackageMapper;
  125. private final ClaudeService claudeService;
  126. private final UserDistributePosterRecordMapper userDistributePosterRecordMapper;
  127. private final OrderDonRenewRecordMapper orderDonRenewRecordMapper;
  128. private final AccountMapper accountMapper;
  129. private final UserBenefitsMapper userBenefitsMapper;
  130. private final GroupsRelationChangeService changeTickerRelation;
  131. private final NetflixTempAccountUserMapper netflixTempAccountUserMapper;
  132. private final NetflixUserAccountStatusRecordMapper netflixUserAccountStatusRecordMapper;
  133. private final AccountNetflixStateMapper accountNetflixStateMapper;
  134. private final UserMirrorShopMapper userMirrorShopMapper;
  135. private final CommonMailService commonMailService;
  136. private final SysConfigMapper sysConfigMapper;
  137. private final GptMirrorDayCardGiveawayActivityMapper gptMirrorDayCardGiveawayActivityMapper;
  138. private final MirrorTicketPopupsRecordMapper mirrorTicketPopupsRecordMapper;
  139. private final VipUserMapper vipUserMapper;
  140. private final VipGoodsSkuMapper vipGoodsSkuMapper;
  141. private static final Sequence SEQUENCE = new Sequence(0);
  142. private final NetflixUserChangeOtherTicketRecordMapper netflixUserChangeOtherTicketRecordMapper;
  143. private final NetflixUserAccountSubmitRecoverRecordMapper netflixUserAccountSubmitRecoverRecordMapper;
  144. private final OrderRefundService orderRefundService;
  145. private final GroupRelationClearService clearService;
  146. private final UserBenefitsService userBenefitsService;
  147. @Override
  148. public SearchResult<RenewalView> getMyTicket(long userId, Boolean isLoginPopularize, String customId, String account, String orderNo, Integer cmt) {
  149. User u = userMapper.selectById(userId);
  150. if (u == null) throw BusinessRuntimeException.getGatewayApiCode(GatewayApiCode.CMS_TOKEN_VALID);
  151. if (u.getIsBlack()) {
  152. return new SearchResult<>();
  153. }
  154. List<Long> userIds = userBindRelationService.getRelationUserIdList(userId, u);
  155. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  156. if (cmt != null && cmt == 2 && StrUtil.isNotBlank(customId)) {
  157. UserMirrorShop userMirrorShop = userMirrorShopMapper.selectOne(Wrappers.lambdaQuery(UserMirrorShop.class)
  158. .eq(UserMirrorShop::getCustomId, customId)
  159. .last("limit 1"));
  160. if (userMirrorShop != null) {
  161. builder.field(RenewalView::getCmt, cmt).field(RenewalView::getYhsId, userMirrorShop.getId());
  162. }
  163. }
  164. if (cmt == null || cmt != 2) {
  165. builder.field(RenewalView::getCmt, 2).op(Operator.NotEqual);
  166. }
  167. // Long yhsId = yhShopFrontService.getYhsIdByCustomId(customId);
  168. DateTime now = DateTime.now();
  169. if (StrUtil.isNotBlank(account)) {
  170. builder.field(RenewalView::getAccount, account).op(Operator.Contain);
  171. }
  172. String conditionSql = String.format("(gr.expiry_time is null or gr.expiry_time > '%s'", now);
  173. //镜像车票 过期7天后清除
  174. //个人店铺和AI店铺 暂不支持过期续费
  175. if (cmt == null && StrUtil.isEmpty(customId)) {
  176. conditionSql += String.format(" or (sku.is_mirror and gr.expiry_time > '%s')", DateUtil.offsetDay(now, -7));
  177. }
  178. conditionSql += ")";
  179. if (StrUtil.isNotEmpty(orderNo)) {
  180. conditionSql += String.format(" and exists(select 1 from order_don o where o.relation_id = gr.id and o.user_id = gr.user_id and o.order_no = %s and o.status not in ('close','noPayment','refund'))", orderNo);
  181. }
  182. SearchResult<RenewalView> list = beanSearcher.search(RenewalView.class, builder
  183. .field(RenewalView::getUserId, 0).op(Operator.GreaterThan)
  184. .field(RenewalView::getUserId, userIds).op(Operator.InList)
  185. .put("condition", conditionSql)
  186. .field(RenewalView::getStatus, List.of("validity", "overdue", "outside")).op(Operator.InList)
  187. .orderBy(RenewalView::getExpiryTime).asc()
  188. .build());
  189. if (list.getDataList().size() > 0) {
  190. //过滤邀请制、已失效的车位
  191. Integer size = list.getTotalCount().intValue();
  192. //赠送GPT日卡 参与平台ids
  193. List<Long> renewReduceSkuIds = userBenefitsMapper.selectList(Wrappers.lambdaQuery(UserBenefits.class)
  194. .eq(UserBenefits::getType, UserBenefits.Type.RENEW).eq(UserBenefits::getStatus, 1).select(UserBenefits::getSkuId)).stream().map(UserBenefits::getSkuId).collect(Collectors.toList());
  195. List<Long> gptBuyGids = getGptMirrorGiveawayBuyGids();
  196. //gpt日卡活动
  197. GptMirrorDayCardGiveawayActivity gptMirrorDayCardGiveawayActivity = gptMirrorDayCardGiveawayActivityMapper.selectOne(Wrappers.lambdaQuery(GptMirrorDayCardGiveawayActivity.class)
  198. .last("limit 1"));
  199. SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class)
  200. .eq(SysConfig::getSysKey, Constant.EMAIL_PWD_SHOW_GID_KEY)
  201. .last("limit 1"));
  202. List<Long> showEmailPwdGids = new ArrayList<>();
  203. if (sysConfig != null && StrUtil.isNotBlank(sysConfig.getSysValue())) {
  204. try {
  205. showEmailPwdGids.addAll(Jsons.parseList(sysConfig.getSysValue(), Long.class));
  206. } catch (Exception e) {
  207. }
  208. }
  209. List<Long> vipSkuIds = vipGoodsSkuMapper.selectList(null).stream().map(VipGoodsSku::getSkuId).collect(Collectors.toList());
  210. Integer vipUserCount = vipUserMapper.selectCount(Wrappers.lambdaQuery(VipUser.class)
  211. .in(VipUser::getUserId, userIds)
  212. .gt(VipUser::getExpiryTime, now));
  213. list.getDataList().forEach(ticket -> {
  214. if (ticket.getSkuId() == null || ticket.getGoodsId() == null) {
  215. return;
  216. }
  217. if (ticket.getGoodsId() == 18 && ticket.getSkuNum() > 1) {
  218. ticket.setPassword(null);
  219. }
  220. ticket.setIsMulti(false);
  221. if (ticket.getSkuNum() > 1) {
  222. ticket.setIsMulti(true);
  223. }
  224. //chatGPT直连
  225. if (ticket.getGoodsId().equals(42l)) {
  226. if (StrUtil.isNotBlank(ticket.getAccount())) {
  227. String accessToken;
  228. try {
  229. accessToken = chatGptAuthService.getAccessToken(ticket.getAccount());
  230. ticket.setGptRefreshToken(accessToken);
  231. } catch (Exception e) {
  232. redisService.set(RedisService.key.CHAT_GPT_TOKEN_EXCEPTION_KEY.getName() + ticket.getAccount(), ticket.getAccount());
  233. }
  234. }
  235. ticket.setAccount(null);
  236. ticket.setPassword(null);
  237. } else {
  238. ticket.setGptRefreshToken(null);
  239. }
  240. if (ticket.getSecondType() == 1) {
  241. ticket.setAccount(null);
  242. ticket.setPassword(null);
  243. }
  244. //gpt 2月的独立账号可看到邮箱密码
  245. if (ticket.getGoodsId() == 18l && ticket.getSkuNum() == 1 && ticket.getAccount() != null && ticket.getCreatedTime().compareTo(DateUtil.parse("2025-02-01")) > 0) {
  246. } else {
  247. if (!showEmailPwdGids.contains(ticket.getGoodsId())) {
  248. ticket.setGptEmailPwd(null);
  249. }
  250. }
  251. if (isLoginPopularize != null && isLoginPopularize) {
  252. GoodsDonViews views = beanSearcher.searchFirst(GoodsDonViews.class, MapUtils.builder().field(GoodsDonViews::getId, ticket.getGoodsId()).build());
  253. views.setSpecs(beanSearcher.searchFirst(GoodsDonSpecFrontView.class, MapUtils.builder().field(GoodsDonSpecFrontView::getGoodsId, views.getId()).build()));
  254. views.setSkuList(beanSearcher.searchAll(GoodsDonSkuFrontView.class, MapUtils.builder().field(GoodsDonSkuFrontView::getGoodsId, views.getId()).field(GoodsDonSkuFrontView::getStatus, true).build()));
  255. ticket.setGoodsDonViews(views);
  256. }
  257. if (ticket.getMonths() != null && ticket.getMonths() >= 300) {
  258. ticket.setExpiryTime(null);
  259. }
  260. if (ticket.getExpiryTime() != null && ticket.getGoodsType() == 1) {
  261. String remainDays = DateUtil.formatBetween(now, ticket.getExpiryTime(), BetweenFormater.Level.DAY);
  262. if ("0天".equals(remainDays)) {
  263. remainDays = "小于1天";
  264. } else {
  265. //当天也算
  266. int days = Integer.parseInt(remainDays.substring(0, remainDays.indexOf("天")));
  267. remainDays = days + 1 + "天";
  268. }
  269. ticket.setRemainDays(remainDays);
  270. ticket.setTotal(DateUtil.betweenDay(ticket.getStartTime(), ticket.getExpiryTime(), false) + 1);
  271. }
  272. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  273. .eq(OrderDon::getRelationId, ticket.getRelationId())
  274. .eq(OrderDon::getUserId, ticket.getUserId())
  275. .eq(OrderDon::getOrderType, 1)
  276. .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
  277. .orderByDesc(OrderDon::getId)
  278. .last("limit 1"));
  279. if (orderDon != null) {
  280. ticket.setOrderNo(orderDon.getOrderNo());
  281. ticket.setOrderId(orderDon.getId());
  282. ticket.setTransactionId(orderDon.getTransactionId());
  283. if (ticket.getSecondType() == 1) {
  284. //是否完成邀请 根据订单状态
  285. ticket.setIsComplete(orderDon.getStatus() == OrderDon.Status.complete ? true : false);
  286. }
  287. }
  288. if (size > 1 && ticket.getGoodsId() != 18) {
  289. Date payTime = null;
  290. if (orderDon != null) {
  291. payTime = Optional.ofNullable(orderDon.getPayTime()).orElse(orderDon.getCreatedTime());
  292. }
  293. //加购订单
  294. if (orderDon == null) {
  295. List<OrderDiscountPlusPurchaseSkuRelation> plusPurchaseSkuRelations = orderDiscountPlusPurchaseSkuRelationMapper.selectPlusRelationByIdAndUserId(ticket.getRelationId(), ticket.getUserId());
  296. for (OrderDiscountPlusPurchaseSkuRelation data : plusPurchaseSkuRelations) {
  297. String relationIds = data.getRelationIds();
  298. if (StrUtil.isEmpty(relationIds)) {
  299. return;
  300. }
  301. try {
  302. List<Long> relationList = Jsons.parseList(relationIds, Long.class);
  303. if (relationList.contains(ticket.getRelationId())) {
  304. OrderDon plusOrder = orderDonMapper.selectById(data.getOrderId());
  305. if (plusOrder != null) {
  306. payTime = Optional.ofNullable(plusOrder.getPayTime()).orElse(plusOrder.getCreatedTime());
  307. }
  308. break;
  309. }
  310. } catch (Exception e) {
  311. }
  312. }
  313. }
  314. ticket.setPayTime(payTime);
  315. }
  316. if (ticket.getSkuNum() != null && ticket.getSkuNum() == 1) {
  317. }
  318. //奈飞、gpt
  319. if (ticket.getGoodsId() == 1 || ticket.getGoodsId() == 18 || ticket.getGoodsId() == 33l) {
  320. ticket.setEmail(null);
  321. }
  322. //设置镜像车票信息
  323. setMirrorTicketInfo(ticket);
  324. // List<GroupsRelationUserView> relationUserViews = groupsRelationMapper.getUserRelationUserViews(ticket.getGroupId(), ticket.getRelationId(), ticket.getUserId(), List.of(GroupsRelation.Status.validity, GroupsRelation.Status.outside));
  325. // ticket.setRelationViews(relationUserViews);
  326. if (ticket.getExpiryTime() != null) {
  327. ticket.setExpiryTimeStr(DateUtil.format(ticket.getExpiryTime(), "yyyy-MM-dd"));
  328. }
  329. //查看双重验证码
  330. if (StrUtil.isNotEmpty(ticket.getApiSecret())) {
  331. Boolean isHasVerifyCode = StrUtil.isNotBlank(ticket.getVerifyCode()) ? true : false;
  332. Integer codeNum = getUserCodeNumBySkuId(ticket.getUserId(), ticket.getSkuId(), isHasVerifyCode);
  333. ticket.setCodeNum(codeNum);
  334. }
  335. Long qaId = groupsRelationMapper.selectGoodsDonQaByGoodsId(ticket.getGoodsId());
  336. ticket.setQa(qaId > 0 ? true : false);
  337. //是否存在海报
  338. ticket.setPosterInfo(setGoodsPoster(ticket, u, userIds));
  339. //续费升级折扣
  340. setRenewDiscount(ticket);
  341. if (CollUtil.isNotEmpty(renewReduceSkuIds) && renewReduceSkuIds.contains(ticket.getSkuId())) {
  342. ticket.setIsRenewReduce(true);
  343. }
  344. if (CollUtil.isNotEmpty(gptBuyGids) && gptBuyGids.contains(ticket.getGoodsId())) {
  345. if (gptMirrorDayCardGiveawayActivity != null && orderDon != null && orderDon.getCreatedTime().compareTo(gptMirrorDayCardGiveawayActivity.getCreatedTime()) > 0) {
  346. //是否领取过GPT日卡
  347. Long orderId = orderDon.getId();
  348. Number count = beanSearcher.searchCount(GptMirrorDayCardActivityRecord.class, MapUtils.builder().field(GptMirrorDayCardActivityRecord::getOrderId, orderId).build());
  349. ticket.setReceivedGptGift(count.intValue() > 0);
  350. }
  351. }
  352. markVipDeductTicket(userIds, ticket, vipSkuIds, vipUserCount > 0);
  353. });
  354. }
  355. userLoginRecordService.saveUserLoginDayRecord(userId);
  356. return list;
  357. }
  358. @Override
  359. public Boolean isJoinCorpWx(Long userId, Boolean isJoin, Boolean noCoupon) {
  360. if (!isJoin) {
  361. User user = userMapper.selectById(userId);
  362. if (user == null || StrUtil.isEmpty(user.getUnionid())) {
  363. return true;
  364. }
  365. // CorpUser corpUser = corpUserMapper.selectOne(Wrappers.lambdaQuery(CorpUser.class)
  366. // .ne(CorpUser::getUnionid, "")
  367. // .eq(CorpUser::getUnionid, user.getUnionid())
  368. // .last("limit 1")
  369. // .select(CorpUser::getId)
  370. // );
  371. CorpUserFollowView corpUserFollowView = beanSearcher.searchFirst(CorpUserFollowView.class, MapUtils.builder()
  372. .field(CorpUserFollowView::getUnionId, user.getUnionid())
  373. .field(CorpUserFollowView::getStatus, 1).build());
  374. if (corpUserFollowView == null) {
  375. return false;
  376. }
  377. }
  378. //用户是否买了奈飞车票
  379. Number number = beanSearcher.searchCount(RenewalView.class, MapUtils.builder()
  380. .field(RenewalView::getUserId, userId)
  381. .field(RenewalView::getGoodsId, 1)
  382. .field(RenewalView::getStatus, List.of("validity", "overdue")).op(Operator.InList)
  383. .build());
  384. if (number.intValue() > 0 && !noCoupon) {
  385. couponCommonService.sendCouponToUser(userId, 34l, 0l, 0l, 0l, CouponUser.Channel.wxCorp);
  386. }
  387. return true;
  388. }
  389. @Override
  390. public GroupsRelation getSimilarExpiredGroupRelation(GoodsDonSku sku, List<Long> errorsRelationIds, String ip) {
  391. Long skuId = sku.getId();
  392. Integer days = sku.getDays();
  393. Integer months = sku.getMonths();
  394. Integer maxNum = sku.getNum();
  395. DateTime expiryTime = null;
  396. if (days != null && days > 0) {
  397. expiryTime = DateUtil.offsetDay(DateTime.now(), days);
  398. } else {
  399. expiryTime = DateUtil.offsetMonth(DateTime.now(), months);
  400. }
  401. Integer extraNum = 5;
  402. if (sku.getId() == 178l) {
  403. extraNum = 2;
  404. }
  405. maxNum += extraNum;
  406. DateTime minExpiryTime = DateUtil.offsetDay(expiryTime, -5);
  407. Long groupsId = groupsRelationMapper.selectSimilarExpiredRelation(skuId, minExpiryTime, maxNum, ip);
  408. if (groupsId == null) {
  409. log.info("暂无相似过期时间车队");
  410. return null;
  411. }
  412. GroupsRelation relation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
  413. .notIn(errorsRelationIds != null && errorsRelationIds.size() > 0, GroupsRelation::getId, errorsRelationIds)
  414. .eq(GroupsRelation::getGroupsId, groupsId)
  415. .eq(GroupsRelation::getStatus, GroupsRelation.Status.none)
  416. .eq(GroupsRelation::getUserId, 0)
  417. .orderByAsc(GroupsRelation::getNum)
  418. .last("limit 1"));
  419. if (relation != null) {
  420. log.info("用户userId:{},获取相似车队的车位relationId:{}", relation.getId());
  421. }
  422. return relation;
  423. }
  424. @Override
  425. public Page<UserRecommendGoodsFrontView> getRecommendGoodsViews(Boolean isPayNf, List<Long> filter_goods_id, Boolean filterCourseGoods, Integer start, Integer limit) {
  426. return groupsRelationMapper.getRecommendGoodsViews(new Page<>(start, limit), filter_goods_id, isPayNf, filterCourseGoods);
  427. }
  428. @Override
  429. @Transactional(rollbackFor = Throwable.class)
  430. public void sendCourseWare(SpecialGoodsTicketReq request) throws Exception {
  431. Long userId = request.getUserId();
  432. Long relationId = request.getRelationId();
  433. String email = request.getEmail();
  434. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  435. RenewalView relation = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder().field(RenewalView::getRelationId, relationId).build());
  436. Assert.notNull(relation, "您的车票不存在");
  437. Long relationUserId = relation.getUserId();
  438. if (!userIdList.contains(relationUserId)) {
  439. throw BusinessRuntimeException.getInstance("您的车票不存在");
  440. }
  441. if (relation.getSpecialType() != GoodsDon.SpecialType.courseware) {
  442. throw BusinessRuntimeException.getInstance("");
  443. }
  444. //是否已发送
  445. if (StrUtil.isNotBlank(relation.getGrAccount())) {
  446. throw BusinessRuntimeException.getInstance("已领取课件,请前往邮箱查看");
  447. }
  448. if (!Validator.isEmail(email)) {
  449. throw BusinessRuntimeException.getInstance("请输入正确的邮箱");
  450. }
  451. int update = groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
  452. .set(GroupsRelation::getAccount, email)
  453. .and(qr -> qr.isNull(GroupsRelation::getAccount).or().eq(GroupsRelation::getAccount, StrUtil.EMPTY))
  454. .eq(GroupsRelation::getId, relationId));
  455. if (update > 0) {
  456. orderDonMapper.update(null, Wrappers.lambdaUpdate(OrderDon.class)
  457. .set(OrderDon::getStatus, OrderDon.Status.complete)
  458. .eq(OrderDon::getRelationId, relationId)
  459. .eq(OrderDon::getUserId, userId)
  460. .eq(OrderDon::getStatus, OrderDon.Status.hasPayment));
  461. mailService.sendCoursewareByGoodsId(relation.getGoodsId(), relation.getRelationId(), relation.getUserId(), relation.getTitle(), email);
  462. }
  463. }
  464. @Override
  465. public GroupsRelation getRelation(Long payRelationId, Long userId, GoodsDonSku sku, String ip) {
  466. GroupsRelation relation = null;
  467. Integer maxNum = sku.getNum();
  468. if (payRelationId != null && payRelationId > 0) {
  469. relation = groupsRelationMapper.selectById(payRelationId);
  470. if (relation == null) {
  471. throw new BusinessRuntimeException(GROUP_INDEX_NOT_FIND.getCode(), GROUP_INDEX_NOT_FIND.getMsg());
  472. }
  473. if (relation.getUserId() != null && relation.getUserId() > 0L) {
  474. throw new BusinessRuntimeException(GROUP_INDEX_ALREADY_BE_USER.getCode(), GROUP_INDEX_ALREADY_BE_USER.getMsg());
  475. }
  476. } else {
  477. GroupsTrips groups = null;
  478. //ChatGpt Plus、Midjourney非独立规格 上车先获取硬塞座位 硬塞5个座位
  479. //主账号过期时间25天以外
  480. //23-12-06 MidJourney 不硬塞
  481. if (sku.getNum() > 1 && Constant.AI_goodsIds.get(0) == sku.getGoodsId()) {
  482. Integer extraNum = 5;
  483. if (sku.getId() == 178l || sku.getId() == 423l) {
  484. extraNum = 2;
  485. }
  486. maxNum += extraNum;
  487. DateTime time = DateUtil.offsetDay(DateUtil.beginOfDay(DateTime.now()), 25);
  488. //不塞存在该用户的车队
  489. Long groupsId = groupsRelationMapper.getOutSideGroupsTripsRelationByGoodsId(sku.getId(), time, extraNum, userId, sku.getNum(), maxNum);
  490. if (groupsId != null) {
  491. relation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
  492. .eq(GroupsRelation::getGroupsId, groupsId)
  493. .eq(GroupsRelation::getUserId, 0)
  494. .eq(GroupsRelation::getStatus, GroupsRelation.Status.outside)
  495. .last("limit 1"));
  496. if (relation == null) {
  497. relation = new GroupsRelation();
  498. relation.setStatus(GroupsRelation.Status.outside);
  499. relation.setUserId(userId);
  500. relation.setGroupsId(groupsId);
  501. GroupsRelation maxNumRelation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId, groupsId).orderByDesc(GroupsRelation::getNum).last("limit 1"));
  502. Integer nextNum = maxNumRelation.getNum() + 1;
  503. if (redisService.setNx(RedisService.key.GROUPS_TRIPS_OUTSIDE_RELATION.getNameFormat(groupsId, nextNum), nextNum, RedisService.key.GROUPS_TRIPS_OUTSIDE_RELATION.getTimeout())) {
  504. relation.setNum(nextNum);
  505. groupsRelationMapper.insert(relation);
  506. } else {
  507. throw BusinessRuntimeException.getInstance("系统繁忙,请重试...");
  508. }
  509. }
  510. groups = groupsMapper.selectById(groupsId);
  511. }
  512. }
  513. if (groups == null) {
  514. //寻找差不多过期时间规格的车位
  515. relation = getSimilarExpiredGroupRelation(sku, null, ip);
  516. if (relation == null) {
  517. //TODO 如果没指定座位,等待逻辑确认
  518. DateTime filterAccountTime = null;
  519. //奈飞过滤6月23号之前的车队
  520. if (sku.getGoodsId() == Constant.NETFLIX_GID) {
  521. filterAccountTime = DateUtil.parse("2025-06-23");
  522. }
  523. groups = groupsMapper.selectTicketGroups(sku.getId(), GroupsTrips.Status.validity, maxNum, ip, filterAccountTime);
  524. if (groups == null) {
  525. //待发车
  526. groups = groupsMapper.selectTicketGroups(sku.getId(), GroupsTrips.Status.waiting, maxNum, ip, null);
  527. }
  528. //新增空车队
  529. if (groups == null) {
  530. relation = groupsFuncService.createNewGroupsTrips(sku);
  531. }
  532. //获取车位
  533. if (relation == null) {
  534. //寻找快满编车队进行占座
  535. relation = groupsRelationMapper.selectRandomOneRelationByGroupsId(groups.getId());
  536. if (relation == null) {
  537. Long groupsId = groups.getId();
  538. TASK_EXECUTOR.execute(() -> groupsMapper.updateAvailableNum(groupsId));
  539. throw BusinessRuntimeException.getInstance("系统繁忙,请重试...");
  540. }
  541. }
  542. }
  543. }
  544. if (groups == null) {
  545. groups = groupsMapper.selectById(relation.getGroupsId());
  546. }
  547. //车队目前可用停车数
  548. relation.setGroupsAvailParkingNum(groups.getAvailableParkingNum());
  549. relation.setMaxNum(maxNum);
  550. }
  551. return relation;
  552. }
  553. @Override
  554. public String getAiVerifyCode(TicketLoginReq loginReq) throws Exception {
  555. Integer type = loginReq.getType();
  556. RenewalView renewalView;
  557. String account;
  558. Long userId = 0l;
  559. Long relationId = 0l;
  560. if (loginReq.getMerchants()) {
  561. renewalView = new RenewalView();
  562. renewalView.setGoodsId(loginReq.getGoodsId());
  563. renewalView.setAccount(loginReq.getAccount());
  564. if (loginReq.getAccountId() != null && loginReq.getAccountId() != 0l) {
  565. GroupsTrips groupsTrips = groupsMapper.selectOne(Wrappers.lambdaQuery(GroupsTrips.class)
  566. .eq(GroupsTrips::getAccountId, loginReq.getAccountId())
  567. .last("limit 1"));
  568. if (groupsTrips != null) {
  569. renewalView.setSkuId(groupsTrips.getSkuId());
  570. }
  571. }
  572. renewalView.setUserId(userId);
  573. account = loginReq.getAccount();
  574. } else {
  575. userId = loginReq.getUserId();
  576. relationId = loginReq.getRelationId();
  577. List<Long> relationUserIdList = userBindRelationService.getRelationUserIdList(userId, null);
  578. renewalView = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder().field(RenewalView::getUserId, relationUserIdList).op(Operator.InList)
  579. .field(RenewalView::getRelationId, relationId).build());
  580. if (renewalView == null) {
  581. throw BusinessRuntimeException.getInstance("您的车票不存在...");
  582. }
  583. if (StrUtil.isEmpty(renewalView.getAccount())) {
  584. throw BusinessRuntimeException.getInstance("车票未配置账号");
  585. }
  586. account = renewalView.getAccount();
  587. String password = renewalView.getPassword();
  588. if (StrUtil.hasEmpty(account, password)) {
  589. throw BusinessRuntimeException.getInstance("您的车票账号密码异常,请联系客服...");
  590. }
  591. }
  592. if (renewalView.getGoodsId() == 1l) {
  593. Integer linkType = loginReq.getLinkType();
  594. if (type == null && linkType == null) {
  595. throw BusinessRuntimeException.getInstance("系统异常");
  596. }
  597. if (linkType != null) {
  598. String key = RedisService.key.NF_UPGRAD_USER_LIMIT_KEY.getNameFormat(DateUtil.hour(DateTime.now(), true), linkType, renewalView.getUserId());
  599. if (!redisService.hasKey(key)) {
  600. redisService.set(key, 0l, RedisService.key.NF_UPGRAD_USER_LIMIT_KEY.getTimeout());
  601. } else {
  602. Integer value = (Integer) redisService.get(key);
  603. if (value >= 5) {
  604. throw BusinessRuntimeException.getInstance("获取奈飞同步更新装置过于频繁");
  605. }
  606. }
  607. }
  608. }
  609. String code = commonMailService.getTicketMailCode(renewalView.getGoodsId(), account, renewalView.getRelateEmail(), renewalView.getGptEmailPwd(), renewalView.getCreatedTime(), type, loginReq.getLinkType(), false);
  610. GroupsRelationLoginCodeRecord record = new GroupsRelationLoginCodeRecord();
  611. record.setUserId(userId);
  612. record.setRelationId(relationId);
  613. record.setAccount(renewalView.getAccount());
  614. record.setGoodsId(renewalView.getGoodsId());
  615. record.setSkuId(renewalView.getSkuId());
  616. groupsRelationLoginCodeRecordMapper.insert(record);
  617. //奈飞同步装置
  618. if (renewalView.getGoodsId() == 1l && loginReq.getLinkType() != null) {
  619. String key = RedisService.key.NF_UPGRAD_USER_LIMIT_KEY.getNameFormat(DateUtil.hour(DateTime.now(), true), loginReq.getLinkType(), renewalView.getUserId());
  620. redisService.incr(key, 1L);
  621. }
  622. return code;
  623. }
  624. @Override
  625. public void checkGroupsAvailParkingNum(Long skuId, Long groupsId, Integer availableParkingNum, Integer maxNum) {
  626. //GPT10人月付、4人月付
  627. if (skuId == 161l || skuId == 178l) {
  628. //为0时,不参与限制
  629. if (availableParkingNum == 0) {
  630. return;
  631. }
  632. //目前车队可用停车数
  633. //目前上车人数
  634. Integer currentParkingNum = groupsRelationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
  635. .eq(GroupsRelation::getGroupsId, groupsId)
  636. .ne(GroupsRelation::getUserId, 0)
  637. .le(GroupsRelation::getNum, maxNum));
  638. if (currentParkingNum > availableParkingNum) {
  639. throw BusinessRuntimeException.getInstance("系统繁忙,请重试...");
  640. }
  641. }
  642. }
  643. @Override
  644. public void adjustGroupsParkingTime(Long relationId, GoodsDonSku sku) {
  645. if (sku == null) {
  646. return;
  647. }
  648. Long skuId = sku.getId();
  649. //更新GPT Plus10人、4人月付车队状态 待优化
  650. if (skuId == 161l || skuId == 178l || skuId == 422l || skuId == 423l) {
  651. GroupsRelation relation = groupsRelationMapper.selectById(relationId);
  652. Long groupsId = relation.getGroupsId();
  653. GroupsTrips groupsTrips = groupsMapper.selectById(groupsId);
  654. Integer availableParkingNum = groupsTrips.getAvailableParkingNum();
  655. //为0时 不参与限制
  656. if (availableParkingNum == 0) {
  657. return;
  658. }
  659. Integer extraNum = 5;
  660. if (skuId == 178l || skuId == 423l) {
  661. extraNum = 2;
  662. }
  663. Integer maxNum = sku.getNum() + extraNum;
  664. if (availableParkingNum >= maxNum) {
  665. return;
  666. }
  667. //目前上车人数
  668. Integer currentParkingNum = groupsRelationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
  669. .eq(GroupsRelation::getGroupsId, groupsId)
  670. .ne(GroupsRelation::getUserId, 0)
  671. .le(GroupsRelation::getNum, maxNum));
  672. if (currentParkingNum >= availableParkingNum) {
  673. //锁住
  674. DateTime parkingTime = DateUtil.offsetDay(DateTime.now(), 1);
  675. groupsMapper.update(null, Wrappers.lambdaUpdate(GroupsTrips.class)
  676. .eq(GroupsTrips::getId, groupsTrips.getId())
  677. .set(GroupsTrips::getParkingTime, parkingTime)
  678. .isNull(GroupsTrips::getParkingTime));
  679. }
  680. }
  681. }
  682. @Override
  683. public void setGroupsTripsParkingTimeEmpty(GoodsDonSku sku, Long relationId) {
  684. if (sku == null) {
  685. return;
  686. }
  687. Long skuId = sku.getId();
  688. if (skuId == 161l || skuId == 178l) {
  689. GroupsRelation relation = groupsRelationMapper.selectById(relationId);
  690. Long groupsId = relation.getGroupsId();
  691. GroupsTrips groupsTrips = groupsMapper.selectById(groupsId);
  692. Integer availableParkingNum = groupsTrips.getAvailableParkingNum();
  693. if (groupsTrips.getParkingTime() == null || availableParkingNum == 0) {
  694. return;
  695. }
  696. Integer extraNum = 5;
  697. if (skuId == 178l) {
  698. extraNum = 2;
  699. }
  700. Integer skuNum = sku.getNum();
  701. if (availableParkingNum >= skuNum + extraNum) {
  702. return;
  703. }
  704. //目前上车人数
  705. Integer currentParkingNum = groupsRelationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
  706. .eq(GroupsRelation::getGroupsId, groupsId)
  707. .ne(GroupsRelation::getUserId, 0));
  708. if (currentParkingNum < availableParkingNum) {
  709. groupsMapper.update(null, Wrappers.lambdaUpdate(GroupsTrips.class)
  710. .eq(GroupsTrips::getId, groupsTrips.getId())
  711. .set(GroupsTrips::getParkingTime, null)
  712. .isNotNull(GroupsTrips::getParkingTime));
  713. }
  714. }
  715. }
  716. @Override
  717. public Integer getUserCodeNumBySkuId(Long userId, Long skuId, Boolean isHasVerifyCode) {
  718. //查看双重验证码
  719. UserFuncProperty userFuncProperty = userFuncPropertyMapper.selectOne(Wrappers.lambdaQuery(UserFuncProperty.class).eq(UserFuncProperty::getUserId, userId).last("limit 1"));
  720. //默认2次
  721. int num = 2;
  722. //有安全码查看次数 置为0
  723. if (isHasVerifyCode) {
  724. num = 0;
  725. //存在身份实名认证有两次机会
  726. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  727. Integer selectCount = userCertRecordMapper.selectCount(Wrappers.lambdaQuery(UserCertRecord.class)
  728. .in(UserCertRecord::getUserId, userIdList));
  729. if (selectCount > 0) {
  730. num = 2;
  731. }
  732. return num;
  733. }
  734. //查看双重验证码次数
  735. GoodsDonSku sku = goodsDonSkuMapper.selectById(skuId);
  736. if (sku.getCodeNum() > 0) {
  737. num = sku.getCodeNum();
  738. }
  739. if (userFuncProperty != null) num = userFuncProperty.getVerifyCheckNum();
  740. return num;
  741. }
  742. @Override
  743. public List<RenewalView> getHasPayGoods(long userId) {
  744. User user = userMapper.selectById(userId);
  745. List<Long> userIds = userBindRelationService.getRelationUserIdList(userId, user);
  746. DateTime now = DateTime.now();
  747. List<RenewalView> list = groupsRelationMapper.getUserHasPayGoods(userIds, now);
  748. return list;
  749. }
  750. @Override
  751. public List<RenewalView> getExpiryRenewalNotify(Long userId) {
  752. DateTime now = DateTime.now();
  753. DateTime thisZero = DateUtil.beginOfDay(now);
  754. String thisDate = thisZero.toDateStr();
  755. Object exists = redisService.get(RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getNameFormat(thisDate, userId));
  756. if (exists != null) {
  757. return null;
  758. }
  759. User u = userMapper.selectById(userId);
  760. if (u == null) throw BusinessRuntimeException.getGatewayApiCode(GatewayApiCode.CMS_TOKEN_VALID);
  761. if (u.getIsBlack()) {
  762. redisService.setNx(RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getNameFormat(thisDate, userId), userId, RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getTimeout());
  763. return null;
  764. }
  765. List<Long> userIds = userBindRelationService.getRelationUserIdList(userId, u);
  766. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  767. List<RenewalView> list = beanSearcher.searchAll(RenewalView.class, builder
  768. .field(RenewalView::getUserId, 0).op(Operator.GreaterThan)
  769. .field(RenewalView::getUserId, userIds).op(Operator.InList)
  770. .field(RenewalView::getExpiryTime, now).op(Operator.GreaterThan)
  771. .field(RenewalView::getStatus, List.of("validity", "overdue", "outside")).op(Operator.InList)
  772. .field(RenewalView::getAqType, 1)
  773. .field(RenewalView::getIsVip, Boolean.TRUE)
  774. .orderBy(RenewalView::getExpiryTime).asc()
  775. .build());
  776. if (list.size() > 5) {
  777. redisService.setNx(RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getNameFormat(thisDate, userId), userId, RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getTimeout());
  778. return null;
  779. }
  780. List<Long> renewReduceSkuIds = userBenefitsMapper.selectList(Wrappers.lambdaQuery(UserBenefits.class)
  781. .eq(UserBenefits::getType, UserBenefits.Type.RENEW).eq(UserBenefits::getStatus, 1).select(UserBenefits::getSkuId)).stream().map(UserBenefits::getSkuId).collect(Collectors.toList());
  782. //提前7天
  783. AtomicInteger day = new AtomicInteger(7);
  784. List<RenewalView> expiryTickets = list.stream().filter(ticket -> {
  785. //下架
  786. if (ticket.getGoodsStatus() == null || ticket.getSkuStatus() == null || !ticket.getGoodsStatus() || !ticket.getSkuStatus()) {
  787. return false;
  788. }
  789. Integer skuMonths = ticket.getMonths();
  790. if (skuMonths == null) {
  791. return false;
  792. }
  793. Date expiryTime = ticket.getExpiryTime();
  794. //年付 提前一个月
  795. if (skuMonths >= 12) {
  796. day.set(30);
  797. } else {
  798. day.set(7);
  799. }
  800. if (DateUtil.beginOfDay(expiryTime).compareTo(DateUtil.offsetDay(thisZero, day.get())) <= 0) {
  801. ticket.setExpiryTimeStr(DateUtil.format(ticket.getExpiryTime(), "yyyy-MM-dd"));
  802. //GPT镜像信息
  803. setGptMirrorInfo(ticket);
  804. ticket.setExpiryTime(null);
  805. if (CollUtil.isNotEmpty(renewReduceSkuIds) && renewReduceSkuIds.contains(ticket.getSkuId())) {
  806. ticket.setIsRenewReduce(true);
  807. }
  808. return true;
  809. }
  810. return false;
  811. }).collect(Collectors.toList());
  812. redisService.setNx(RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getNameFormat(thisDate, userId), userId, RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getTimeout());
  813. if (CollUtil.isNotEmpty(expiryTickets)) {
  814. groupsRelationExpiryNotifyRecordMapper.batchInsert(userId, expiryTickets);
  815. }
  816. return expiryTickets;
  817. }
  818. @Override
  819. public void getCollegeStudentUserGptTicket(Long userId) {
  820. Retryer<Boolean> build = RetryerBuilder.<Boolean>newBuilder()
  821. .retryIfException()
  822. //运行时异常重试
  823. .retryIfRuntimeException()
  824. //false重试
  825. .retryIfResult(res -> Objects.equals(res, false))
  826. //1s 间隔
  827. .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))
  828. //停止策略 : 尝试请求3次
  829. .withStopStrategy(StopStrategies.stopAfterAttempt(3)).build();
  830. try {
  831. build.call(() -> {
  832. //体验规格
  833. Long gptMirrorSkuId = Constant.GPT_MIRROR_SKU_ID;
  834. GroupsTrips groups = groupsMapper.selectCollegeStudentUserGptTicket(gptMirrorSkuId);
  835. if (groups == null) {
  836. groups = groupsMapper.selectOne(Wrappers.lambdaQuery(GroupsTrips.class)
  837. .eq(GroupsTrips::getStatus, GroupsTrips.Status.waiting)
  838. .eq(GroupsTrips::getSkuId, gptMirrorSkuId)
  839. .last("limit 1"));
  840. }
  841. GroupsRelation relation = null;
  842. if (groups != null) {
  843. relation = groupsRelationMapper.selectRandomOneRelationByGroupsId(groups.getId());
  844. }
  845. if (relation == null) {
  846. //生成空车队
  847. GoodsDonSku sku = goodsDonSkuMapper.selectById(gptMirrorSkuId);
  848. relation = groupsFuncService.createNewGroupsTrips(sku);
  849. }
  850. DateTime startTime = DateTime.now();
  851. DateTime expiryTime = DateUtil.offsetDay(startTime, 1);
  852. int update = groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
  853. .set(GroupsRelation::getUserId, userId)
  854. .set(GroupsRelation::getStartTime, startTime)
  855. .set(GroupsRelation::getExpiryTime, expiryTime)
  856. .set(GroupsRelation::getStatus, GroupsRelation.Status.validity)
  857. .set(GroupsRelation::getAqType, 2)
  858. .eq(GroupsRelation::getId, relation.getId())
  859. .eq(GroupsRelation::getUserId, 0));
  860. if (update > 0) {
  861. groupsMapper.decrAvailableNum(relation.getGroupsId());
  862. return true;
  863. }
  864. log.error("发送用户:{} GPT镜像车票体验失败,进行重试", userId);
  865. return false;
  866. });
  867. } catch (ExecutionException | RetryException e) {
  868. log.error("发送用户GPT镜像体验失败:{}", StringUtil.getErrorText(e));
  869. }
  870. }
  871. @Override
  872. public RenewalView getTicketDetail(long userId, Long relationId) {
  873. User u = userMapper.selectById(userId);
  874. List<Long> userIds = userBindRelationService.getRelationUserIdList(userId, u);
  875. MapBuilder builder = MapUtils.builder();
  876. RenewalView renewalView = beanSearcher.searchFirst(RenewalView.class, builder
  877. .field(RenewalView::getRelationId, relationId)
  878. .field(RenewalView::getUserId, 0).op(Operator.GreaterThan)
  879. .field(RenewalView::getUserId, userIds).op(Operator.InList)
  880. .field("condition", String.format("(gr.expiry_time is null or gr.expiry_time > '%s')", DateTime.now()))
  881. .field(RenewalView::getStatus, List.of("validity", "outside")).op(Operator.InList)
  882. .orderBy(RenewalView::getExpiryTime).asc()
  883. .onlySelect(RenewalView::getRelationId, RenewalView::getExpiryTime, RenewalView::getTitle, RenewalView::getSpecVal, RenewalView::getGoodsId, RenewalView::getSkuId, RenewalView::getSkuNum, RenewalView::getSkuStatus, RenewalView::getGoodsStatus)
  884. .build());
  885. if (renewalView != null) {
  886. renewalView.setExpiryTimeStr(DateUtil.format(renewalView.getExpiryTime(), "yyyy-MM-dd"));
  887. }
  888. return renewalView;
  889. }
  890. @Override
  891. public GroupsRelation ticketLogin(TicketLoginReq loginReq) {
  892. String account = loginReq.getAccount();
  893. if (StrUtil.isEmpty(account)) {
  894. throw BusinessRuntimeException.getInstance("请输入对应账号");
  895. }
  896. if (loginReq.getMerchants()) {
  897. if (account.contains(Constant.YH_EMAIL_DOMAIN_SUFFIX)) {
  898. SysEmail sysEmail = sysEmailMapper.selectOne(Wrappers.lambdaQuery(SysEmail.class)
  899. .eq(SysEmail::getEmail, account)
  900. .last("limit 1"));
  901. if (sysEmail == null) {
  902. throw BusinessRuntimeException.getInstance("账号错误,请联系客服");
  903. }
  904. }
  905. GroupsRelation relation = new GroupsRelation();
  906. relation.setId(0l);
  907. return relation;
  908. }
  909. String password = loginReq.getPassword();
  910. Integer num = loginReq.getNum();
  911. if (ObjectUtil.hasEmpty(password, num)) {
  912. throw BusinessRuntimeException.getInstance("参数错误");
  913. }
  914. Long goodsId = Optional.ofNullable(loginReq.getGoodsId()).orElse(1l);
  915. GroupsRelation relation = groupsRelationMapper.selectRelationByAccountAndNum(account, password, goodsId, num);
  916. if (relation == null) {
  917. log.info("外部入口使用账号account:{},pwd:{},座位num:{}登录失败...", account, password, num);
  918. if (StrUtil.equals(loginReq.getLang(), Constant.EN_US)) {
  919. throw BusinessRuntimeException.getInstance("Please enter the correct account information");
  920. }
  921. throw BusinessRuntimeException.getInstance("请输入正确的账号信息");
  922. }
  923. return relation;
  924. }
  925. @Override
  926. public String outsideTicketVerify(TicketLoginReq loginReq) {
  927. String account = loginReq.getAccount();
  928. if (StrUtil.isEmpty(account)) {
  929. throw BusinessRuntimeException.getInstance("账号错误");
  930. }
  931. if (loginReq.getMerchants()) {
  932. if (account.contains(Constant.YH_EMAIL_DOMAIN_SUFFIX)) {
  933. SysEmail sysEmail = sysEmailMapper.selectOne(Wrappers.lambdaQuery(SysEmail.class)
  934. .eq(SysEmail::getEmail, account)
  935. .last("limit 1"));
  936. if (sysEmail == null) {
  937. throw BusinessRuntimeException.getInstance("账号错误,请联系客服");
  938. }
  939. }
  940. loginReq.setGoodsId(1l);
  941. loginReq.setAccountId(0l);
  942. } else {
  943. String password = loginReq.getPassword();
  944. Integer num = loginReq.getNum();
  945. if (ObjectUtil.hasEmpty(password, num)) {
  946. throw BusinessRuntimeException.getInstance("参数错误");
  947. }
  948. Long goodsId = Optional.ofNullable(loginReq.getGoodsId()).orElse(1l);
  949. GroupsRelation relation = groupsRelationMapper.selectRelationByAccountAndNum(account, password, goodsId, num);
  950. if (relation == null) {
  951. log.info("外部入口使用账号account:{},pwd:{},座位num:{}同步验证链接失败...", account, password, num);
  952. if (StrUtil.equals(loginReq.getLang(), Constant.EN_US)) {
  953. throw BusinessRuntimeException.getInstance("This account is no longer valid");
  954. }
  955. throw BusinessRuntimeException.getInstance("该账号已失效");
  956. }
  957. loginReq.setUserId(relation.getUserId());
  958. loginReq.setRelationId(relation.getId());
  959. loginReq.setGoodsId(goodsId);
  960. }
  961. try {
  962. String aiVerifyCode = getAiVerifyCode(loginReq);
  963. if (StrUtil.isEmpty(aiVerifyCode)) {
  964. throw BusinessRuntimeException.getInstance("");
  965. }
  966. return aiVerifyCode;
  967. } catch (Exception e) {
  968. if (StrUtil.equals(loginReq.getLang(), Constant.EN_US)) {
  969. throw BusinessRuntimeException.getInstance("Please verify that the link was sent");
  970. }
  971. throw BusinessRuntimeException.getInstance("请校验链接是否已发送");
  972. }
  973. }
  974. @Override
  975. public String getNfHouseholdDevice(long userId, Long relationId) throws Exception {
  976. List<Long> relationUserIdList = userBindRelationService.getRelationUserIdList(userId, null);
  977. RenewalView renewalView = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder().field(RenewalView::getUserId, relationUserIdList).op(Operator.InList)
  978. .field(RenewalView::getRelationId, relationId).build());
  979. if (renewalView == null) {
  980. throw BusinessRuntimeException.getInstance("您的车票不存在...");
  981. }
  982. if (StrUtil.isEmpty(renewalView.getAccount())) {
  983. throw BusinessRuntimeException.getInstance("车票未配置账号");
  984. }
  985. String account = renewalView.getAccount();
  986. String password = renewalView.getPassword();
  987. if (StrUtil.hasEmpty(account, password)) {
  988. throw BusinessRuntimeException.getInstance("您的车票账号密码异常,请联系客服...");
  989. }
  990. String code = commonMailService.getTicketMailCode(renewalView.getGoodsId(), account, renewalView.getRelateEmail(), renewalView.getGptEmailPwd(), renewalView.getCreatedTime(), null, 1, true);
  991. return code;
  992. }
  993. @Override
  994. public String getVerifyCode(String account) throws Exception {
  995. Account at = accountMapper.selectOne(Wrappers.lambdaQuery(Account.class)
  996. .eq(Account::getAccount, account)
  997. .eq(Account::getGoodsId, 18));
  998. if (at == null) {
  999. return null;
  1000. }
  1001. String password = at.getPassword();
  1002. if (StrUtil.hasEmpty(account, password)) {
  1003. throw BusinessRuntimeException.getInstance("账号异常");
  1004. }
  1005. String code = commonMailService.getTicketMailCode(at.getGoodsId(), account, at.getRelateEmail(), at.getGptEmailPwd(), at.getCreatedTime(), null, null, false);
  1006. return code;
  1007. }
  1008. @Override
  1009. public void changeTicket(long userId, Long relationId) {
  1010. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  1011. RenewalView renewalView = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder()
  1012. .field(RenewalView::getUserId, userIdList).op(Operator.InList)
  1013. .field(RenewalView::getRelationId, relationId)
  1014. .field(RenewalView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
  1015. .build());
  1016. if (renewalView == null) {
  1017. throw BusinessRuntimeException.getInstance("车票不存在");
  1018. }
  1019. if (renewalView.getGoodsId() != 1) {
  1020. throw BusinessRuntimeException.getInstance("只针对奈飞车票");
  1021. }
  1022. AccountNetflixState accountNetflixState = accountNetflixStateMapper.selectOne(Wrappers.lambdaQuery(AccountNetflixState.class)
  1023. .eq(AccountNetflixState::getAccount, renewalView.getAccount())
  1024. .orderByDesc(AccountNetflixState::getId)
  1025. .last("limit 1"));
  1026. if (accountNetflixState == null || accountNetflixState.getState() != 3) {
  1027. throw BusinessRuntimeException.getInstance("只支持异常账号更换车票");
  1028. }
  1029. String key = RedisService.key.NETFLIX_CHANGE_TICKET_KEY.getName() + relationId;
  1030. if (!redisService.setNx(key, relationId, RedisService.key.NETFLIX_CHANGE_TICKET_KEY.getTimeout())) {
  1031. throw BusinessRuntimeException.getInstance("正在更换车票中..");
  1032. }
  1033. Long groupId = renewalView.getGroupId();
  1034. Long skuId = renewalView.getSkuId();
  1035. Date expiryTime = renewalView.getExpiryTime();
  1036. //随机选择同规格得车票
  1037. Long item_groupsId = groupsMapper.selectRandomGroupsTripsBySkuId(groupId, skuId, expiryTime);
  1038. GroupsRelation newRelation = null;
  1039. if (item_groupsId != null) {
  1040. //存在就随机分配一个,没有就不做处理
  1041. newRelation = changeTickerRelation.changeTicket(renewalView.getUserId(), relationId, item_groupsId, renewalView.getSkuId());
  1042. }
  1043. //若无移至空车队
  1044. if (newRelation == null) {
  1045. newRelation = changeTickerRelation.getEmptyGroupsRelation(renewalView.getUserId(), renewalView.getRelationId(), renewalView.getSkuId());
  1046. }
  1047. if (newRelation == null) {
  1048. throw BusinessRuntimeException.getInstance("更换异常,请重新提交");
  1049. }
  1050. NetflixUserAccountStatusRecord netflixUserAccountStatusRecord = netflixUserAccountStatusRecordMapper.selectOne(Wrappers.lambdaQuery(NetflixUserAccountStatusRecord.class)
  1051. .eq(NetflixUserAccountStatusRecord::getRelationId, relationId)
  1052. .last("limit 1"));
  1053. if (netflixUserAccountStatusRecord != null) {
  1054. netflixUserAccountStatusRecord.setDeleted(false);
  1055. netflixUserAccountStatusRecordMapper.updateById(netflixUserAccountStatusRecord);
  1056. }
  1057. redisService.del(key);
  1058. }
  1059. @Override
  1060. public AccountView getTempAccount(long userId, Long relationId) {
  1061. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  1062. RenewalView renewalView = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder()
  1063. .field(RenewalView::getRelationId, relationId)
  1064. .field(RenewalView::getUserId, userIdList).op(Operator.InList)
  1065. .field(RenewalView::getGroupsStatus, GroupsTrips.Status.waiting.name())
  1066. .field(RenewalView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
  1067. .build());
  1068. if (renewalView == null) {
  1069. throw BusinessRuntimeException.getInstance("车票不存在");
  1070. }
  1071. AccountView accountView = new AccountView();
  1072. //奈飞超过3帐车票不给看
  1073. Number number = beanSearcher.searchCount(RenewalView.class, MapUtils.builder()
  1074. .field(RenewalView::getUserId, userIdList).op(Operator.InList)
  1075. .field(RenewalView::getGoodsId, 1)
  1076. .field(RenewalView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
  1077. .build());
  1078. if (number.intValue() > 2) {
  1079. return accountView;
  1080. }
  1081. NetflixTempAccountUser netflixTempAccountUser = netflixTempAccountUserMapper.selectOne(Wrappers.lambdaQuery(NetflixTempAccountUser.class)
  1082. .eq(NetflixTempAccountUser::getRelationId, relationId)
  1083. .in(NetflixTempAccountUser::getUserId, userIdList)
  1084. .last("limit 1"));
  1085. Account account;
  1086. if (netflixTempAccountUser == null) {
  1087. account = accountMapper.selectNetflixTempAccount();
  1088. if (account != null) {
  1089. netflixTempAccountUser = new NetflixTempAccountUser();
  1090. netflixTempAccountUser.setAccountId(account.getId());
  1091. netflixTempAccountUser.setRelationId(relationId);
  1092. netflixTempAccountUser.setUserId(renewalView.getUserId());
  1093. netflixTempAccountUserMapper.insert(netflixTempAccountUser);
  1094. }
  1095. } else {
  1096. account = accountMapper.selectById(netflixTempAccountUser.getAccountId());
  1097. }
  1098. if (account != null) {
  1099. accountView.setAccount(account.getAccount());
  1100. accountView.setPassword(account.getPassword());
  1101. }
  1102. return accountView;
  1103. }
  1104. @Override
  1105. public void nfErrorAccountSubmit(long userId, Long relationId) {
  1106. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  1107. RenewalView renewalView = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder()
  1108. .field(RenewalView::getUserId, userIdList).op(Operator.InList)
  1109. .field(RenewalView::getRelationId, relationId)
  1110. .field(RenewalView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan).build());
  1111. if (renewalView == null) {
  1112. throw BusinessRuntimeException.getInstance("车票不存在");
  1113. }
  1114. if (renewalView.getGoodsId() != 1) {
  1115. throw BusinessRuntimeException.getInstance("只支持奈飞车票");
  1116. }
  1117. String account = renewalView.getAccount();
  1118. String password = renewalView.getPassword();
  1119. Integer selectCount = netflixUserAccountStatusRecordMapper.selectCount(Wrappers.lambdaQuery(NetflixUserAccountStatusRecord.class)
  1120. .eq(NetflixUserAccountStatusRecord::getRelationId, relationId)
  1121. .in(NetflixUserAccountStatusRecord::getUserId, userIdList)
  1122. .eq(NetflixUserAccountStatusRecord::getAccount, account)
  1123. .eq(NetflixUserAccountStatusRecord::getState, 0)
  1124. .eq(NetflixUserAccountStatusRecord::getDeleted, 1));
  1125. if (selectCount > 0) {
  1126. throw BusinessRuntimeException.getInstance("该账号正在检测中");
  1127. }
  1128. AccountNetflixState accountNetflixState = accountNetflixStateMapper.selectOne(Wrappers.lambdaQuery(AccountNetflixState.class)
  1129. .eq(AccountNetflixState::getAccount, account)
  1130. .last("limit 1"));
  1131. Integer state = 0;
  1132. if (accountNetflixState != null && accountNetflixState.getState() == 3) {
  1133. state = accountNetflixState.getState();
  1134. }
  1135. //非正常状态
  1136. if (state != 1) {
  1137. //20号之前得校验 直接失败
  1138. if (renewalView.getCreatedTime().compareTo(DateUtil.parse("2025-06-20")) < 0) {
  1139. state = 3;
  1140. }
  1141. //如果账号下架了 直接失败
  1142. if (StrUtil.equals(GroupsTrips.Status.down.name(), renewalView.getGroupsStatus()) && renewalView.getSkuNum() > 1) {
  1143. state = 3;
  1144. }
  1145. }
  1146. NetflixUserAccountStatusRecord netflixUserAccountStatusRecord = new NetflixUserAccountStatusRecord();
  1147. netflixUserAccountStatusRecord.setAccount(account);
  1148. netflixUserAccountStatusRecord.setRelationId(relationId);
  1149. netflixUserAccountStatusRecord.setPassword(password);
  1150. netflixUserAccountStatusRecord.setUserId(renewalView.getUserId());
  1151. netflixUserAccountStatusRecord.setState(state);
  1152. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  1153. .eq(OrderDon::getRelationId, renewalView.getRelationId())
  1154. .in(OrderDon::getUserId, userIdList)
  1155. .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
  1156. .orderByDesc(OrderDon::getId)
  1157. .last("limit 1"));
  1158. if (orderDon != null) {
  1159. netflixUserAccountStatusRecord.setOrderId(orderDon.getId());
  1160. }
  1161. netflixUserAccountStatusRecordMapper.insert(netflixUserAccountStatusRecord);
  1162. }
  1163. @Override
  1164. public NetflixErrorAccountStatus getNetflixErrorAccountStatus(long userId, Long relationId) {
  1165. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  1166. NetflixUserAccountStatusRecord netflixUseAccountStatusRecord = netflixUserAccountStatusRecordMapper.selectOne(Wrappers.lambdaQuery(NetflixUserAccountStatusRecord.class)
  1167. .eq(NetflixUserAccountStatusRecord::getRelationId, relationId)
  1168. .eq(NetflixUserAccountStatusRecord::getDeleted, 1)
  1169. .in(NetflixUserAccountStatusRecord::getUserId, userIdList)
  1170. .last("limit 1"));
  1171. NetflixErrorAccountStatus status = new NetflixErrorAccountStatus();
  1172. if (netflixUseAccountStatusRecord == null) {
  1173. //是否已经更换
  1174. NetflixUserAccountSubmitRecoverRecord replaceRecover = netflixUserAccountSubmitRecoverRecordMapper.selectOne(Wrappers.lambdaQuery(NetflixUserAccountSubmitRecoverRecord.class)
  1175. .eq(NetflixUserAccountSubmitRecoverRecord::getUserId, userIdList)
  1176. .eq(NetflixUserAccountSubmitRecoverRecord::getReplaceRelationId, relationId)
  1177. .last("limit 1"));
  1178. if (replaceRecover != null) {
  1179. status.setRecoverStatus(replaceRecover.getStatus());
  1180. }
  1181. return status;
  1182. }
  1183. Integer state = netflixUseAccountStatusRecord.getState();
  1184. //2是登录失败 3是被封号
  1185. // if (state == 3) {
  1186. // if (netflixUseAccountStatusRecord.getCreatedTime().compareTo(DateUtil.offsetMinute(DateTime.now(), -10)) > 0) {
  1187. // state = 0;
  1188. // }
  1189. // }
  1190. //登录失败不算异常
  1191. if (state == 2) {
  1192. state = 1;
  1193. }
  1194. //账号检测状态
  1195. status.setNfStatus(state);
  1196. NetflixUserAccountSubmitRecoverRecord recoverRecord = netflixUserAccountSubmitRecoverRecordMapper.selectOne(Wrappers.lambdaQuery(NetflixUserAccountSubmitRecoverRecord.class)
  1197. .in(NetflixUserAccountSubmitRecoverRecord::getUserId, userIdList)
  1198. .eq(NetflixUserAccountSubmitRecoverRecord::getRelationId, relationId)
  1199. .last("limit 1"));
  1200. if (recoverRecord != null) {
  1201. //申请恢复状态
  1202. status.setRecoverStatus(recoverRecord.getStatus());
  1203. }
  1204. return status;
  1205. }
  1206. @Override
  1207. public List<NetflixUserAccountStatusRecord> getUserNetflixSubmitRecord(long userId) {
  1208. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  1209. List<NetflixUserAccountStatusRecord> netflixUseAccountStatusRecords = netflixUserAccountStatusRecordMapper.selectList(Wrappers.lambdaQuery(NetflixUserAccountStatusRecord.class)
  1210. .in(NetflixUserAccountStatusRecord::getUserId, userIdList)
  1211. .eq(NetflixUserAccountStatusRecord::getDeleted, 1)
  1212. .select(NetflixUserAccountStatusRecord::getOrderId, NetflixUserAccountStatusRecord::getRelationId, NetflixUserAccountStatusRecord::getState, NetflixUserAccountStatusRecord::getCreatedTime));
  1213. return netflixUseAccountStatusRecords;
  1214. }
  1215. @Override
  1216. public Long getTrialTicketByUserIdAndGoodsId(Long userId, Long trialSkuId, Integer ticketType) {
  1217. Retryer<Long> build = RetryerBuilder.<Long>newBuilder()
  1218. .retryIfException()
  1219. //运行时异常重试
  1220. .retryIfRuntimeException()
  1221. //false重试
  1222. .retryIfResult(res -> res == null)
  1223. //1s 间隔
  1224. .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))
  1225. //停止策略 : 尝试请求3次
  1226. .withStopStrategy(StopStrategies.stopAfterAttempt(3)).build();
  1227. try {
  1228. return build.call(() -> {
  1229. //体验规格
  1230. GroupsTrips groups = groupsMapper.selectCollegeStudentUserGptTicket(trialSkuId);
  1231. GroupsRelation relation = null;
  1232. if (groups == null) {
  1233. groups = groupsMapper.selectOne(Wrappers.lambdaQuery(GroupsTrips.class)
  1234. .eq(GroupsTrips::getStatus, GroupsTrips.Status.waiting)
  1235. .eq(GroupsTrips::getSkuId, trialSkuId)
  1236. .last("limit 1"));
  1237. }
  1238. if (groups != null) {
  1239. relation = groupsRelationMapper.selectRandomOneRelationByGroupsId(groups.getId());
  1240. }
  1241. if (relation == null) {
  1242. //生成空车队
  1243. GoodsDonSku sku = goodsDonSkuMapper.selectById(trialSkuId);
  1244. relation = groupsFuncService.createNewGroupsTrips(sku);
  1245. }
  1246. DateTime startTime = DateTime.now();
  1247. DateTime expiryTime = DateUtil.offsetDay(startTime, 1);
  1248. int update = groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
  1249. .set(GroupsRelation::getUserId, userId)
  1250. .set(GroupsRelation::getStartTime, startTime)
  1251. .set(GroupsRelation::getExpiryTime, expiryTime)
  1252. .set(GroupsRelation::getStatus, GroupsRelation.Status.validity)
  1253. .set(GroupsRelation::getAqType, ticketType)
  1254. .eq(GroupsRelation::getId, relation.getId())
  1255. .eq(GroupsRelation::getUserId, 0));
  1256. if (update > 0) {
  1257. groupsMapper.decrAvailableNum(relation.getGroupsId());
  1258. return relation.getId();
  1259. }
  1260. log.error("发送用户:{} GPT镜像车票体验失败,进行重试", userId);
  1261. return null;
  1262. });
  1263. } catch (ExecutionException | RetryException e) {
  1264. log.error("发送用户GPT镜像体验失败:{}", StringUtil.getErrorText(e));
  1265. return null;
  1266. }
  1267. }
  1268. @Override
  1269. public Boolean getMirrorPopups(long userId) {
  1270. //是否弹窗 默认否
  1271. Boolean flag = false;
  1272. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  1273. Number number = beanSearcher.searchCount(RenewalView.class, MapUtils.builder()
  1274. .field(RenewalView::getUserId, userIdList).op(Operator.InList)
  1275. .field(RenewalView::getStatus, GroupsRelation.Status.validity.name())
  1276. .field(RenewalView::getIsMirror, true)
  1277. .build());
  1278. if (number.intValue() == 0) {
  1279. flag = true;
  1280. }
  1281. if (!flag) {
  1282. String thisWeekBegin = DateUtil.beginOfWeek(DateTime.now()).toDateStr();
  1283. Integer selectCount = mirrorTicketPopupsRecordMapper.selectCount(Wrappers.lambdaQuery(MirrorTicketPopupsRecord.class)
  1284. .in(MirrorTicketPopupsRecord::getUserId, userIdList)
  1285. .eq(MirrorTicketPopupsRecord::getThisWeek, thisWeekBegin));
  1286. if (selectCount == 0) {
  1287. MirrorTicketPopupsRecord mirrorTicketPopupsRecord = new MirrorTicketPopupsRecord();
  1288. mirrorTicketPopupsRecord.setThisWeek(thisWeekBegin);
  1289. mirrorTicketPopupsRecord.setUserId(userId);
  1290. mirrorTicketPopupsRecordMapper.insert(mirrorTicketPopupsRecord);
  1291. } else {
  1292. flag = true;
  1293. }
  1294. }
  1295. return flag;
  1296. }
  1297. @Override
  1298. public Long getTicket(Long userId, Long skuId, Boolean isVip) {
  1299. Retryer<Long> build = RetryerBuilder.<Long>newBuilder()
  1300. .retryIfException()
  1301. //运行时异常重试
  1302. .retryIfRuntimeException()
  1303. //false重试
  1304. .retryIfResult(res -> res == null)
  1305. //1s 间隔
  1306. .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))
  1307. //停止策略 : 尝试请求1次
  1308. .withStopStrategy(StopStrategies.stopAfterAttempt(1)).build();
  1309. try {
  1310. return build.call(() -> {
  1311. GroupsTrips groups = groupsMapper.selectCollegeStudentUserGptTicket(skuId);
  1312. GroupsRelation relation = null;
  1313. if (groups == null) {
  1314. groups = groupsMapper.selectOne(Wrappers.lambdaQuery(GroupsTrips.class)
  1315. .eq(GroupsTrips::getStatus, GroupsTrips.Status.waiting)
  1316. .eq(GroupsTrips::getSkuId, skuId)
  1317. .last("limit 1"));
  1318. }
  1319. if (groups != null) {
  1320. relation = groupsRelationMapper.selectRandomOneRelationByGroupsId(groups.getId());
  1321. }
  1322. GoodsDonSku sku = goodsDonSkuMapper.selectById(skuId);
  1323. //生成空车队
  1324. if (relation == null) {
  1325. relation = groupsFuncService.createNewGroupsTrips(sku);
  1326. }
  1327. DateTime startTime = DateTime.now();
  1328. DateTime expiryTime;
  1329. if (sku.getDays() != null) {
  1330. expiryTime = DateUtil.offsetDay(startTime, sku.getDays());
  1331. } else {
  1332. expiryTime = DateUtil.offsetMonth(startTime, sku.getMonths());
  1333. }
  1334. int update = groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
  1335. .set(GroupsRelation::getUserId, userId)
  1336. .set(GroupsRelation::getStartTime, startTime)
  1337. .set(GroupsRelation::getExpiryTime, expiryTime)
  1338. .set(GroupsRelation::getStatus, GroupsRelation.Status.validity)
  1339. .set(GroupsRelation::getIsVip, isVip)
  1340. .eq(GroupsRelation::getId, relation.getId())
  1341. .eq(GroupsRelation::getUserId, 0));
  1342. if (update > 0) {
  1343. groupsMapper.decrAvailableNum(relation.getGroupsId());
  1344. return relation.getId();
  1345. }
  1346. log.error("发送用户:{} GPT镜像车票失败,进行重试", userId);
  1347. return null;
  1348. });
  1349. } catch (ExecutionException | RetryException e) {
  1350. log.error("发送用户GPT镜像失败:{}", StringUtil.getErrorText(e));
  1351. return null;
  1352. }
  1353. }
  1354. @Override
  1355. public Boolean getVipMirrorTicketIfEmpty(long userId, Long skuId) {
  1356. Integer selectCount = vipGoodsSkuMapper.selectCount(Wrappers.lambdaQuery(VipGoodsSku.class)
  1357. .eq(VipGoodsSku::getSkuId, skuId));
  1358. if (selectCount == 0) {
  1359. return false;
  1360. }
  1361. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  1362. VipUser vipUser = vipUserMapper.selectOne(Wrappers.lambdaQuery(VipUser.class)
  1363. .in(VipUser::getUserId, userIdList)
  1364. .gt(VipUser::getExpiryTime, DateTime.now())
  1365. .last("limit 1"));
  1366. //不是会员 或已到期
  1367. if (vipUser == null) {
  1368. return false;
  1369. }
  1370. Number number = beanSearcher.searchCount(GroupsRelationView.class, MapUtils.builder()
  1371. .field(GroupsRelationView::getUserId, userIdList).op(Operator.InList)
  1372. .field(GroupsRelationView::getSkuId, skuId)
  1373. .field(GroupsRelationView::getIsVip, Boolean.TRUE)
  1374. .build());
  1375. if (number.intValue() == 0) {
  1376. Long relationId = getTicket(vipUser.getUserId(), skuId, Boolean.TRUE);
  1377. //同步时间
  1378. groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
  1379. .set(GroupsRelation::getExpiryTime, vipUser.getExpiryTime())
  1380. .eq(GroupsRelation::getUserId, vipUser.getUserId())
  1381. .eq(GroupsRelation::getId, relationId));
  1382. return true;
  1383. }
  1384. return false;
  1385. }
  1386. @Override
  1387. public NetflixRecoverResp netflixSubmitRecover(ErrorNetflixChangeReq req) {
  1388. Long relationId = req.getRelationId();
  1389. Long userId = req.getUserId();
  1390. //校验奈飞账号状态
  1391. GroupsRelationView netflixRelationView = checkNetflixErrorStatus(relationId, userId);
  1392. Date expiryTime = netflixRelationView.getExpiryTime();
  1393. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  1394. NetflixUserAccountSubmitRecoverRecord recoverRecord = netflixUserAccountSubmitRecoverRecordMapper.selectOne(Wrappers.lambdaQuery(NetflixUserAccountSubmitRecoverRecord.class)
  1395. .eq(NetflixUserAccountSubmitRecoverRecord::getRelationId, relationId)
  1396. .in(NetflixUserAccountSubmitRecoverRecord::getUserId, userIdList)
  1397. .eq(NetflixUserAccountSubmitRecoverRecord::getStatus, 0)
  1398. .last("limit 1"));
  1399. if (recoverRecord == null) {
  1400. recoverRecord = new NetflixUserAccountSubmitRecoverRecord();
  1401. recoverRecord.setRelationId(relationId);
  1402. recoverRecord.setUserId(netflixRelationView.getUserId());
  1403. recoverRecord.setSkuId(netflixRelationView.getSkuId());
  1404. recoverRecord.setAccount(netflixRelationView.getAccount());
  1405. Long betweenDay = DateUtil.betweenDay(DateTime.now(), expiryTime, false) + 1;
  1406. recoverRecord.setRemainDays(betweenDay);
  1407. recoverRecord.setAccount(netflixRelationView.getTripsAccount());
  1408. try {
  1409. netflixUserAccountSubmitRecoverRecordMapper.insert(recoverRecord);
  1410. } catch (DuplicateKeyException e) {
  1411. throw BusinessRuntimeException.getInstance("您已申请恢复");
  1412. }
  1413. }
  1414. //等待位数
  1415. Integer selectCount = netflixUserAccountSubmitRecoverRecordMapper.selectCount(Wrappers.lambdaQuery(NetflixUserAccountSubmitRecoverRecord.class)
  1416. .lt(NetflixUserAccountSubmitRecoverRecord::getCreatedTime, recoverRecord.getCreatedTime())
  1417. .eq(NetflixUserAccountSubmitRecoverRecord::getStatus, 0));
  1418. NetflixRecoverResp recoverResp = new NetflixRecoverResp();
  1419. recoverResp.setIndex(selectCount + 1);
  1420. recoverResp.setRemainDays(recoverRecord.getRemainDays());
  1421. return recoverResp;
  1422. }
  1423. @Override
  1424. @Transactional(rollbackFor = Throwable.class)
  1425. public void errorNetflixSubmitChangeOtherTicket(ErrorNetflixChangeReq req) throws Exception {
  1426. Long relationId = req.getRelationId();
  1427. Long userId = req.getUserId();
  1428. //校验奈飞账号状态
  1429. GroupsRelationView netflixRelationView = checkNetflixErrorStatus(relationId, userId);
  1430. Date expiryTime = netflixRelationView.getExpiryTime();
  1431. //小于15 不可替换其他车票
  1432. Long betweenDay = DateUtil.betweenDay(DateTime.now(), expiryTime, false) + 1;
  1433. if (betweenDay <= 15) {
  1434. throw BusinessRuntimeException.getInstance("您的车票有效期未超过15天");
  1435. }
  1436. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  1437. Integer count = netflixUserAccountSubmitRecoverRecordMapper.selectCount(Wrappers.lambdaQuery(NetflixUserAccountSubmitRecoverRecord.class)
  1438. .eq(NetflixUserAccountSubmitRecoverRecord::getRelationId, relationId)
  1439. .in(NetflixUserAccountSubmitRecoverRecord::getUserId, userIdList));
  1440. if (count > 0) {
  1441. throw BusinessRuntimeException.getInstance("您的车票申请恢复中...");
  1442. }
  1443. String key = RedisService.key.NETFLIX_CHANGE_TICKET_KEY.getName() + relationId;
  1444. if (!redisService.setNx(key, relationId, RedisService.key.NETFLIX_CHANGE_TICKET_KEY.getTimeout())) {
  1445. throw BusinessRuntimeException.getInstance("正在更换其他车票中..");
  1446. }
  1447. try {
  1448. Long skuId = req.getSkuId();
  1449. SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class)
  1450. .eq(SysConfig::getSysKey, Constant.ERROR_NETFLIX_CHANGE_OTHER_SKU_KEY)
  1451. .last("limit 1"));
  1452. List<ErrorNetflixChangeOtherSkuTicketDto> errorNetflixChangeOtherSkuTicketDtos = Jsons.parseList(sysConfig.getSysValue(), ErrorNetflixChangeOtherSkuTicketDto.class);
  1453. ErrorNetflixChangeOtherSkuTicketDto errorNetflixChangeOtherSkuTicketDto;
  1454. //自定义顺序 直接获取
  1455. if (betweenDay <= 60) {
  1456. //检验规格
  1457. errorNetflixChangeOtherSkuTicketDto = errorNetflixChangeOtherSkuTicketDtos.get(0);
  1458. } else if (betweenDay <= 120) {
  1459. errorNetflixChangeOtherSkuTicketDto = errorNetflixChangeOtherSkuTicketDtos.get(1);
  1460. } else {
  1461. errorNetflixChangeOtherSkuTicketDto = errorNetflixChangeOtherSkuTicketDtos.get(2);
  1462. }
  1463. List<Long> availableSkuIds = errorNetflixChangeOtherSkuTicketDto.getSkuIds();
  1464. if (!availableSkuIds.contains(skuId)) {
  1465. throw BusinessRuntimeException.getInstance("不支持更换该类型车票");
  1466. }
  1467. //替换车票
  1468. TicketChangeDto changeDto = getTicketAndGenerateOrder(userId, skuId, null, null);
  1469. if (changeDto == null) {
  1470. throw BusinessRuntimeException.getInstance("系统繁忙,请重试");
  1471. }
  1472. //替换记录
  1473. NetflixUserChangeOtherTicketRecord changeOtherTicketRecord = new NetflixUserChangeOtherTicketRecord();
  1474. changeOtherTicketRecord.setUserId(userId);
  1475. changeOtherTicketRecord.setRelationId(relationId);
  1476. changeOtherTicketRecord.setOtherRelationId(changeDto.getOtherRelationId());
  1477. changeOtherTicketRecord.setRelateOrderId(changeDto.getOtherOrderId());
  1478. //该车票订单id
  1479. changeOtherTicketRecord.setOrderId(netflixRelationView.getNfOriOrderId());
  1480. changeOtherTicketRecord.setStartTime(netflixRelationView.getStartTime());
  1481. changeOtherTicketRecord.setExpiryTime(expiryTime);
  1482. netflixUserChangeOtherTicketRecordMapper.insert(changeOtherTicketRecord);
  1483. if (netflixRelationView.getNfCheckId() != null) {
  1484. netflixUserAccountStatusRecordMapper.update(null, Wrappers.lambdaUpdate(NetflixUserAccountStatusRecord.class)
  1485. .set(NetflixUserAccountStatusRecord::getDeleted, false)
  1486. .eq(NetflixUserAccountStatusRecord::getId, netflixRelationView.getNfCheckId()));
  1487. }
  1488. Long ticketUserId = null;
  1489. //清除车票
  1490. GroupsRelation clearRelation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
  1491. .eq(GroupsRelation::getId, relationId)
  1492. .in(GroupsRelation::getUserId, userIdList)
  1493. .last("limit 1"));
  1494. if (clearRelation != null) {
  1495. ticketUserId = clearRelation.getUserId();
  1496. clearService.clearTicket(clearRelation, UserTicketClearedRecord.Source.ticket_replace);
  1497. }
  1498. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  1499. .eq(OrderDon::getRelationId, relationId)
  1500. .in(OrderDon::getUserId, userIdList)
  1501. .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
  1502. .orderByDesc(OrderDon::getId)
  1503. .last("limit 1"));
  1504. if (orderDon != null) {
  1505. if (ticketUserId == null) {
  1506. ticketUserId = orderDon.getUserId();
  1507. }
  1508. //替换sku
  1509. GoodsDonSku replaceSku = goodsDonSkuMapper.selectById(skuId);
  1510. GoodsDonSku oriTicketSku = goodsDonSkuMapper.selectById(orderDon.getSkuId());
  1511. //每天单价
  1512. BigDecimal dayPrice = oriTicketSku.getPrice().divide(BigDecimal.valueOf(oriTicketSku.getMonths() * 30), RoundingMode.DOWN);
  1513. //需要退款金额
  1514. BigDecimal refundMoney = dayPrice.multiply(BigDecimal.valueOf(betweenDay));
  1515. BigDecimal balance = Optional.ofNullable(orderDon.getBalance()).orElse(BigDecimal.ZERO);
  1516. BigDecimal finalOrderMoney = orderDon.getMoney().add(balance);
  1517. if (refundMoney.compareTo(finalOrderMoney) > 0) {
  1518. refundMoney = finalOrderMoney;
  1519. }
  1520. //车票价格高于替换的规格价格 退余额
  1521. if (refundMoney.compareTo(replaceSku.getPrice()) > 0) {
  1522. BigDecimal refundBalance = refundMoney.subtract(replaceSku.getPrice());
  1523. if (refundBalance.compareTo(BigDecimal.ZERO) > 0) {
  1524. userBenefitsService.addUserBalance(ticketUserId, refundBalance, UserBalanceSourceRecord.Source.TICKET_REPLACE, 0L, orderDon.getId(), UserBalanceSourceRecord.Source.TICKET_REPLACE.getDesc(), true);
  1525. }
  1526. }
  1527. }
  1528. } finally {
  1529. redisService.del(key);
  1530. }
  1531. }
  1532. @Override
  1533. public List<GoodsCategorySkuView> getNetflixReplaceOtherSkus(long userId, Long relationId) throws Exception {
  1534. //校验奈飞账号状态
  1535. GroupsRelationView netflixRelationView = checkNetflixErrorStatus(relationId, userId);
  1536. Date expiryTime = netflixRelationView.getExpiryTime();
  1537. //小于15 不可替换其他车票
  1538. Long betweenDay = DateUtil.betweenDay(DateTime.now(), expiryTime, false) + 1;
  1539. SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class)
  1540. .eq(SysConfig::getSysKey, Constant.ERROR_NETFLIX_CHANGE_OTHER_SKU_KEY)
  1541. .last("limit 1"));
  1542. List<ErrorNetflixChangeOtherSkuTicketDto> errorNetflixChangeOtherSkuTicketDtos = Jsons.parseList(sysConfig.getSysValue(), ErrorNetflixChangeOtherSkuTicketDto.class);
  1543. ErrorNetflixChangeOtherSkuTicketDto errorNetflixChangeOtherSkuTicketDto;
  1544. if (betweenDay <= 15) {
  1545. return null;
  1546. }
  1547. //自定义顺序 直接获取
  1548. if (betweenDay <= 60) {
  1549. //检验规格
  1550. errorNetflixChangeOtherSkuTicketDto = errorNetflixChangeOtherSkuTicketDtos.get(0);
  1551. } else if (betweenDay <= 120) {
  1552. errorNetflixChangeOtherSkuTicketDto = errorNetflixChangeOtherSkuTicketDtos.get(1);
  1553. } else {
  1554. errorNetflixChangeOtherSkuTicketDto = errorNetflixChangeOtherSkuTicketDtos.get(2);
  1555. }
  1556. List<GoodsCategorySkuView> goodsCategorySkuViews = beanSearcher.searchAll(GoodsCategorySkuView.class, MapUtils.builder()
  1557. .field(GoodsCategorySkuView::getSkuId, errorNetflixChangeOtherSkuTicketDto.getSkuIds()).op(Operator.InList)
  1558. .orderBy(GoodsCategorySkuView::getPrice).desc()
  1559. .build());
  1560. return goodsCategorySkuViews;
  1561. }
  1562. @Override
  1563. public NetflixRefundInfoResp refundNetflixInfo(Long userId, Long relationId) {
  1564. //校验奈飞账号状态
  1565. GroupsRelationView netflixRelationView = checkNetflixErrorStatus(relationId, userId);
  1566. Date expiryTime = netflixRelationView.getExpiryTime();
  1567. //小于15
  1568. Long betweenDay = DateUtil.betweenDay(DateTime.now(), expiryTime, false) + 1;
  1569. NetflixRefundInfoResp refundInfoResp = new NetflixRefundInfoResp();
  1570. refundInfoResp.setIsRefund(true);
  1571. if (betweenDay > 15) {
  1572. refundInfoResp.setIsRefund(false);
  1573. }
  1574. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  1575. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  1576. .eq(OrderDon::getRelationId, relationId)
  1577. .in(OrderDon::getUserId, userIdList)
  1578. .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
  1579. .orderByDesc(OrderDon::getId)
  1580. .last("limit 1"));
  1581. if (orderDon == null) {
  1582. refundInfoResp.setIsRefund(false);
  1583. }
  1584. Long skuId = netflixRelationView.getSkuId();
  1585. GoodsDonSku sku = goodsDonSkuMapper.selectById(skuId);
  1586. BigDecimal orderMoney = orderDon == null ? BigDecimal.ZERO : orderDon.getMoney();
  1587. if (orderMoney.compareTo(BigDecimal.ZERO) == 0) {
  1588. refundInfoResp.setIsRefund(false);
  1589. }
  1590. //每天单价
  1591. BigDecimal dayPrice = sku.getPrice().divide(BigDecimal.valueOf(sku.getMonths() * 30), RoundingMode.DOWN);
  1592. //需要退款金额
  1593. BigDecimal refundMoney = dayPrice.multiply(BigDecimal.valueOf(betweenDay));
  1594. refundInfoResp.setPrice(refundMoney);
  1595. BigDecimal balance = orderDon == null ? BigDecimal.ZERO : Optional.ofNullable(orderDon.getBalance()).orElse(BigDecimal.ZERO);
  1596. //超过本身订单金额 不允许退款
  1597. if (refundMoney.compareTo(orderMoney) > 0) {
  1598. refundInfoResp.setIsRefund(false);
  1599. }
  1600. if (refundMoney.compareTo(orderMoney.add(balance)) > 0) {
  1601. refundInfoResp.setPrice(orderMoney.add(balance));
  1602. }
  1603. refundInfoResp.setRemainDays(betweenDay);
  1604. return refundInfoResp;
  1605. }
  1606. @Override
  1607. public Boolean refundNetflix(ErrorNetflixChangeReq req) throws Exception {
  1608. Long relationId = req.getRelationId();
  1609. Long userId = req.getUserId();
  1610. //校验奈飞账号状态
  1611. GroupsRelationView netflixRelationView = checkNetflixErrorStatus(relationId, userId);
  1612. Date expiryTime = netflixRelationView.getExpiryTime();
  1613. //小于15
  1614. Long betweenDay = DateUtil.betweenDay(DateTime.now(), expiryTime, false) + 1;
  1615. if (betweenDay > 15) {
  1616. throw BusinessRuntimeException.getInstance("车票有效期不超过15天可退款");
  1617. }
  1618. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  1619. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  1620. .eq(OrderDon::getRelationId, relationId)
  1621. .in(OrderDon::getUserId, userIdList)
  1622. .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
  1623. .orderByDesc(OrderDon::getId)
  1624. .last("limit 1"));
  1625. if (orderDon == null) {
  1626. throw BusinessRuntimeException.getInstance("对应车票订单不存在,请联系客服");
  1627. }
  1628. Long skuId = netflixRelationView.getSkuId();
  1629. GoodsDonSku sku = goodsDonSkuMapper.selectById(skuId);
  1630. BigDecimal orderMoney = orderDon.getMoney();
  1631. if (orderMoney.compareTo(BigDecimal.ZERO) == 0) {
  1632. throw BusinessRuntimeException.getInstance("不符合退款条件");
  1633. }
  1634. //每天单价
  1635. BigDecimal dayPrice = sku.getPrice().divide(BigDecimal.valueOf(sku.getMonths() * 30), RoundingMode.DOWN);
  1636. //需要退款金额
  1637. BigDecimal refundMoney = dayPrice.multiply(BigDecimal.valueOf(betweenDay));
  1638. if (refundMoney.compareTo(orderMoney) > 0) {
  1639. throw BusinessRuntimeException.getInstance("不符合退款条件");
  1640. }
  1641. //调用第三方接口退款
  1642. OrderRefundReq refundReq = new OrderRefundReq();
  1643. refundReq.setRefundMoney(refundMoney);
  1644. refundReq.setRefundType("money");
  1645. String outNo = orderRefundService.centerRefund(refundReq, orderDon);
  1646. orderDon.setStatus(OrderDon.Status.refund);
  1647. orderDon.setRefundMoney(refundMoney);
  1648. GoodsDon goodsDon = goodsDonMapper.selectById(orderDon.getGoodsId());
  1649. orderDonMapper.updateById(orderDon);
  1650. orderRefundService.refundRecord(orderDon, refundMoney, "系统", goodsDon, sku, outNo, orderDon.getType().name(), null);
  1651. //清除车票
  1652. GroupsRelation relation = groupsRelationMapper.selectById(relationId);
  1653. clearService.clearTicket(relation, UserTicketClearedRecord.Source.refund);
  1654. if (netflixRelationView.getNfCheckId() != null) {
  1655. netflixUserAccountStatusRecordMapper.update(null, Wrappers.lambdaUpdate(NetflixUserAccountStatusRecord.class)
  1656. .set(NetflixUserAccountStatusRecord::getDeleted, false)
  1657. .eq(NetflixUserAccountStatusRecord::getId, netflixRelationView.getNfCheckId()));
  1658. }
  1659. return true;
  1660. }
  1661. private DistributePosterGoodsDetailView setGoodsPoster(RenewalView ticket, User user, List<Long> userIds) {
  1662. DistributePosterGoodsDetailView distributePosterGoodsDetailView = beanSearcher.searchFirst(DistributePosterGoodsDetailView.class, MapUtils.flatBuilder(request.getParameterMap())
  1663. .field(DistributePosterGoodsFrontView::getGoodsId, ticket.getGoodsId()).build());
  1664. if (distributePosterGoodsDetailView == null) {
  1665. return null;
  1666. }
  1667. distributePosterGoodsDetailView.setNickname(user.getNickname());
  1668. UserDistribute userDistribute = userDistributeMapper.selectOne(Wrappers.lambdaQuery(UserDistribute.class)
  1669. .eq(UserDistribute::getUserId, user.getId())
  1670. .eq(UserDistribute::getDeleted, 1)
  1671. .last("limit 1"));
  1672. if (userDistribute != null) {
  1673. UserPlatDistributeRate userPlatDistributeRate = userPlatDistributeRateMapper.selectOne(Wrappers.lambdaQuery(UserPlatDistributeRate.class)
  1674. .eq(UserPlatDistributeRate::getDistributeId, userDistribute.getId())
  1675. .eq(UserPlatDistributeRate::getGoodsId, ticket.getGoodsId())
  1676. .last("limit 1"));
  1677. if (userPlatDistributeRate != null) {
  1678. distributePosterGoodsDetailView.setRate(userPlatDistributeRate.getRate());
  1679. }
  1680. }
  1681. GoodsDon goodsDon = goodsDonMapper.selectById(ticket.getGoodsId());
  1682. if (goodsDon.getIsSkuDistribute()) {
  1683. if (userDistribute != null) {
  1684. UserPlatSkuDistributeRate userPlatSkuDistributeRate = userPlatSkuDistributeRateMapper.selectOne(Wrappers.lambdaQuery(UserPlatSkuDistributeRate.class)
  1685. .eq(UserPlatSkuDistributeRate::getDistributeId, userDistribute.getId())
  1686. .eq(UserPlatSkuDistributeRate::getSkuId, ticket.getSkuId())
  1687. .last("limit 1"));
  1688. if (userPlatSkuDistributeRate != null) {
  1689. distributePosterGoodsDetailView.setRate(userPlatSkuDistributeRate.getRate());
  1690. }
  1691. } else {
  1692. DistributeGeneralSkuRate distributeGeneralSkuRate = distributeGeneralSkuRateMapper.selectOne(Wrappers.lambdaQuery(DistributeGeneralSkuRate.class)
  1693. .eq(DistributeGeneralSkuRate::getSkuId, ticket.getSkuId())
  1694. .last("limit 1"));
  1695. if (distributeGeneralSkuRate != null) {
  1696. distributePosterGoodsDetailView.setRate(distributeGeneralSkuRate.getRate());
  1697. }
  1698. }
  1699. }
  1700. //用户推广海报记录id
  1701. Optional.ofNullable(userDistributePosterRecordMapper.selectOne(Wrappers.lambdaQuery(UserDistributePosterRecord.class)
  1702. .eq(UserDistributePosterRecord::getPosterId, distributePosterGoodsDetailView.getId())
  1703. .in(UserDistributePosterRecord::getUserId, userIds)
  1704. .last("limit 1"))).ifPresent(re -> distributePosterGoodsDetailView.setRid(re.getId()));
  1705. return distributePosterGoodsDetailView;
  1706. }
  1707. private void setRenewDiscount(RenewalView ticket) {
  1708. //镜像或者独立规格
  1709. if (ticket.getIsMirror() || (ticket.getSkuNum() != null && ticket.getSkuNum() == 1)) {
  1710. RenewUpgradePackage renewUpgradePackage = renewUpgradePackageMapper.selectOne(Wrappers.lambdaQuery(RenewUpgradePackage.class)
  1711. .eq(RenewUpgradePackage::getGoodsId, ticket.getGoodsId())
  1712. .eq(RenewUpgradePackage::getStatus, 1)
  1713. .eq(RenewUpgradePackage::getDeleted, 1)
  1714. .eq(RenewUpgradePackage::getSkuId, ticket.getSkuId())
  1715. .orderByAsc(RenewUpgradePackage::getDiscount)
  1716. .last("limit 1")
  1717. .select(RenewUpgradePackage::getId, RenewUpgradePackage::getDiscount));
  1718. if (renewUpgradePackage != null) {
  1719. ticket.setDiscount(renewUpgradePackage.getDiscount());
  1720. }
  1721. }
  1722. }
  1723. private void setMirrorTicketInfo(RenewalView ticket) {
  1724. //设置MJ镜像信息
  1725. setMjMirrorInfo(ticket);
  1726. //设置GPT镜像信息
  1727. setGptMirrorInfo(ticket);
  1728. //clause pro
  1729. setClaudeProInfo(ticket);
  1730. //luma
  1731. setLumaInfo(ticket);
  1732. }
  1733. private void setMjMirrorInfo(RenewalView ticket) {
  1734. if (ticket.getGoodsId() == 26 && ticket.getIsMirror()) {
  1735. List<UpgradePackage> upgradePackages = upgradePackageMapper.selectList(Wrappers.lambdaQuery(UpgradePackage.class).apply("JSON_CONTAINS(sku_ids,'\"" + ticket.getSkuId() + "\"')").eq(UpgradePackage::getStatus, Boolean.TRUE).eq(UpgradePackage::getDeleted, Boolean.TRUE));
  1736. if (CollectionUtil.isNotEmpty(upgradePackages)) {
  1737. upgradePackages.stream().map(e -> e.getPrice().divide(BigDecimal.valueOf(e.getNum()), 2, RoundingMode.HALF_UP)).min(BigDecimal::compareTo).ifPresent(ticket::setMjPackagePrice);
  1738. }
  1739. try {
  1740. MidjourneyUserView search = beanSearcher.searchFirst(MidjourneyUserView.class, MapUtils.flatBuilder(request.getParameterMap())
  1741. .field(MidjourneyUserView::getUserId, ticket.getUserId()).field(MidjourneyUserView::getRelationId, ticket.getRelationId())
  1742. .onlySelect(MidjourneyUserView::getMjUserRelaxNum, MidjourneyUserView::getMjUserFastNum,MidjourneyUserView::getMjUserUpgradeNum, MidjourneyUserView::getIsCar)
  1743. .build());
  1744. if (search == null) {
  1745. MidjourneyUser midjourneyUser = midjourneyAccountService.getMidjourneyUserToken(ticket.getUserId(), ticket.getRelationId());
  1746. ticket.setMjUserFastNum(midjourneyUser.getMjFastNum());
  1747. ticket.setMjUserUpgradeNum(midjourneyUser.getMjUpgradeNum());
  1748. ticket.setMjUserRelaxNum(midjourneyUser.getMjRelaxNum());
  1749. ticket.setIsCar(false);
  1750. } else {
  1751. ticket.setIsCar(search.getIsCar());
  1752. ticket.setMjUserFastNum(search.getMjUserFastNum());
  1753. ticket.setMjUserUpgradeNum(search.getMjUserUpgradeNum());
  1754. ticket.setMjUserRelaxNum(search.getMjUserRelaxNum());
  1755. }
  1756. midjourneyAccountService.updateUserExpiryTime(ticket.getRelationId(), ticket.getExpiryTime());
  1757. } catch (Exception e) {
  1758. }
  1759. }
  1760. }
  1761. public void setGptMirrorInfo(RenewalView ticket) {
  1762. if (ticket.getGoodsId() == 18 && ticket.getIsCar() && ticket.getExpiryTime() != null) {
  1763. try {
  1764. //gpt用户镜像
  1765. ChatGptUserView userInfo = chatGptAccountService.getUserInfo(ticket.getUserId(), ticket.getRelationId());
  1766. ticket.setGptUserLimitNum(userInfo.getLimitNum());
  1767. ticket.setGptUserLimitTime(userInfo.getLimitTime());
  1768. ticket.setUserDsLimitNum(userInfo.getDsLimitNum());
  1769. ticket.setUserDsLimitTime(userInfo.getDsLimitTime());
  1770. ticket.setUserOtherLimitNum(userInfo.getOtherLimitNum());
  1771. ticket.setUserOtherLimitTime(userInfo.getOtherLimitTime());
  1772. ticket.setUse(userInfo.getUse());
  1773. ticket.setModel(userInfo.getModel());
  1774. Integer count = upgradePackageMapper.selectCount(Wrappers.lambdaQuery(UpgradePackage.class).apply("JSON_CONTAINS(sku_ids,'\"" + ticket.getSkuId() + "\"')")
  1775. .gt(UpgradePackage::getNum, userInfo.getLimitNum())
  1776. .eq(UpgradePackage::getStatus, Boolean.TRUE)
  1777. .eq(UpgradePackage::getGptType, 0)
  1778. .eq(UpgradePackage::getDeleted, Boolean.TRUE));
  1779. ticket.setUpgrade(count > 0 ? true : false);
  1780. ticket.setPackageId(userInfo.getPackageId());
  1781. if (userInfo.getPackageId() != 0) {
  1782. UpgradePackage upgradePackage = upgradePackageMapper.selectById(userInfo.getPackageId());
  1783. ticket.setPackagePrice(upgradePackage.getPrice());
  1784. Long upgradeOrderDonSkuId = upgradePackage.getUpgradeSkuId();
  1785. GoodsDonSkuRenewView renewSku = beanSearcher.searchFirst(GoodsDonSkuRenewView.class, MapUtils.builder().field(GoodsDonSkuRenewView::getSkuId, upgradeOrderDonSkuId).build());
  1786. renewSku.setPackageId(userInfo.getPackageId());
  1787. ticket.setRenewSku(renewSku);
  1788. }
  1789. ticket.setIsRenewPackage(userInfo.getIsRenewPackage());
  1790. Long renewSkuId = userInfo.getRenewSkuId();
  1791. if (renewSkuId != null) {
  1792. GoodsDonSkuRenewView upgradeSkuRenewView = ticket.getRenewSku();
  1793. GoodsDonSkuRenewView renewSku = beanSearcher.searchFirst(GoodsDonSkuRenewView.class, MapUtils.builder().field(GoodsDonSkuRenewView::getSkuId, renewSkuId).build());
  1794. //续费升级是更高级规格
  1795. if (upgradeSkuRenewView == null || (renewSku.getGptUserLimitNum() >= upgradeSkuRenewView.getGptUserLimitNum() && renewSku.getMonths() >= userInfo.getMonths())) {
  1796. Optional.ofNullable(orderDonRenewRecordMapper.selectOne(Wrappers.lambdaQuery(OrderDonRenewRecord.class)
  1797. .eq(OrderDonRenewRecord::getOrderId, userInfo.getRenewOrderId())
  1798. .last("limit 1"))).ifPresent(e -> renewSku.setOriginalOrderId(e.getOrderId()));
  1799. ticket.setRenewSku(renewSku);
  1800. }
  1801. }
  1802. } catch (Exception e) {
  1803. }
  1804. }
  1805. }
  1806. public void setClaudeProInfo(RenewalView ticket) {
  1807. //clause pro
  1808. if (ticket.getGoodsId() == 75l && ticket.getExpiryTime() != null) {
  1809. try {
  1810. ClaudeUserView userInfo = claudeService.getUserInfo(ticket.getUserId(), ticket.getRelationId());
  1811. ticket.setGptUserLimitNum(userInfo.getLimitNum());
  1812. ticket.setGptUserLimitTime(userInfo.getLimitTime());
  1813. ticket.setUse(userInfo.getUse());
  1814. Long renewSkuId = userInfo.getRenewSkuId();
  1815. if (renewSkuId != null) {
  1816. GoodsDonSkuRenewView renewSku = beanSearcher.searchFirst(GoodsDonSkuRenewView.class, MapUtils.builder().field(GoodsDonSkuRenewView::getSkuId, renewSkuId).build());
  1817. ticket.setRenewSku(renewSku);
  1818. }
  1819. } catch (Exception e) {
  1820. }
  1821. }
  1822. }
  1823. public void setLumaInfo(RenewalView ticket) {
  1824. if (ticket.getGoodsId() == Constant.LUMA_GOODS_ID && ticket.getExpiryTime() != null) {
  1825. try {
  1826. List<UpgradePackage> upgradePackages = upgradePackageMapper.selectList(Wrappers.lambdaQuery(UpgradePackage.class).apply("JSON_CONTAINS(sku_ids,'\"" + ticket.getSkuId() + "\"')").eq(UpgradePackage::getStatus, Boolean.TRUE).eq(UpgradePackage::getDeleted, Boolean.TRUE));
  1827. if (CollectionUtil.isNotEmpty(upgradePackages)){
  1828. upgradePackages.stream().map(e -> e.getPrice().divide(BigDecimal.valueOf(e.getNum()), 2, RoundingMode.HALF_UP)).min(BigDecimal::compareTo).ifPresent(ticket::setLumaPackagePrice);
  1829. }
  1830. LumaUser lumaUser = lumaService.getLumaUser(ticket.getUserId(), ticket.getRelationId());
  1831. ticket.setLumaUserNum(lumaUser.getNum());
  1832. } catch (Exception e) {
  1833. }
  1834. }
  1835. }
  1836. /**
  1837. * 获取gpt镜像天卡活动参与平台ids
  1838. * @return
  1839. */
  1840. private List<Long> getGptMirrorGiveawayBuyGids() {
  1841. GptMirrorDayCardGiveawayActivity gptMirrorDayCardGiveawayActivity = beanSearcher.searchFirst(GptMirrorDayCardGiveawayActivity.class, MapUtils.builder().build());
  1842. if (gptMirrorDayCardGiveawayActivity != null) {
  1843. String buyGoodsIds = gptMirrorDayCardGiveawayActivity.getBuyGoodsIds();
  1844. try {
  1845. return Jsons.parseList(buyGoodsIds, Long.class);
  1846. } catch (Exception e) {
  1847. }
  1848. }
  1849. return null;
  1850. }
  1851. /**
  1852. * 标记是否可抵扣会员车票
  1853. */
  1854. private void markVipDeductTicket(List<Long> userIds, RenewalView ticket, List<Long> vipSkuIds, Boolean isVip) {
  1855. if (CollUtil.isNotEmpty(vipSkuIds) && !isVip) {
  1856. Long skuId = ticket.getSkuId();
  1857. if (vipSkuIds.contains(skuId)) {
  1858. //是否时长最长
  1859. Number number = beanSearcher.searchCount(RenewalView.class, MapUtils.builder()
  1860. .field(RenewalView::getRelationId, ticket.getRelationId()).op(Operator.NotEqual)
  1861. .field(RenewalView::getSkuId, skuId)
  1862. .field(RenewalView::getUserId, userIds).op(Operator.InList)
  1863. .field(RenewalView::getExpiryTime, ticket.getExpiryTime()).op(Operator.GreaterThan)
  1864. .build());
  1865. if (number.intValue() == 0) {
  1866. ticket.setIsVipDeduct(true);
  1867. }
  1868. }
  1869. }
  1870. }
  1871. /**
  1872. * 检验奈飞账号状态
  1873. */
  1874. public GroupsRelationView checkNetflixErrorStatus(Long relationId, Long userId) {
  1875. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  1876. NetflixUserAccountStatusRecord netflixUseAccountStatusRecord = netflixUserAccountStatusRecordMapper.selectOne(Wrappers.lambdaQuery(NetflixUserAccountStatusRecord.class)
  1877. .eq(NetflixUserAccountStatusRecord::getRelationId, relationId)
  1878. .eq(NetflixUserAccountStatusRecord::getDeleted, 1)
  1879. .in(NetflixUserAccountStatusRecord::getUserId, userIdList)
  1880. .last("limit 1"));
  1881. if (netflixUseAccountStatusRecord == null) {
  1882. throw BusinessRuntimeException.getInstance("请先提交账号检测");
  1883. }
  1884. Integer state = netflixUseAccountStatusRecord.getState();
  1885. if (state == 0) {
  1886. throw BusinessRuntimeException.getInstance("您的账号检测中");
  1887. }
  1888. //2是登录失败 3是被封号
  1889. if (state != 3) {
  1890. throw BusinessRuntimeException.getInstance("您的账号未封禁");
  1891. }
  1892. GroupsRelationView netflixRelationView = beanSearcher.searchFirst(GroupsRelationView.class, MapUtils.builder()
  1893. .field(GroupsRelationView::getId, relationId)
  1894. .field(GroupsRelationView::getUserId, userIdList).op(Operator.InList)
  1895. .field(GroupsRelationView::getGoodsId, Constant.NETFLIX_GID)
  1896. .field(GroupsRelationView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
  1897. .build());
  1898. if (netflixRelationView == null) {
  1899. throw BusinessRuntimeException.getInstance("奈飞车票已过期");
  1900. }
  1901. netflixRelationView.setNfCheckId(netflixUseAccountStatusRecord.getId());
  1902. netflixRelationView.setNfOriOrderId(netflixUseAccountStatusRecord.getOrderId());
  1903. return netflixRelationView;
  1904. }
  1905. /**
  1906. * 获取车票 并生成订单
  1907. */
  1908. public TicketChangeDto getTicketAndGenerateOrder(Long userId, Long skuId, Date startTime, Date expiryTime) {
  1909. Retryer<TicketChangeDto> build = RetryerBuilder.<TicketChangeDto>newBuilder()
  1910. .retryIfException()
  1911. //运行时异常重试
  1912. .retryIfRuntimeException()
  1913. //false重试
  1914. .retryIfResult(res -> res == null)
  1915. //1s 间隔
  1916. .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))
  1917. //停止策略 : 尝试请求1次
  1918. .withStopStrategy(StopStrategies.stopAfterAttempt(1)).build();
  1919. if (startTime == null) {
  1920. startTime = DateTime.now();
  1921. }
  1922. GoodsDonSku sku = goodsDonSkuMapper.selectById(skuId);
  1923. if (expiryTime == null) {
  1924. if (sku.getDays() != null) {
  1925. expiryTime = DateUtil.offsetDay(startTime, sku.getDays());
  1926. } else {
  1927. expiryTime = DateUtil.offsetMonth(startTime, sku.getMonths());
  1928. }
  1929. }
  1930. Date finalStartTime = startTime;
  1931. Date finalExpiryTime = expiryTime;
  1932. try {
  1933. return build.call(() -> {
  1934. GroupsTrips groups = groupsMapper.selectCollegeStudentUserGptTicket(skuId);
  1935. if (groups == null) {
  1936. groups = groupsMapper.selectOne(Wrappers.lambdaQuery(GroupsTrips.class)
  1937. .eq(GroupsTrips::getStatus, GroupsTrips.Status.waiting)
  1938. .eq(GroupsTrips::getSkuId, skuId)
  1939. .last("limit 1"));
  1940. }
  1941. GroupsRelation relation = null;
  1942. if (groups != null) {
  1943. relation = groupsRelationMapper.selectRandomOneRelationByGroupsId(groups.getId());
  1944. }
  1945. //生成空车队
  1946. if (relation == null) {
  1947. relation = groupsFuncService.createNewGroupsTrips(sku);
  1948. }
  1949. int update = groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
  1950. .set(GroupsRelation::getUserId, userId)
  1951. .set(GroupsRelation::getStartTime, finalStartTime)
  1952. .set(GroupsRelation::getExpiryTime, finalExpiryTime)
  1953. .set(GroupsRelation::getStatus, GroupsRelation.Status.validity)
  1954. .eq(GroupsRelation::getId, relation.getId())
  1955. .eq(GroupsRelation::getUserId, 0));
  1956. if (update > 0) {
  1957. groupsMapper.decrAvailableNum(relation.getGroupsId());
  1958. Long relationId = relation.getId();
  1959. //生成新的订单
  1960. OrderDon otherOrderDon = generateNewTicketOrder(relationId, userId, sku);
  1961. TicketChangeDto ticketChangeDto = new TicketChangeDto();
  1962. ticketChangeDto.setOtherRelationId(relationId);
  1963. ticketChangeDto.setOtherOrderId(otherOrderDon.getId());
  1964. return ticketChangeDto;
  1965. }
  1966. log.error("发送用户:{} GPT镜像车票失败,进行重试", userId);
  1967. return null;
  1968. });
  1969. } catch (ExecutionException | RetryException e) {
  1970. log.error("发送用户GPT镜像失败:{}", StringUtil.getErrorText(e));
  1971. return null;
  1972. }
  1973. }
  1974. private OrderDon generateNewTicketOrder(Long relationId, Long userId, GoodsDonSku sku) {
  1975. OrderDon orderDon = new OrderDon();
  1976. orderDon.setUserId(userId);
  1977. orderDon.setOrderNo(SEQUENCE.nextId().toString());
  1978. orderDon.setMoney(BigDecimal.ZERO);
  1979. orderDon.setRelationId(relationId);
  1980. orderDon.setGoodsId(sku.getGoodsId());
  1981. orderDon.setSkuId(sku.getId());
  1982. orderDon.setType(OrderDon.Type.TICKET_REPLACE);
  1983. orderDon.setStatus(OrderDon.Status.complete);
  1984. orderDonMapper.insert(orderDon);
  1985. return orderDon;
  1986. }
  1987. }