GroupRelationFrontServiceImpl.java 21 KB

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