GroupRelationFrontServiceImpl.java 25 KB

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