GroupRelationFrontServiceImpl.java 28 KB

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