localizer
lzq 2025-10-11 17:37:14 +08:00
parent 4194ce06fc
commit 47918629f7
5 changed files with 70 additions and 96 deletions

View File

@ -45,6 +45,7 @@ public class SearchTruckResult {
*
*/
private String vnCode;
private Boolean online;
/**
*

View File

@ -27,6 +27,7 @@ import java.util.List;
@RequiredArgsConstructor
public class OrderInfoController {
private final OrderInfoService orderInfoService;
// private final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 100, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100), new ThreadPoolExecutor.CallerRunsPolicy());
/**
*
@ -43,8 +44,14 @@ public class OrderInfoController {
if (estimatedQuantity != null && estimatedQuantity > 0) {
addOrderInfoParam.setEstimatedQuantity(estimatedQuantity / truckCount);
}
orderInfoService.batchAdd(addOrderInfoParam);
/* CompletableFuture.runAsync(() -> {
}, threadPoolExecutor).whenComplete((v, e) -> {
if (e != null) {
log.error("新增订单失败", e);
}
}); */
return R.success();
}

View File

@ -42,6 +42,7 @@ public class AddOrderInfoParam implements Constrained {
*
*/
private String contacts;
private String sn;
/**
*

View File

@ -2,6 +2,7 @@ package com.njzscloud.supervisory.order.service;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
@ -47,9 +48,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import java.math.BigDecimal;
import java.time.Duration;
@ -57,8 +56,7 @@ import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import static com.njzscloud.supervisory.constant.Constant.CERTIFICATE_SN_CODE;
@ -84,7 +82,6 @@ public class OrderInfoService extends ServiceImpl<OrderInfoMapper, OrderInfoEnti
private final StationManageService stationManageService;
private final PlatformTransactionManager transactionManager;
private final CloudVoiceboxService cloudVoiceboxService;
private final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 100, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100), new ThreadPoolExecutor.CallerRunsPolicy());
private static void stopTuqiangTrack(String gpsId) {
CompletableFuture.runAsync(() -> {
@ -104,7 +101,7 @@ public class OrderInfoService extends ServiceImpl<OrderInfoMapper, OrderInfoEnti
/**
*
*/
public void add(AddOrderInfoParam addOrderInfoParam) {
public void add(AddOrderInfoParam addOrderInfoParam, String orderSn) {
AddOrderCargoPlaceParam cargoPlace = addOrderInfoParam.getCargoPlace();
long cargoPlaceId = orderCargoPlaceService.add(cargoPlace);
Long userId = SecurityUtil.currentUserId();
@ -117,9 +114,8 @@ public class OrderInfoService extends ServiceImpl<OrderInfoMapper, OrderInfoEnti
Long orderGoodsId = orderGoodsService.add(goodsId);
String orderSn = SnUtil.next();
OrderInfoEntity orderInfoEntity = BeanUtil.copyProperties(addOrderInfoParam, OrderInfoEntity.class)
.setSn(orderSn)
.setSn(orderSn == null ? SnUtil.next() : orderSn)
.setCargoPlaceId(cargoPlaceId)
.setUserId(userId)
.setOrderStatus(orderStatus)
@ -135,10 +131,10 @@ public class OrderInfoService extends ServiceImpl<OrderInfoMapper, OrderInfoEnti
OrderCategory orderCategory = addOrderInfoParam.getOrderCategory();
if (orderCategory == OrderCategory.DuanBoChu) {
this.add(addOrderInfoParam.setOrderCategory(OrderCategory.DuanBoRu)
.setTargetOrderId(orderId)
.setTargetStationId(addOrderInfoParam.getStationId())
.setStationId(addOrderInfoParam.getTargetStationId())
);
.setTargetOrderId(orderId)
.setTargetStationId(addOrderInfoParam.getStationId())
.setStationId(addOrderInfoParam.getTargetStationId()),
null);
} else if (orderCategory == OrderCategory.DuanBoRu) {
this.updateById(new OrderInfoEntity().setId(addOrderInfoParam.getTargetOrderId())
@ -704,15 +700,23 @@ public class OrderInfoService extends ServiceImpl<OrderInfoMapper, OrderInfoEnti
);
}
log.info("mqtt 收到消息:{} {}", gpsId + "/track_location", realtimeLocationResult);
String time = realtimeLocationResult.getTime();
double latitude = realtimeLocationResult.getLatitude();
double longitude = realtimeLocationResult.getLongitude();
if (latitude <= 0 && longitude <= 0) {
return;
}
TruckLocationTrackEntity entity = new TruckLocationTrackEntity()
.setOrderId(orderInfoId)
.setTruckId(truckId)
.setTerminalId(gpsId)
.setLatitude(realtimeLocationResult.getLatitude())
.setLongitude(realtimeLocationResult.getLongitude())
.setLatitude(latitude)
.setLongitude(longitude)
.setAltitude(realtimeLocationResult.getAltitude())
.setSpeed(realtimeLocationResult.getSpeed())
.setLocationTime(LocalDateTime.now())
.setLocationTime(DateUtil.parseLocalDateTime(time))
.setDirection(realtimeLocationResult.getDirection())
.setOverspeed(realtimeLocationResult.isOverspeed())
.setCompensate(realtimeLocationResult.getType() == 1);
@ -768,10 +772,12 @@ public class OrderInfoService extends ServiceImpl<OrderInfoMapper, OrderInfoEnti
.setId(orderInfo.getId())
.setOrderStatus(OrderStatus.YiQuXiao)
);
Long driverId = orderInfo.getDriverId();
Long truckId = orderInfo.getTruckId();
if (driverId != null) baseMapper.busyDriver(driverId, Boolean.FALSE);
if (truckId != null) baseMapper.busyTruck(truckId, Boolean.FALSE);
// TODO 关闭 GPS 定位
CompletableFuture.runAsync(() -> {
Long truckId = orderInfo.getTruckId();
if (truckId == null) {
return;
}
@ -1109,59 +1115,15 @@ public class OrderInfoService extends ServiceImpl<OrderInfoMapper, OrderInfoEnti
return BeanUtil.copyProperties(orderDetail, TrainBillResult.class);
}
@Transactional(rollbackFor = Exception.class)
public void batchAdd(AddOrderInfoParam addOrderInfoParam) {
Integer truckCount = addOrderInfoParam.getTruckCount();
CountDownLatch latch = new CountDownLatch(truckCount);
AtomicBoolean hasError = new AtomicBoolean(false);
List<TransactionStatus> transactionStatuses = new CopyOnWriteArrayList<>();
String orderSn = null;
if (truckCount > 1) {
orderSn = SnUtil.next();
}
for (int i = 0; i < truckCount; i++) {
CompletableFuture.runAsync(() -> {
// 每个线程创建独立事务
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
TransactionStatus status = transactionManager.getTransaction(def);
try {
// 执行具体的插入操作
add(addOrderInfoParam);
// 暂存事务状态
transactionStatuses.add(status);
} catch (Exception e) {
// 标记错误状态
hasError.set(true);
// 记录错误但不立即回滚,等待统一处理
transactionStatuses.add(status);
throw Exceptions.error(e, "新增订单失败");
}
}, threadPoolExecutor).whenComplete((v, e) -> {
latch.countDown();
if (e != null) {
log.error("新增订单失败", e);
}
});
add(addOrderInfoParam, orderSn == null ? null : orderSn + "-" + (i + 1));
}
try {
latch.await();
} catch (InterruptedException e) {
hasError.set(true);
log.warn("任务中断", e);
}
// 根据执行结果统一处理事务
if (hasError.get()) {
// 有错误,全部回滚
for (TransactionStatus status : transactionStatuses) {
if (!status.isCompleted()) {
transactionManager.rollback(status);
}
}
throw Exceptions.exception("批量创建订单失败");
} else {
// 全部成功,统一提交
for (TransactionStatus status : transactionStatuses) {
if (!status.isCompleted()) {
transactionManager.commit(status);
}
}
}
}
}

View File

@ -9,8 +9,7 @@
<result property="qualification" column="qualification" typeHandler="com.njzscloud.common.mp.support.handler.j.JsonTypeHandler"/>
</resultMap>
<select id="paging" resultMap="SearchTruckResultMap">
SELECT
a.id,
SELECT a.id,
a.company_id,
b.company_name,
b.uscc,
@ -33,9 +32,11 @@
a.gps,
a.picture,
a.truck_category,
a.disabled
a.disabled,
c.last_time >= DATE_SUB(NOW(), INTERVAL 5 MINUTE) `online`
FROM biz_truck a
INNER JOIN biz_company b ON b.id = a.company_id
INNER JOIN device_localizer c ON c.terminal_id = a.gps
<if test="ew.customSqlSegment != null and ew.customSqlSegment != ''">
${ew.customSqlSegment}
</if>
@ -43,32 +44,34 @@
</select>
<select id="selectDetailById" resultMap="SearchTruckResultMap">
SELECT
a.id,
a.company_id,
b.company_name,
b.uscc,
a.license_plate,
a.truck_license,
a.vn_code,
a.qualification,
a.carrying_capacity,
a.tare_weight,
a.audit_status,
a.audit_memo,
a.creator_id,
a.modifier_id,
a.create_time,
a.modify_time,
a.truck_license_date,
a.qualification_date,
a.deleted,
a.gps,
a.picture,
a.truck_category,
a.disabled
SELECT a.id,
a.company_id,
b.company_name,
b.uscc,
a.license_plate,
a.truck_license,
a.vn_code,
a.qualification,
a.carrying_capacity,
a.tare_weight,
a.audit_status,
a.audit_memo,
a.creator_id,
a.modifier_id,
a.create_time,
a.modify_time,
a.truck_license_date,
a.qualification_date,
a.deleted,
a.busy,
a.gps,
a.picture,
a.truck_category,
a.disabled,
c.last_time >= DATE_SUB(NOW(), INTERVAL 5 MINUTE) `online`
FROM biz_truck a
INNER JOIN biz_company b ON b.id = a.company_id AND b.deleted = 0
INNER JOIN biz_company b ON b.id = a.company_id
INNER JOIN device_localizer c ON c.terminal_id = a.gps
WHERE a.id = #{id} AND a.deleted = 0
</select>
</mapper>