开锁逻辑封装

This commit is contained in:
attiya 2025-04-17 15:31:30 +08:00
parent fbf4c288a0
commit ac6ef73cfe
4 changed files with 63 additions and 22 deletions

View File

@ -9,6 +9,7 @@ import com.cdzy.orders.mapper.EbikeParamValueMapper;
import com.cdzy.orders.model.entity.EbikeCmd; import com.cdzy.orders.model.entity.EbikeCmd;
import com.cdzy.orders.model.entity.EbikeParam; import com.cdzy.orders.model.entity.EbikeParam;
import com.cdzy.orders.model.entity.EbikeParamValue; import com.cdzy.orders.model.entity.EbikeParamValue;
import com.ebike.feign.model.rsp.FeignEbikeEcuInfo;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -41,16 +42,14 @@ public class EbikeCoreHandler {
/** /**
* 下发命令 * 下发命令
* *
* @param ecuID 中控ID * @param ebikeEcuInfo 中控信息
* @return 一致性返回 * @return 一致性返回
*/ */
public CompletableFuture<String> executeCommand(Long ecuID, String cmdCode,Long bikeId,String userId) { public CompletableFuture<String> executeCommand(FeignEbikeEcuInfo ebikeEcuInfo, String cmdCode, Long bikeId, String userId) {
CompletableFuture<String> future = new CompletableFuture<>(); CompletableFuture<String> future = new CompletableFuture<>();
// EbikeEcuInfoService ebikeEcuInfoService = SpringContextHolder.getBean(EbikeEcuInfoService.class);
// EbikeEcuInfo ebikeEcuInfo = ebikeEcuInfoService.getById(ecuID);
CMDMsg cmdMsg = new CMDMsg(); CMDMsg cmdMsg = new CMDMsg();
// cmdMsg.setDeviceId(ebikeEcuInfo.getEcuSn()); cmdMsg.setDeviceId(ebikeEcuInfo.getEcuSn());
// cmdMsg.setGroup(ebikeEcuInfo.getOperatorCode()); cmdMsg.setGroup(ebikeEcuInfo.getOperatorCode());
String tid = UUID.randomUUID().toString().replace("-", ""); String tid = UUID.randomUUID().toString().replace("-", "");
String command = getCommand(cmdCode, tid); String command = getCommand(cmdCode, tid);
cmdMsg.setCommand(command); cmdMsg.setCommand(command);
@ -173,7 +172,6 @@ public class EbikeCoreHandler {
}); });
jsonObject.put(ebikeParam.getParamName(), param); jsonObject.put(ebikeParam.getParamName(), param);
}); });
System.out.println(jsonObject.toJSONString());
return jsonObject.toJSONString(); return jsonObject.toJSONString();
} }
} }

View File

@ -130,4 +130,7 @@ public class UserOrders implements Serializable {
*/ */
private Integer tempLock; private Integer tempLock;
@Column(isLogicDelete = true,onInsertValue = "0")
private Integer isDelete;
} }

View File

@ -2,8 +2,11 @@ package com.cdzy.orders.service.impl;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.cdzy.common.enums.Code; import com.cdzy.common.enums.Code;
import com.cdzy.common.model.CoreResult;
import com.cdzy.common.model.JsonResult; import com.cdzy.common.model.JsonResult;
import com.cdzy.common.model.ResGPSDto; import com.cdzy.common.model.ResGPSDto;
import com.cdzy.orders.component.EbikeCoreHandler;
import com.cdzy.orders.enums.CmdCode;
import com.cdzy.orders.enums.EbikeRegionInOperation; import com.cdzy.orders.enums.EbikeRegionInOperation;
import com.cdzy.orders.enums.OrderStatus; import com.cdzy.orders.enums.OrderStatus;
import com.cdzy.orders.enums.OrderType; import com.cdzy.orders.enums.OrderType;
@ -31,6 +34,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import static com.cdzy.orders.model.entity.table.UserOrdersTableDef.USER_ORDERS; import static com.cdzy.orders.model.entity.table.UserOrdersTableDef.USER_ORDERS;
@ -55,10 +59,14 @@ public class UserOrdersServiceImpl extends ServiceImpl<UserOrdersMapper, UserOrd
@Resource @Resource
OperateFeignClient operateFeignClient; OperateFeignClient operateFeignClient;
@Resource
EbikeCoreHandler ebikeCoreHandler;
@Override @Override
@Transactional @Transactional
public Long saveRide(ReqOrderDto orderDto) { public Long saveRide(ReqOrderDto orderDto) {
UserOrders history = checkHistoryOrder(orderDto.getUserId()); String userId = orderDto.getUserId();
UserOrders history = checkHistoryOrder(userId);
if (history != null && history.getStatus() == OrderStatus.IN_PROGRESS) { if (history != null && history.getStatus() == OrderStatus.IN_PROGRESS) {
throw new RuntimeException("请完成当前订单后再试"); throw new RuntimeException("请完成当前订单后再试");
} }
@ -66,22 +74,22 @@ public class UserOrdersServiceImpl extends ServiceImpl<UserOrdersMapper, UserOrd
throw new RuntimeException("请支付未支付订单后再试"); throw new RuntimeException("请支付未支付订单后再试");
} }
UserOrders userOrders = new UserOrders(); UserOrders userOrders = new UserOrders();
userOrders.setUserId(orderDto.getUserId()); userOrders.setUserId(userId);
userOrders.setBikeCode(orderDto.getBikeCode()); userOrders.setBikeCode(orderDto.getBikeCode());
userOrders.setOrderType(OrderType.ONCE); userOrders.setOrderType(OrderType.ONCE);
userOrders.setStartTime(LocalDateTime.now()); userOrders.setStartTime(LocalDateTime.now());
JsonResult<FeignEbikeBikeInfoDto> jsonResult = maintenanceFeignClient.getEbikeInfoByCode(orderDto.getBikeCode()); JsonResult<FeignEbikeBikeInfoDto> jsonResult = maintenanceFeignClient.getEbikeInfoByCode(orderDto.getBikeCode());
if (jsonResult.getCode() != Code.SUCCESS){ if (jsonResult.getCode() != Code.SUCCESS) {
throw new RuntimeException("获取车辆信息失败"); throw new RuntimeException("获取车辆信息失败");
} }
FeignEbikeBikeInfoDto bikeInfoDto = jsonResult.getData(); FeignEbikeBikeInfoDto bikeInfoDto = jsonResult.getData();
FeignEbikeEcuInfo ecuInfo = bikeInfoDto.getEbikeEcuInfo(); FeignEbikeEcuInfo ecuInfo = bikeInfoDto.getEbikeEcuInfo();
JsonResult<FeignEbikeRegionDto> operationResult = operateFeignClient.getOperationById(bikeInfoDto.getReginId()); JsonResult<FeignEbikeRegionDto> operationResult = operateFeignClient.getOperationById(bikeInfoDto.getReginId());
if (operationResult.getCode() != Code.SUCCESS){ if (operationResult.getCode() != Code.SUCCESS) {
throw new RuntimeException("获取运营区信息失败"); throw new RuntimeException("获取运营区信息失败");
} }
FeignEbikeRegionDto regionDto = operationResult.getData(); FeignEbikeRegionDto regionDto = operationResult.getData();
if (regionDto.getRegionId() == EbikeRegionInOperation.OUT){ if (regionDto.getRegionId() == EbikeRegionInOperation.OUT) {
throw new RuntimeException("当前运营区已停止运营"); throw new RuntimeException("当前运营区已停止运营");
} }
String jsonString = JSONObject.toJSONString(redisUtil.get(ecuInfo.getEcuSn())); String jsonString = JSONObject.toJSONString(redisUtil.get(ecuInfo.getEcuSn()));
@ -90,8 +98,13 @@ public class UserOrdersServiceImpl extends ServiceImpl<UserOrdersMapper, UserOrd
if (!pointInOperation){ if (!pointInOperation){
throw new RuntimeException("当前车辆再运营区外"); throw new RuntimeException("当前车辆再运营区外");
} }
//TODO:开锁,并且等待结果 //开锁,并且等待结果
CompletableFuture<String> stringCompletableFuture = ebikeCoreHandler.executeCommand(ecuInfo, CmdCode.UNLOCK, Long.valueOf(bikeInfoDto.getBikeId()), userId);
String response = stringCompletableFuture.join();
JSONObject jsonObject = JSONObject.parseObject(response);
if (jsonObject.getInteger("code") != 0) {
throw new RuntimeException("开锁失败");
}
userOrders.setBikeId(bikeInfoDto.getBikeId()); userOrders.setBikeId(bikeInfoDto.getBikeId());
String stringBuilder = resGpsDto.getLongitude() + "," + resGpsDto.getLatitude(); String stringBuilder = resGpsDto.getLongitude() + "," + resGpsDto.getLatitude();
userOrders.setRidePoint(stringBuilder); userOrders.setRidePoint(stringBuilder);
@ -111,7 +124,7 @@ public class UserOrdersServiceImpl extends ServiceImpl<UserOrdersMapper, UserOrd
UserOrders userOrders = getOrder(orderDto); UserOrders userOrders = getOrder(orderDto);
userOrders.setEndTime(LocalDateTime.now()); userOrders.setEndTime(LocalDateTime.now());
JsonResult<FeignEbikeBikeInfoDto> jsonResult = maintenanceFeignClient.getEbikeInfoByCode(orderDto.getBikeCode()); JsonResult<FeignEbikeBikeInfoDto> jsonResult = maintenanceFeignClient.getEbikeInfoByCode(orderDto.getBikeCode());
if (jsonResult.getCode() != Code.SUCCESS){ if (jsonResult.getCode() != Code.SUCCESS) {
throw new RuntimeException("获取车辆信息失败"); throw new RuntimeException("获取车辆信息失败");
} }
FeignEbikeBikeInfoDto bikeInfoDto = jsonResult.getData(); FeignEbikeBikeInfoDto bikeInfoDto = jsonResult.getData();
@ -121,11 +134,11 @@ public class UserOrdersServiceImpl extends ServiceImpl<UserOrdersMapper, UserOrd
userOrders.setReturnPoint(resGpsDto.getLongitude() + "," + resGpsDto.getLatitude()); userOrders.setReturnPoint(resGpsDto.getLongitude() + "," + resGpsDto.getLatitude());
//计算骑行时长 //计算骑行时长
JsonResult<ResFeignEbikeSysRcostsetDto> operateJsonResult = operateFeignClient.getRegionFeeConfigById(bikeInfoDto.getReginId()); JsonResult<ResFeignEbikeSysRcostsetDto> operateJsonResult = operateFeignClient.getRegionFeeConfigById(bikeInfoDto.getReginId());
if (operateJsonResult.getCode() != Code.SUCCESS){ if (operateJsonResult.getCode() != Code.SUCCESS) {
throw new RuntimeException("获取计费规则失败"); throw new RuntimeException("获取计费规则失败");
} }
JsonResult<FeignEbikeRegionDto> regionResult = operateFeignClient.getOperationById(bikeInfoDto.getReginId()); JsonResult<FeignEbikeRegionDto> regionResult = operateFeignClient.getOperationById(bikeInfoDto.getReginId());
if (regionResult.getCode() != Code.SUCCESS){ if (regionResult.getCode() != Code.SUCCESS) {
throw new RuntimeException("获取运营区信息失败"); throw new RuntimeException("获取运营区信息失败");
} }
FeignEbikeRegionDto regionDto = regionResult.getData(); FeignEbikeRegionDto regionDto = regionResult.getData();
@ -168,15 +181,15 @@ public class UserOrdersServiceImpl extends ServiceImpl<UserOrdersMapper, UserOrd
@Override @Override
public List<RspBikeDto> bikeList(ReqBikeDto bikeDto) { public List<RspBikeDto> bikeList(ReqBikeDto bikeDto) {
List<RedisPoint> nearbyMembers = redisUtil.findNearbyMembers(bikeDto.getLongitude(), bikeDto.getLatitude(), bikeDto.getRadius()==null?3.0:bikeDto.getRadius()); List<RedisPoint> nearbyMembers = redisUtil.findNearbyMembers(bikeDto.getLongitude(), bikeDto.getLatitude(), bikeDto.getRadius() == null ? 3.0 : bikeDto.getRadius());
return nearbyMembers.stream().filter(Objects::nonNull).map(redisPoint -> { return nearbyMembers.stream().filter(Objects::nonNull).map(redisPoint -> {
String jsonString = JSONObject.toJSONString(redisUtil.get(redisPoint.getMember())); String jsonString = JSONObject.toJSONString(redisUtil.get(redisPoint.getMember()));
ResGPSDto resGpsDto = JSONObject.parseObject(jsonString, ResGPSDto.class); ResGPSDto resGpsDto = JSONObject.parseObject(jsonString, ResGPSDto.class);
return new RspBikeDto(redisPoint.getMember(), resGpsDto.getSoc(),redisPoint.getPoint().getX(), redisPoint.getPoint().getY()); return new RspBikeDto(redisPoint.getMember(), resGpsDto.getSoc(), redisPoint.getPoint().getX(), redisPoint.getPoint().getY());
}).toList(); }).toList();
} }
boolean bikeInOperation(double lng, double lat,Long orgId,Long regionId){ boolean bikeInOperation(double lng, double lat, Long orgId, Long regionId) {
return redisUtil.isPointInOperation(lng, lat, orgId, regionId); return redisUtil.isPointInOperation(lng, lat, orgId, regionId);
} }
} }

View File

@ -1,15 +1,23 @@
package com.cdzy.orders; package com.cdzy.orders;
import com.cdzy.common.enums.Code;
import com.cdzy.common.model.JsonResult;
import com.cdzy.orders.component.EbikeCoreHandler;
import com.cdzy.orders.enums.CmdCode;
import com.cdzy.orders.mapper.UserOrdersMapper; import com.cdzy.orders.mapper.UserOrdersMapper;
import com.cdzy.orders.model.dto.res.RedisPoint; import com.cdzy.orders.model.dto.res.RedisPoint;
import com.cdzy.orders.model.entity.UserOrders; import com.cdzy.orders.model.entity.UserOrders;
import com.cdzy.orders.uitls.RedisUtil; import com.cdzy.orders.uitls.RedisUtil;
import com.cdzy.orders.uitls.TimeUtils; import com.cdzy.orders.uitls.TimeUtils;
import com.ebike.feign.clients.MaintenanceFeignClient;
import com.ebike.feign.model.rsp.FeignEbikeBikeInfoDto;
import com.ebike.feign.model.rsp.FeignEbikeEcuInfo;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture;
@SpringBootTest @SpringBootTest
class EbikeOrdersApplicationTests { class EbikeOrdersApplicationTests {
@ -20,6 +28,12 @@ class EbikeOrdersApplicationTests {
@Resource @Resource
UserOrdersMapper ordersMapper; UserOrdersMapper ordersMapper;
@Resource
EbikeCoreHandler ebikeCoreHandler;
@Resource
MaintenanceFeignClient maintenanceFeignClient;
@Test @Test
void contextLoads() { void contextLoads() {
double lng = 103.975457 ; double lng = 103.975457 ;
@ -35,4 +49,17 @@ class EbikeOrdersApplicationTests {
System.out.println(minutes); System.out.println(minutes);
} }
@Test
void send() {
JsonResult<FeignEbikeBikeInfoDto> jsonResult = maintenanceFeignClient.getEbikeInfoByCode("250306001002");
if (jsonResult.getCode() != Code.SUCCESS){
throw new RuntimeException("获取车辆信息失败");
}
FeignEbikeBikeInfoDto bikeInfoDto = jsonResult.getData();
FeignEbikeEcuInfo ecuInfo = bikeInfoDto.getEbikeEcuInfo();
CompletableFuture<String> stringCompletableFuture = ebikeCoreHandler.executeCommand(ecuInfo, CmdCode.FIND_EBIKE, 259806209647882240L, "267412794681929728");
stringCompletableFuture.join();
System.out.println("成功");
}
} }