GroupRelationFrontServiceImpl.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. package com.cyksj.service.relation.impl;
  2. import cn.hutool.core.date.BetweenFormater;
  3. import cn.hutool.core.date.DateTime;
  4. import cn.hutool.core.date.DateUtil;
  5. import cn.hutool.core.lang.Assert;
  6. import cn.hutool.core.lang.Validator;
  7. import cn.hutool.core.util.StrUtil;
  8. import cn.hutool.http.HttpRequest;
  9. import cn.hutool.http.HttpStatus;
  10. import cn.hutool.http.HttpUtil;
  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.enums.GatewayApiCode;
  18. import com.cyksj.mapper.*;
  19. import com.cyksj.mapper.sys.SysEmailMapper;
  20. import com.cyksj.model.entity.*;
  21. import com.cyksj.model.request.SpecialGoodsTicketReq;
  22. import com.cyksj.model.views.*;
  23. import com.cyksj.redis.RedisService;
  24. import com.cyksj.service.chatgpt.ChatGptAuthService;
  25. import com.cyksj.service.groups.GroupsFuncService;
  26. import com.cyksj.service.mail.MailService;
  27. import com.cyksj.service.mange.coupon.CouponCommonService;
  28. import com.cyksj.service.relation.GroupRelationFrontService;
  29. import com.cyksj.service.shop.YhShopFrontService;
  30. import com.cyksj.service.user.UserBindRelationService;
  31. import com.cyksj.service.user.UserLoginRecordService;
  32. import com.ejlchina.searcher.BeanSearcher;
  33. import com.ejlchina.searcher.SearchResult;
  34. import com.ejlchina.searcher.param.Operator;
  35. import com.ejlchina.searcher.util.MapBuilder;
  36. import com.ejlchina.searcher.util.MapUtils;
  37. import lombok.RequiredArgsConstructor;
  38. import lombok.extern.slf4j.Slf4j;
  39. import org.springframework.stereotype.Service;
  40. import org.springframework.transaction.annotation.Transactional;
  41. import javax.servlet.http.HttpServletRequest;
  42. import java.util.*;
  43. import java.util.regex.Matcher;
  44. import java.util.regex.Pattern;
  45. import static com.cyksj.enums.GatewayApiCode.GROUP_INDEX_ALREADY_BE_USER;
  46. import static com.cyksj.enums.GatewayApiCode.GROUP_INDEX_NOT_FIND;
  47. /*
  48. *项目名: netflix
  49. *文件名: GroupRelationFrontServiceImpl
  50. *创建者: JavaZou
  51. *创建时间:2022/12/29 15:42
  52. */
  53. @Service
  54. @RequiredArgsConstructor
  55. @Slf4j
  56. public class GroupRelationFrontServiceImpl implements GroupRelationFrontService {
  57. private final UserBindRelationService userBindRelationService;
  58. private final UserMapper userMapper;
  59. private final BeanSearcher beanSearcher;
  60. private final CouponCommonService couponCommonService;
  61. private final GroupsRelationMapper groupsRelationMapper;
  62. private final GroupsMapper groupsMapper;
  63. private final ChatGptAuthService chatGptAuthService;
  64. private final RedisService redisService;
  65. private final MailService mailService;
  66. private final OrderDonMapper orderDonMapper;
  67. private final YhShopFrontService yhShopFrontService;
  68. private final HttpServletRequest request;
  69. private final GroupsFuncService groupsFuncService;
  70. private final OrderDiscountPlusPurchaseSkuRelationMapper orderDiscountPlusPurchaseSkuRelationMapper;
  71. private final GroupsRelationLoginCodeRecordMapper groupsRelationLoginCodeRecordMapper;
  72. private final SysEmailMapper sysEmailMapper;
  73. private final GoodsDonSkuMapper goodsDonSkuMapper;
  74. private final UserFuncPropertyMapper userFuncPropertyMapper;
  75. private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
  76. private final UserLoginRecordService userLoginRecordService;
  77. private final static String VERIFY_CODE_SECRET = "q1w2e3";
  78. @Override
  79. public SearchResult<RenewalView> getMyTicket(long userId, Boolean isLoginPopularize, String customId, String account) {
  80. User u = userMapper.selectById(userId);
  81. if (u == null) throw BusinessRuntimeException.getGatewayApiCode(GatewayApiCode.CMS_TOKEN_VALID);
  82. if (u.getIsBlack()) {
  83. return new SearchResult<>();
  84. }
  85. List<Long> userIds = userBindRelationService.getRelationUserIdList(userId, u);
  86. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  87. //Long yhsId = yhShopFrontService.getYhsIdByCustomId(customId);
  88. DateTime now = DateTime.now();
  89. if (StrUtil.isNotBlank(account)) {
  90. builder.field(RenewalView::getAccount, account).op(Operator.Contain);
  91. }
  92. SearchResult<RenewalView> list = beanSearcher.search(RenewalView.class, builder
  93. .field(RenewalView::getUserId, 0).op(Operator.GreaterThan)
  94. .field(RenewalView::getUserId, userIds).op(Operator.InList)
  95. //.field(RenewalView::getYhsId, yhsId)
  96. .field("condition", String.format("(gr.expiry_time is null or gr.expiry_time > '%s')", now))
  97. .field(RenewalView::getStatus, List.of("validity", "overdue", "outside")).op(Operator.InList)
  98. .orderBy(RenewalView::getExpiryTime).asc()
  99. .build());
  100. //过滤邀请制、已失效的车位
  101. Integer size = list.getTotalCount().intValue();
  102. list.getDataList().forEach(ticket -> {
  103. if (ticket.getSkuId() == null || ticket.getGoodsId() == null) {
  104. return;
  105. }
  106. if (ticket.getGoodsId() == 18 && ticket.getSkuNum() > 1) {
  107. ticket.setPassword(null);
  108. }
  109. ticket.setIsMulti(false);
  110. if (ticket.getSkuNum() > 1) {
  111. ticket.setIsMulti(true);
  112. }
  113. //chatGPT直连
  114. if (ticket.getGoodsId().equals(42l)) {
  115. if (StrUtil.isNotBlank(ticket.getAccount())) {
  116. String accessToken;
  117. try {
  118. accessToken = chatGptAuthService.getAccessToken(ticket.getAccount());
  119. ticket.setGptRefreshToken(accessToken);
  120. } catch (Exception e) {
  121. redisService.set(RedisService.key.CHAT_GPT_TOKEN_EXCEPTION_KEY.getName() + ticket.getAccount(), ticket.getAccount());
  122. }
  123. }
  124. ticket.setAccount(null);
  125. ticket.setPassword(null);
  126. } else {
  127. ticket.setGptRefreshToken(null);
  128. }
  129. if (ticket.getSecondType() == 1) {
  130. ticket.setAccount(null);
  131. ticket.setPassword(null);
  132. }
  133. if (isLoginPopularize != null && isLoginPopularize) {
  134. GoodsDonViews views = beanSearcher.searchFirst(GoodsDonViews.class, MapUtils.builder().field(GoodsDonViews::getId, ticket.getGoodsId()).build());
  135. views.setSpecs(beanSearcher.searchFirst(GoodsDonSpecFrontView.class, MapUtils.builder().field(GoodsDonSpecFrontView::getGoodsId, views.getId()).build()));
  136. views.setSkuList(beanSearcher.searchAll(GoodsDonSkuFrontView.class, MapUtils.builder().field(GoodsDonSkuFrontView::getGoodsId, views.getId()).field(GoodsDonSkuFrontView::getStatus, true).build()));
  137. ticket.setGoodsDonViews(views);
  138. }
  139. if (ticket.getMonths() != null && ticket.getMonths() >= 300) {
  140. ticket.setExpiryTime(null);
  141. }
  142. if (ticket.getExpiryTime() != null && ticket.getGoodsType() == 1 && ticket.getSecondType() == 2) {
  143. String remainDays = DateUtil.formatBetween(now, ticket.getExpiryTime(), BetweenFormater.Level.DAY);
  144. if ("0天".equals(remainDays)) {
  145. remainDays = "小于1天";
  146. } else {
  147. //当天也算
  148. int days = Integer.parseInt(remainDays.substring(0, remainDays.indexOf("天")));
  149. remainDays = days + 1 + "天";
  150. }
  151. ticket.setRemainDays(remainDays);
  152. }
  153. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  154. .eq(OrderDon::getRelationId, ticket.getRelationId())
  155. .eq(OrderDon::getUserId, ticket.getUserId())
  156. .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
  157. .orderByDesc(OrderDon::getId)
  158. .last("limit 1"));
  159. if (orderDon != null) {
  160. ticket.setOrderNo(orderDon.getOrderNo());
  161. ticket.setTransactionId(orderDon.getTransactionId());
  162. }
  163. if (size > 1 && ticket.getGoodsId() != 18) {
  164. Date payTime = null;
  165. if (orderDon != null) {
  166. payTime = Optional.ofNullable(orderDon.getPayTime()).orElse(orderDon.getCreatedTime());
  167. }
  168. //加购订单
  169. if (orderDon == null) {
  170. List<OrderDiscountPlusPurchaseSkuRelation> plusPurchaseSkuRelations = orderDiscountPlusPurchaseSkuRelationMapper.selectPlusRelationByIdAndUserId(ticket.getRelationId(), ticket.getUserId());
  171. for (OrderDiscountPlusPurchaseSkuRelation data : plusPurchaseSkuRelations) {
  172. String relationIds = data.getRelationIds();
  173. if (StrUtil.isEmpty(relationIds)) {
  174. return;
  175. }
  176. try {
  177. List<Long> relationList = Jsons.parseList(relationIds, Long.class);
  178. if (relationList.contains(ticket.getRelationId())) {
  179. OrderDon plusOrder = orderDonMapper.selectById(data.getOrderId());
  180. if (plusOrder != null) {
  181. payTime = Optional.ofNullable(plusOrder.getPayTime()).orElse(plusOrder.getCreatedTime());
  182. }
  183. break;
  184. }
  185. } catch (Exception e) {
  186. }
  187. }
  188. }
  189. ticket.setPayTime(payTime);
  190. }
  191. List<GroupsRelationUserView> relationUserViews = groupsRelationMapper.getUserRelationUserViews(ticket.getGroupId(), ticket.getRelationId(), ticket.getUserId(), List.of(GroupsRelation.Status.validity, GroupsRelation.Status.outside));
  192. ticket.setRelationViews(relationUserViews);
  193. if (ticket.getExpiryTime() != null) {
  194. ticket.setExpiryTimeStr(DateUtil.format(ticket.getExpiryTime(), "yyyy-MM-dd"));
  195. }
  196. //查看双重验证码
  197. if (StrUtil.isNotEmpty(ticket.getApiSecret())) {
  198. Boolean isHasVerifyCode = StrUtil.isNotBlank(ticket.getVerifyCode()) ? true : false;
  199. Integer codeNum = getUserCodeNumBySkuId(ticket.getUserId(), ticket.getSkuId(), isHasVerifyCode);
  200. ticket.setCodeNum(codeNum);
  201. }
  202. });
  203. userLoginRecordService.saveUserLoginDayRecord(userId);
  204. return list;
  205. }
  206. @Override
  207. public Boolean isJoinCorpWx(Long userId, Boolean isJoin, Boolean noCoupon) {
  208. if (!isJoin) {
  209. User user = userMapper.selectById(userId);
  210. if (user == null || StrUtil.isEmpty(user.getUnionid())) {
  211. return true;
  212. }
  213. // CorpUser corpUser = corpUserMapper.selectOne(Wrappers.lambdaQuery(CorpUser.class)
  214. // .ne(CorpUser::getUnionid, "")
  215. // .eq(CorpUser::getUnionid, user.getUnionid())
  216. // .last("limit 1")
  217. // .select(CorpUser::getId)
  218. // );
  219. CorpUserFollowView corpUserFollowView = beanSearcher.searchFirst(CorpUserFollowView.class, MapUtils.builder()
  220. .field(CorpUserFollowView::getUnionId, user.getUnionid())
  221. .field(CorpUserFollowView::getStatus, 1).build());
  222. if (corpUserFollowView == null) {
  223. return false;
  224. }
  225. }
  226. //用户是否买了奈飞车票
  227. Number number = beanSearcher.searchCount(RenewalView.class, MapUtils.builder()
  228. .field(RenewalView::getUserId, userId)
  229. .field(RenewalView::getGoodsId, 1)
  230. .field(RenewalView::getStatus, List.of("validity", "overdue")).op(Operator.InList)
  231. .build());
  232. if (number.intValue() > 0 && !noCoupon) {
  233. couponCommonService.sendCouponToUser(userId, 34l, 0l, 0l, 0l, CouponUser.Channel.wxCorp);
  234. }
  235. return true;
  236. }
  237. @Override
  238. public GroupsRelation getSimilarExpiredGroupRelation(GoodsDonSku sku, List<Long> errorsRelationIds) {
  239. Long skuId = sku.getId();
  240. Integer days = sku.getDays();
  241. Integer months = sku.getMonths();
  242. Integer maxNum = sku.getNum();
  243. DateTime expiryTime = null;
  244. if (days != null && days > 0) {
  245. expiryTime = DateUtil.offsetDay(DateTime.now(), days);
  246. } else {
  247. expiryTime = DateUtil.offsetMonth(DateTime.now(), months);
  248. }
  249. Integer extraNum = 5;
  250. if (sku.getId() == 178l) {
  251. extraNum = 2;
  252. }
  253. maxNum += extraNum;
  254. DateTime minExpiryTime = DateUtil.offsetDay(expiryTime, -5);
  255. Long groupsId = groupsRelationMapper.selectSimilarExpiredRelation(skuId, minExpiryTime, maxNum);
  256. if (groupsId == null) {
  257. log.info("暂无相似过期时间车队");
  258. return null;
  259. }
  260. GroupsRelation relation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
  261. .notIn(errorsRelationIds != null && errorsRelationIds.size() > 0, GroupsRelation::getId, errorsRelationIds)
  262. .eq(GroupsRelation::getGroupsId, groupsId)
  263. .eq(GroupsRelation::getStatus, GroupsRelation.Status.none)
  264. .eq(GroupsRelation::getUserId, 0)
  265. .orderByAsc(GroupsRelation::getNum)
  266. .last("limit 1"));
  267. if (relation != null) {
  268. log.info("用户userId:{},获取相似车队的车位relationId:{}", relation.getId());
  269. }
  270. return relation;
  271. }
  272. @Override
  273. public Page<UserRecommendGoodsFrontView> getRecommendGoodsViews(Boolean isPayNf, List<Long> filter_goods_id, Integer start, Integer limit) {
  274. return groupsRelationMapper.getRecommendGoodsViews(new Page<>(start, limit),filter_goods_id, isPayNf);
  275. }
  276. @Override
  277. @Transactional(rollbackFor = Throwable.class)
  278. public void sendCourseWare(SpecialGoodsTicketReq request) throws Exception {
  279. Long userId = request.getUserId();
  280. Long relationId = request.getRelationId();
  281. String email = request.getEmail();
  282. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  283. RenewalView relation = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder().field(RenewalView::getRelationId, relationId).build());
  284. Assert.notNull(relation, "您的车票不存在");
  285. Long relationUserId = relation.getUserId();
  286. if (!userIdList.contains(relationUserId)) {
  287. throw BusinessRuntimeException.getInstance("您的车票不存在");
  288. }
  289. if (relation.getSpecialType() != GoodsDon.SpecialType.courseware) {
  290. throw BusinessRuntimeException.getInstance("");
  291. }
  292. //是否已发送
  293. if (StrUtil.isNotBlank(relation.getGrAccount())) {
  294. throw BusinessRuntimeException.getInstance("已领取课件,请前往邮箱查看");
  295. }
  296. if (!Validator.isEmail(email)) {
  297. throw BusinessRuntimeException.getInstance("请输入正确的邮箱");
  298. }
  299. int update = groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
  300. .set(GroupsRelation::getAccount, email)
  301. .and(qr -> qr.isNull(GroupsRelation::getAccount).or().eq(GroupsRelation::getAccount, StrUtil.EMPTY))
  302. .eq(GroupsRelation::getId, relationId));
  303. if (update > 0) {
  304. orderDonMapper.update(null, Wrappers.lambdaUpdate(OrderDon.class)
  305. .set(OrderDon::getStatus, OrderDon.Status.complete)
  306. .eq(OrderDon::getRelationId, relationId)
  307. .eq(OrderDon::getUserId, userId)
  308. .eq(OrderDon::getStatus, OrderDon.Status.hasPayment));
  309. mailService.sendCoursewareByGoodsId(relation.getGoodsId(), relation.getRelationId(), relation.getUserId(), relation.getTitle(), email);
  310. }
  311. }
  312. @Override
  313. public GroupsRelation getRelation(Long payRelationId, Long userId, GoodsDonSku sku) {
  314. GroupsRelation relation = null;
  315. Integer maxNum = sku.getNum();
  316. if (payRelationId != null && payRelationId > 0) {
  317. relation = groupsRelationMapper.selectById(payRelationId);
  318. if (relation == null) {
  319. throw new BusinessRuntimeException(GROUP_INDEX_NOT_FIND.getCode(), GROUP_INDEX_NOT_FIND.getMsg());
  320. }
  321. if (relation.getUserId() != null && relation.getUserId() > 0L) {
  322. throw new BusinessRuntimeException(GROUP_INDEX_ALREADY_BE_USER.getCode(), GROUP_INDEX_ALREADY_BE_USER.getMsg());
  323. }
  324. } else {
  325. GroupsTrips groups = null;
  326. //ChatGpt Plus、Midjourney非独立规格 上车先获取硬塞座位 硬塞5个座位
  327. //主账号过期时间25天以外
  328. //23-12-06 MidJourney 不硬塞
  329. if (sku.getNum() > 1 && Constant.AI_goodsIds.get(0) == sku.getGoodsId()) {
  330. Integer extraNum = 5;
  331. if (sku.getId() == 178l || sku.getId() == 423l) {
  332. extraNum = 2;
  333. }
  334. maxNum += extraNum;
  335. DateTime time = DateUtil.offsetDay(DateUtil.beginOfDay(DateTime.now()), 25);
  336. //不塞存在该用户的车队
  337. Long groupsId = groupsRelationMapper.getOutSideGroupsTripsRelationByGoodsId(sku.getId(), time, extraNum, userId, sku.getNum(), maxNum);
  338. if (groupsId != null) {
  339. relation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
  340. .eq(GroupsRelation::getGroupsId, groupsId)
  341. .eq(GroupsRelation::getUserId, 0)
  342. .eq(GroupsRelation::getStatus, GroupsRelation.Status.outside)
  343. .last("limit 1"));
  344. if (relation == null) {
  345. relation = new GroupsRelation();
  346. relation.setStatus(GroupsRelation.Status.outside);
  347. relation.setUserId(userId);
  348. relation.setGroupsId(groupsId);
  349. GroupsRelation maxNumRelation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId, groupsId).orderByDesc(GroupsRelation::getNum).last("limit 1"));
  350. Integer nextNum = maxNumRelation.getNum() + 1;
  351. if (redisService.setNx(RedisService.key.GROUPS_TRIPS_OUTSIDE_RELATION.getNameFormat(groupsId, nextNum), nextNum, RedisService.key.GROUPS_TRIPS_OUTSIDE_RELATION.getTimeout())) {
  352. relation.setNum(nextNum);
  353. groupsRelationMapper.insert(relation);
  354. } else {
  355. throw BusinessRuntimeException.getInstance("系统繁忙,请重试...");
  356. }
  357. }
  358. groups = groupsMapper.selectById(groupsId);
  359. }
  360. }
  361. if (groups == null) {
  362. //寻找差不多过期时间规格的车位
  363. relation = getSimilarExpiredGroupRelation(sku,null);
  364. if (relation == null) {
  365. //TODO 如果没指定座位,等待逻辑确认
  366. groups = groupsMapper.selectOne(Wrappers.lambdaQuery(GroupsTrips.class)
  367. .eq(GroupsTrips::getSkuId, sku.getId())
  368. .gt(GroupsTrips::getAvailableNum, 0).last("limit 1")
  369. .apply(String.format("(available_parking_num = 0 or available_parking_num > (select count(*) from groups_relation gr where gr.groups_id = groups_trips.id and gr.user_id != 0 and gr.num <= %s))", maxNum))
  370. .eq(GroupsTrips::getStatus, GroupsTrips.Status.validity)
  371. .orderByAsc(GroupsTrips::getAvailableNum));
  372. if (groups == null) {
  373. //待发车
  374. groups = groupsMapper.selectOne(Wrappers.lambdaQuery(GroupsTrips.class)
  375. .eq(GroupsTrips::getSkuId, sku.getId())
  376. .gt(GroupsTrips::getAvailableNum, 0).last("limit 1")
  377. .eq(GroupsTrips::getStatus, GroupsTrips.Status.waiting)
  378. .apply(String.format("(available_parking_num = 0 or available_parking_num > (select count(*) from groups_relation gr where gr.groups_id = groups_trips.id and gr.user_id != 0 and gr.num <= %s))", maxNum))
  379. .orderByAsc(GroupsTrips::getAvailableNum));
  380. }
  381. //新增空车队
  382. if (groups == null) {
  383. relation = groupsFuncService.createNewGroupsTrips(sku);
  384. }
  385. //获取车位
  386. if (relation == null) {
  387. //寻找快满编车队进行占座
  388. relation = groupsRelationMapper.selectRandomOneRelationByGroupsId(groups.getId());
  389. if (relation == null) {
  390. Long groupsId = groups.getId();
  391. TASK_EXECUTOR.execute(() -> groupsMapper.updateAvailableNum(groupsId));
  392. throw BusinessRuntimeException.getInstance("系统繁忙,请重试...");
  393. }
  394. }
  395. }
  396. }
  397. if (groups == null) {
  398. groups = groupsMapper.selectById(relation.getGroupsId());
  399. }
  400. //车队目前可用停车数
  401. relation.setGroupsAvailParkingNum(groups.getAvailableParkingNum());
  402. relation.setMaxNum(maxNum);
  403. }
  404. return relation;
  405. }
  406. @Override
  407. public String getAiVerifyCode(Long userId, Long relationId, Integer type) throws Exception {
  408. List<Long> relationUserIdList = userBindRelationService.getRelationUserIdList(userId, null);
  409. RenewalView renewalView = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder().field(RenewalView::getUserId, relationUserIdList).op(Operator.InList)
  410. .field(RenewalView::getRelationId, relationId).build());
  411. if (renewalView == null) {
  412. throw BusinessRuntimeException.getInstance("您的车票不存在...");
  413. }
  414. if (StrUtil.isEmpty(renewalView.getAccount())) {
  415. throw BusinessRuntimeException.getInstance("车票未配置账号");
  416. }
  417. String account = renewalView.getAccount();
  418. String password = renewalView.getPassword();
  419. if (StrUtil.hasEmpty(account, password)) {
  420. throw BusinessRuntimeException.getInstance("您的车票账号密码异常,请联系客服...");
  421. }
  422. //获取邮箱密码
  423. SysEmail sysEmail = sysEmailMapper.selectOne(Wrappers.lambdaQuery(SysEmail.class)
  424. .eq(SysEmail::getEmail, account)
  425. .last("limit 1"));
  426. if (sysEmail == null) {
  427. log.info("获取账号:{}验证码异常,未配置对应系统邮箱", account);
  428. throw BusinessRuntimeException.getInstance("获取账号验证码异常,请联系客服...");
  429. }
  430. String url = "http://yinhelx.com/api/controller.php";
  431. Map<String, Object> params = new HashMap<>();
  432. params.put("imap_key", "imap_56568niua@@");
  433. params.put("fun", "imap_read");
  434. params.put("imap_ip", "yinhelx.com");
  435. params.put("port", "143");
  436. params.put("username", account);
  437. params.put("password", sysEmail.getPassword());
  438. params.put("mbox_id", "0");
  439. params.put("delete", "0");
  440. HttpRequest post = HttpUtil.createPost(url);
  441. post.form(params);
  442. cn.hutool.http.HttpResponse execute = post.execute();
  443. if (execute.getStatus() != HttpStatus.HTTP_OK) {
  444. throw BusinessRuntimeException.getInstance("获取验证码异常,请联系客服...");
  445. }
  446. String code = getDifferentGoodsCode(renewalView.getGoodsId(), execute.body(), type);
  447. GroupsRelationLoginCodeRecord record = new GroupsRelationLoginCodeRecord();
  448. record.setUserId(userId);
  449. record.setRelationId(relationId);
  450. record.setAccount(renewalView.getAccount());
  451. record.setGoodsId(renewalView.getGoodsId());
  452. record.setSkuId(renewalView.getSkuId());
  453. groupsRelationLoginCodeRecordMapper.insert(record);
  454. return code;
  455. }
  456. @Override
  457. public void checkGroupsAvailParkingNum(Long skuId, Long groupsId, Integer availableParkingNum, Integer maxNum) {
  458. //GPT10人月付、4人月付
  459. if (skuId == 161l || skuId == 178l) {
  460. //为0时,不参与限制
  461. if (availableParkingNum == 0) {
  462. return;
  463. }
  464. //目前车队可用停车数
  465. //目前上车人数
  466. Integer currentParkingNum = groupsRelationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
  467. .eq(GroupsRelation::getGroupsId, groupsId)
  468. .ne(GroupsRelation::getUserId, 0)
  469. .le(GroupsRelation::getNum, maxNum));
  470. if (currentParkingNum > availableParkingNum) {
  471. throw BusinessRuntimeException.getInstance("系统繁忙,请重试...");
  472. }
  473. }
  474. }
  475. @Override
  476. public void adjustGroupsParkingTime(Long relationId, GoodsDonSku sku) {
  477. Long skuId = sku.getId();
  478. //更新GPT Plus10人、4人月付车队状态 待优化
  479. if (skuId == 161l || skuId == 178l || skuId == 422l || skuId == 423l) {
  480. GroupsRelation relation = groupsRelationMapper.selectById(relationId);
  481. Long groupsId = relation.getGroupsId();
  482. GroupsTrips groupsTrips = groupsMapper.selectById(groupsId);
  483. Integer availableParkingNum = groupsTrips.getAvailableParkingNum();
  484. //为0时 不参与限制
  485. if (availableParkingNum == 0) {
  486. return;
  487. }
  488. Integer extraNum = 5;
  489. if (skuId == 178l || skuId == 423l) {
  490. extraNum = 2;
  491. }
  492. Integer maxNum = sku.getNum() + extraNum;
  493. if (availableParkingNum >= maxNum) {
  494. return;
  495. }
  496. //目前上车人数
  497. Integer currentParkingNum = groupsRelationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
  498. .eq(GroupsRelation::getGroupsId, groupsId)
  499. .ne(GroupsRelation::getUserId, 0)
  500. .le(GroupsRelation::getNum, maxNum));
  501. if (currentParkingNum >= availableParkingNum) {
  502. //锁住
  503. DateTime parkingTime = DateUtil.offsetDay(DateTime.now(), 1);
  504. groupsMapper.update(null, Wrappers.lambdaUpdate(GroupsTrips.class)
  505. .eq(GroupsTrips::getId, groupsTrips.getId())
  506. .set(GroupsTrips::getParkingTime, parkingTime)
  507. .isNull(GroupsTrips::getParkingTime));
  508. }
  509. }
  510. }
  511. @Override
  512. public void setGroupsTripsParkingTimeEmpty(GoodsDonSku sku, Long relationId) {
  513. Long skuId = sku.getId();
  514. if (skuId == 161l || skuId == 178l) {
  515. GroupsRelation relation = groupsRelationMapper.selectById(relationId);
  516. Long groupsId = relation.getGroupsId();
  517. GroupsTrips groupsTrips = groupsMapper.selectById(groupsId);
  518. Integer availableParkingNum = groupsTrips.getAvailableParkingNum();
  519. if (groupsTrips.getParkingTime() == null || availableParkingNum == 0) {
  520. return;
  521. }
  522. Integer extraNum = 5;
  523. if (skuId == 178l) {
  524. extraNum = 2;
  525. }
  526. Integer skuNum = sku.getNum();
  527. if (availableParkingNum >= skuNum + extraNum) {
  528. return;
  529. }
  530. //目前上车人数
  531. Integer currentParkingNum = groupsRelationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
  532. .eq(GroupsRelation::getGroupsId, groupsId)
  533. .ne(GroupsRelation::getUserId, 0));
  534. if (currentParkingNum < availableParkingNum) {
  535. groupsMapper.update(null, Wrappers.lambdaUpdate(GroupsTrips.class)
  536. .eq(GroupsTrips::getId, groupsTrips.getId())
  537. .set(GroupsTrips::getParkingTime, null)
  538. .isNotNull(GroupsTrips::getParkingTime));
  539. }
  540. }
  541. }
  542. @Override
  543. public Integer getUserCodeNumBySkuId(Long userId, Long skuId, Boolean isHasVerifyCode) {
  544. //查看双重验证码
  545. UserFuncProperty userFuncProperty = userFuncPropertyMapper.selectOne(Wrappers.lambdaQuery(UserFuncProperty.class).eq(UserFuncProperty::getUserId, userId).last("limit 1"));
  546. //默认0次
  547. int num = 0;
  548. //有安全码查看次数 置为0
  549. if (isHasVerifyCode) {
  550. return num;
  551. }
  552. //查看双重验证码次数
  553. GoodsDonSku sku = goodsDonSkuMapper.selectById(skuId);
  554. if (sku.getCodeNum() > 0) {
  555. num = sku.getCodeNum();
  556. }
  557. if (userFuncProperty != null) num = userFuncProperty.getVerifyCheckNum();
  558. return num;
  559. }
  560. @Override
  561. public List<RenewalView> getHasPayGoods(long userId) {
  562. User user = userMapper.selectById(userId);
  563. List<Long> userIds = userBindRelationService.getRelationUserIdList(userId, user);
  564. DateTime now = DateTime.now();
  565. List<RenewalView> list = groupsRelationMapper.getUserHasPayGoods(userIds, now);
  566. return list;
  567. }
  568. public String getDifferentGoodsCode(Long goodsId, String body, Integer type) {
  569. String code = null;
  570. //奈飞
  571. if (goodsId == 1) {
  572. if (body.contains("輸入此代碼登入")) {
  573. code = StrUtil.subBetween(body, "輸入此代碼登入", "請在裝置上輸入上");
  574. } else {
  575. String urlPref = "https://www.netflix.com/account/update-primary-location";
  576. code = StrUtil.subBetween(body, "[" + urlPref, "]");
  577. if (StrUtil.isEmpty(code)) {
  578. urlPref = "https://www.netflix.com/account/travel/verify";
  579. code = StrUtil.subBetween(body, "[" + urlPref, "]");
  580. if (StrUtil.isEmpty(code) && type == null) {
  581. throw BusinessRuntimeException.getInstance("获取认证链接失败,请重新获取");
  582. }
  583. }
  584. if (StrUtil.isNotBlank(code)) {
  585. return urlPref + code;
  586. }
  587. }
  588. if (type != null && type == 1) {
  589. throw BusinessRuntimeException.getInstance("查询失败,请检查是否发送了登陆码..");
  590. }
  591. }
  592. //迪士尼
  593. if (goodsId == 3) {
  594. int index = body.indexOf("otp_code");
  595. if (index == -1) {
  596. index = body.indexOf(" 15 ");
  597. }
  598. code = StrUtil.subWithLength(body, index, 215);
  599. }
  600. //PS
  601. if (goodsId == 33l) {
  602. code = StrUtil.subWithLength(body, body.indexOf("ADOBE"), 215);
  603. }
  604. code = getVerifyCode(code);
  605. if (!Validator.isNumber(code)) {
  606. log.error("获取账号验证码失败:{}", StrUtil.subSufByLength(code, 512));
  607. throw BusinessRuntimeException.getInstance("获取验证码失败,请重新获取..");
  608. }
  609. return code.trim();
  610. }
  611. public String getVerifyCode(String code) {
  612. if (StrUtil.isNotEmpty(code)) {
  613. Pattern pattern = Pattern.compile("\\d{4,}");
  614. Matcher matcher = pattern.matcher(code);
  615. while (matcher.find()) {
  616. return matcher.group();
  617. }
  618. }
  619. return StrUtil.EMPTY;
  620. }
  621. }