Browse Source

ChatGPT Plus、midJourney下单额外车位上车

zoujiajian 3 years ago
parent
commit
6de819c91f

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

@@ -51,4 +51,6 @@ public interface GroupsRelationMapper extends BaseMapper<GroupsRelation> {
 	List<Long> getTicketGoodsId(@Param("list") List<Long> userIds, @Param("status") List<String> status);
 
 	Page<UserRecommendGoodsFrontView> getRecommendGoodsViews(@Param("page") Page<UserRecommendGoodsFrontView> page, @Param("list") List<Long> filter_goods_id, @Param("isPayNf") Boolean isPayNf);
+
+	Long getOutSideGroupsTripsRelationByGoodsId(@Param("skuId") Long skuId, @Param("beginOfDay") DateTime beginOfDay, @Param("extraNum") Integer extraNum);
 }

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

@@ -211,4 +211,16 @@
         </if>
         and deleted is true and status is true) s order by s.sorted desc,sort_by desc
     </select>
+
+    <select id="getOutSideGroupsTripsRelationByGoodsId" resultType="java.lang.Long">
+        select gt.id
+        from `groups_trips` gt
+                 inner join account a on a.id = gt.account_id
+        where gt.sku_id = #{skuId}
+          and gt.status = 'validity'
+          and a.expiry_time >= #{beginOfDay}
+          and (select count(*) from groups_relation gr where gr.status ='outside' and gr.groups_id = gt.id and
+            gr.user_id != 0) &lt; #{extraNum}
+        order by RAND() limit 1
+    </select>
 </mapper>

+ 3 - 0
netflix-service/src/main/java/com/cyksj/redis/RedisService.java

@@ -633,6 +633,9 @@ public class RedisService {
         ORDER_COMMENT_KEY("order_comment_key:", "订单评价", 10l),
         USER_BIND_ACCOUNT_INFO_KEY("user_bind_account_info_key:", "用户绑定其他账号key", 10l),
         TASK_RECEIVED_KEY("task_received_key:%s:%s", "领取任务奖励key", 10l),
+        CORP_GROUP_CHAT_LIST("corp_group_chat_list:%s", "企业微信客户群列表", 60 * 60 * 24),
+        PLUS_GOODS_ADDRESS_KEY("plus_goods_address_key:", "优惠加购实物地址key", 10 * 60l),
+        GROUPS_TRIPS_OUTSIDE_RELATION("groups_trips_outside_relation:key", "额外车位", 30l),
         ;
 
         private String name;

+ 53 - 23
netflix-service/src/main/java/com/cyksj/service/exchange/impl/ExchangeCodeServiceImpl.java

@@ -309,6 +309,10 @@ public class ExchangeCodeServiceImpl extends ServiceImpl<ExchangeCodeMapper, Exc
 			//兑换码兑换
 			orderDon.setType(orderDonTye);
 			orderDon.setUserId(user.getId());
+			StringBuilder payTitle = new StringBuilder();
+			payTitle.append(goodsDon.getTitle());
+			payTitle.append(sku.getSpecVal());
+			orderDon.setPayTitle(payTitle.toString());
 			orderDonMapper.insert(orderDon);
 			log.info("用户{}兑换规格:{}车位成功", user.getId(), orderDon.getSkuId());
 		}
@@ -369,9 +373,9 @@ public class ExchangeCodeServiceImpl extends ServiceImpl<ExchangeCodeMapper, Exc
 						.orderByAsc(GroupsTrips::getAvailableNum));
 			}
 			if (groups == null) {
-				//新增车位 并关联
-				relation = createNewGroupTripsAndRelation(sku, groups, relation);
-				log.info("无合适规格车队,新增新车队,获取车位relationId:{}", relation.getId());
+				//若是ChatGPT Plus 、MidJourney
+				//获取额外的车位
+				relation = getOutSideRelationIfAi(userId, groups, sku);
 			} else {
 				//寻找快满编车队进行占座
 				relation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
@@ -385,15 +389,14 @@ public class ExchangeCodeServiceImpl extends ServiceImpl<ExchangeCodeMapper, Exc
 				log.info("寻找快满编车队的车位relationId:{}", relation.getId());
 				//如果占位失败 新增车位并关联
 				if (relation == null) {
-					relation = createNewGroupTripsAndRelation(sku, groups, relation);
-					log.info("占位新车队车位失败,再次新增新车队,获取车位relationId:{}", relation.getId());
+					relation = getOutSideRelationIfAi(userId, groups, sku);
 				}
 			}
 		}
 
 		try {
 			//锁定座位
-			setRelation(relation.getId(), userId);
+			setRelation(relation.getId(), userId, relation.getGroupsId(), relation.getStatus());
 			relation.setUserId(userId);
 			GroupsTrips groupsTrips = groupsMapper.selectById(relation.getGroupsId());
 			if (GroupsTrips.Status.validity.equals(groupsTrips.getStatus())) {
@@ -408,19 +411,10 @@ public class ExchangeCodeServiceImpl extends ServiceImpl<ExchangeCodeMapper, Exc
 				relation.setExpiryTime(newDate);
 				relation.setStartTime(relation.getStartTime() == null ? new Date() : null);
 			}
-			relation.setStatus(GroupsRelation.Status.validity);
-			int isSuccess = groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
-					.set(GroupsRelation::getUserId, userId)
-					.set(GroupsRelation::getStatus, GroupsRelation.Status.validity)
-					.set(GroupsRelation::getStartTime, relation.getStartTime())
-					.set(GroupsRelation::getExpiryTime, relation.getExpiryTime())
-					.eq(GroupsRelation::getId, relation.getId())
-					.eq(GroupsRelation::getUserId, 0)
-					.eq(GroupsRelation::getStatus, GroupsRelation.Status.none));
-			if (isSuccess != 1) {
-				log.error("该车位已被占,用户userId:{}更新上车relationId:{}状态失败", userId, relation.getId());
-				throw BusinessRuntimeException.getInstance("该车位已被占");
+			if (relation.getStatus() != GroupsRelation.Status.outside) {
+				relation.setStatus(GroupsRelation.Status.validity);
 			}
+			groupsRelationMapper.updateById(relation);
 			redisService.del(RedisKey.GROUPS_RELATION_NUM_KEY + relation.getId());
 		} catch (Exception e) {
 			Object value = redisService.get(RedisKey.GROUPS_RELATION_NUM_KEY + relation.getId());
@@ -438,7 +432,7 @@ public class ExchangeCodeServiceImpl extends ServiceImpl<ExchangeCodeMapper, Exc
 		return relation;
 	}
 
-	private void setRelation(Long relationId, Long userId) {
+	private void setRelation(Long relationId, Long userId, Long groupsId, GroupsRelation.Status status) {
 		if (redisService.get(RedisKey.GROUPS_RELATION_NUM_KEY + relationId) != null) {
 			log.info("=====>用户:{}获取了已有人座位:{}", userId, relationId);
 			throw new BusinessRuntimeException("客官手慢啦,该座位已经有人啦!");
@@ -447,11 +441,13 @@ public class ExchangeCodeServiceImpl extends ServiceImpl<ExchangeCodeMapper, Exc
 				log.info("=====>用户:{}未抢到座位:{}", userId, relationId);
 				throw new BusinessRuntimeException("客官手慢啦,该座位已经有人啦!");
 			}
-			GroupsRelation relation = groupsRelationMapper.selectById(relationId);
-			int count = groupsMapper.decrAvailableNum(relation.getGroupsId());
+			if (status == GroupsRelation.Status.outside) {
+				return;
+			}
+			int count = groupsMapper.decrAvailableNum(groupsId);
 			if (count == 0) {
-				log.error("车位异常 groupId:{}", relation.getGroupsId());
-				groupsMapper.updateAvailableNum(relation.getGroupsId());
+				log.error("车位异常 groupId:{}", groupsId);
+				groupsMapper.updateAvailableNum(groupsId);
 				throw new BusinessRuntimeException("车位异常");
 			}
 		}
@@ -502,4 +498,38 @@ public class ExchangeCodeServiceImpl extends ServiceImpl<ExchangeCodeMapper, Exc
 		}
 		return relation;
 	}
+
+	public GroupsRelation getOutSideRelationIfAi(Long userId, GroupsTrips groups, GoodsDonSku sku) {
+		//若是MidJourney、ChatGPT PLUS每个已满车队 硬塞5个座位
+		//主账号过期时间10天以外
+		GroupsRelation relation = null;
+		if (Constant.AI_goodsIds.contains(sku.getGoodsId())) {
+			DateTime tenExpiry = DateUtil.offsetDay(DateUtil.beginOfDay(DateTime.now()), 10);
+			Long groupsId = groupsRelationMapper.getOutSideGroupsTripsRelationByGoodsId(sku.getId(), tenExpiry, 5);
+			if (groupsId != null) {
+				relation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
+						.eq(GroupsRelation::getGroupsId, groupsId)
+						.eq(GroupsRelation::getUserId, 0)
+						.eq(GroupsRelation::getStatus, GroupsRelation.Status.outside)
+						.last("limit 1"));
+				if (relation == null) {
+					if (!redisService.setNx(RedisService.key.GROUPS_TRIPS_OUTSIDE_RELATION.getName() + groupsId, groupsId, RedisService.key.GROUPS_TRIPS_OUTSIDE_RELATION.getTimeout())) {
+						relation = new GroupsRelation();
+						relation.setStatus(GroupsRelation.Status.outside);
+						relation.setUserId(userId);
+						relation.setGroupsId(groupsId);
+						GroupsRelation maxNumRelation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId, groupsId).orderByDesc(GroupsRelation::getNum).last("limit 1"));
+						relation.setNum(maxNumRelation.getNum() + 1);
+						groupsRelationMapper.insert(relation);
+					}
+				}
+			}
+		}
+		if (relation == null) {
+			//新增车位 并关联
+			relation = createNewGroupTripsAndRelation(sku, groups, relation);
+			log.info("无合适规格车队,新增新车队,获取车位relationId:{}", relation.getId());
+		}
+		return relation;
+	}
 }

+ 2 - 1
netflix-service/src/main/java/com/cyksj/service/order/OrderCommonService.java

@@ -1,6 +1,7 @@
 package com.cyksj.service.order;
 
 import com.cyksj.model.entity.GoodsDonSku;
+import com.cyksj.model.entity.GroupsRelation;
 import com.cyksj.model.entity.OrderDon;
 
 /*
@@ -19,5 +20,5 @@ public interface OrderCommonService {
 	/**
 	 * 清除座位缓存,可用座位数增加
 	 */
-	void delRelation(Long relationId);
+	void delRelation(GroupsRelation relation);
 }

+ 6 - 3
netflix-service/src/main/java/com/cyksj/service/order/impl/OrderCommonServiceImpl.java

@@ -113,7 +113,7 @@ public class OrderCommonServiceImpl implements OrderCommonService {
 					relation.setAccount(null);
 					groupsRelationMapper.updateExpiryTime(relation, expiryTime);
 					if (relation.getStatus() != GroupsRelation.Status.outside) {
-						delRelation(relation.getId());
+						delRelation(relation);
 					}
 				} else {
 					if (expiryTime != null) {
@@ -134,9 +134,12 @@ public class OrderCommonServiceImpl implements OrderCommonService {
 	}
 
 	@Override
-	public void delRelation(Long relationId) {
+	public void delRelation(GroupsRelation relation) {
+		Long relationId = relation.getId();
 		redisService.del(RedisKey.GROUPS_RELATION_NUM_KEY + relationId);
-		GroupsRelation relation = groupsRelationMapper.selectById(relationId);
+		if (relation.getStatus() == GroupsRelation.Status.outside) {
+			return;
+		}
 		int count = groupsMapper.incrAvailableNum(relation.getGroupsId());
 		if (count == 0) {
 			log.error("车位异常 groupId:{}", relation.getGroupsId());

+ 41 - 7
netflix-service/src/main/java/com/cyksj/service/order/impl/OrderDonServiceImpl.java

@@ -670,12 +670,14 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 						}
 						//throw new BusinessRuntimeException("订单用户与车位用户不匹配!");
 					} else {
-						if (GroupsRelation.Status.locking.equals(groupsRelation.getStatus())) {
+						if (GroupsRelation.Status.locking == groupsRelation.getStatus() || GroupsRelation.Status.outside == groupsRelation.getStatus()) {
 							log.info("清理用户userId:{}未支付订单", orderDon.getUserId());
 							groupsRelation.setUserId(0L);
-							groupsRelation.setStatus(GroupsRelation.Status.none);
+							if (GroupsRelation.Status.outside != groupsRelation.getStatus()) {
+								groupsRelation.setStatus(GroupsRelation.Status.none);
+							}
 							groupsRelationMapper.updateById(groupsRelation);
-							orderCommonService.delRelation(groupsRelation.getId());
+							orderCommonService.delRelation(groupsRelation);
 
 							//车位空余发送模板消息
 //						TASK_POOL.execute(() -> {
@@ -968,7 +970,7 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 		});
 	}
 
-	public void setRelation(Long relationId, Long groupsId, Long userId, Long goodsId, Boolean isLoginPopularize) {
+	public void setRelation(Long relationId, Long groupsId, Long userId, Long goodsId, Boolean isLoginPopularize, GroupsRelation.Status status) {
 		if (isLoginPopularize == null || !isLoginPopularize) {
 			int noPayCount = count(Wrappers.lambdaQuery(OrderDon.class).eq(OrderDon::getUserId, userId).eq(OrderDon::getGoodsId, goodsId).eq(OrderDon::getStatus, OrderDon.Status.noPayment.toString()));
 			if (noPayCount > 0) {
@@ -979,6 +981,9 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 			log.info("=====>用户:{}未抢到座位:{}", userId, relationId);
 			throw new BusinessRuntimeException("客官手慢啦,该座位已经有人啦!");
 		}
+		if (status == GroupsRelation.Status.outside) {
+			return;
+		}
 		int count = groupsMapper.decrAvailableNum(groupsId);
 		if (count == 0) {
 			log.error("车位异常 groupId:{}", groupsId);
@@ -1971,7 +1976,34 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 							.orderByAsc(GroupsTrips::getAvailableNum));
 				}
 				if (groups == null) {
-					relation = groupsFuncService.createNewGroupsTrips(sku);
+					//若是MidJourney、ChatGPT PLUS每个已满车队 硬塞5个座位
+					//主账号过期时间10天以外
+					if (Constant.AI_goodsIds.contains(sku.getGoodsId())) {
+						DateTime tenExpiry = DateUtil.offsetDay(DateUtil.beginOfDay(DateTime.now()), 10);
+						Long groupsId = groupsRelationMapper.getOutSideGroupsTripsRelationByGoodsId(sku.getId(), tenExpiry, 5);
+						if (groupsId != null) {
+							relation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
+									.eq(GroupsRelation::getGroupsId, groupsId)
+									.eq(GroupsRelation::getUserId, 0)
+									.eq(GroupsRelation::getStatus, GroupsRelation.Status.outside)
+									.last("limit 1"));
+							if (relation == null) {
+								if (redisService.setNx(RedisService.key.GROUPS_TRIPS_OUTSIDE_RELATION.getName() + groupsId, groupsId, RedisService.key.GROUPS_TRIPS_OUTSIDE_RELATION.getTimeout())) {
+									relation = new GroupsRelation();
+									relation.setStatus(GroupsRelation.Status.outside);
+									relation.setUserId(userId);
+									relation.setGroupsId(groupsId);
+									GroupsRelation maxNumRelation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId, groupsId).orderByDesc(GroupsRelation::getNum).last("limit 1"));
+									relation.setNum(maxNumRelation.getNum() + 1);
+									groupsRelationMapper.insert(relation);
+									redisService.del(RedisService.key.GROUPS_TRIPS_OUTSIDE_RELATION.getName() + groupsId);
+								}
+							}
+						}
+					}
+					if (relation == null) {
+						relation = groupsFuncService.createNewGroupsTrips(sku);
+					}
 				} else {
 					//寻找快满编车队进行占座
 					relation = groupsRelationMapper.selectRandomOneRelationByGroupsId(groups.getId());
@@ -1983,9 +2015,11 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 		}
 
 		//锁定座位
-		setRelation(relation.getId(), relation.getGroupsId(), userId, sku.getGoodsId(), isLoginPopularize);
+		setRelation(relation.getId(), relation.getGroupsId(), userId, sku.getGoodsId(), isLoginPopularize, relation.getStatus());
 		relation.setUserId(userId);
-		relation.setStatus(GroupsRelation.Status.locking);
+		if (relation.getStatus() != GroupsRelation.Status.outside) {
+			relation.setStatus(GroupsRelation.Status.locking);
+		}
 		groupsRelationMapper.updateById(relation);
 		redisService.del(RedisKey.GROUPS_RELATION_NUM_KEY + relation.getId());
 		return relation.getId();