GroupRelationFrontServiceImpl.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.cyksj.service.relation.impl;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.cyksj.common.exception.BusinessRuntimeException;
  7. import com.cyksj.enums.GatewayApiCode;
  8. import com.cyksj.mapper.GroupsRelationMapper;
  9. import com.cyksj.mapper.UserMapper;
  10. import com.cyksj.mapper.corp.CorpUserMapper;
  11. import com.cyksj.model.entity.*;
  12. import com.cyksj.model.views.GoodsDonViews;
  13. import com.cyksj.model.views.RenewalView;
  14. import com.cyksj.service.mange.coupon.CouponCommonService;
  15. import com.cyksj.service.relation.GroupRelationFrontService;
  16. import com.cyksj.service.user.UserService;
  17. import com.ejlchina.searcher.BeanSearcher;
  18. import com.ejlchina.searcher.param.Operator;
  19. import com.ejlchina.searcher.util.MapUtils;
  20. import lombok.RequiredArgsConstructor;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.springframework.stereotype.Service;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.stream.Collectors;
  26. /*
  27. *项目名: netflix
  28. *文件名: GroupRelationFrontServiceImpl
  29. *创建者: JavaZou
  30. *创建时间:2022/12/29 15:42
  31. */
  32. @Service
  33. @RequiredArgsConstructor
  34. @Slf4j
  35. public class GroupRelationFrontServiceImpl implements GroupRelationFrontService {
  36. private final UserService userService;
  37. private final UserMapper userMapper;
  38. private final BeanSearcher beanSearcher;
  39. private final CorpUserMapper corpUserMapper;
  40. private final CouponCommonService couponCommonService;
  41. private final GroupsRelationMapper groupsRelationMapper;
  42. @Override
  43. public List<RenewalView> getMyTicket(long userId, Boolean isLoginPopularize) {
  44. User u = userMapper.selectById(userId);
  45. if (u == null) throw BusinessRuntimeException.getGatewayApiCode(GatewayApiCode.CMS_TOKEN_VALID);
  46. if (u.getIsBlack()) {
  47. return new ArrayList<>();
  48. }
  49. List<Long> userIds = userService.getRelationUserIdList(userId, u);
  50. List<RenewalView> list = beanSearcher.searchAll(RenewalView.class, MapUtils.builder()
  51. .field(RenewalView::getUserId, 0).op(Operator.GreaterThan)
  52. .field(RenewalView::getUserId, userIds).op(Operator.InList)
  53. .field(RenewalView::getStatus, List.of("validity", "overdue")).op(Operator.InList)
  54. .orderBy(RenewalView::getExpiryTime).asc()
  55. .build());
  56. //过滤邀请制、已失效的车位
  57. DateTime now = DateTime.now();
  58. List<RenewalView> collect = list.stream().filter(ticket -> {
  59. if (ticket.getExpiryTime() != null && ticket.getExpiryTime().before(now)) {
  60. return false;
  61. }
  62. if (ticket.getSecondType() == 1) {
  63. ticket.setAccount(null);
  64. ticket.setPassword(null);
  65. }
  66. if (isLoginPopularize != null && isLoginPopularize) {
  67. GoodsDonViews views = beanSearcher.searchFirst(GoodsDonViews.class, MapUtils.builder().field(GoodsDonViews::getId, ticket.getGoodsId()).build());
  68. views.setSpecs(beanSearcher.searchFirst(GoodsDonSpec.class, MapUtils.builder().field(GoodsDonSpec::getGoodsId, views.getId()).build()));
  69. views.setSkuList(beanSearcher.searchAll(GoodsDonSku.class, MapUtils.builder().field(GoodsDonSku::getGoodsId, views.getId()).field(GoodsDonSku::getStatus, true).build()));
  70. ticket.setGoodsDonViews(views);
  71. }
  72. return true;
  73. }).collect(Collectors.toList());
  74. return collect;
  75. }
  76. @Override
  77. public Boolean isJoinCorpWx(Long userId, Boolean isJoin, Boolean noCoupon) {
  78. if (!isJoin) {
  79. User user = userMapper.selectById(userId);
  80. if (user == null || StrUtil.isEmpty(user.getUnionid())) {
  81. return true;
  82. }
  83. CorpUser corpUser = corpUserMapper.selectOne(Wrappers.lambdaQuery(CorpUser.class)
  84. .ne(CorpUser::getUnionid, "")
  85. .eq(CorpUser::getUnionid, user.getUnionid())
  86. .last("limit 1")
  87. .select(CorpUser::getId)
  88. );
  89. if (corpUser == null) {
  90. return false;
  91. }
  92. }
  93. //用户是否买了奈飞车票
  94. Number number = beanSearcher.searchCount(RenewalView.class, MapUtils.builder()
  95. .field(RenewalView::getUserId, userId)
  96. .field(RenewalView::getGoodsId, 1)
  97. .field(RenewalView::getStatus, List.of("validity", "overdue")).op(Operator.InList)
  98. .build());
  99. if (number.intValue() > 0 && !noCoupon) {
  100. couponCommonService.sendCouponToUser(userId, 34l, 0l, 0l, 0l, CouponUser.Channel.wxCorp);
  101. }
  102. return true;
  103. }
  104. @Override
  105. public GroupsRelation getSimilarExpiredGroupRelation(Long skuId, Integer months, List<Long> errorsRelationIds) {
  106. DateTime expiryTime = DateUtil.offsetMonth(DateTime.now(), months);
  107. DateTime minExpiryTime = DateUtil.offsetDay(expiryTime, -5);
  108. Long groupsId = groupsRelationMapper.selectSimilarExpiredRelation(skuId, minExpiryTime);
  109. if (groupsId == null) {
  110. log.info("暂无相似过期时间车队");
  111. return null;
  112. }
  113. GroupsRelation relation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
  114. .notIn(errorsRelationIds != null && errorsRelationIds.size() > 0, GroupsRelation::getId, errorsRelationIds)
  115. .eq(GroupsRelation::getGroupsId, groupsId)
  116. .eq(GroupsRelation::getStatus, GroupsRelation.Status.none)
  117. .eq(GroupsRelation::getUserId, 0)
  118. .orderByAsc(GroupsRelation::getNum)
  119. .last("limit 1"));
  120. if (relation != null) {
  121. log.info("用户userId:{},获取相似车队的车位relationId:{}", relation.getId());
  122. }
  123. return relation;
  124. }
  125. }