用户开锁
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.entity.EbikeOrder;
|
||||||
import com.cdzy.user.model.vo.EbikeOrderVo;
|
import com.cdzy.user.model.vo.EbikeOrderVo;
|
||||||
import com.cdzy.user.service.EbikeOrderService;
|
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.FeignEbikeDto;
|
||||||
import com.ebike.feign.model.dto.FeignEbikeUserBikeInfo;
|
import com.ebike.feign.model.dto.FeignEbikeUserBikeInfo;
|
||||||
import com.ebike.feign.model.dto.FeignOrderPaymentDto;
|
import com.ebike.feign.model.dto.FeignOrderPaymentDto;
|
||||||
@ -31,9 +30,6 @@ public class EbikeOrderController {
|
|||||||
@Resource
|
@Resource
|
||||||
private EbikeOrderService ebikeOrderService;
|
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.OrderStatus;
|
||||||
import com.cdzy.user.enums.OrderType;
|
import com.cdzy.user.enums.OrderType;
|
||||||
import com.cdzy.user.mapper.EbikeOrderMapper;
|
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.dto.EbikeUserCyclingDto;
|
||||||
import com.cdzy.user.model.entity.EbikeOrder;
|
import com.cdzy.user.model.entity.EbikeOrder;
|
||||||
import com.cdzy.user.model.vo.EbikeOrderVo;
|
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.dto.FeignOrderPaymentDto;
|
||||||
import com.ebike.feign.model.vo.FeignEbikeBikeInfoVo;
|
import com.ebike.feign.model.vo.FeignEbikeBikeInfoVo;
|
||||||
import com.ebike.feign.model.vo.FeignEbikeBikeRadiusVo;
|
import com.ebike.feign.model.vo.FeignEbikeBikeRadiusVo;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import com.mybatisflex.core.query.QueryWrapper;
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
import com.mybatisflex.spring.service.impl.ServiceImpl;
|
import com.mybatisflex.spring.service.impl.ServiceImpl;
|
||||||
import feign.FeignException;
|
import feign.FeignException;
|
||||||
@ -44,63 +44,75 @@ import static com.cdzy.user.model.entity.table.EbikeOrderTableDef.EBIKE_ORDER;
|
|||||||
@Service
|
@Service
|
||||||
public class EbikeOrderImpl extends ServiceImpl<EbikeOrderMapper, EbikeOrder> implements EbikeOrderService {
|
public class EbikeOrderImpl extends ServiceImpl<EbikeOrderMapper, EbikeOrder> implements EbikeOrderService {
|
||||||
|
|
||||||
@Resource
|
|
||||||
private RedisUtil redisUtil;
|
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private EbikeOrderMapper ebikeOrderTransactionMapper;
|
private EbikeOrderMapper ebikeOrderTransactionMapper;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private OperationsFeignClient operationsFeignClient;
|
private OperationsFeignClient operationsFeignClient;
|
||||||
|
|
||||||
@Resource
|
|
||||||
private ObjectMapper objectMapper;
|
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@Override
|
@Override
|
||||||
public EbikeOrderVo saveRide(EbikeUserCyclingDto orderDto) {
|
public EbikeOrderVo saveRide(EbikeUserCyclingDto orderDto) {
|
||||||
Long userId = orderDto.getUserId();
|
Long userId = orderDto.getUserId();
|
||||||
|
String bikeCode = orderDto.getBikeCode();
|
||||||
// 校验用户当前是否存在订单
|
// 校验用户当前是否存在订单
|
||||||
EbikeOrder history = checkHistoryOrder(userId);
|
EbikeOrder history = checkHistoryOrder(userId);
|
||||||
if (Objects.nonNull(history)) {
|
if (Objects.nonNull(history)) {
|
||||||
if (history.getOrderStatus() == OrderStatus.IN_PROGRESS) {
|
if (history.getOrderStatus() == OrderStatus.IN_PROGRESS) {
|
||||||
throw new RuntimeException("请完成当前订单后再试");
|
throw new EbikeException("请完成当前订单后再试");
|
||||||
}
|
}
|
||||||
if (history.getOrderStatus() == OrderStatus.PENDING_PAYMENT) {
|
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()
|
EbikeOrder order = EbikeOrder.builder()
|
||||||
.userId(orderDto.getUserId())
|
.userId(userId)
|
||||||
|
.operatorId(bikeInfo.getOperatorId())
|
||||||
.bikeCode(orderDto.getBikeCode())
|
.bikeCode(orderDto.getBikeCode())
|
||||||
.orderType(OrderType.ONCE)
|
.orderType(OrderType.ONCE)
|
||||||
.startLocation(orderDto.getStartPoint())
|
.startLocation(orderDto.getStartPoint())
|
||||||
.startTime(LocalDateTime.now())
|
.startTime(LocalDateTime.now())
|
||||||
|
.baseFee(bikeInfo.getBaseFee())
|
||||||
|
.durationFee(bikeInfo.getDurationFee())
|
||||||
|
.freeDurationMinutes(bikeInfo.getFreeDurationMinutes())
|
||||||
|
.chargeDurationMinutes(bikeInfo.getChargeDurationMinutes())
|
||||||
|
.createTime(LocalDateTime.now())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
save(order);
|
save(order);
|
||||||
// 开锁
|
|
||||||
try {
|
try {
|
||||||
JsonResult<?> lockResult = operationsFeignClient.openLock(orderDto.getBikeCode());
|
// 尝试开锁
|
||||||
if (lockResult == null || !"200".equals(lockResult.getCode())) {
|
EbikeUnlockResultDto unlockResult = attemptUnlockWithValidation(bikeCode, userId);
|
||||||
String errorMsg = (lockResult != null && lockResult.getMessage() != null)
|
if (!unlockResult.isSuccess()) {
|
||||||
? lockResult.getMessage()
|
throw new EbikeException("开锁失败: " + unlockResult.getErrorMsg());
|
||||||
: "开锁服务返回异常";
|
|
||||||
throw new EbikeException("开锁失败: " + errorMsg);
|
|
||||||
}
|
}
|
||||||
} catch (FeignException e) {
|
// 开锁成功
|
||||||
log.error("开锁服务调用失败, bikeCode={}, userId={}", orderDto.getBikeCode(), orderDto.getUserId(), e);
|
EbikeOrder orders = EbikeOrder.builder()
|
||||||
throw new EbikeException("开锁服务暂时不可用,请稍后重试");
|
.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) {
|
} catch (Exception e) {
|
||||||
log.error("开锁过程发生系统异常", e);
|
log.error("开锁失败, userId={}, bikeCode={}", userId, bikeCode, e);
|
||||||
throw new EbikeException("系统繁忙,开锁失败");
|
throw new RuntimeException("开锁失败", e);
|
||||||
}
|
}
|
||||||
EbikeOrderVo userOrder = new EbikeOrderVo();
|
|
||||||
userOrder.setOrderId(order.getOrderId());
|
|
||||||
userOrder.setOrderTime(order.getCreateTime());
|
|
||||||
return userOrder;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EbikeOrder checkHistoryOrder(Long userId) {
|
public EbikeOrder checkHistoryOrder(Long userId) {
|
||||||
QueryWrapper queryWrapper = QueryWrapper.create()
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||||
@ -209,4 +221,37 @@ public class EbikeOrderImpl extends ServiceImpl<EbikeOrderMapper, EbikeOrder> im
|
|||||||
order.setOrderStatus(targetStatus);
|
order.setOrderStatus(targetStatus);
|
||||||
mapper.update(order);
|
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_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_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