使用多线程优化自动跑数功能执行速度

public AjaxResult run(@RequestBody OrderNumDto orderNumDto) {

    //声明一个对象数组存储导出结果
    ArrayList<LogisticsOrderInfoTestVo> LogisticsOrderInfoTestVoList = new ArrayList<>();
    List<LogisticsOrderInfo> sorderDtos = new ArrayList<>();

    /**
     * 1.根据传入的数组查询,获得LogisticsOrderInfo对象数组
     * 2.遍历LogisticsOrderInfo对象数组,记录验证字段到logisticsOrderInfoTestVo类,触发自动计算,将计算结果记录到logisticsOrderInfoTestVo类
     * 3.数据比对,输出结果
     * */
    
    LambdaQueryWrapper<LogisticsOrderInfo> logisticsOrderInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
    logisticsOrderInfoLambdaQueryWrapper.in(LogisticsOrderInfo::getId, orderNumDto.getIds());

    List<LogisticsOrderInfo> orderInfoList = logisticsOrderInfoService.list(logisticsOrderInfoLambdaQueryWrapper);


    Set<Callable<List<LogisticsOrderInfo>>> callable = new HashSet<>();
    List<List<LogisticsOrderInfo>> idsGroup = Lists.partition(orderInfoList, 10);

    //开启线程
    ExecutorService executorService = PUSH_EXECUTORS;
    //组装子线程任务列表
    for (List<LogisticsOrderInfo> orderInfos : idsGroup) {
        callable.add(()->{
            for (LogisticsOrderInfo info : orderInfos) {
                LogisticsOrderInfoTestVo logisticsOrderInfoTestVo = new LogisticsOrderInfoTestVo();
                //记录验证数据
                /**订单编号*/
                if (info.getOrderNumber() != null) {
                    logisticsOrderInfoTestVo.setOrderNumber(info.getOrderNumber());
                }
                /**承运商(验证使用)*/
                if (info.getCarrierUseTest() != null) {
                    logisticsOrderInfoTestVo.setCarrierUseTest(info.getCarrierUseTest());
                }
                /**服务方式/收件类型(验证使用)*/
                if (info.getServiceTypeUseTest() != null) {
                    logisticsOrderInfoTestVo.setServiceTypeUseTest(info.getServiceTypeUseTest());
                }
                /**运输方式/路线(验证使用)*/
                if (info.getTransportTypeUseTest() != null) {
                    logisticsOrderInfoTestVo.setTransportTypeUseTest(info.getTransportTypeUseTest());
                }
                /**运输时效(验证使用)*/
                if (info.getTransportPrescriptionUseTest() != null) {
                    logisticsOrderInfoTestVo.setTransportPrescriptionUseTest(info.getTransportPrescriptionUseTest());
                }
                /** 预计到达时间/物流截止时间(验证使用)  */
                if (info.getTimeTestOne() != null) {
                    logisticsOrderInfoTestVo.setTimeTestOne(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", info.getTimeTestOne()));
                }
                /** 最快可出库时间/航班起飞时间(验证使用)  */
                if (info.getTimeTestTwo() != null) {
                    logisticsOrderInfoTestVo.setTimeTestTwo(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", info.getTimeTestTwo()));
                }
                /** 客户要求到货时间(验证使用)  */
                if (info.getTimeTestThree() != null) {
                    logisticsOrderInfoTestVo.setTimeTestThree(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", info.getTimeTestThree()));
                }

                //触发自动计算
                try {
                    OrderInfoDto dto = logisticsOrderInfoService.convertOrderInfoDto(info.getOrderNumber());
                    dto.setRunTime(dto.getEnoughTime());
                    /**
                     * 1.计算成功,获取计算后的值
                     * 2.区分内外销
                     * */
                    CalculateVo calculateVo = logisticsOrderInfoService.startOrderProcess(dto);

                    if (calculateVo.getLogisticsInLogisticsResConf() != null) {
                        /**承运商ID*/
                        logisticsOrderInfoTestVo.setCarrierCode(calculateVo.getLogisticsInLogisticsResConf().getCarrierCode());
                        /**承运商名称*/
                        logisticsOrderInfoTestVo.setCarrierName(calculateVo.getLogisticsInLogisticsResConf().getCarrierName());
                        /**服务方式* shippingTypeName*/
                        logisticsOrderInfoTestVo.setShippingTypeName(calculateVo.getLogisticsInLogisticsResConf().getShippingType());
                        /**运输方式*/
                        logisticsOrderInfoTestVo.setShippingTypeFullName(calculateVo.getLogisticsInLogisticsResConf().getShippingTypeFullName());
                        /**运输时效*/
                        logisticsOrderInfoTestVo.setTransportPrescription(calculateVo.getLogisticsInLogisticsResConf().getTransportPrescription().toString());
                        /**预计到达时间/物流截止时间(验证使用) DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", new Date(calculateVo.getLogisticsInLogisticsResConf().getEstimatedArrivalTime()))*/
                        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        logisticsOrderInfoTestVo.setEstimatedArrivalTime(sdf1.format(calculateVo.getLogisticsInLogisticsResConf().getEstimatedArrivalTime()));
                        /**最快可出库时间(验证使用)*/
                        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        logisticsOrderInfoTestVo.setTheFastestDeliveryTime(sdf2.format(calculateVo.getTheFastestDeliveryTime()));
                        /**客户要求到货时间/航班起飞时间(验证使用)*/
                        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        logisticsOrderInfoTestVo.setCustomerRequestedDeliveryTime(sdf3.format(calculateVo.getComputedCustomerNeedTime()));
                    } else {
                        /**承运商ID*/
                        logisticsOrderInfoTestVo.setCarrierCode(calculateVo.getExLogisticsResConf().getCarrierCode());
                        /**承运商名称*/
                        logisticsOrderInfoTestVo.setCarrierName(calculateVo.getExLogisticsResConf().getCarrierName());
                        /**服务方式* shippingTypeName*/
                        logisticsOrderInfoTestVo.setShippingTypeName(calculateVo.getExLogisticsResConf().getShippingTypeName());
                        /**预计到达时间/物流截止时间(验证使用)*/
                        logisticsOrderInfoTestVo.setEstimatedArrivalTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", new Date(calculateVo.getExLogisticsResConf().getReceivingDeadlineDate())));
                        /**运输方式/路线(验证使用)*/
                        logisticsOrderInfoTestVo.setTransportTypeUseTest(calculateVo.getExLogisticsResConf().getLineName());
                        /**路线*/
                        logisticsOrderInfoTestVo.setLineName(calculateVo.getExLogisticsResConf().getLineName());
                        /**收件类型 receiptTypeName*/
                        logisticsOrderInfoTestVo.setReceiptTypeName(calculateVo.getExLogisticsResConf().getReceiptTypeName());
                        /**物流截止时间*/
                        logisticsOrderInfoTestVo.setLogisticsDeadlineTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", new Date(calculateVo.getExLogisticsResConf().getReceivingDeadlineDate())));
                    }

                    //拿到返回结果和执行过程参数对比,修改test_agreement测试结果
                    /**承运商名称*/
                    String carrierCodeName = null;
                    if (StringUtils.isNotEmpty(logisticsOrderInfoTestVo.getCarrierCode())) {
                        carrierCodeName = iSysDictDataService.selectDictLabel("logistice_express_name", logisticsOrderInfoTestVo.getCarrierCode());
                    }
                    /**服务方式*/
                    String shippingTypeName = null;
                    if (StringUtils.isNotEmpty(logisticsOrderInfoTestVo.getShippingTypeName())) {
                        shippingTypeName = logisticsOrderInfoTestVo.getShippingTypeName();
                    }
                    //判空
                    /**承运商(验证使用)*/
                    String carrierUseTest = logisticsOrderInfoTestVo.getCarrierUseTest() == null ? "" : logisticsOrderInfoTestVo.getCarrierUseTest();
                    /**服务方式/收件类型(验证使用)*/
                    String serviceTypeUseTest = logisticsOrderInfoTestVo.getServiceTypeUseTest() == null ? "" : logisticsOrderInfoTestVo.getServiceTypeUseTest();
                    /**运输时效(验证使用)*/
                    String transportPrescriptionUseTest = logisticsOrderInfoTestVo.getTransportPrescriptionUseTest() == null ? "" : logisticsOrderInfoTestVo.getTransportPrescriptionUseTest();
                    /**运输时效(小时)*/
                    String transportPrescription = logisticsOrderInfoTestVo.getTransportPrescription() == null ? "" : logisticsOrderInfoTestVo.getTransportPrescription();
                    /**预计到达时间/物流截止时间(验证使用)*/
                    String timeTestOne = logisticsOrderInfoTestVo.getTimeTestOne() == null ? "" : logisticsOrderInfoTestVo.getTimeTestOne().toString();
                    /**最快可出库时间(验证使用)*/
                    String timeTestTwo = logisticsOrderInfoTestVo.getTimeTestTwo() == null ? "" : logisticsOrderInfoTestVo.getTimeTestTwo().toString();
                    /**客户要求到货时间/航班起飞时间(验证使用)*/
                    String timeTestThree = logisticsOrderInfoTestVo.getTimeTestThree() == null ? "" : logisticsOrderInfoTestVo.getTimeTestThree().toString();
                    /**预计到达时间*/
                    String estimatedArrivalTime = logisticsOrderInfoTestVo.getEstimatedArrivalTime() == null ? "" : logisticsOrderInfoTestVo.getEstimatedArrivalTime().toString();
                    /**最快可出库时间*/
                    String theFastestDeliveryTime = logisticsOrderInfoTestVo.getTheFastestDeliveryTime() == null ? "" : logisticsOrderInfoTestVo.getTheFastestDeliveryTime().toString();
                    /**客户要求到货时间*/
                    String customerRequestedDeliveryTime = logisticsOrderInfoTestVo.getCustomerRequestedDeliveryTime() == null ? "" : logisticsOrderInfoTestVo.getCustomerRequestedDeliveryTime().toString();
                    /**运输方式/路线(验证使用)*/
                    String TransportTypeUseTest = logisticsOrderInfoTestVo.getTransportTypeUseTest() == null ? "" : logisticsOrderInfoTestVo.getTransportTypeUseTest();
                    /**路线*/
                    String lineName = logisticsOrderInfoTestVo.getLineName() == null ? "" : logisticsOrderInfoTestVo.getLineName();
                    /**收件类型*/
                    String receiptTypeName = logisticsOrderInfoTestVo.getReceiptTypeName() == null ? "" : logisticsOrderInfoTestVo.getReceiptTypeName();

                    if (calculateVo.getLogisticsInLogisticsResConf() == null) {
                        //外销
                        if (carrierCodeName.trim().equals(carrierUseTest.trim())
                                && timeTestOne.equals(estimatedArrivalTime)
                                && TransportTypeUseTest.trim().equals(lineName.trim())
                                && receiptTypeName.trim().equals(serviceTypeUseTest.trim())) {
                            //如果执行结果一致,则修改testAgreement测试结果1
                            LambdaUpdateWrapper<LogisticsOrderInfo> logisticsOrderInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                            logisticsOrderInfoLambdaUpdateWrapper.set(LogisticsOrderInfo::getTestAgreement, "1").eq(LogisticsOrderInfo::getId, info.getId());
                            boolean update = logisticsOrderInfoService.update(logisticsOrderInfoLambdaUpdateWrapper);
                            if (update) {
                                logisticsOrderInfoTestVo.setTestAgreement("pass");
                            }
                        } else {
                            //记录字段不一致
                            String x1 = "";
                            String x2 = "";
                            String x3 = "";
                            String x4 = "";
                            if (StringUtil.isNotEmpty(logisticsOrderInfoTestVo.getCarrierUseTest()) && logisticsOrderInfoTestVo.getCarrierUseTest() != "") {
                                x1 = !carrierCodeName.equals(logisticsOrderInfoTestVo.getCarrierUseTest()) ? "[承运商名称不一致]" : "";
                            } else {
                                x1 = "[承运商名称为空]";
                            }
                            if (StringUtil.isNotEmpty(timeTestOne) && timeTestOne != "") {
                                x2 = !timeTestOne.equals(estimatedArrivalTime) ? "[物流截止时间不一致]" : "";
                            } else {
                                x2 = "[物流截止时间为空]";
                            }
                            if (StringUtil.isNotEmpty(TransportTypeUseTest) && TransportTypeUseTest != "") {
                                x3 = !TransportTypeUseTest.equals(lineName) ? "[路线不一致]" : "";
                            } else {
                                x3 = "[路线为空]";
                            }
                            if (StringUtil.isNotEmpty(serviceTypeUseTest) && serviceTypeUseTest != "") {
                                x4 = !serviceTypeUseTest.equals(receiptTypeName) ? "[收件类型不一致]" : "";
                            } else {
                                x4 = "[收件类型为空]";
                            }
                            logisticsOrderInfoTestVo.setTestAgreement("FAIL:" + x1 + x2 + x3 + x4);
                        }
                    } else {
                        //内销
                        if (carrierCodeName.trim().equals(carrierUseTest.trim())
                                && timeTestOne.trim().equals(estimatedArrivalTime)
                                && timeTestThree.trim().equals(customerRequestedDeliveryTime)
                                && shippingTypeName.trim().equals(serviceTypeUseTest.trim())
                                && transportPrescriptionUseTest.trim().equals(transportPrescription.trim())) {
                            //如果执行结果一致,则修改testAgreement测试结果1
                            LambdaUpdateWrapper<LogisticsOrderInfo> logisticsOrderInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                            logisticsOrderInfoLambdaUpdateWrapper.set(LogisticsOrderInfo::getTestAgreement, "1").eq(LogisticsOrderInfo::getId, info.getId());
                            boolean update = logisticsOrderInfoService.update(logisticsOrderInfoLambdaUpdateWrapper);
                            if (update) {
                                logisticsOrderInfoTestVo.setTestAgreement("pass");
                            }
                        } else {
                            //记录字段不一致
                            String x1 = "";
                            String x2 = "";
                            String x3 = "";
                            String x4 = "";
                            String x5 = "";

                            if (StringUtil.isNotEmpty(logisticsOrderInfoTestVo.getCarrierUseTest()) && logisticsOrderInfoTestVo.getCarrierUseTest() != "") {
                                x1 = !carrierCodeName.equals(logisticsOrderInfoTestVo.getCarrierUseTest()) ? "[承运商名称不一致]" : "";
                            } else {
                                x1 = "[承运商名称为空]";
                            }
                            if (StringUtil.isNotEmpty(logisticsOrderInfoTestVo.getServiceTypeUseTest()) && logisticsOrderInfoTestVo.getServiceTypeUseTest() != "") {
                                x2 = !shippingTypeName.trim().equals(logisticsOrderInfoTestVo.getServiceTypeUseTest().trim()) ? "[服务方式不一致]" : "";
                            } else {
                                x2 = "[服务方式为空]";
                            }
                            if (StringUtil.isNotEmpty(timeTestOne) && timeTestOne != "") {
                                x3 = !timeTestOne.trim().equals(estimatedArrivalTime.trim()) ? "[预计到达时间不一致]" : "";
                            } else {
                                x3 = "[预计到达时间为空]";
                            }
                            if (StringUtil.isNotEmpty(timeTestThree) && timeTestThree != "") {
                                x5 = !timeTestThree.trim().equals(customerRequestedDeliveryTime) ? "[客户要求到货时间不一致]" : "";
                            } else {
                                x5 = "[客户要求到货时间为空]";
                            }
                            if (StringUtil.isNotEmpty(logisticsOrderInfoTestVo.getTransportPrescriptionUseTest()) && logisticsOrderInfoTestVo.getTransportPrescriptionUseTest() != "") {
                                x4 = !logisticsOrderInfoTestVo.getTransportPrescriptionUseTest().equals(logisticsOrderInfoTestVo.getTransportPrescription()) ? "[运输时效不一致]" : "";
                            } else {
                                x4 = "[运输时效为空]";
                            }
                            logisticsOrderInfoTestVo.setTestAgreement("FAIL:" + x1 + x2 + x3 + x4 + x5);
                        }
                    }
                    LogisticsOrderInfoTestVoList.add(logisticsOrderInfoTestVo);

                } catch (RangeException e) {
                    //不在范围内
                    e.setMsg(CalMessage.MODEL_OUT.getMsg());
                    e.setCode(CalMessage.MODEL_OUT.getCode());
                    logisticsOrderInfoTestVo.setTestAgreement("pass [" + e + "]");
                    LogisticsOrderInfoTestVoList.add(logisticsOrderInfoTestVo);
                } catch (InsideException e) {
                    //内部送货、自提
                    e.setMsg(CalMessage.FILTER.getMsg());
                    e.setCode(CalMessage.FILTER.getCode());
                    logisticsOrderInfoTestVo.setTestAgreement("pass [" + e + "]");
                    LogisticsOrderInfoTestVoList.add(logisticsOrderInfoTestVo);
                } catch (Exception e) {
                    /**承运商ID*/
                    logisticsOrderInfoTestVo.setCarrierCode("/");
                    /**承运商名称*/
                    logisticsOrderInfoTestVo.setCarrierName("/");
                    /**服务方式* shippingTypeName*/
                    logisticsOrderInfoTestVo.setShippingTypeName("/");
                    /**运输方式*/
                    logisticsOrderInfoTestVo.setShippingTypeFullName("/");
                    /** 预计到达时间*/
                    //记录异常
                    logisticsOrderInfoTestVo.setTestAgreement("FAIL:" + e.getMessage());
                    LogisticsOrderInfoTestVoList.add(logisticsOrderInfoTestVo);
                    e.printStackTrace();
                }
            }
            return orderInfos;
        });
    }
    try {
        List<Future<List<LogisticsOrderInfo>>> futures = executorService.invokeAll(callable);
        //处理线程返回结果
        if (futures.size() > 0) {
            for (Future<List<LogisticsOrderInfo>> future : futures) {
                sorderDtos.addAll(future.get());
            }
        }
    } catch (InterruptedException |
            ExecutionException e) {
    }
    ExcelUtil<LogisticsOrderInfoTestVo> util = new ExcelUtil<LogisticsOrderInfoTestVo>(LogisticsOrderInfoTestVo.class);
    return util.exportExcel(LogisticsOrderInfoTestVoList, "自动跑单比对结果");

}

注意:

PUSH_EXECUTORS 为开启的30条线程
public class ThreadPoolConstants {
public static final ExecutorService PUSH_EXECUTORS = Executors.newFixedThreadPool(30);
}

10表示10条数据分为一组,即50条分5组
List<List> idsGroup = Lists.partition(orderInfoList, 10);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值