Bladeren bron

奈飞更换车票

zoujiajian 1 jaar geleden
bovenliggende
commit
9ef4883bde

+ 2 - 0
netflix-dao/src/main/java/com/cyksj/mapper/GroupsMapper.java

@@ -43,4 +43,6 @@ public interface GroupsMapper extends BaseMapper<GroupsTrips> {
 	GroupsTrips selectTicketGroups(@Param("skuId") Long skuId, @Param("status") GroupsTrips.Status status, @Param("maxNum") Integer maxNum, @Param("ip") String ip);
 
 	GroupsTrips selectCollegeStudentUserGptTicket(Long skuId);
+
+	Long selectRandomGroupsTripsBySkuId(@Param("groupId") Long groupId, @Param("skuId") Long skuId, @Param("expiryTime") Date expiryTime);
 }

+ 12 - 0
netflix-dao/src/main/resources/mapper/GroupsMapper.xml

@@ -107,4 +107,16 @@
           and gt.available_num > 0
         order by rand() limit 1
     </select>
+
+    <select id="selectRandomGroupsTripsBySkuId" resultType="java.lang.Long">
+        select gt.id
+        from `groups_trips` gt
+                 inner join account a on a.id = gt.account_id
+        where gt.id != #{groupsId}
+          and gt.sku_id = #{skuId}
+          and gt.status = 'validity'
+          and a.expiry_time >= #{expiryTime}
+          and gt.available_num > 0
+        order by a.expiry_time asc limit 1
+    </select>
 </mapper>

+ 2 - 0
netflix-service/src/main/java/com/cyksj/service/relation/GroupRelationFrontService.java

@@ -68,4 +68,6 @@ public interface GroupRelationFrontService {
 	String getNfHouseholdDevice(long userId, Long relationId) throws Exception;
 
 	String getVerifyCode(String account) throws Exception;
+
+	void changeTicket(long userId, Long relationId);
 }

+ 16 - 0
netflix-service/src/main/java/com/cyksj/service/relation/GroupsRelationChangeService.java

@@ -0,0 +1,16 @@
+package com.cyksj.service.relation;
+
+import com.cyksj.model.entity.GroupsRelation;
+
+/**
+ * 项目名: yhlxj2
+ * 文件名: GroupsRelationChangeService
+ * 创建者: JavaZou
+ * 创建时间:2024/10/28 14:14
+ */
+public interface GroupsRelationChangeService {
+
+	GroupsRelation changeTicket(Long userId, Long oriRelationId, Long itemGroupsId, Long skuId);
+
+	GroupsRelation getEmptyGroupsRelation(Long userId, Long oriRelationId, Long skuId);
+}

+ 36 - 0
netflix-service/src/main/java/com/cyksj/service/relation/impl/GroupRelationFrontServiceImpl.java

@@ -40,6 +40,7 @@ import com.cyksj.service.mail.YhMailService;
 import com.cyksj.service.mange.coupon.CouponCommonService;
 import com.cyksj.service.midjourney.MidjourneyAccountService;
 import com.cyksj.service.relation.GroupRelationFrontService;
+import com.cyksj.service.relation.GroupsRelationChangeService;
 import com.cyksj.service.relation.GroupsRelationNetflixService;
 import com.cyksj.service.user.UserBindRelationService;
 import com.cyksj.service.user.UserLoginRecordService;
@@ -157,6 +158,8 @@ public class GroupRelationFrontServiceImpl implements GroupRelationFrontService
 
 	private final UserBenefitsMapper userBenefitsMapper;
 
+	private final GroupsRelationChangeService changeTickerRelation;
+
 	@Override
 	public SearchResult<RenewalView> getMyTicket(long userId, Boolean isLoginPopularize, String customId, String account, String orderNo) {
 		User u = userMapper.selectById(userId);
@@ -998,6 +1001,39 @@ public class GroupRelationFrontServiceImpl implements GroupRelationFrontService
 		return code;
 	}
 
+	@Override
+	public void changeTicket(long userId, Long relationId) {
+		List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
+		RenewalView renewalView = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder()
+				.field(RenewalView::getUserId, userIdList).op(Operator.InList)
+				.field(RenewalView::getRelationId, relationId)
+				.field(RenewalView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
+				.build());
+		if (renewalView == null) {
+			throw BusinessRuntimeException.getInstance("车票不存在");
+		}
+		if (renewalView.getGoodsId() != 1) {
+			throw BusinessRuntimeException.getInstance("只针对奈飞车票");
+		}
+		Long groupId = renewalView.getGroupId();
+		Long skuId = renewalView.getSkuId();
+		Date expiryTime = renewalView.getExpiryTime();
+		//随机选择同规格得车票
+		Long item_groupsId = groupsMapper.selectRandomGroupsTripsBySkuId(groupId, skuId, expiryTime);
+		GroupsRelation newRelation = null;
+		if (item_groupsId != null) {
+			//存在就随机分配一个,没有就不做处理
+			newRelation = changeTickerRelation.changeTicket(renewalView.getUserId(), relationId, item_groupsId, renewalView.getSkuId());
+		}
+		//若无移至空车队
+		if (newRelation == null) {
+			newRelation = changeTickerRelation.getEmptyGroupsRelation(renewalView.getRelationId(), renewalView.getUserId(), renewalView.getSkuId());
+		}
+		if (newRelation == null) {
+			throw BusinessRuntimeException.getInstance("更换异常,请重新提交");
+		}
+	}
+
 	private DistributePosterGoodsDetailView setGoodsPoster(RenewalView ticket, User user, List<Long> userIds) {
 		DistributePosterGoodsDetailView distributePosterGoodsDetailView = beanSearcher.searchFirst(DistributePosterGoodsDetailView.class, MapUtils.flatBuilder(request.getParameterMap())
 				.field(DistributePosterGoodsFrontView::getGoodsId, ticket.getGoodsId()).build());

+ 131 - 0
netflix-service/src/main/java/com/cyksj/service/relation/impl/GroupsRelationChangeServiceImpl.java

@@ -0,0 +1,131 @@
+package com.cyksj.service.relation.impl;
+
+import cn.hutool.core.bean.BeanUtil;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.cyksj.common.constant.Constant;
+import com.cyksj.dto.RedisKey;
+import com.cyksj.mapper.GoodsDonSkuMapper;
+import com.cyksj.mapper.GroupsMapper;
+import com.cyksj.mapper.GroupsRelationMapper;
+import com.cyksj.mapper.OrderDonMapper;
+import com.cyksj.model.entity.GoodsDonSku;
+import com.cyksj.model.entity.GroupsRelation;
+import com.cyksj.model.entity.GroupsTrips;
+import com.cyksj.model.entity.OrderDon;
+import com.cyksj.redis.RedisService;
+import com.cyksj.service.groups.GroupsFuncService;
+import com.cyksj.service.relation.GroupsRelationChangeService;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+
+/**
+ * 项目名: yhlxj2
+ * 文件名: GroupsRelationChangeServiceImpl
+ * 创建者: JavaZou
+ * 创建时间:2024/10/28 14:15
+ */
+@Service
+@RequiredArgsConstructor
+@Slf4j
+public class GroupsRelationChangeServiceImpl implements GroupsRelationChangeService {
+	private final GroupsRelationMapper relationMapper;
+
+	private final RedisService redisService;
+
+	private final GroupsMapper groupsMapper;
+
+	private final GroupsFuncService groupsFuncService;
+
+	private final GoodsDonSkuMapper skuMapper;
+
+	private final OrderDonMapper orderDonMapper;
+
+	@Override
+	public GroupsRelation changeTicket(Long userId, Long oriRelationId, Long itemGroupsId, Long skuId) {
+		GroupsRelation relation = getRelationThisTrips(userId, itemGroupsId, 1);
+		if (relation == null) {
+			return relation;
+		}
+		syncGroupsRelation(oriRelationId, relation);
+		return relation;
+	}
+
+	@Override
+	public GroupsRelation getEmptyGroupsRelation(Long userId, Long oriRelationId, Long skuId) {
+		return getEmptyGroupsRelationIfRetry(userId, oriRelationId, skuId, 1);
+	}
+
+	public GroupsRelation getEmptyGroupsRelationIfRetry(Long userId, Long oriRelationId, Long skuId, Integer retryNum) {
+		GroupsTrips groupsTrips = groupsMapper.selectOne(Wrappers.lambdaQuery(GroupsTrips.class)
+				.eq(GroupsTrips::getStatus, GroupsTrips.Status.waiting)
+				.eq(GroupsTrips::getSkuId, skuId)
+				.last("limit 1"));
+		GroupsRelation relation;
+		if (groupsTrips != null) {
+			relation = relationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
+					.eq(GroupsRelation::getGroupsId, groupsTrips.getId())
+					.eq(GroupsRelation::getUserId, 0)
+					.last("limit 1"));
+		} else {
+			//新增空车队
+			GoodsDonSku sku = skuMapper.selectById(skuId);
+			relation = groupsFuncService.createNewGroupsTrips(sku);
+		}
+		if (relation == null) {
+			return null;
+		}
+		if (!redisService.setNx(RedisKey.GROUPS_RELATION_NUM_KEY + relation.getId(), userId, 60 * 5L)) {
+			retryNum++;
+			return getEmptyGroupsRelationIfRetry(skuId, oriRelationId, userId, retryNum);
+		}
+		syncGroupsRelation(oriRelationId, relation);
+		return relation;
+	}
+
+
+	/**
+	 * 寻找该车队空余车位
+	 */
+	public GroupsRelation getRelationThisTrips(Long userId, Long groupsId, Integer retryNum) {
+		//寻找快满编车队进行占座
+		GroupsRelation relation = relationMapper.selectRandomOneRelationByGroupsId(groupsId);
+		if (relation == null) {
+			log.error("该车队人员已满,请选择另一车队");
+			return null;
+		}
+		Long relationId = relation.getId();
+		if (!redisService.setNx(RedisKey.GROUPS_RELATION_NUM_KEY + relationId, userId, 60 * 5L)) {
+			if (retryNum == 7){
+				log.error("该车队人员已满,请选择另一车队");
+				return null;
+			}
+			retryNum++;
+			return getRelationThisTrips(userId, groupsId, retryNum);
+		}
+		int count = groupsMapper.decrAvailableNum(relation.getGroupsId());
+		if (count == 0) {
+			log.error("车位异常 groupId:{}", groupsId);
+			groupsMapper.updateAvailableNum(groupsId);
+			log.error("车位异常");
+			return null;
+		}
+		return relation;
+	}
+
+	public void syncGroupsRelation(Long oriRelationId, GroupsRelation newRelation) {
+		GroupsRelation oriRelation = relationMapper.selectById(oriRelationId);
+		BeanUtil.copyProperties(oriRelation, newRelation);
+		relationMapper.updateById(newRelation);
+		OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
+				.eq(OrderDon::getRelationId, oriRelationId)
+				.eq(OrderDon::getUserId, oriRelation.getUserId())
+				.notIn(OrderDon::getStatus, Constant.noOrderStatus)
+				.orderByDesc(OrderDon::getId)
+				.last("limit 1"));
+		if (orderDon != null) {
+			orderDon.setRelationId(newRelation.getId());
+			orderDonMapper.updateById(orderDon);
+		}
+	}
+}

+ 10 - 0
netflix-web/src/main/java/com/cyksj/web/controller/group/GroupRelationController.java

@@ -1068,4 +1068,14 @@ public class GroupRelationController {
         data.setRenewCount(renewCount);
         return GatewayResponse.SUCCESS.newBuilder().toResult(data);
     }
+
+    /**
+     * 申请更换车票
+     */
+    @PostMapping("/change/ticket/{relationId}")
+    public Result<String> changeTicket(@PathVariable Long relationId) {
+        long userId = StpUserUtil.getLoginIdAsLong();
+        groupRelationFrontService.changeTicket(userId, relationId);
+        return GatewayResponse.SUCCESS.newBuilder().toResult();
+    }
 }