GroupRelationFrontServiceImpl.java 23 KB

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