GroupRelationFrontServiceImpl.java 26 KB

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