GroupRelationFrontServiceImpl.java 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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.task.GlobalThreadPoolTaskExecutor;
  16. import com.cyksj.common.util.Jsons;
  17. import com.cyksj.common.util.StringUtil;
  18. import com.cyksj.enums.GatewayApiCode;
  19. import com.cyksj.mapper.*;
  20. import com.cyksj.mapper.manage.distribute.DistributeGeneralSkuRateMapper;
  21. import com.cyksj.mapper.manage.distribute.UserDistributeMapper;
  22. import com.cyksj.mapper.manage.distribute.UserPlatDistributeRateMapper;
  23. import com.cyksj.mapper.manage.distribute.UserPlatSkuDistributeRateMapper;
  24. import com.cyksj.mapper.renew.RenewUpgradePackageMapper;
  25. import com.cyksj.mapper.sys.SysEmailMapper;
  26. import com.cyksj.model.entity.*;
  27. import com.cyksj.model.request.SpecialGoodsTicketReq;
  28. import com.cyksj.model.request.TicketLoginReq;
  29. import com.cyksj.model.views.*;
  30. import com.cyksj.redis.RedisService;
  31. import com.cyksj.service.chatgpt.ChatGptAccountService;
  32. import com.cyksj.service.chatgpt.ChatGptAuthService;
  33. import com.cyksj.service.claude.ClaudeService;
  34. import com.cyksj.service.groups.GroupsFuncService;
  35. import com.cyksj.service.luma.LumaService;
  36. import com.cyksj.service.mail.CuiQiuMailService;
  37. import com.cyksj.service.mail.MailService;
  38. import com.cyksj.service.mail.YhMailService;
  39. import com.cyksj.service.mange.coupon.CouponCommonService;
  40. import com.cyksj.service.midjourney.MidjourneyAccountService;
  41. import com.cyksj.service.relation.GroupRelationFrontService;
  42. import com.cyksj.service.relation.GroupsRelationNetflixService;
  43. import com.cyksj.service.user.UserBindRelationService;
  44. import com.cyksj.service.user.UserLoginRecordService;
  45. import com.ejlchina.searcher.BeanSearcher;
  46. import com.ejlchina.searcher.SearchResult;
  47. import com.ejlchina.searcher.param.Operator;
  48. import com.ejlchina.searcher.util.MapBuilder;
  49. import com.ejlchina.searcher.util.MapUtils;
  50. import com.github.rholder.retry.*;
  51. import lombok.RequiredArgsConstructor;
  52. import lombok.extern.slf4j.Slf4j;
  53. import org.springframework.stereotype.Service;
  54. import org.springframework.transaction.annotation.Transactional;
  55. import javax.servlet.http.HttpServletRequest;
  56. import java.math.BigDecimal;
  57. import java.math.RoundingMode;
  58. import java.util.Date;
  59. import java.util.List;
  60. import java.util.Objects;
  61. import java.util.Optional;
  62. import java.util.concurrent.ExecutionException;
  63. import java.util.concurrent.TimeUnit;
  64. import java.util.concurrent.atomic.AtomicInteger;
  65. import java.util.stream.Collectors;
  66. import static com.cyksj.enums.GatewayApiCode.GROUP_INDEX_ALREADY_BE_USER;
  67. import static com.cyksj.enums.GatewayApiCode.GROUP_INDEX_NOT_FIND;
  68. /*
  69. *项目名: netflix
  70. *文件名: GroupRelationFrontServiceImpl
  71. *创建者: JavaZou
  72. *创建时间:2022/12/29 15:42
  73. */
  74. @Service
  75. @RequiredArgsConstructor
  76. @Slf4j
  77. public class GroupRelationFrontServiceImpl implements GroupRelationFrontService {
  78. private final UserBindRelationService userBindRelationService;
  79. private final UserMapper userMapper;
  80. private final BeanSearcher beanSearcher;
  81. private final CouponCommonService couponCommonService;
  82. private final GroupsRelationMapper groupsRelationMapper;
  83. private final GroupsMapper groupsMapper;
  84. private final ChatGptAuthService chatGptAuthService;
  85. private final RedisService redisService;
  86. private final MailService mailService;
  87. private final OrderDonMapper orderDonMapper;
  88. private final GroupsRelationExpiryNotifyRecordMapper groupsRelationExpiryNotifyRecordMapper;
  89. private final HttpServletRequest request;
  90. private final GroupsFuncService groupsFuncService;
  91. private final OrderDiscountPlusPurchaseSkuRelationMapper orderDiscountPlusPurchaseSkuRelationMapper;
  92. private final GroupsRelationLoginCodeRecordMapper groupsRelationLoginCodeRecordMapper;
  93. private final SysEmailMapper sysEmailMapper;
  94. private final GoodsDonSkuMapper goodsDonSkuMapper;
  95. private final UserFuncPropertyMapper userFuncPropertyMapper;
  96. private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
  97. private final UserLoginRecordService userLoginRecordService;
  98. private final CuiQiuMailService cuiQiuMailService;
  99. private final UserCertRecordMapper userCertRecordMapper;
  100. private final MidjourneyAccountService midjourneyAccountService;
  101. private final ChatGptAccountService chatGptAccountService;
  102. private final UpgradePackageMapper upgradePackageMapper;
  103. private final LumaService lumaService;
  104. private final GroupsRelationNetflixService groupsRelationNetflixService;
  105. private final UserDistributeMapper userDistributeMapper;
  106. private final UserPlatDistributeRateMapper userPlatDistributeRateMapper;
  107. private final GoodsDonMapper goodsDonMapper;
  108. private final UserPlatSkuDistributeRateMapper userPlatSkuDistributeRateMapper;
  109. private final DistributeGeneralSkuRateMapper distributeGeneralSkuRateMapper;
  110. private final RenewUpgradePackageMapper renewUpgradePackageMapper;
  111. private final ClaudeService claudeService;
  112. private final YhMailService yhMailService;
  113. private final UserDistributePosterRecordMapper userDistributePosterRecordMapper;
  114. private final OrderDonRenewRecordMapper orderDonRenewRecordMapper;
  115. private final AccountMapper accountMapper;
  116. @Override
  117. public SearchResult<RenewalView> getMyTicket(long userId, Boolean isLoginPopularize, String customId, String account) {
  118. User u = userMapper.selectById(userId);
  119. if (u == null) throw BusinessRuntimeException.getGatewayApiCode(GatewayApiCode.CMS_TOKEN_VALID);
  120. if (u.getIsBlack()) {
  121. return new SearchResult<>();
  122. }
  123. List<Long> userIds = userBindRelationService.getRelationUserIdList(userId, u);
  124. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  125. //Long yhsId = yhShopFrontService.getYhsIdByCustomId(customId);
  126. DateTime now = DateTime.now();
  127. if (StrUtil.isNotBlank(account)) {
  128. builder.field(RenewalView::getAccount, account).op(Operator.Contain);
  129. }
  130. SearchResult<RenewalView> list = beanSearcher.search(RenewalView.class, builder
  131. .field(RenewalView::getUserId, 0).op(Operator.GreaterThan)
  132. .field(RenewalView::getUserId, userIds).op(Operator.InList)
  133. //.field(RenewalView::getYhsId, yhsId)
  134. .put("condition", String.format("(gr.expiry_time is null or gr.expiry_time > '%s')", now))
  135. .field(RenewalView::getStatus, List.of("validity", "overdue", "outside")).op(Operator.InList)
  136. .orderBy(RenewalView::getExpiryTime).asc()
  137. .build());
  138. //过滤邀请制、已失效的车位
  139. Integer size = list.getTotalCount().intValue();
  140. list.getDataList().forEach(ticket -> {
  141. if (ticket.getSkuId() == null || ticket.getGoodsId() == null) {
  142. return;
  143. }
  144. if (ticket.getGoodsId() == 18 && ticket.getSkuNum() > 1) {
  145. ticket.setPassword(null);
  146. }
  147. ticket.setIsMulti(false);
  148. if (ticket.getSkuNum() > 1) {
  149. ticket.setIsMulti(true);
  150. }
  151. //chatGPT直连
  152. if (ticket.getGoodsId().equals(42l)) {
  153. if (StrUtil.isNotBlank(ticket.getAccount())) {
  154. String accessToken;
  155. try {
  156. accessToken = chatGptAuthService.getAccessToken(ticket.getAccount());
  157. ticket.setGptRefreshToken(accessToken);
  158. } catch (Exception e) {
  159. redisService.set(RedisService.key.CHAT_GPT_TOKEN_EXCEPTION_KEY.getName() + ticket.getAccount(), ticket.getAccount());
  160. }
  161. }
  162. ticket.setAccount(null);
  163. ticket.setPassword(null);
  164. } else {
  165. ticket.setGptRefreshToken(null);
  166. }
  167. if (ticket.getSecondType() == 1) {
  168. ticket.setAccount(null);
  169. ticket.setPassword(null);
  170. }
  171. if (ticket.getGoodsId() != 30l) {
  172. ticket.setGptEmailPwd(null);
  173. }
  174. if (isLoginPopularize != null && isLoginPopularize) {
  175. GoodsDonViews views = beanSearcher.searchFirst(GoodsDonViews.class, MapUtils.builder().field(GoodsDonViews::getId, ticket.getGoodsId()).build());
  176. views.setSpecs(beanSearcher.searchFirst(GoodsDonSpecFrontView.class, MapUtils.builder().field(GoodsDonSpecFrontView::getGoodsId, views.getId()).build()));
  177. views.setSkuList(beanSearcher.searchAll(GoodsDonSkuFrontView.class, MapUtils.builder().field(GoodsDonSkuFrontView::getGoodsId, views.getId()).field(GoodsDonSkuFrontView::getStatus, true).build()));
  178. ticket.setGoodsDonViews(views);
  179. }
  180. if (ticket.getMonths() != null && ticket.getMonths() >= 300) {
  181. ticket.setExpiryTime(null);
  182. }
  183. if (ticket.getExpiryTime() != null && ticket.getGoodsType() == 1 && ticket.getSecondType() == 2) {
  184. String remainDays = DateUtil.formatBetween(now, ticket.getExpiryTime(), BetweenFormater.Level.DAY);
  185. if ("0天".equals(remainDays)) {
  186. remainDays = "小于1天";
  187. } else {
  188. //当天也算
  189. int days = Integer.parseInt(remainDays.substring(0, remainDays.indexOf("天")));
  190. remainDays = days + 1 + "天";
  191. }
  192. ticket.setRemainDays(remainDays);
  193. }
  194. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  195. .eq(OrderDon::getRelationId, ticket.getRelationId())
  196. .eq(OrderDon::getUserId, ticket.getUserId())
  197. .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
  198. .orderByDesc(OrderDon::getId)
  199. .last("limit 1"));
  200. if (orderDon != null) {
  201. ticket.setOrderNo(orderDon.getOrderNo());
  202. ticket.setOrderId(orderDon.getId());
  203. ticket.setTransactionId(orderDon.getTransactionId());
  204. }
  205. if (size > 1 && ticket.getGoodsId() != 18) {
  206. Date payTime = null;
  207. if (orderDon != null) {
  208. payTime = Optional.ofNullable(orderDon.getPayTime()).orElse(orderDon.getCreatedTime());
  209. }
  210. //加购订单
  211. if (orderDon == null) {
  212. List<OrderDiscountPlusPurchaseSkuRelation> plusPurchaseSkuRelations = orderDiscountPlusPurchaseSkuRelationMapper.selectPlusRelationByIdAndUserId(ticket.getRelationId(), ticket.getUserId());
  213. for (OrderDiscountPlusPurchaseSkuRelation data : plusPurchaseSkuRelations) {
  214. String relationIds = data.getRelationIds();
  215. if (StrUtil.isEmpty(relationIds)) {
  216. return;
  217. }
  218. try {
  219. List<Long> relationList = Jsons.parseList(relationIds, Long.class);
  220. if (relationList.contains(ticket.getRelationId())) {
  221. OrderDon plusOrder = orderDonMapper.selectById(data.getOrderId());
  222. if (plusOrder != null) {
  223. payTime = Optional.ofNullable(plusOrder.getPayTime()).orElse(plusOrder.getCreatedTime());
  224. }
  225. break;
  226. }
  227. } catch (Exception e) {
  228. }
  229. }
  230. }
  231. ticket.setPayTime(payTime);
  232. }
  233. if (ticket.getSkuNum() != null && ticket.getSkuNum() == 1) {
  234. }
  235. //设置镜像车票信息
  236. setMirrorTicketInfo(ticket);
  237. List<GroupsRelationUserView> relationUserViews = groupsRelationMapper.getUserRelationUserViews(ticket.getGroupId(), ticket.getRelationId(), ticket.getUserId(), List.of(GroupsRelation.Status.validity, GroupsRelation.Status.outside));
  238. ticket.setRelationViews(relationUserViews);
  239. if (ticket.getExpiryTime() != null) {
  240. ticket.setExpiryTimeStr(DateUtil.format(ticket.getExpiryTime(), "yyyy-MM-dd"));
  241. }
  242. //查看双重验证码
  243. if (StrUtil.isNotEmpty(ticket.getApiSecret())) {
  244. Boolean isHasVerifyCode = StrUtil.isNotBlank(ticket.getVerifyCode()) ? true : false;
  245. Integer codeNum = getUserCodeNumBySkuId(ticket.getUserId(), ticket.getSkuId(), isHasVerifyCode);
  246. ticket.setCodeNum(codeNum);
  247. }
  248. Long qaId = groupsRelationMapper.selectGoodsDonQaByGoodsId(ticket.getGoodsId());
  249. ticket.setQa(qaId > 0 ? true : false);
  250. //是否存在海报
  251. ticket.setPosterInfo(setGoodsPoster(ticket, u, userIds));
  252. //续费升级折扣
  253. setRenewDiscount(ticket);
  254. });
  255. userLoginRecordService.saveUserLoginDayRecord(userId);
  256. return list;
  257. }
  258. @Override
  259. public Boolean isJoinCorpWx(Long userId, Boolean isJoin, Boolean noCoupon) {
  260. if (!isJoin) {
  261. User user = userMapper.selectById(userId);
  262. if (user == null || StrUtil.isEmpty(user.getUnionid())) {
  263. return true;
  264. }
  265. // CorpUser corpUser = corpUserMapper.selectOne(Wrappers.lambdaQuery(CorpUser.class)
  266. // .ne(CorpUser::getUnionid, "")
  267. // .eq(CorpUser::getUnionid, user.getUnionid())
  268. // .last("limit 1")
  269. // .select(CorpUser::getId)
  270. // );
  271. CorpUserFollowView corpUserFollowView = beanSearcher.searchFirst(CorpUserFollowView.class, MapUtils.builder()
  272. .field(CorpUserFollowView::getUnionId, user.getUnionid())
  273. .field(CorpUserFollowView::getStatus, 1).build());
  274. if (corpUserFollowView == null) {
  275. return false;
  276. }
  277. }
  278. //用户是否买了奈飞车票
  279. Number number = beanSearcher.searchCount(RenewalView.class, MapUtils.builder()
  280. .field(RenewalView::getUserId, userId)
  281. .field(RenewalView::getGoodsId, 1)
  282. .field(RenewalView::getStatus, List.of("validity", "overdue")).op(Operator.InList)
  283. .build());
  284. if (number.intValue() > 0 && !noCoupon) {
  285. couponCommonService.sendCouponToUser(userId, 34l, 0l, 0l, 0l, CouponUser.Channel.wxCorp);
  286. }
  287. return true;
  288. }
  289. @Override
  290. public GroupsRelation getSimilarExpiredGroupRelation(GoodsDonSku sku, List<Long> errorsRelationIds, String ip) {
  291. Long skuId = sku.getId();
  292. Integer days = sku.getDays();
  293. Integer months = sku.getMonths();
  294. Integer maxNum = sku.getNum();
  295. DateTime expiryTime = null;
  296. if (days != null && days > 0) {
  297. expiryTime = DateUtil.offsetDay(DateTime.now(), days);
  298. } else {
  299. expiryTime = DateUtil.offsetMonth(DateTime.now(), months);
  300. }
  301. Integer extraNum = 5;
  302. if (sku.getId() == 178l) {
  303. extraNum = 2;
  304. }
  305. maxNum += extraNum;
  306. DateTime minExpiryTime = DateUtil.offsetDay(expiryTime, -5);
  307. Long groupsId = groupsRelationMapper.selectSimilarExpiredRelation(skuId, minExpiryTime, maxNum, ip);
  308. if (groupsId == null) {
  309. log.info("暂无相似过期时间车队");
  310. return null;
  311. }
  312. GroupsRelation relation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
  313. .notIn(errorsRelationIds != null && errorsRelationIds.size() > 0, GroupsRelation::getId, errorsRelationIds)
  314. .eq(GroupsRelation::getGroupsId, groupsId)
  315. .eq(GroupsRelation::getStatus, GroupsRelation.Status.none)
  316. .eq(GroupsRelation::getUserId, 0)
  317. .orderByAsc(GroupsRelation::getNum)
  318. .last("limit 1"));
  319. if (relation != null) {
  320. log.info("用户userId:{},获取相似车队的车位relationId:{}", relation.getId());
  321. }
  322. return relation;
  323. }
  324. @Override
  325. public Page<UserRecommendGoodsFrontView> getRecommendGoodsViews(Boolean isPayNf, List<Long> filter_goods_id, Boolean filterCourseGoods, Integer start, Integer limit) {
  326. return groupsRelationMapper.getRecommendGoodsViews(new Page<>(start, limit), filter_goods_id, isPayNf, filterCourseGoods);
  327. }
  328. @Override
  329. @Transactional(rollbackFor = Throwable.class)
  330. public void sendCourseWare(SpecialGoodsTicketReq request) throws Exception {
  331. Long userId = request.getUserId();
  332. Long relationId = request.getRelationId();
  333. String email = request.getEmail();
  334. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  335. RenewalView relation = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder().field(RenewalView::getRelationId, relationId).build());
  336. Assert.notNull(relation, "您的车票不存在");
  337. Long relationUserId = relation.getUserId();
  338. if (!userIdList.contains(relationUserId)) {
  339. throw BusinessRuntimeException.getInstance("您的车票不存在");
  340. }
  341. if (relation.getSpecialType() != GoodsDon.SpecialType.courseware) {
  342. throw BusinessRuntimeException.getInstance("");
  343. }
  344. //是否已发送
  345. if (StrUtil.isNotBlank(relation.getGrAccount())) {
  346. throw BusinessRuntimeException.getInstance("已领取课件,请前往邮箱查看");
  347. }
  348. if (!Validator.isEmail(email)) {
  349. throw BusinessRuntimeException.getInstance("请输入正确的邮箱");
  350. }
  351. int update = groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
  352. .set(GroupsRelation::getAccount, email)
  353. .and(qr -> qr.isNull(GroupsRelation::getAccount).or().eq(GroupsRelation::getAccount, StrUtil.EMPTY))
  354. .eq(GroupsRelation::getId, relationId));
  355. if (update > 0) {
  356. orderDonMapper.update(null, Wrappers.lambdaUpdate(OrderDon.class)
  357. .set(OrderDon::getStatus, OrderDon.Status.complete)
  358. .eq(OrderDon::getRelationId, relationId)
  359. .eq(OrderDon::getUserId, userId)
  360. .eq(OrderDon::getStatus, OrderDon.Status.hasPayment));
  361. mailService.sendCoursewareByGoodsId(relation.getGoodsId(), relation.getRelationId(), relation.getUserId(), relation.getTitle(), email);
  362. }
  363. }
  364. @Override
  365. public GroupsRelation getRelation(Long payRelationId, Long userId, GoodsDonSku sku, String ip) {
  366. GroupsRelation relation = null;
  367. Integer maxNum = sku.getNum();
  368. if (payRelationId != null && payRelationId > 0) {
  369. relation = groupsRelationMapper.selectById(payRelationId);
  370. if (relation == null) {
  371. throw new BusinessRuntimeException(GROUP_INDEX_NOT_FIND.getCode(), GROUP_INDEX_NOT_FIND.getMsg());
  372. }
  373. if (relation.getUserId() != null && relation.getUserId() > 0L) {
  374. throw new BusinessRuntimeException(GROUP_INDEX_ALREADY_BE_USER.getCode(), GROUP_INDEX_ALREADY_BE_USER.getMsg());
  375. }
  376. } else {
  377. GroupsTrips groups = null;
  378. //ChatGpt Plus、Midjourney非独立规格 上车先获取硬塞座位 硬塞5个座位
  379. //主账号过期时间25天以外
  380. //23-12-06 MidJourney 不硬塞
  381. if (sku.getNum() > 1 && Constant.AI_goodsIds.get(0) == sku.getGoodsId()) {
  382. Integer extraNum = 5;
  383. if (sku.getId() == 178l || sku.getId() == 423l) {
  384. extraNum = 2;
  385. }
  386. maxNum += extraNum;
  387. DateTime time = DateUtil.offsetDay(DateUtil.beginOfDay(DateTime.now()), 25);
  388. //不塞存在该用户的车队
  389. Long groupsId = groupsRelationMapper.getOutSideGroupsTripsRelationByGoodsId(sku.getId(), time, extraNum, userId, sku.getNum(), maxNum);
  390. if (groupsId != null) {
  391. relation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
  392. .eq(GroupsRelation::getGroupsId, groupsId)
  393. .eq(GroupsRelation::getUserId, 0)
  394. .eq(GroupsRelation::getStatus, GroupsRelation.Status.outside)
  395. .last("limit 1"));
  396. if (relation == null) {
  397. relation = new GroupsRelation();
  398. relation.setStatus(GroupsRelation.Status.outside);
  399. relation.setUserId(userId);
  400. relation.setGroupsId(groupsId);
  401. GroupsRelation maxNumRelation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId, groupsId).orderByDesc(GroupsRelation::getNum).last("limit 1"));
  402. Integer nextNum = maxNumRelation.getNum() + 1;
  403. if (redisService.setNx(RedisService.key.GROUPS_TRIPS_OUTSIDE_RELATION.getNameFormat(groupsId, nextNum), nextNum, RedisService.key.GROUPS_TRIPS_OUTSIDE_RELATION.getTimeout())) {
  404. relation.setNum(nextNum);
  405. groupsRelationMapper.insert(relation);
  406. } else {
  407. throw BusinessRuntimeException.getInstance("系统繁忙,请重试...");
  408. }
  409. }
  410. groups = groupsMapper.selectById(groupsId);
  411. }
  412. }
  413. if (groups == null) {
  414. //寻找差不多过期时间规格的车位
  415. relation = getSimilarExpiredGroupRelation(sku, null, ip);
  416. if (relation == null) {
  417. //TODO 如果没指定座位,等待逻辑确认
  418. groups = groupsMapper.selectTicketGroups(sku.getId(), GroupsTrips.Status.validity, maxNum, ip);
  419. if (groups == null) {
  420. //待发车
  421. groups = groupsMapper.selectTicketGroups(sku.getId(), GroupsTrips.Status.waiting, maxNum, ip);
  422. }
  423. //新增空车队
  424. if (groups == null) {
  425. relation = groupsFuncService.createNewGroupsTrips(sku);
  426. }
  427. //获取车位
  428. if (relation == null) {
  429. //寻找快满编车队进行占座
  430. relation = groupsRelationMapper.selectRandomOneRelationByGroupsId(groups.getId());
  431. if (relation == null) {
  432. Long groupsId = groups.getId();
  433. TASK_EXECUTOR.execute(() -> groupsMapper.updateAvailableNum(groupsId));
  434. throw BusinessRuntimeException.getInstance("系统繁忙,请重试...");
  435. }
  436. }
  437. }
  438. }
  439. if (groups == null) {
  440. groups = groupsMapper.selectById(relation.getGroupsId());
  441. }
  442. //车队目前可用停车数
  443. relation.setGroupsAvailParkingNum(groups.getAvailableParkingNum());
  444. relation.setMaxNum(maxNum);
  445. }
  446. return relation;
  447. }
  448. @Override
  449. public String getAiVerifyCode(TicketLoginReq loginReq) throws Exception {
  450. Integer type = loginReq.getType();
  451. RenewalView renewalView;
  452. String account;
  453. Long userId = 0l;
  454. Long relationId = 0l;
  455. if (loginReq.getMerchants()) {
  456. renewalView = new RenewalView();
  457. renewalView.setGoodsId(loginReq.getGoodsId());
  458. renewalView.setAccount(loginReq.getAccount());
  459. if (loginReq.getAccountId() != null && loginReq.getAccountId() != 0l) {
  460. GroupsTrips groupsTrips = groupsMapper.selectOne(Wrappers.lambdaQuery(GroupsTrips.class)
  461. .eq(GroupsTrips::getAccountId, loginReq.getAccountId())
  462. .last("limit 1"));
  463. if (groupsTrips != null) {
  464. renewalView.setSkuId(groupsTrips.getSkuId());
  465. }
  466. }
  467. renewalView.setUserId(userId);
  468. account = loginReq.getAccount();
  469. } else {
  470. userId = loginReq.getUserId();
  471. relationId = loginReq.getRelationId();
  472. List<Long> relationUserIdList = userBindRelationService.getRelationUserIdList(userId, null);
  473. renewalView = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder().field(RenewalView::getUserId, relationUserIdList).op(Operator.InList)
  474. .field(RenewalView::getRelationId, relationId).build());
  475. if (renewalView == null) {
  476. throw BusinessRuntimeException.getInstance("您的车票不存在...");
  477. }
  478. if (StrUtil.isEmpty(renewalView.getAccount())) {
  479. throw BusinessRuntimeException.getInstance("车票未配置账号");
  480. }
  481. account = renewalView.getAccount();
  482. //收取邮件的关联邮箱
  483. if (StrUtil.isNotEmpty(renewalView.getRelateEmail())) {
  484. account = renewalView.getRelateEmail();
  485. }
  486. String password = renewalView.getPassword();
  487. if (StrUtil.hasEmpty(account, password)) {
  488. throw BusinessRuntimeException.getInstance("您的车票账号密码异常,请联系客服...");
  489. }
  490. }
  491. if (renewalView.getGoodsId() == 1l) {
  492. Integer linkType = loginReq.getLinkType();
  493. if (type == null && linkType == null) {
  494. throw BusinessRuntimeException.getInstance("系统异常");
  495. }
  496. String key = RedisService.key.NF_UPGRAD_USER_LIMIT_KEY.getNameFormat(DateUtil.hour(DateTime.now(), true), linkType, renewalView.getUserId());
  497. if (!redisService.hasKey(key)) {
  498. redisService.set(key, 1l, RedisService.key.NF_UPGRAD_USER_LIMIT_KEY.getTimeout());
  499. } else {
  500. Integer value = (Integer) redisService.get(key);
  501. if (value >= 5) {
  502. throw BusinessRuntimeException.getInstance("获取失败,请联系客服");
  503. }
  504. redisService.incr(key, 1L);
  505. }
  506. }
  507. if (cuiQiuMailService.isCuiQiuEmail(renewalView.getAccount())) {
  508. return cuiQiuMailService.getVerifyCode(renewalView.getAccount(), renewalView.getGoodsId(), type, loginReq.getLinkType(), false);
  509. }
  510. //银河域名邮件
  511. String code = yhMailService.getVerifyCode(account, renewalView.getGoodsId(), type, loginReq.getLinkType(), false);
  512. GroupsRelationLoginCodeRecord record = new GroupsRelationLoginCodeRecord();
  513. record.setUserId(userId);
  514. record.setRelationId(relationId);
  515. record.setAccount(renewalView.getAccount());
  516. record.setGoodsId(renewalView.getGoodsId());
  517. record.setSkuId(renewalView.getSkuId());
  518. groupsRelationLoginCodeRecordMapper.insert(record);
  519. return code;
  520. }
  521. @Override
  522. public void checkGroupsAvailParkingNum(Long skuId, Long groupsId, Integer availableParkingNum, Integer maxNum) {
  523. //GPT10人月付、4人月付
  524. if (skuId == 161l || skuId == 178l) {
  525. //为0时,不参与限制
  526. if (availableParkingNum == 0) {
  527. return;
  528. }
  529. //目前车队可用停车数
  530. //目前上车人数
  531. Integer currentParkingNum = groupsRelationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
  532. .eq(GroupsRelation::getGroupsId, groupsId)
  533. .ne(GroupsRelation::getUserId, 0)
  534. .le(GroupsRelation::getNum, maxNum));
  535. if (currentParkingNum > availableParkingNum) {
  536. throw BusinessRuntimeException.getInstance("系统繁忙,请重试...");
  537. }
  538. }
  539. }
  540. @Override
  541. public void adjustGroupsParkingTime(Long relationId, GoodsDonSku sku) {
  542. if (sku == null) {
  543. return;
  544. }
  545. Long skuId = sku.getId();
  546. //更新GPT Plus10人、4人月付车队状态 待优化
  547. if (skuId == 161l || skuId == 178l || skuId == 422l || skuId == 423l) {
  548. GroupsRelation relation = groupsRelationMapper.selectById(relationId);
  549. Long groupsId = relation.getGroupsId();
  550. GroupsTrips groupsTrips = groupsMapper.selectById(groupsId);
  551. Integer availableParkingNum = groupsTrips.getAvailableParkingNum();
  552. //为0时 不参与限制
  553. if (availableParkingNum == 0) {
  554. return;
  555. }
  556. Integer extraNum = 5;
  557. if (skuId == 178l || skuId == 423l) {
  558. extraNum = 2;
  559. }
  560. Integer maxNum = sku.getNum() + extraNum;
  561. if (availableParkingNum >= maxNum) {
  562. return;
  563. }
  564. //目前上车人数
  565. Integer currentParkingNum = groupsRelationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
  566. .eq(GroupsRelation::getGroupsId, groupsId)
  567. .ne(GroupsRelation::getUserId, 0)
  568. .le(GroupsRelation::getNum, maxNum));
  569. if (currentParkingNum >= availableParkingNum) {
  570. //锁住
  571. DateTime parkingTime = DateUtil.offsetDay(DateTime.now(), 1);
  572. groupsMapper.update(null, Wrappers.lambdaUpdate(GroupsTrips.class)
  573. .eq(GroupsTrips::getId, groupsTrips.getId())
  574. .set(GroupsTrips::getParkingTime, parkingTime)
  575. .isNull(GroupsTrips::getParkingTime));
  576. }
  577. }
  578. }
  579. @Override
  580. public void setGroupsTripsParkingTimeEmpty(GoodsDonSku sku, Long relationId) {
  581. Long skuId = sku.getId();
  582. if (skuId == 161l || skuId == 178l) {
  583. GroupsRelation relation = groupsRelationMapper.selectById(relationId);
  584. Long groupsId = relation.getGroupsId();
  585. GroupsTrips groupsTrips = groupsMapper.selectById(groupsId);
  586. Integer availableParkingNum = groupsTrips.getAvailableParkingNum();
  587. if (groupsTrips.getParkingTime() == null || availableParkingNum == 0) {
  588. return;
  589. }
  590. Integer extraNum = 5;
  591. if (skuId == 178l) {
  592. extraNum = 2;
  593. }
  594. Integer skuNum = sku.getNum();
  595. if (availableParkingNum >= skuNum + extraNum) {
  596. return;
  597. }
  598. //目前上车人数
  599. Integer currentParkingNum = groupsRelationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
  600. .eq(GroupsRelation::getGroupsId, groupsId)
  601. .ne(GroupsRelation::getUserId, 0));
  602. if (currentParkingNum < availableParkingNum) {
  603. groupsMapper.update(null, Wrappers.lambdaUpdate(GroupsTrips.class)
  604. .eq(GroupsTrips::getId, groupsTrips.getId())
  605. .set(GroupsTrips::getParkingTime, null)
  606. .isNotNull(GroupsTrips::getParkingTime));
  607. }
  608. }
  609. }
  610. @Override
  611. public Integer getUserCodeNumBySkuId(Long userId, Long skuId, Boolean isHasVerifyCode) {
  612. //查看双重验证码
  613. UserFuncProperty userFuncProperty = userFuncPropertyMapper.selectOne(Wrappers.lambdaQuery(UserFuncProperty.class).eq(UserFuncProperty::getUserId, userId).last("limit 1"));
  614. //默认2次
  615. int num = 2;
  616. //有安全码查看次数 置为0
  617. if (isHasVerifyCode) {
  618. num = 0;
  619. //存在身份实名认证有两次机会
  620. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  621. Integer selectCount = userCertRecordMapper.selectCount(Wrappers.lambdaQuery(UserCertRecord.class)
  622. .in(UserCertRecord::getUserId, userIdList));
  623. if (selectCount > 0) {
  624. num = 2;
  625. }
  626. return num;
  627. }
  628. //查看双重验证码次数
  629. GoodsDonSku sku = goodsDonSkuMapper.selectById(skuId);
  630. if (sku.getCodeNum() > 0) {
  631. num = sku.getCodeNum();
  632. }
  633. if (userFuncProperty != null) num = userFuncProperty.getVerifyCheckNum();
  634. return num;
  635. }
  636. @Override
  637. public List<RenewalView> getHasPayGoods(long userId) {
  638. User user = userMapper.selectById(userId);
  639. List<Long> userIds = userBindRelationService.getRelationUserIdList(userId, user);
  640. DateTime now = DateTime.now();
  641. List<RenewalView> list = groupsRelationMapper.getUserHasPayGoods(userIds, now);
  642. return list;
  643. }
  644. @Override
  645. public List<RenewalView> getExpiryRenewalNotify(Long userId) {
  646. DateTime now = DateTime.now();
  647. DateTime thisZero = DateUtil.beginOfDay(now);
  648. String thisDate = thisZero.toDateStr();
  649. Object exists = redisService.get(RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getNameFormat(thisDate, userId));
  650. if (exists != null) {
  651. return null;
  652. }
  653. User u = userMapper.selectById(userId);
  654. if (u == null) throw BusinessRuntimeException.getGatewayApiCode(GatewayApiCode.CMS_TOKEN_VALID);
  655. if (u.getIsBlack()) {
  656. redisService.setNx(RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getNameFormat(thisDate, userId), userId, RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getTimeout());
  657. return null;
  658. }
  659. List<Long> userIds = userBindRelationService.getRelationUserIdList(userId, u);
  660. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  661. List<RenewalView> list = beanSearcher.searchAll(RenewalView.class, builder
  662. .field(RenewalView::getUserId, 0).op(Operator.GreaterThan)
  663. .field(RenewalView::getUserId, userIds).op(Operator.InList)
  664. .field(RenewalView::getExpiryTime, now).op(Operator.GreaterThan)
  665. .field(RenewalView::getStatus, List.of("validity", "overdue", "outside")).op(Operator.InList)
  666. .field(RenewalView::getAqType, 1)
  667. .orderBy(RenewalView::getExpiryTime).asc()
  668. .build());
  669. if (list.size() > 5) {
  670. redisService.setNx(RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getNameFormat(thisDate, userId), userId, RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getTimeout());
  671. return null;
  672. }
  673. //提前7天
  674. AtomicInteger day = new AtomicInteger(7);
  675. List<RenewalView> expiryTickets = list.stream().filter(ticket -> {
  676. //下架
  677. if (ticket.getGoodsStatus() == null || ticket.getSkuStatus() == null || !ticket.getGoodsStatus() || !ticket.getSkuStatus()) {
  678. return false;
  679. }
  680. Integer skuMonths = ticket.getMonths();
  681. if (skuMonths == null) {
  682. return false;
  683. }
  684. Date expiryTime = ticket.getExpiryTime();
  685. //年付 提前一个月
  686. if (skuMonths >= 12) {
  687. day.set(30);
  688. } else {
  689. day.set(7);
  690. }
  691. if (DateUtil.beginOfDay(expiryTime).compareTo(DateUtil.offsetDay(thisZero, day.get())) <= 0) {
  692. ticket.setExpiryTimeStr(DateUtil.format(ticket.getExpiryTime(), "yyyy-MM-dd"));
  693. //GPT镜像信息
  694. setGptMirrorInfo(ticket);
  695. ticket.setExpiryTime(null);
  696. return true;
  697. }
  698. return false;
  699. }).collect(Collectors.toList());
  700. redisService.setNx(RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getNameFormat(thisDate, userId), userId, RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getTimeout());
  701. if (CollUtil.isNotEmpty(expiryTickets)) {
  702. groupsRelationExpiryNotifyRecordMapper.batchInsert(userId, expiryTickets);
  703. }
  704. return expiryTickets;
  705. }
  706. @Override
  707. public void getCollegeStudentUserGptTicket(Long userId) {
  708. Retryer<Boolean> build = RetryerBuilder.<Boolean>newBuilder()
  709. .retryIfException()
  710. //运行时异常重试
  711. .retryIfRuntimeException()
  712. //false重试
  713. .retryIfResult(res -> Objects.equals(res, false))
  714. //1s 间隔
  715. .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))
  716. //停止策略 : 尝试请求3次
  717. .withStopStrategy(StopStrategies.stopAfterAttempt(3)).build();
  718. try {
  719. build.call(() -> {
  720. //体验规格
  721. Long gptMirrorSkuId = Constant.GPT_MIRROR_SKU_ID;
  722. GroupsTrips groups = groupsMapper.selectCollegeStudentUserGptTicket(gptMirrorSkuId);
  723. GroupsRelation relation = null;
  724. if (groups != null) {
  725. relation = groupsRelationMapper.selectRandomOneRelationByGroupsId(groups.getId());
  726. }
  727. if (relation == null) {
  728. //生成空车队
  729. GoodsDonSku sku = goodsDonSkuMapper.selectById(gptMirrorSkuId);
  730. relation = groupsFuncService.createNewGroupsTrips(sku);
  731. }
  732. DateTime startTime = DateTime.now();
  733. DateTime expiryTime = DateUtil.offsetDay(startTime, 1);
  734. int update = groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
  735. .set(GroupsRelation::getUserId, userId)
  736. .set(GroupsRelation::getStartTime, startTime)
  737. .set(GroupsRelation::getExpiryTime, expiryTime)
  738. .set(GroupsRelation::getStatus, GroupsRelation.Status.validity)
  739. .set(GroupsRelation::getAqType, 2)
  740. .eq(GroupsRelation::getId, relation.getId())
  741. .eq(GroupsRelation::getUserId, 0));
  742. if (update > 0) {
  743. groupsMapper.decrAvailableNum(groups.getId());
  744. return true;
  745. }
  746. log.error("发送用户:{} GPT镜像车票体验失败,进行重试", userId);
  747. return false;
  748. });
  749. } catch (ExecutionException | RetryException e) {
  750. log.error("发送用户GPT镜像体验失败:{}", StringUtil.getErrorText(e));
  751. }
  752. }
  753. @Override
  754. public RenewalView getTicketDetail(long userId, Long relationId) {
  755. User u = userMapper.selectById(userId);
  756. List<Long> userIds = userBindRelationService.getRelationUserIdList(userId, u);
  757. MapBuilder builder = MapUtils.builder();
  758. RenewalView renewalView = beanSearcher.searchFirst(RenewalView.class, builder
  759. .field(RenewalView::getRelationId, relationId)
  760. .field(RenewalView::getUserId, 0).op(Operator.GreaterThan)
  761. .field(RenewalView::getUserId, userIds).op(Operator.InList)
  762. .field("condition", String.format("(gr.expiry_time is null or gr.expiry_time > '%s')", DateTime.now()))
  763. .field(RenewalView::getStatus, List.of("validity", "outside")).op(Operator.InList)
  764. .orderBy(RenewalView::getExpiryTime).asc()
  765. .onlySelect(RenewalView::getRelationId, RenewalView::getExpiryTime, RenewalView::getTitle, RenewalView::getSpecVal, RenewalView::getGoodsId, RenewalView::getSkuId, RenewalView::getSkuNum, RenewalView::getSkuStatus, RenewalView::getGoodsStatus)
  766. .build());
  767. if (renewalView != null) {
  768. renewalView.setExpiryTimeStr(DateUtil.format(renewalView.getExpiryTime(), "yyyy-MM-dd"));
  769. }
  770. return renewalView;
  771. }
  772. @Override
  773. public GroupsRelation ticketLogin(TicketLoginReq loginReq) {
  774. String account = loginReq.getAccount();
  775. if (StrUtil.isEmpty(account)) {
  776. throw BusinessRuntimeException.getInstance("请输入对应账号");
  777. }
  778. if (loginReq.getMerchants()) {
  779. if (account.contains(Constant.YH_EMAIL_DOMAIN_SUFFIX)) {
  780. SysEmail sysEmail = sysEmailMapper.selectOne(Wrappers.lambdaQuery(SysEmail.class)
  781. .eq(SysEmail::getEmail, account)
  782. .last("limit 1"));
  783. if (sysEmail == null) {
  784. throw BusinessRuntimeException.getInstance("账号错误,请联系客服");
  785. }
  786. }
  787. GroupsRelation relation = new GroupsRelation();
  788. relation.setId(0l);
  789. return relation;
  790. }
  791. String password = loginReq.getPassword();
  792. Integer num = loginReq.getNum();
  793. if (ObjectUtil.hasEmpty(password, num)) {
  794. throw BusinessRuntimeException.getInstance("参数错误");
  795. }
  796. Long goodsId = Optional.ofNullable(loginReq.getGoodsId()).orElse(1l);
  797. GroupsRelation relation = groupsRelationMapper.selectRelationByAccountAndNum(account, password, goodsId, num);
  798. if (relation == null) {
  799. log.info("外部入口使用账号account:{},pwd:{},座位num:{}登录失败...", account, password, num);
  800. if (StrUtil.equals(loginReq.getLang(), Constant.EN_US)) {
  801. throw BusinessRuntimeException.getInstance("Please enter the correct account information");
  802. }
  803. throw BusinessRuntimeException.getInstance("请输入正确的账号信息");
  804. }
  805. return relation;
  806. }
  807. @Override
  808. public String outsideTicketVerify(TicketLoginReq loginReq) {
  809. String account = loginReq.getAccount();
  810. if (StrUtil.isEmpty(account)) {
  811. throw BusinessRuntimeException.getInstance("账号错误");
  812. }
  813. if (loginReq.getMerchants()) {
  814. if (account.contains(Constant.YH_EMAIL_DOMAIN_SUFFIX)) {
  815. SysEmail sysEmail = sysEmailMapper.selectOne(Wrappers.lambdaQuery(SysEmail.class)
  816. .eq(SysEmail::getEmail, account)
  817. .last("limit 1"));
  818. if (sysEmail == null) {
  819. throw BusinessRuntimeException.getInstance("账号错误,请联系客服");
  820. }
  821. }
  822. loginReq.setGoodsId(1l);
  823. loginReq.setAccountId(0l);
  824. } else {
  825. String password = loginReq.getPassword();
  826. Integer num = loginReq.getNum();
  827. if (ObjectUtil.hasEmpty(password, num)) {
  828. throw BusinessRuntimeException.getInstance("参数错误");
  829. }
  830. Long goodsId = Optional.ofNullable(loginReq.getGoodsId()).orElse(1l);
  831. GroupsRelation relation = groupsRelationMapper.selectRelationByAccountAndNum(account, password, goodsId, num);
  832. if (relation == null) {
  833. log.info("外部入口使用账号account:{},pwd:{},座位num:{}同步验证链接失败...", account, password, num);
  834. if (StrUtil.equals(loginReq.getLang(), Constant.EN_US)) {
  835. throw BusinessRuntimeException.getInstance("This account is no longer valid");
  836. }
  837. throw BusinessRuntimeException.getInstance("该账号已失效");
  838. }
  839. loginReq.setUserId(relation.getUserId());
  840. loginReq.setRelationId(relation.getId());
  841. loginReq.setGoodsId(goodsId);
  842. }
  843. try {
  844. String aiVerifyCode = getAiVerifyCode(loginReq);
  845. if (StrUtil.isEmpty(aiVerifyCode)) {
  846. throw BusinessRuntimeException.getInstance("");
  847. }
  848. return aiVerifyCode;
  849. } catch (Exception e) {
  850. if (StrUtil.equals(loginReq.getLang(), Constant.EN_US)) {
  851. throw BusinessRuntimeException.getInstance("Please verify that the link was sent");
  852. }
  853. throw BusinessRuntimeException.getInstance("请校验链接是否已发送");
  854. }
  855. }
  856. @Override
  857. public String getNfHouseholdDevice(long userId, Long relationId) throws Exception {
  858. List<Long> relationUserIdList = userBindRelationService.getRelationUserIdList(userId, null);
  859. RenewalView renewalView = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder().field(RenewalView::getUserId, relationUserIdList).op(Operator.InList)
  860. .field(RenewalView::getRelationId, relationId).build());
  861. if (renewalView == null) {
  862. throw BusinessRuntimeException.getInstance("您的车票不存在...");
  863. }
  864. if (StrUtil.isEmpty(renewalView.getAccount())) {
  865. throw BusinessRuntimeException.getInstance("车票未配置账号");
  866. }
  867. String account = renewalView.getAccount();
  868. //收取邮件的关联邮箱
  869. if (StrUtil.isNotEmpty(renewalView.getRelateEmail())) {
  870. account = renewalView.getRelateEmail();
  871. }
  872. String password = renewalView.getPassword();
  873. if (StrUtil.hasEmpty(account, password)) {
  874. throw BusinessRuntimeException.getInstance("您的车票账号密码异常,请联系客服...");
  875. }
  876. if (cuiQiuMailService.isCuiQiuEmail(renewalView.getAccount())) {
  877. return cuiQiuMailService.getVerifyCode(renewalView.getAccount(), renewalView.getGoodsId(), null, 1, true);
  878. }
  879. //银河域名邮件
  880. return yhMailService.getVerifyCode(account, renewalView.getGoodsId(), null, 1, true);
  881. }
  882. private DistributePosterGoodsDetailView setGoodsPoster(RenewalView ticket, User user, List<Long> userIds) {
  883. DistributePosterGoodsDetailView distributePosterGoodsDetailView = beanSearcher.searchFirst(DistributePosterGoodsDetailView.class, MapUtils.flatBuilder(request.getParameterMap())
  884. .field(DistributePosterGoodsFrontView::getGoodsId, ticket.getGoodsId()).build());
  885. if (distributePosterGoodsDetailView == null) {
  886. return null;
  887. }
  888. distributePosterGoodsDetailView.setNickname(user.getNickname());
  889. UserDistribute userDistribute = userDistributeMapper.selectOne(Wrappers.lambdaQuery(UserDistribute.class)
  890. .eq(UserDistribute::getUserId, user.getId())
  891. .eq(UserDistribute::getDeleted, 1)
  892. .last("limit 1"));
  893. if (userDistribute != null) {
  894. UserPlatDistributeRate userPlatDistributeRate = userPlatDistributeRateMapper.selectOne(Wrappers.lambdaQuery(UserPlatDistributeRate.class)
  895. .eq(UserPlatDistributeRate::getDistributeId, userDistribute.getId())
  896. .eq(UserPlatDistributeRate::getGoodsId, ticket.getGoodsId())
  897. .last("limit 1"));
  898. if (userPlatDistributeRate != null) {
  899. distributePosterGoodsDetailView.setRate(userPlatDistributeRate.getRate());
  900. }
  901. }
  902. GoodsDon goodsDon = goodsDonMapper.selectById(ticket.getGoodsId());
  903. if (goodsDon.getIsSkuDistribute()) {
  904. if (userDistribute != null) {
  905. UserPlatSkuDistributeRate userPlatSkuDistributeRate = userPlatSkuDistributeRateMapper.selectOne(Wrappers.lambdaQuery(UserPlatSkuDistributeRate.class)
  906. .eq(UserPlatSkuDistributeRate::getDistributeId, userDistribute.getId())
  907. .eq(UserPlatSkuDistributeRate::getSkuId, ticket.getSkuId())
  908. .last("limit 1"));
  909. if (userPlatSkuDistributeRate != null) {
  910. distributePosterGoodsDetailView.setRate(userPlatSkuDistributeRate.getRate());
  911. }
  912. } else {
  913. DistributeGeneralSkuRate distributeGeneralSkuRate = distributeGeneralSkuRateMapper.selectOne(Wrappers.lambdaQuery(DistributeGeneralSkuRate.class)
  914. .eq(DistributeGeneralSkuRate::getSkuId, ticket.getSkuId())
  915. .last("limit 1"));
  916. if (distributeGeneralSkuRate != null) {
  917. distributePosterGoodsDetailView.setRate(distributeGeneralSkuRate.getRate());
  918. }
  919. }
  920. }
  921. //用户推广海报记录id
  922. Optional.ofNullable(userDistributePosterRecordMapper.selectOne(Wrappers.lambdaQuery(UserDistributePosterRecord.class)
  923. .eq(UserDistributePosterRecord::getPosterId, distributePosterGoodsDetailView.getId())
  924. .in(UserDistributePosterRecord::getUserId, userIds)
  925. .last("limit 1"))).ifPresent(re -> distributePosterGoodsDetailView.setRid(re.getId()));
  926. return distributePosterGoodsDetailView;
  927. }
  928. private void setRenewDiscount(RenewalView ticket) {
  929. //镜像或者独立规格
  930. if (ticket.getIsMirror() || (ticket.getSkuNum() != null && ticket.getSkuNum() == 1)) {
  931. RenewUpgradePackage renewUpgradePackage = renewUpgradePackageMapper.selectOne(Wrappers.lambdaQuery(RenewUpgradePackage.class)
  932. .eq(RenewUpgradePackage::getGoodsId, ticket.getGoodsId())
  933. .eq(RenewUpgradePackage::getStatus, 1)
  934. .eq(RenewUpgradePackage::getDeleted, 1)
  935. .eq(RenewUpgradePackage::getSkuId, ticket.getSkuId())
  936. .orderByAsc(RenewUpgradePackage::getDiscount)
  937. .last("limit 1")
  938. .select(RenewUpgradePackage::getId, RenewUpgradePackage::getDiscount));
  939. if (renewUpgradePackage != null) {
  940. ticket.setDiscount(renewUpgradePackage.getDiscount());
  941. }
  942. }
  943. }
  944. private void setMirrorTicketInfo(RenewalView ticket) {
  945. //设置MJ镜像信息
  946. setMjMirrorInfo(ticket);
  947. //设置GPT镜像信息
  948. setGptMirrorInfo(ticket);
  949. //clause pro
  950. setClaudeProInfo(ticket);
  951. //luma
  952. setLumaInfo(ticket);
  953. }
  954. private void setMjMirrorInfo(RenewalView ticket) {
  955. if (ticket.getGoodsId() == 26 && ticket.getIsMirror()) {
  956. 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));
  957. if (CollectionUtil.isNotEmpty(upgradePackages)) {
  958. upgradePackages.stream().map(e -> e.getPrice().divide(BigDecimal.valueOf(e.getNum()), 2, RoundingMode.HALF_UP)).min(BigDecimal::compareTo).ifPresent(ticket::setMjPackagePrice);
  959. }
  960. try {
  961. MidjourneyUserView search = beanSearcher.searchFirst(MidjourneyUserView.class, MapUtils.flatBuilder(request.getParameterMap())
  962. .field(MidjourneyUserView::getUserId, ticket.getUserId()).field(MidjourneyUserView::getRelationId, ticket.getRelationId())
  963. .onlySelect(MidjourneyUserView::getMjUserRelaxNum, MidjourneyUserView::getMjUserFastNum, MidjourneyUserView::getIsCar)
  964. .build());
  965. if (search == null) {
  966. MidjourneyUser midjourneyUser = midjourneyAccountService.getMidjourneyUserToken(ticket.getUserId(), ticket.getRelationId());
  967. ticket.setMjUserFastNum(midjourneyUser.getMjFastNum());
  968. ticket.setMjUserRelaxNum(midjourneyUser.getMjRelaxNum());
  969. ticket.setIsCar(false);
  970. } else {
  971. ticket.setIsCar(search.getIsCar());
  972. ticket.setMjUserFastNum(search.getMjUserFastNum());
  973. ticket.setMjUserRelaxNum(search.getMjUserRelaxNum());
  974. }
  975. } catch (Exception e) {
  976. }
  977. }
  978. }
  979. public void setGptMirrorInfo(RenewalView ticket) {
  980. if (ticket.getGoodsId() == 18 && ticket.getIsCar() && ticket.getExpiryTime() != null) {
  981. try {
  982. //gpt用户镜像
  983. ChatGptUserView userInfo = chatGptAccountService.getUserInfo(ticket.getUserId(), ticket.getRelationId());
  984. ticket.setGptUserLimitNum(userInfo.getLimitNum());
  985. ticket.setGptUserLimitTime(userInfo.getLimitTime());
  986. ticket.setUse(userInfo.getUse());
  987. Integer count = upgradePackageMapper.selectCount(Wrappers.lambdaQuery(UpgradePackage.class).apply("JSON_CONTAINS(sku_ids,'\"" + ticket.getSkuId() + "\"')")
  988. .gt(UpgradePackage::getNum, userInfo.getLimitNum())
  989. .eq(UpgradePackage::getStatus, Boolean.TRUE)
  990. .eq(UpgradePackage::getGptType, 0)
  991. .eq(UpgradePackage::getDeleted, Boolean.TRUE));
  992. ticket.setUpgrade(count > 0 ? true : false);
  993. ticket.setPackageId(userInfo.getPackageId());
  994. if (userInfo.getPackageId() != 0) {
  995. UpgradePackage upgradePackage = upgradePackageMapper.selectById(userInfo.getPackageId());
  996. ticket.setPackagePrice(upgradePackage.getPrice());
  997. Long upgradeOrderDonSkuId = upgradePackage.getUpgradeSkuId();
  998. GoodsDonSkuRenewView renewSku = beanSearcher.searchFirst(GoodsDonSkuRenewView.class, MapUtils.builder().field(GoodsDonSkuRenewView::getSkuId, upgradeOrderDonSkuId).build());
  999. renewSku.setPackageId(userInfo.getPackageId());
  1000. ticket.setRenewSku(renewSku);
  1001. }
  1002. ticket.setIsRenewPackage(userInfo.getIsRenewPackage());
  1003. Long renewSkuId = userInfo.getRenewSkuId();
  1004. if (renewSkuId != null) {
  1005. GoodsDonSkuRenewView upgradeSkuRenewView = ticket.getRenewSku();
  1006. GoodsDonSkuRenewView renewSku = beanSearcher.searchFirst(GoodsDonSkuRenewView.class, MapUtils.builder().field(GoodsDonSkuRenewView::getSkuId, renewSkuId).build());
  1007. //续费升级是更高级规格
  1008. if (upgradeSkuRenewView == null || (renewSku.getGptUserLimitNum() >= upgradeSkuRenewView.getGptUserLimitNum() && renewSku.getMonths() >= userInfo.getMonths())) {
  1009. Optional.ofNullable(orderDonRenewRecordMapper.selectOne(Wrappers.lambdaQuery(OrderDonRenewRecord.class)
  1010. .eq(OrderDonRenewRecord::getOrderId, userInfo.getRenewOrderId())
  1011. .last("limit 1"))).ifPresent(e -> renewSku.setOriginalOrderId(e.getOrderId()));
  1012. ticket.setRenewSku(renewSku);
  1013. }
  1014. }
  1015. } catch (Exception e) {
  1016. }
  1017. }
  1018. }
  1019. public void setClaudeProInfo(RenewalView ticket) {
  1020. //clause pro
  1021. if (ticket.getGoodsId() == 75l && ticket.getExpiryTime() != null) {
  1022. try {
  1023. ClaudeUserView userInfo = claudeService.getUserInfo(ticket.getUserId(), ticket.getRelationId());
  1024. ticket.setGptUserLimitNum(userInfo.getLimitNum());
  1025. ticket.setGptUserLimitTime(userInfo.getLimitTime());
  1026. ticket.setUse(userInfo.getUse());
  1027. } catch (Exception e) {
  1028. }
  1029. }
  1030. }
  1031. public void setLumaInfo(RenewalView ticket) {
  1032. if (ticket.getGoodsId() == Constant.LUMA_GOODS_ID && ticket.getExpiryTime() != null) {
  1033. try {
  1034. 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));
  1035. if (CollectionUtil.isNotEmpty(upgradePackages)){
  1036. upgradePackages.stream().map(e -> e.getPrice().divide(BigDecimal.valueOf(e.getNum()), 2, RoundingMode.HALF_UP)).min(BigDecimal::compareTo).ifPresent(ticket::setLumaPackagePrice);
  1037. }
  1038. LumaUser lumaUser = lumaService.getLumaUser(ticket.getUserId(), ticket.getRelationId());
  1039. ticket.setLumaUserNum(lumaUser.getNum());
  1040. } catch (Exception e) {
  1041. }
  1042. }
  1043. }
  1044. }