用户开锁
This commit is contained in:
parent
12a019b996
commit
32ad4e050e
@ -5,7 +5,6 @@ import com.cdzy.user.model.dto.EbikeUserCyclingDto;
|
||||
import com.cdzy.user.model.entity.EbikeOrder;
|
||||
import com.cdzy.user.model.vo.EbikeOrderVo;
|
||||
import com.cdzy.user.service.EbikeOrderService;
|
||||
import com.ebike.feign.clients.OperationsFeignClient;
|
||||
import com.ebike.feign.model.dto.FeignEbikeDto;
|
||||
import com.ebike.feign.model.dto.FeignEbikeUserBikeInfo;
|
||||
import com.ebike.feign.model.dto.FeignOrderPaymentDto;
|
||||
@ -31,9 +30,6 @@ public class EbikeOrderController {
|
||||
@Resource
|
||||
private EbikeOrderService ebikeOrderService;
|
||||
|
||||
@Resource
|
||||
private OperationsFeignClient operationsFeignClient;
|
||||
|
||||
/**
|
||||
* 开始骑行(生成订单、开锁)
|
||||
*
|
||||
|
||||
@ -0,0 +1,19 @@
|
||||
package com.cdzy.user.model.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 开锁结果
|
||||
*
|
||||
* @author: yanglei
|
||||
* @since: 2025-11-11 11:34
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class EbikeUnlockResultDto {
|
||||
|
||||
private boolean success;
|
||||
|
||||
private String errorMsg;
|
||||
}
|
||||
@ -7,6 +7,7 @@ import com.cdzy.common.model.response.JsonResult;
|
||||
import com.cdzy.user.enums.OrderStatus;
|
||||
import com.cdzy.user.enums.OrderType;
|
||||
import com.cdzy.user.mapper.EbikeOrderMapper;
|
||||
import com.cdzy.user.model.dto.EbikeUnlockResultDto;
|
||||
import com.cdzy.user.model.dto.EbikeUserCyclingDto;
|
||||
import com.cdzy.user.model.entity.EbikeOrder;
|
||||
import com.cdzy.user.model.vo.EbikeOrderVo;
|
||||
@ -18,7 +19,6 @@ import com.ebike.feign.model.dto.FeignEbikeUserBikeInfo;
|
||||
import com.ebike.feign.model.dto.FeignOrderPaymentDto;
|
||||
import com.ebike.feign.model.vo.FeignEbikeBikeInfoVo;
|
||||
import com.ebike.feign.model.vo.FeignEbikeBikeRadiusVo;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.spring.service.impl.ServiceImpl;
|
||||
import feign.FeignException;
|
||||
@ -44,63 +44,75 @@ import static com.cdzy.user.model.entity.table.EbikeOrderTableDef.EBIKE_ORDER;
|
||||
@Service
|
||||
public class EbikeOrderImpl extends ServiceImpl<EbikeOrderMapper, EbikeOrder> implements EbikeOrderService {
|
||||
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
@Resource
|
||||
private EbikeOrderMapper ebikeOrderTransactionMapper;
|
||||
|
||||
@Resource
|
||||
private OperationsFeignClient operationsFeignClient;
|
||||
|
||||
@Resource
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public EbikeOrderVo saveRide(EbikeUserCyclingDto orderDto) {
|
||||
Long userId = orderDto.getUserId();
|
||||
String bikeCode = orderDto.getBikeCode();
|
||||
// 校验用户当前是否存在订单
|
||||
EbikeOrder history = checkHistoryOrder(userId);
|
||||
if (Objects.nonNull(history)) {
|
||||
if (history.getOrderStatus() == OrderStatus.IN_PROGRESS) {
|
||||
throw new RuntimeException("请完成当前订单后再试");
|
||||
throw new EbikeException("请完成当前订单后再试");
|
||||
}
|
||||
if (history.getOrderStatus() == OrderStatus.PENDING_PAYMENT) {
|
||||
throw new RuntimeException("请完成未支付订单后再试");
|
||||
throw new EbikeException("请完成未支付订单后再试");
|
||||
}
|
||||
}
|
||||
|
||||
// 创建订单
|
||||
FeignEbikeUserBikeInfo bikeInfo = queryBikeInfo(bikeCode);
|
||||
if (bikeInfo == null) {
|
||||
log.error("开锁成功后查询车辆信息为空, bikeCode: {}, userId: {}", bikeCode, userId);
|
||||
throw new EbikeException("当前车辆信息不存在");
|
||||
}
|
||||
EbikeOrder order = EbikeOrder.builder()
|
||||
.userId(orderDto.getUserId())
|
||||
.userId(userId)
|
||||
.operatorId(bikeInfo.getOperatorId())
|
||||
.bikeCode(orderDto.getBikeCode())
|
||||
.orderType(OrderType.ONCE)
|
||||
.startLocation(orderDto.getStartPoint())
|
||||
.startTime(LocalDateTime.now())
|
||||
.baseFee(bikeInfo.getBaseFee())
|
||||
.durationFee(bikeInfo.getDurationFee())
|
||||
.freeDurationMinutes(bikeInfo.getFreeDurationMinutes())
|
||||
.chargeDurationMinutes(bikeInfo.getChargeDurationMinutes())
|
||||
.createTime(LocalDateTime.now())
|
||||
.build();
|
||||
|
||||
save(order);
|
||||
// 开锁
|
||||
|
||||
try {
|
||||
JsonResult<?> lockResult = operationsFeignClient.openLock(orderDto.getBikeCode());
|
||||
if (lockResult == null || !"200".equals(lockResult.getCode())) {
|
||||
String errorMsg = (lockResult != null && lockResult.getMessage() != null)
|
||||
? lockResult.getMessage()
|
||||
: "开锁服务返回异常";
|
||||
throw new EbikeException("开锁失败: " + errorMsg);
|
||||
// 尝试开锁
|
||||
EbikeUnlockResultDto unlockResult = attemptUnlockWithValidation(bikeCode, userId);
|
||||
if (!unlockResult.isSuccess()) {
|
||||
throw new EbikeException("开锁失败: " + unlockResult.getErrorMsg());
|
||||
}
|
||||
} catch (FeignException e) {
|
||||
log.error("开锁服务调用失败, bikeCode={}, userId={}", orderDto.getBikeCode(), orderDto.getUserId(), e);
|
||||
throw new EbikeException("开锁服务暂时不可用,请稍后重试");
|
||||
// 开锁成功
|
||||
EbikeOrder orders = EbikeOrder.builder()
|
||||
.orderId(order.getOrderId())
|
||||
.orderStatus(OrderStatus.IN_PROGRESS)
|
||||
.updateTime(LocalDateTime.now())
|
||||
.build();
|
||||
updateById(orders);
|
||||
|
||||
EbikeOrderVo userOrder = new EbikeOrderVo();
|
||||
userOrder.setOrderId(order.getOrderId());
|
||||
userOrder.setOrderTime(order.getCreateTime());
|
||||
return userOrder;
|
||||
} catch (Exception e) {
|
||||
log.error("开锁过程发生系统异常", e);
|
||||
throw new EbikeException("系统繁忙,开锁失败");
|
||||
log.error("开锁失败, userId={}, bikeCode={}", userId, bikeCode, e);
|
||||
throw new RuntimeException("开锁失败", e);
|
||||
}
|
||||
EbikeOrderVo userOrder = new EbikeOrderVo();
|
||||
userOrder.setOrderId(order.getOrderId());
|
||||
userOrder.setOrderTime(order.getCreateTime());
|
||||
return userOrder;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public EbikeOrder checkHistoryOrder(Long userId) {
|
||||
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||
@ -209,4 +221,37 @@ public class EbikeOrderImpl extends ServiceImpl<EbikeOrderMapper, EbikeOrder> im
|
||||
order.setOrderStatus(targetStatus);
|
||||
mapper.update(order);
|
||||
}
|
||||
|
||||
/**
|
||||
* 开锁
|
||||
*/
|
||||
private EbikeUnlockResultDto attemptUnlockWithValidation(String bikeCode, Long userId) {
|
||||
try {
|
||||
JsonResult<?> jsonResult = operationsFeignClient.openLock(bikeCode);
|
||||
if (jsonResult == null) {
|
||||
String errorMsg = "开锁服务返回结果为空";
|
||||
log.error("开锁失败 - 响应为空, bikeCode={}, userId={}", bikeCode, userId);
|
||||
return new EbikeUnlockResultDto(false, errorMsg);
|
||||
}
|
||||
Integer code = jsonResult.getCode();
|
||||
String msg = jsonResult.getMessage() != null ? jsonResult.getMessage() : "未知错误";
|
||||
if (code != null && code == 200) {
|
||||
log.info("开锁成功,用户: {}, 车辆: {}", userId, bikeCode);
|
||||
return new EbikeUnlockResultDto(true, null);
|
||||
} else {
|
||||
log.warn("开锁失败,用户: {}, 车辆: {}, 错误码: {}, 原因: {}",
|
||||
userId, bikeCode, code, msg);
|
||||
return new EbikeUnlockResultDto(false, msg);
|
||||
}
|
||||
} catch (FeignException e) {
|
||||
String errorMsg = "开锁服务调用失败";
|
||||
log.error("Feign 开锁失败, bikeCode={}, userId={}, status={}, response={}",
|
||||
bikeCode, userId, e.status(), e.contentUTF8(), e);
|
||||
return new EbikeUnlockResultDto(false, errorMsg);
|
||||
} catch (Exception e) {
|
||||
String errorMsg = "系统繁忙,请稍后重试";
|
||||
log.error("开锁过程发生未知异常, bikeCode={}, userId={}", bikeCode, userId, e);
|
||||
return new EbikeUnlockResultDto(false, errorMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -564,7 +564,7 @@ ALTER TABLE "public"."ebike_order" ADD CONSTRAINT "ebike_order_pkey" PRIMARY KEY
|
||||
-- ----------------------------
|
||||
CREATE INDEX idx_ebike_order_user_id ON public.ebike_order (user_id);
|
||||
CREATE INDEX idx_ebike_order_operator_id ON public.ebike_order (operator_id);
|
||||
CREATE INDEX idx_ebike_order_bike_id ON public.ebike_order (bike_id);
|
||||
CREATE INDEX idx_ebike_order_bike_code ON public.ebike_order (bike_code);
|
||||
|
||||
|
||||
-- ----------------------------
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user