|
|
@@ -251,56 +251,19 @@ public class ExchangeCodeServiceImpl extends ServiceImpl<ExchangeCodeMapper, Exc
|
|
|
|
|
|
@Override
|
|
|
public GroupsRelation getRelationNoOrder(GoodsDonSku sku, Long userId) {
|
|
|
- GroupsRelation relation = null;
|
|
|
Integer retryNum = 1;
|
|
|
List<Long> errorsRelationIds = new ArrayList<>();
|
|
|
- relation = getFinalRelation(sku, userId, relation, retryNum, errorsRelationIds);
|
|
|
+ GroupsRelation relation = getFinalRelation(sku, userId, retryNum, errorsRelationIds);
|
|
|
return relation;
|
|
|
}
|
|
|
|
|
|
- public GroupsRelation getFinalRelation(GoodsDonSku sku, Long userId, GroupsRelation relation, Integer retryNum, List<Long> errorsRelationIds) {
|
|
|
- //寻找差不多过期时间规格的车位
|
|
|
- relation = groupRelationFrontService.getSimilarExpiredGroupRelation(sku.getId(), sku.getDays(), sku.getMonths(), errorsRelationIds);
|
|
|
- if (relation == null) {
|
|
|
- GroupsTrips groups = groupsMapper.selectOne(Wrappers.lambdaQuery(GroupsTrips.class)
|
|
|
- .eq(GroupsTrips::getSkuId, sku.getId())
|
|
|
- .gt(GroupsTrips::getAvailableNum, 0).last("limit 1")
|
|
|
- .eq(GroupsTrips::getStatus, GroupsTrips.Status.validity)
|
|
|
- .orderByAsc(GroupsTrips::getAvailableNum));
|
|
|
- //待发车
|
|
|
- if (groups == null) {
|
|
|
- groups = groupsMapper.selectOne(Wrappers.lambdaQuery(GroupsTrips.class)
|
|
|
- .eq(GroupsTrips::getSkuId, sku.getId())
|
|
|
- .gt(GroupsTrips::getAvailableNum, 0).last("limit 1")
|
|
|
- .eq(GroupsTrips::getStatus, GroupsTrips.Status.waiting)
|
|
|
- .orderByAsc(GroupsTrips::getAvailableNum));
|
|
|
- }
|
|
|
- if (groups == null) {
|
|
|
- //新增车位 并关联
|
|
|
- relation = createNewGroupTripsAndRelation(sku, groups, relation);
|
|
|
- log.info("无合适规格车队,新增新车队,获取车位relationId:{}", relation.getId());
|
|
|
- } else {
|
|
|
- //寻找快满编车队进行占座
|
|
|
- relation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
|
|
|
- .eq(GroupsRelation::getGroupsId, groups.getId())
|
|
|
- .notIn(errorsRelationIds != null && errorsRelationIds.size() > 0, GroupsRelation::getId, errorsRelationIds)
|
|
|
- .eq(GroupsRelation::getStatus, "none")
|
|
|
- .eq(GroupsRelation::getUserId, 0)
|
|
|
- .orderByAsc(GroupsRelation::getNum)
|
|
|
- .last("limit 1")
|
|
|
- );
|
|
|
- log.info("寻找快满编车队的车位relationId:{}", relation.getId());
|
|
|
- //如果占位失败 新增车位并关联
|
|
|
- if (relation == null) {
|
|
|
- relation = createNewGroupTripsAndRelation(sku, groups, relation);
|
|
|
- log.info("占位新车队车位失败,再次新增新车队,获取车位relationId:{}", relation.getId());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ public GroupsRelation getFinalRelation(GoodsDonSku sku, Long userId, Integer retryNum, List<Long> errorsRelationIds) {
|
|
|
+ //获取车票车位
|
|
|
+ GroupsRelation relation = groupRelationFrontService.getRelation(null, userId, sku, null);
|
|
|
|
|
|
try {
|
|
|
//锁定座位
|
|
|
- setRelation(relation.getId(), userId);
|
|
|
+ setRelation(relation);
|
|
|
relation.setUserId(userId);
|
|
|
GroupsTrips groupsTrips = groupsMapper.selectById(relation.getGroupsId());
|
|
|
if (GroupsTrips.Status.validity.equals(groupsTrips.getStatus())) {
|
|
|
@@ -310,26 +273,21 @@ public class ExchangeCodeServiceImpl extends ServiceImpl<ExchangeCodeMapper, Exc
|
|
|
if (sku.getDays() != null && sku.getDays() > 0) {
|
|
|
newDate = DateUtil.offsetDay(expiryTime, sku.getDays() * 1);
|
|
|
} else {
|
|
|
- newDate = DateUtil.offset(expiryTime, DateField.MONTH, sku.getMonths() * 1);
|
|
|
+ //奈飞月付 按30天计算
|
|
|
+ if (sku.getId() == 4) {
|
|
|
+ newDate = DateUtil.offsetDay(expiryTime, 30);
|
|
|
+ } else {
|
|
|
+ newDate = DateUtil.offset(expiryTime, DateField.MONTH, sku.getMonths() * 1);
|
|
|
+ }
|
|
|
}
|
|
|
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);
|
|
|
}
|
|
|
- redisService.del(RedisKey.GROUPS_RELATION_NUM_KEY + relation.getId());
|
|
|
+ groupsRelationMapper.updateById(relation);
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
|
Object value = redisService.get(RedisKey.GROUPS_RELATION_NUM_KEY + relation.getId());
|
|
|
if (value != null && userId.equals(Long.parseLong(value.toString()))) {
|
|
|
@@ -338,31 +296,37 @@ public class ExchangeCodeServiceImpl extends ServiceImpl<ExchangeCodeMapper, Exc
|
|
|
errorsRelationIds.add(relation.getId());
|
|
|
if (retryNum == 15) {
|
|
|
log.error("用户:{}获取车票:{}异常", userId, relation.getId());
|
|
|
- throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("get_ticket_error"));
|
|
|
+ throw BusinessRuntimeException.getInstance("获取车票异常,请联系客服");
|
|
|
}
|
|
|
//失败重新给他分配车位
|
|
|
- getFinalRelation(sku, userId, relation, ++retryNum, errorsRelationIds);
|
|
|
+ return getFinalRelation(sku, userId, ++retryNum, errorsRelationIds);
|
|
|
}
|
|
|
return relation;
|
|
|
}
|
|
|
|
|
|
- private void setRelation(Long relationId, Long userId) {
|
|
|
- if (redisService.get(RedisKey.GROUPS_RELATION_NUM_KEY + relationId) != null) {
|
|
|
- log.info("=====>用户:{}获取了已有人座位:{}", userId, relationId);
|
|
|
- throw new BusinessRuntimeException("客官手慢啦,该座位已经有人啦!");
|
|
|
- } else {
|
|
|
- if (!redisService.setNx(RedisKey.GROUPS_RELATION_NUM_KEY + relationId, userId, 60 * 5L)) {
|
|
|
+ public void setRelation(GroupsRelation relation) {
|
|
|
+ Long relationId = relation.getId();
|
|
|
+ Long userId = relation.getUserId();
|
|
|
+ Long groupsId = relation.getGroupsId();
|
|
|
+ GroupsRelation.Status status = relation.getStatus();
|
|
|
+ String relation_num_key = RedisKey.GROUPS_RELATION_NUM_KEY + relationId;
|
|
|
+ if (!redisService.setNx(relation_num_key, userId, 60 * 5L)) {
|
|
|
+ GroupsRelation dbRelation = groupsRelationMapper.selectById(relationId);
|
|
|
+ if (dbRelation.getUserId() != 0) {
|
|
|
log.info("=====>用户:{}未抢到座位:{}", userId, relationId);
|
|
|
- throw new BusinessRuntimeException("客官手慢啦,该座位已经有人啦!");
|
|
|
- }
|
|
|
- GroupsRelation relation = groupsRelationMapper.selectById(relationId);
|
|
|
- int count = groupsMapper.decrAvailableNum(relation.getGroupsId());
|
|
|
- if (count == 0) {
|
|
|
- log.error("车位异常 groupId:{}", relation.getGroupsId());
|
|
|
- groupsMapper.updateAvailableNum(relation.getGroupsId());
|
|
|
- throw new BusinessRuntimeException("车位异常");
|
|
|
+ throw new BusinessRuntimeException("系统繁忙,请重试...");
|
|
|
}
|
|
|
}
|
|
|
+ if (status == GroupsRelation.Status.outside) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ int count = groupsMapper.decrAvailableNum(groupsId);
|
|
|
+ if (count == 0) {
|
|
|
+ log.error("车位异常 groupId:{}", groupsId);
|
|
|
+ groupsMapper.updateAvailableNum(groupsId);
|
|
|
+ redisService.del(RedisKey.GROUPS_RELATION_NUM_KEY + relationId);
|
|
|
+ throw new BusinessRuntimeException("车位异常");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|