GroupRelationFrontServiceImpl.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. //23-12-06 MidJourney 不硬塞
  308. if (sku.getNum() > 1 && Constant.AI_goodsIds.get(0) == sku.getGoodsId()) {
  309. Integer extraNum = 5;
  310. if (sku.getId() == 178l) {
  311. extraNum = 2;
  312. }
  313. maxNum += extraNum;
  314. DateTime time = DateUtil.offsetDay(DateUtil.beginOfDay(DateTime.now()), 25);
  315. //不塞存在该用户的车队
  316. Long groupsId = groupsRelationMapper.getOutSideGroupsTripsRelationByGoodsId(sku.getId(), time, extraNum, userId, sku.getNum(), maxNum);
  317. if (groupsId != null) {
  318. relation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
  319. .eq(GroupsRelation::getGroupsId, groupsId)
  320. .eq(GroupsRelation::getUserId, 0)
  321. .eq(GroupsRelation::getStatus, GroupsRelation.Status.outside)
  322. .last("limit 1"));
  323. if (relation == null) {
  324. relation = new GroupsRelation();
  325. relation.setStatus(GroupsRelation.Status.outside);
  326. relation.setUserId(userId);
  327. relation.setGroupsId(groupsId);
  328. GroupsRelation maxNumRelation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId, groupsId).orderByDesc(GroupsRelation::getNum).last("limit 1"));
  329. Integer nextNum = maxNumRelation.getNum() + 1;
  330. if (redisService.setNx(RedisService.key.GROUPS_TRIPS_OUTSIDE_RELATION.getNameFormat(groupsId, nextNum), nextNum, RedisService.key.GROUPS_TRIPS_OUTSIDE_RELATION.getTimeout())) {
  331. relation.setNum(nextNum);
  332. groupsRelationMapper.insert(relation);
  333. } else {
  334. throw BusinessRuntimeException.getInstance("系统繁忙,请重试...");
  335. }
  336. }
  337. groups = groupsMapper.selectById(groupsId);
  338. }
  339. }
  340. if (groups == null) {
  341. //寻找差不多过期时间规格的车位
  342. relation = getSimilarExpiredGroupRelation(sku,null);
  343. if (relation == null) {
  344. //TODO 如果没指定座位,等待逻辑确认
  345. groups = groupsMapper.selectOne(Wrappers.lambdaQuery(GroupsTrips.class)
  346. .eq(GroupsTrips::getSkuId, sku.getId())
  347. .gt(GroupsTrips::getAvailableNum, 0).last("limit 1")
  348. .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))
  349. .eq(GroupsTrips::getStatus, GroupsTrips.Status.validity)
  350. .orderByAsc(GroupsTrips::getAvailableNum));
  351. if (groups == null) {
  352. //待发车
  353. groups = groupsMapper.selectOne(Wrappers.lambdaQuery(GroupsTrips.class)
  354. .eq(GroupsTrips::getSkuId, sku.getId())
  355. .gt(GroupsTrips::getAvailableNum, 0).last("limit 1")
  356. .eq(GroupsTrips::getStatus, GroupsTrips.Status.waiting)
  357. .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))
  358. .orderByAsc(GroupsTrips::getAvailableNum));
  359. }
  360. //新增空车队
  361. if (groups == null) {
  362. relation = groupsFuncService.createNewGroupsTrips(sku);
  363. }
  364. //获取车位
  365. if (relation == null) {
  366. //寻找快满编车队进行占座
  367. relation = groupsRelationMapper.selectRandomOneRelationByGroupsId(groups.getId());
  368. if (relation == null) {
  369. Long groupsId = groups.getId();
  370. TASK_EXECUTOR.execute(() -> groupsMapper.updateAvailableNum(groupsId));
  371. throw BusinessRuntimeException.getInstance("系统繁忙,请重试...");
  372. }
  373. }
  374. }
  375. }
  376. if (groups == null) {
  377. groups = groupsMapper.selectById(relation.getGroupsId());
  378. }
  379. //车队目前可用停车数
  380. relation.setGroupsAvailParkingNum(groups.getAvailableParkingNum());
  381. relation.setMaxNum(maxNum);
  382. }
  383. return relation;
  384. }
  385. @Override
  386. public String getAiVerifyCode(Long userId, Long relationId) throws Exception {
  387. List<Long> relationUserIdList = userBindRelationService.getRelationUserIdList(userId, null);
  388. RenewalView renewalView = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder().field(RenewalView::getUserId, relationUserIdList).op(Operator.InList)
  389. .field(RenewalView::getRelationId, relationId).build());
  390. if (renewalView == null) {
  391. throw BusinessRuntimeException.getInstance("您的车票不存在...");
  392. }
  393. if (StrUtil.isEmpty(renewalView.getAccount())) {
  394. throw BusinessRuntimeException.getInstance("车票未配置账号");
  395. }
  396. String account = renewalView.getAccount();
  397. String password = renewalView.getPassword();
  398. if (StrUtil.hasEmpty(account, password)) {
  399. throw BusinessRuntimeException.getInstance("您的车票账号密码异常,请联系客服...");
  400. }
  401. //获取邮箱密码
  402. SysEmail sysEmail = sysEmailMapper.selectOne(Wrappers.lambdaQuery(SysEmail.class)
  403. .eq(SysEmail::getEmail, account)
  404. .last("limit 1"));
  405. if (sysEmail == null) {
  406. log.info("获取账号:{}验证码异常,未配置对应系统邮箱", account);
  407. throw BusinessRuntimeException.getInstance("获取账号验证码异常,请联系客服...");
  408. }
  409. String url = "http://yinhelx.com/api/controller.php";
  410. Map<String,Object> params = new HashMap<>();
  411. params.put("imap_key", "imap_56568niua@@");
  412. params.put("fun", "imap_read");
  413. params.put("imap_ip", "yinhelx.com");
  414. params.put("port", "143");
  415. params.put("username", account);
  416. params.put("password", sysEmail.getPassword());
  417. params.put("mbox_id", "0");
  418. params.put("delete", "0");
  419. HttpRequest post = HttpUtil.createPost(url);
  420. post.form(params);
  421. cn.hutool.http.HttpResponse execute = post.execute();
  422. if (execute.getStatus() != HttpStatus.HTTP_OK) {
  423. throw BusinessRuntimeException.getInstance("获取验证码异常,请联系客服...");
  424. }
  425. String code = getDifferentGoodsCode(renewalView.getGoodsId(), execute.body());
  426. GroupsRelationLoginCodeRecord record = new GroupsRelationLoginCodeRecord();
  427. record.setUserId(userId);
  428. record.setRelationId(relationId);
  429. record.setAccount(renewalView.getAccount());
  430. record.setGoodsId(renewalView.getGoodsId());
  431. record.setSkuId(renewalView.getSkuId());
  432. groupsRelationLoginCodeRecordMapper.insert(record);
  433. return code;
  434. }
  435. @Override
  436. public void checkGroupsAvailParkingNum(Long skuId, Long groupsId, Integer availableParkingNum, Integer maxNum) {
  437. //GPT10人月付、4人月付
  438. if (skuId == 161l || skuId == 178l) {
  439. //为0时,不参与限制
  440. if (availableParkingNum == 0) {
  441. return;
  442. }
  443. //目前车队可用停车数
  444. //目前上车人数
  445. Integer currentParkingNum = groupsRelationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
  446. .eq(GroupsRelation::getGroupsId, groupsId)
  447. .ne(GroupsRelation::getUserId, 0)
  448. .le(GroupsRelation::getNum, maxNum));
  449. if (currentParkingNum > availableParkingNum) {
  450. throw BusinessRuntimeException.getInstance("系统繁忙,请重试...");
  451. }
  452. }
  453. }
  454. @Override
  455. public void adjustGroupsParkingTime(Long relationId, GoodsDonSku sku) {
  456. Long skuId = sku.getId();
  457. //更新GPT Plus10人、4人月付车队状态
  458. if (skuId == 161l || skuId == 178l) {
  459. GroupsRelation relation = groupsRelationMapper.selectById(relationId);
  460. Long groupsId = relation.getGroupsId();
  461. GroupsTrips groupsTrips = groupsMapper.selectById(groupsId);
  462. Integer availableParkingNum = groupsTrips.getAvailableParkingNum();
  463. //为0时 不参与限制
  464. if (availableParkingNum == 0) {
  465. return;
  466. }
  467. Integer extraNum = 5;
  468. if (skuId == 178l) {
  469. extraNum = 2;
  470. }
  471. Integer maxNum = sku.getNum() + extraNum;
  472. if (availableParkingNum >= maxNum) {
  473. return;
  474. }
  475. //目前上车人数
  476. Integer currentParkingNum = groupsRelationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
  477. .eq(GroupsRelation::getGroupsId, groupsId)
  478. .ne(GroupsRelation::getUserId, 0)
  479. .le(GroupsRelation::getNum, maxNum));
  480. if (currentParkingNum >= availableParkingNum) {
  481. //锁住
  482. DateTime parkingTime = DateUtil.offsetDay(DateTime.now(), 1);
  483. groupsMapper.update(null, Wrappers.lambdaUpdate(GroupsTrips.class)
  484. .eq(GroupsTrips::getId, groupsTrips.getId())
  485. .set(GroupsTrips::getParkingTime, parkingTime)
  486. .isNull(GroupsTrips::getParkingTime));
  487. }
  488. }
  489. }
  490. @Override
  491. public void setGroupsTripsParkingTimeEmpty(GoodsDonSku sku, Long relationId) {
  492. Long skuId = sku.getId();
  493. if (skuId == 161l || skuId == 178l) {
  494. GroupsRelation relation = groupsRelationMapper.selectById(relationId);
  495. Long groupsId = relation.getGroupsId();
  496. GroupsTrips groupsTrips = groupsMapper.selectById(groupsId);
  497. Integer availableParkingNum = groupsTrips.getAvailableParkingNum();
  498. if (groupsTrips.getParkingTime() == null || availableParkingNum == 0) {
  499. return;
  500. }
  501. Integer extraNum = 5;
  502. if (skuId == 178l) {
  503. extraNum = 2;
  504. }
  505. Integer skuNum = sku.getNum();
  506. if (availableParkingNum >= skuNum + extraNum) {
  507. return;
  508. }
  509. //目前上车人数
  510. Integer currentParkingNum = groupsRelationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
  511. .eq(GroupsRelation::getGroupsId, groupsId)
  512. .ne(GroupsRelation::getUserId, 0));
  513. if (currentParkingNum < availableParkingNum) {
  514. groupsMapper.update(null, Wrappers.lambdaUpdate(GroupsTrips.class)
  515. .eq(GroupsTrips::getId, groupsTrips.getId())
  516. .set(GroupsTrips::getParkingTime, null)
  517. .isNotNull(GroupsTrips::getParkingTime));
  518. }
  519. }
  520. }
  521. public String getDifferentGoodsCode(Long goodsId, String body) {
  522. String code = null;
  523. //奈飞
  524. if (goodsId == 1) {
  525. code = StrUtil.subBetween(body, "Doğrulama kodunuz:", "Bu istek sizin");
  526. }
  527. //迪士尼
  528. if (goodsId == 3) {
  529. code = StrUtil.subWithLength(body, body.indexOf(" 15 "), 215);
  530. if (StrUtil.isNotEmpty(code)) {
  531. Pattern pattern = Pattern.compile("\\d{6}");
  532. Matcher matcher = pattern.matcher(code);
  533. while (matcher.find()) {
  534. code = matcher.group();
  535. break;
  536. }
  537. }
  538. }
  539. if (!Validator.isNumber(code)) {
  540. throw BusinessRuntimeException.getInstance("获取验证码失败,请重新获取");
  541. }
  542. if (StrUtil.isEmpty(code)) {
  543. log.error("获取账号验证码失败:{}", StrUtil.subSufByLength(code, 512));
  544. throw BusinessRuntimeException.getInstance("获取验证码失败,请重新获取...");
  545. }
  546. return code.trim();
  547. }
  548. }