尝试优化以下代码效率：public void modifyPalletPacking(ProductLineSms sms) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        String endDate = SDF.format(calendar.getTime());
        calendar.add(Calendar.HOUR_OF_DAY, -5);
        String startDate = SDF.format(calendar.getTime());
        //处理最近5个小时的箱码
        List<LogisticsCellReplace> list = this.logisticsCellReplaceMapper.selectByStatusAndTime(
                this.config.getProductLineId(), this.config.getProductId(), SignalEnum.Status.UNTREATED.getValue(),
                startDate, endDate);

        if (CollectionUtils.isNotEmpty(list)) {
            for (LogisticsCellReplace exceptionPacking : list) {
                LogisticsCellList sourcePacking = null;
                try {
                    List<LogisticsCellList> sourcePackings = this.logisticsCellListMapper.selectListByPackingEpc(exceptionPacking.getSourcePackingEpc());
                    /*
                        表示箱码关联托盘有问题：
                            1.判断list中额托盘id是否相同，是相同删除留下一条数据；
                            2.如果托盘id不一样，需要验证属于那个托盘的箱码，删除剩余的箱码
                     */

                    if (CollectionUtils.isNotEmpty(sourcePackings)) {
                        //同一个箱码关联多个托盘明细表
                        if (sourcePackings.size() > 1) {
                            //根据托盘明细表的托盘主表id去重
                            List<LogisticsCellList> collect = sourcePackings
                                    .stream()
                                    .collect(Collectors.collectingAndThen(
                                            Collectors.toCollection(() ->
                                                    new TreeSet<>(Comparator.comparing(LogisticsCellList::getLogisticsId))),
                                            ArrayList::new));
                            //如果去重后只有一条数据，说明一个箱码关联一个托盘明细，但是有两条记录，删除其中一条即可
                            if (collect.size() == 1) {
                                // 删除其中一条记录
                                log.info("删除其中一条记录, 数据只查出来1条，数据:{}", JSON.toJSONString(collect));
                                this.logisticsCellListMapper.deleteByPrimaryKey(collect.get(0).getId());
                                sourcePacking = this.logisticsCellListMapper.selectByPackingEpc(exceptionPacking.getSourcePackingEpc());
                            } else {
                                //如果去重后，还是有多条记录
                                if (sourcePackings.size() > 1) {
                                    for (LogisticsCellList packing : sourcePackings) {
                                        //根据箱码和主表id查明细表
                                        List<LogisticsCellList> repeatPacks = this.logisticsCellListMapper.selectListByLogisIdAndPackingEpc(packing.getLogisticsId(), packing.getPackingEpc());
                                        if (repeatPacks.size() > 1) {
                                            //如果有多条，则保留一条
                                            log.info("批量删除数据，数据条数:{},数据内容:{}", repeatPacks.size(), JSON.toJSONString(repeatPacks));
                                            repeatPacks.remove(repeatPacks.get(0));
                                            this.logisticsCellListMapper.deleteBatch(repeatPacks);
                                        }
                                    }
                                    //处理同一箱码，不在同一个托盘的情况
                                    sourcePackings = this.logisticsCellListMapper.selectListByPackingEpc(exceptionPacking.getSourcePackingEpc());
                                    //根据箱码查托盘表与替换表对比产线产品是否一致
                                    List<LogisticsCellList> replatePackings = new ArrayList<>(CollectionUtils.size(sourcePackings));
                                    for (LogisticsCellList packing : sourcePackings) {
                                        LogisticsCellInfo logisticsCellInfo = this.logisticsCellInfoMapper.selectByPrimaryKey(packing.getLogisticsId());
                                        if (Objects.equals(logisticsCellInfo.getProductLineId(), exceptionPacking.getProductLineId()) &&
                                                Objects.equals(logisticsCellInfo.getProductId(), exceptionPacking.getProductId())) {
                                            replatePackings.add(packing);
                                        }
                                    }
                                    if (replatePackings.size() == 1) {
                                        sourcePacking = replatePackings.get(0);
                                    } else {
                                        log.error("产线id{}，产品id{},该箱码关联多个托盘请检查{}", list.get(0).getProductLineId(), list.get(0).getProductId(), exceptionPacking.getSourcePackingEpc());
                                        sms.sendWarn(list.get(0).getProductLineId(), "箱码:" + exceptionPacking.getSourcePackingEpc() + ",关联多个托盘请检查->" + list.get(0).getProductLineId());
                                    }
                                }
                            }

                        } else {
                            // 同一个箱码关联一个托盘明细表
                            sourcePacking = sourcePackings.get(0);
                        }

                    }

                    long count = this.packingInfoMapper.findByPackingEpc(this.config.getProductLineId(), this.config.getProductId(), exceptionPacking.getSourcePackingEpc());
                    if (sourcePacking == null || count == 0) {
                        log.error("换箱数据异常{}-->托盘数据{}->瓶箱数据:{}", exceptionPacking.getId(), sourcePacking, count);
                        if (exceptionPacking.getErrorNum() > 5) {
                            this.logisticsCellReplaceMapper.updateResult(exceptionPacking.getId(), SignalEnum.Status.EXCEPTION.getValue(), null);
                            log.error("换箱数据异常{},超过最大重试,放弃", exceptionPacking.getId());
                        } else {
                            this.logisticsCellReplaceMapper.updateResult(exceptionPacking.getId(), null, exceptionPacking.getErrorNum() + 1);
                        }
                        continue;
                    }
                    //如果是复检箱判重，则不需要删除箱表和箱明细表
                    if (!StringUtils.EMPTY.equals(exceptionPacking.getReplacePackingEpc())) {
                        this.packingListMapper.deleteByPackingInfoEpc(exceptionPacking.getSourcePackingEpc());
                        this.packingInfoMapper.deleteByPackingEpc(this.config.getProductLineId(), this.config.getProductId(), exceptionPacking.getSourcePackingEpc());
                    }

                    LogisticsCellInfo pallet = this.logisticsCellInfoMapper.selectByPrimaryKey(sourcePacking.getLogisticsId());
                    pallet.setCodeAmount(pallet.getCodeAmount() - 1);
                    pallet.setRealAmount(this.config.getAllLayerPackingAmount() <= pallet.getCodeAmount() ? this.config.getAllLayerPackingAmount() : pallet.getCodeAmount());
                    pallet.setSurplusAmount(pallet.getRealAmount());

                    this.logisticsCellInfoMapper.updateCodeAmount(pallet);
                    Long id = sourcePacking.getId();
                    log.info("Tools.checkUpdateAfter 删除,id:{}", id);
                    Tools.checkUpdateAfter(this.logisticsCellListMapper.deleteByPrimaryKey(id));
                    this.logisticsCellReplaceMapper.updateResult(exceptionPacking.getId(), SignalEnum.Status.TREATED.getValue(), null);
                    log.info("箱异常处理成功:换箱ID:" + exceptionPacking.getId());
                } catch (Exception e) {
                    log.error("产线id{}，产品id{},换箱失败{}", list.get(0).getProductLineId(), list.get(0).getProductId(), e.getMessage());
                }

            }
        }

    }