diff --git a/electripper-admin/src/main/java/com/ruoyi/web/controller/app/AppVerifyController.java b/electripper-admin/src/main/java/com/ruoyi/web/controller/app/AppVerifyController.java index 5394ea0..bc6568a 100644 --- a/electripper-admin/src/main/java/com/ruoyi/web/controller/app/AppVerifyController.java +++ b/electripper-admin/src/main/java/com/ruoyi/web/controller/app/AppVerifyController.java @@ -1741,4 +1741,22 @@ public class AppVerifyController extends BaseController ajax.put(AjaxResult.DATA_TAG,trajectoryDetails); return ajax; } + + /** + * 根据订单号查询车辆轨迹 + */ + @PostMapping("/trajectoryDetailsByOrderNo") + public AjaxResult trajectoryDetailsByOrderNo(String orderNo) + { + AjaxResult ajax = AjaxResult.success(); + if(StrUtil.isBlank(orderNo)){ + logger.info("【根据订单号查询车辆轨迹】没有orderNo参数:【orderNo={}】",orderNo); + return error("请传订单号"+"【orderNo="+orderNo+"】"); + } + logger.info("【根据订单号查询车辆轨迹】:{}",orderNo); + List trajectoryDetails = asDeviceService.trajectoryDetailsByOrderNo(orderNo); + etLocationLogService.analytic(trajectoryDetails); + ajax.put(AjaxResult.DATA_TAG,trajectoryDetails); + return ajax; + } } diff --git a/electripper-common/src/main/java/com/ruoyi/common/constant/ServiceConstants.java b/electripper-common/src/main/java/com/ruoyi/common/constant/ServiceConstants.java index bf45fc8..17ebbcc 100644 --- a/electripper-common/src/main/java/com/ruoyi/common/constant/ServiceConstants.java +++ b/electripper-common/src/main/java/com/ruoyi/common/constant/ServiceConstants.java @@ -414,6 +414,16 @@ public class ServiceConstants { */ public static final String TRIP_LOG_TYPE_RETRUN_LOCK = "4"; + /** + * 批量操作类型:5-换车关锁 + */ + public static final String TRIP_LOG_TYPE_CHANGE_LOCK = "5"; + + /** + * 批量操作类型:6-换车开锁 + */ + public static final String TRIP_LOG_TYPE_CHANGE_UNLOCK = "6"; + /**----------------------------行程记录类型end----------------------------*/ /**----------------------------退款类型start----------------------------*/ diff --git a/electripper-system/src/main/java/com/ruoyi/system/domain/EtTripLog.java b/electripper-system/src/main/java/com/ruoyi/system/domain/EtTripLog.java index 4470c6a..a3b2dc7 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/domain/EtTripLog.java +++ b/electripper-system/src/main/java/com/ruoyi/system/domain/EtTripLog.java @@ -48,4 +48,8 @@ public class EtTripLog extends BaseEntity @Excel(name = "车辆状态") private String deviceStatus; + /** describe 描述 */ + @Excel(name = "描述") + private String describe; + } diff --git a/electripper-system/src/main/java/com/ruoyi/system/service/IAsDeviceService.java b/electripper-system/src/main/java/com/ruoyi/system/service/IAsDeviceService.java index 87bc4b3..279b762 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/service/IAsDeviceService.java +++ b/electripper-system/src/main/java/com/ruoyi/system/service/IAsDeviceService.java @@ -422,6 +422,11 @@ public interface IAsDeviceService extends IService */ List trajectoryDetails(String sn, String startTime, String endTime); + /** + * 根据订单号查询车辆轨迹 + */ + List trajectoryDetailsByOrderNo(String orderNo); + /** * 根据订单号查询车辆轨迹 */ diff --git a/electripper-system/src/main/java/com/ruoyi/system/service/impl/AsDeviceServiceImpl.java b/electripper-system/src/main/java/com/ruoyi/system/service/impl/AsDeviceServiceImpl.java index 1bf2035..39456d2 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/service/impl/AsDeviceServiceImpl.java +++ b/electripper-system/src/main/java/com/ruoyi/system/service/impl/AsDeviceServiceImpl.java @@ -51,6 +51,8 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import static com.ruoyi.common.constant.HttpStatus.ERROR_CODE_DEVICE_ALREADY_EXISTS_MSG; +import static com.ruoyi.common.constant.ServiceConstants.TRIP_LOG_TYPE_CHANGE_LOCK; +import static com.ruoyi.common.constant.ServiceConstants.TRIP_LOG_TYPE_CHANGE_UNLOCK; import static com.ruoyi.common.utils.SecurityUtils.getUsername; /** @@ -120,6 +122,9 @@ public class AsDeviceServiceImpl extends ServiceImpl i @Resource private AsUserMapper asUserMapper; + @Autowired + private IEtTripLogService etTripLogService; + @Value(value = "${iot.iotUrl}") private String iotUrl; @@ -2837,6 +2842,108 @@ public class AsDeviceServiceImpl extends ServiceImpl i return etLocationLogMapper.selectEtLocationLogListByCreateTime(device.getMac(), startTime, endTime); } + /** + * 根据订单号查询车辆轨迹 + */ + @Override + public List trajectoryDetailsByOrderNo(String orderNo) { + EtOrder order = etOrderMapper.selectEtOrderByOrderNo(orderNo); + ServiceUtil.assertion(ObjectUtil.isNull(order), "订单不存在"); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + if(StrUtil.isBlank(order.getUsedSn())){ + return trajectoryDetails(order.getSn(), sdf.format(order.getUnlockTime()), sdf.format(order.getReturnTime())); + } + + List allLocationLogs = new ArrayList<>(); + List tripLogs = fetchTripLogs(order); + + if (ObjectUtils.isNotEmpty(tripLogs)) { + processTripLogs(tripLogs, allLocationLogs, sdf, order); + } else if (StringUtils.isBlank(order.getUsedSn())) { + // 如果没有行程记录且没有换车情况,直接查询从unlockTime到returnTime的数据 + queryDeviceLocationLogs(order.getSn(), order.getUnlockTime(), order.getReturnTime(), sdf, allLocationLogs); + } + + return allLocationLogs; + } + + private List fetchTripLogs(EtOrder order) { + EtTripLog tripLogQuery = new EtTripLog(); + tripLogQuery.setOrderNo(order.getOrderNo()); + return etTripLogService.selectEtTripLogList(tripLogQuery); + } + + private void processTripLogs(List tripLogs, List allLocationLogs, SimpleDateFormat sdf, EtOrder order) { + for (int i = 0; i < tripLogs.size(); i++) { + EtTripLog currentLog = tripLogs.get(i); + if (TRIP_LOG_TYPE_CHANGE_LOCK.equals(currentLog.getType())) { + handleChangeLock(currentLog, tripLogs, allLocationLogs, sdf, i); + } + } + + if (StringUtils.isNotBlank(order.getUsedSn())) { + handleLastUnlockToReturnTime(tripLogs, allLocationLogs, sdf, order); + } + } + + private void handleChangeLock(EtTripLog currentLog, List tripLogs, List allLocationLogs, SimpleDateFormat sdf, int currentIndex) { + final String TRIP_LOG_TYPE_CHANGE_UNLOCK = "6"; + + for (int j = currentIndex + 1; j < tripLogs.size(); j++) { + EtTripLog nextLog = tripLogs.get(j); + if (TRIP_LOG_TYPE_CHANGE_UNLOCK.equals(nextLog.getType()) && nextLog.getSn() != null) { + AsDevice device = asDeviceMapper.selectAsDeviceBySn(currentLog.getSn()); + if (device == null) { + throw new RuntimeException("设备SN:" + currentLog.getSn() + " 不存在"); + } + List locationLogs = etLocationLogMapper.selectEtLocationLogListByCreateTime( + device.getMac(), + sdf.format(currentLog.getCreateTime()), + sdf.format(nextLog.getCreateTime())); + if (locationLogs != null) { + allLocationLogs.addAll(locationLogs); + } + break; // 找到对应的换车开锁后跳出循环 + } + } + } + + private void handleLastUnlockToReturnTime(List tripLogs, List allLocationLogs, SimpleDateFormat sdf, EtOrder order) { + final String TRIP_LOG_TYPE_CHANGE_UNLOCK = "6"; + Optional lastUnlockLogOpt = tripLogs.stream() + .filter(log -> TRIP_LOG_TYPE_CHANGE_UNLOCK.equals(log.getType())) + .max(Comparator.comparing(EtTripLog::getCreateTime)); + if (lastUnlockLogOpt.isPresent()) { + EtTripLog lastUnlockLog = lastUnlockLogOpt.get(); + AsDevice device = asDeviceMapper.selectAsDeviceBySn(lastUnlockLog.getSn()); + if (device == null) { + throw new RuntimeException("设备SN:" + lastUnlockLog.getSn() + " 不存在"); + } + List finalLocationLogs = etLocationLogMapper.selectEtLocationLogListByCreateTime( + device.getMac(), + sdf.format(lastUnlockLog.getCreateTime()), + sdf.format(order.getReturnTime())); + if (finalLocationLogs != null) { + allLocationLogs.addAll(finalLocationLogs); + } + } + } + + private void queryDeviceLocationLogs(String sn, Date startTime, Date endTime, SimpleDateFormat sdf, List allLocationLogs) { + AsDevice device = asDeviceMapper.selectAsDeviceBySn(sn); + if (device == null) { + throw new RuntimeException("设备SN:" + sn + " 不存在"); + } + List locationLogs = etLocationLogMapper.selectEtLocationLogListByCreateTime( + device.getMac(), + sdf.format(startTime), + sdf.format(endTime)); + if (locationLogs != null) { + allLocationLogs.addAll(locationLogs); + } + } + /** * 根据时间查询车辆轨迹 diff --git a/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtOrderServiceImpl.java b/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtOrderServiceImpl.java index 8d7c444..39512cc 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtOrderServiceImpl.java +++ b/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtOrderServiceImpl.java @@ -160,6 +160,9 @@ public class EtOrderServiceImpl implements IEtOrderService @Resource private EtReconciliationMapper reconciliationMapper; + @Resource + private IEtTripLogService tripLogService; + /** * 查询订单 * @@ -2324,9 +2327,12 @@ public class EtOrderServiceImpl implements IEtOrderService log.info("【换车关锁】改变订单使用过的sn失败"); throw new ServiceException("【换车关锁】改变订单使用过的sn失败"); } + /** 5.记录行程*/ + int tripLog = tripLogService.tripLog(order.getOrderNo(),sn,ServiceConstants.TRIP_LOG_TYPE_CHANGE_LOCK); + ServiceUtil.assertion(tripLog==0,"【换车关锁记录行程失败"); return Boolean.TRUE; }); - if(execute){ + if(Boolean.TRUE.equals(execute)){ return Boolean.TRUE; }else{ return Boolean.FALSE; @@ -2353,7 +2359,7 @@ public class EtOrderServiceImpl implements IEtOrderService if (ObjectUtil.isNull(newDevice)) { throw new ServiceException("设备不存在:"+ newSn); } - if(order.getAreaId() != newDevice.getAreaId()){ + if(!order.getAreaId().equals(newDevice.getAreaId())){ throw new ServiceException("不同运营区,请勿操作"); } /** 1.获取token*/ @@ -2395,9 +2401,12 @@ public class EtOrderServiceImpl implements IEtOrderService log.info("【换车开锁】更新订单sn失败"); throw new ServiceException("【换车开锁】更新订单sn失败"); } + /** 5.记录行程*/ + int tripLog = tripLogService.tripLog(order.getOrderNo(),newSn,ServiceConstants.TRIP_LOG_TYPE_CHANGE_UNLOCK); + ServiceUtil.assertion(tripLog==0,"【换车开锁】记录行程失败"); return Boolean.TRUE; }); - if(!execute)throw new ServiceException("换车开锁失败"); + if(Boolean.FALSE.equals(execute))throw new ServiceException("换车开锁失败"); return Boolean.TRUE; } diff --git a/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtTripLogServiceImpl.java b/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtTripLogServiceImpl.java index ef803a6..13f9dee 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtTripLogServiceImpl.java +++ b/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtTripLogServiceImpl.java @@ -118,13 +118,10 @@ public class EtTripLogServiceImpl implements IEtTripLogService EtTripLog etTripLog = new EtTripLog(); etTripLog.setType(type); etTripLog.setOrderNo(orderNo); - AsDevice asDevice = null; if(StrUtil.isNotBlank(sn)){ - asDevice = asDeviceMapper.selectAsDeviceBySn(sn); + AsDevice asDevice = asDeviceMapper.selectAsDeviceBySn(sn); etTripLog.setLatitude(asDevice.getLatitude()); etTripLog.setLongitude(asDevice.getLongitude()); - String location = asDevice.getLongitude() + ","+asDevice.getLatitude(); -// etTripLog.setAddress(CommonUtil.getAddressByGeo(location)); etTripLog.setSn(sn); } log.info("创建行程记录:{}", JSON.toJSON(etTripLog)); diff --git a/electripper-system/src/main/resources/mapper/system/EtOrderMapper.xml b/electripper-system/src/main/resources/mapper/system/EtOrderMapper.xml index 42ebe65..0bd236e 100644 --- a/electripper-system/src/main/resources/mapper/system/EtOrderMapper.xml +++ b/electripper-system/src/main/resources/mapper/system/EtOrderMapper.xml @@ -437,6 +437,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" u.user_name AS userName, u.phonenumber AS phonenumber, o.rule_id, + o.user_id, o.coupon_id, o.log_id, o.device_mac, diff --git a/electripper-system/src/main/resources/mapper/system/EtTripLogMapper.xml b/electripper-system/src/main/resources/mapper/system/EtTripLogMapper.xml index c49489b..560243a 100644 --- a/electripper-system/src/main/resources/mapper/system/EtTripLogMapper.xml +++ b/electripper-system/src/main/resources/mapper/system/EtTripLogMapper.xml @@ -27,6 +27,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" and address = #{address} and type = #{type} + order by create_time