批量创建订单
parent
a030cc0e38
commit
fc58a14b66
|
|
@ -96,4 +96,8 @@ public class SearchTruckResult {
|
|||
*/
|
||||
private Boolean disabled;
|
||||
|
||||
/**
|
||||
* gps编码
|
||||
*/
|
||||
private String gps;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,14 @@ public class OrderInfoController {
|
|||
Assert.isTrue(orderCategory == OrderCategory.PuTong
|
||||
|| orderCategory == OrderCategory.DuanBoChu
|
||||
, "订单类型必须是普通订单、短驳出");
|
||||
orderInfoService.add(addOrderInfoParam);
|
||||
Integer truckCount = addOrderInfoParam.getTruckCount();
|
||||
Assert.isTrue(truckCount != null && truckCount > 0 && truckCount <= 10, "车辆数量必须大于0小于等于10");
|
||||
Integer estimatedQuantity = addOrderInfoParam.getEstimatedQuantity();
|
||||
if (estimatedQuantity != null && estimatedQuantity > 0) {
|
||||
addOrderInfoParam.setEstimatedQuantity(estimatedQuantity / truckCount);
|
||||
}
|
||||
|
||||
orderInfoService.batchAdd(addOrderInfoParam);
|
||||
return R.success();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ public class AddOrderInfoParam implements Constrained {
|
|||
/**
|
||||
* 预估量
|
||||
*/
|
||||
private String estimatedQuantity;
|
||||
private Integer estimatedQuantity;
|
||||
/**
|
||||
* 站点 Id
|
||||
*/
|
||||
|
|
@ -76,6 +76,7 @@ public class AddOrderInfoParam implements Constrained {
|
|||
* 订单类型; 字典代码:order_category
|
||||
*/
|
||||
private OrderCategory orderCategory;
|
||||
private Integer truckCount;
|
||||
|
||||
/**
|
||||
* 付款方资金账户 Id
|
||||
|
|
|
|||
|
|
@ -48,7 +48,10 @@ import com.njzscloud.supervisory.sys.stationletter.constant.WarnCategory;
|
|||
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;
|
||||
|
|
@ -56,7 +59,8 @@ import java.time.LocalDateTime;
|
|||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.njzscloud.supervisory.constant.Constant.CERTIFICATE_SN_CODE;
|
||||
|
|
@ -80,6 +84,8 @@ public class OrderInfoService extends ServiceImpl<OrderInfoMapper, OrderInfoEnti
|
|||
private final BizWarnService bizWarnService;
|
||||
private final AppProperties appProperties;
|
||||
private final StationManageService stationManageService;
|
||||
private final PlatformTransactionManager transactionManager;
|
||||
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(() -> {
|
||||
|
|
@ -99,7 +105,6 @@ public class OrderInfoService extends ServiceImpl<OrderInfoMapper, OrderInfoEnti
|
|||
/**
|
||||
* 新增
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void add(AddOrderInfoParam addOrderInfoParam) {
|
||||
AddOrderCargoPlaceParam cargoPlace = addOrderInfoParam.getCargoPlace();
|
||||
long cargoPlaceId = orderCargoPlaceService.add(cargoPlace);
|
||||
|
|
@ -1080,4 +1085,60 @@ public class OrderInfoService extends ServiceImpl<OrderInfoMapper, OrderInfoEnti
|
|||
OrderPagingResult orderDetail = detail(orderId);
|
||||
return BeanUtil.copyProperties(orderDetail, TrainBillResult.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<>();
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue