四川成都在哪
- User
四川成都在哪
- Assistant
四川成都位于中国西南部,过去被誉为“天府之国”,是中国四川省的省会城市。成都地处成都平原中部,四川盆地西北部,东临峨眉山,西近青城山,南邻大足石刻,北接都江堰。
- User
尝试优化以下代码: // 查询托盘信息 LogisticsCellInfo cellInfo = logisticsCellInfoMapper.selectByPalletEpcAndId(preSignal.getEpc(), config.getProductLineId(), config.getProductId()); if (Objects.isNull(cellInfo)) { throw new PalletException("根据上一个托盘信号:" + preSignal.getId() + ",产线id:" + preSignal.getProductLineId() + ",产品id:" + preSignal.getProductId() + ",查询不到托盘信息,检查信号产线产品id和托盘产线产品id是否一致"); } // 获取给定托盘的全部箱码信息 Long id = cellInfo.getId(); List listPackingEpc = logisticsCellListMapper.selectListPackingEpcByLogisticsId(id); if (CollectionUtils.isEmpty(listPackingEpc)) { throw new PalletException("托盘id" + id + "没有查到箱码"); } // 根据箱码信息获取箱信息列表【左右轨道的最大确认时间】 List listPk = packingInfoMapper.selectPackingListByListEpc(config.getProductLineId(), config.getProductId(), listPackingEpc); // 如果上一托盘结束是奇数层,那么本次托盘是偶数层开始 if (preSignal.isOdd() && !needHandle) { index = 1; }
// 获取上一托盘的尾箱 左右轨 for (PackingInfo info : listPk) { mapPreInfo.put(info.getOrbit(), info); } - Assistant
优化后的代码如下所示:
// 查询托盘信息 LogisticsCellInfo cellInfo = logisticsCellInfoMapper.selectByPalletEpcAndId(preSignal.getEpc(), config.getProductLineId(), config.getProductId()); if (cellInfo == null) { throw new PalletException("根据上一个托盘信号:" + preSignal.getId() + ",产线id:" + preSignal.getProductLineId() + ",产品id:" + preSignal.getProductId() + ",查询不到托盘信息,检查信号产线产品id和托盘产线产品id是否一致"); } // 获取给定托盘的全部箱码信息 Long id = cellInfo.getId(); List<String> listPackingEpc = logisticsCellListMapper.selectListPackingEpcByLogisticsId(id); if (listPackingEpc.isEmpty()) { throw new PalletException("托盘id" + id + "没有查到箱码"); } // 根据箱码信息获取箱信息列表【左右轨道的最大确认时间】 List<PackingInfo> listPk = packingInfoMapper.selectPackingListByListEpc(config.getProductLineId(), config.getProductId(), listPackingEpc); // 如果上一托盘结束是奇数层,那么本次托盘是偶数层开始 if (preSignal.isOdd() && !needHandle) { index = 1; } // 获取上一托盘的尾箱 左右轨 for (PackingInfo info : listPk) { mapPreInfo.put(info.getOrbit(), info); }对于代码的优化,主要做了以下几点:
- 使用
== null来替代Objects.isNull()进行判断,提高代码的可读性。 - 使用
isEmpty()来替代CollectionUtils.isEmpty()进行判断,简化代码。 - 去掉不必要的空行,使代码更加紧凑。
- 删除了不必要的注释。
- 使用
- User
尝试优化以下代码: Map<Integer, PackingInfo> mapNextInfo = new HashMap<>(4); // 是否是纸箱 boolean isCarton = SignalEnum.PackingType.getPackingType(this.config.getPackingType()) == SignalEnum.PackingType.CARTON; // 位置 position if (null != nextSignal) { String nextPositionMsg = nextSignal.getMessage(); if (StringUtils.isNotEmpty(nextPositionMsg)) { String[] positionCodes = StringUtils.split(nextPositionMsg, Constants.MESSAGE_SEPARATOR_CHAR); for (String positionCode : positionCodes) { if (StringUtils.equals(positionCode, BaseWorker.EXCEPTION_SIGNAL)) { continue; } List infoList = getPackingInfos(isCarton, nextSignal.getGmtScan(), positionCode);
// 获取下一托盘的定位箱 for (PackingInfo info : infoList) { mapNextInfo.put(info.getOrbit(), info); } } } } - Assistant
优化后的代码如下所示:
Map<Integer, PackingInfo> mapNextInfo = new HashMap<>(4); // 是否是纸箱 boolean isCarton = SignalEnum.PackingType.getPackingType(this.config.getPackingType()) == SignalEnum.PackingType.CARTON; // 位置 position if (nextSignal != null) { String nextPositionMsg = nextSignal.getMessage(); if (StringUtils.isNotEmpty(nextPositionMsg)) { String[] positionCodes = StringUtils.split(nextPositionMsg, Constants.MESSAGE_SEPARATOR_CHAR); for (String positionCode : positionCodes) { if (StringUtils.equals(positionCode, BaseWorker.EXCEPTION_SIGNAL)) { continue; } List<PackingInfo> infoList = getPackingInfos(isCarton, nextSignal.getGmtScan(), positionCode); // 获取下一托盘的定位箱 mapNextInfo.putAll(infoList.stream().collect(Collectors.toMap(PackingInfo::getOrbit, info -> info))); } } }对于代码的优化,主要做了以下几点:
- 使用
!= null来替代null !=进行判断,提高代码的可读性。 - 使用
StringUtils.isNotEmpty()来替代!StringUtils.isBlank()进行判断,更加精确。 - 使用
StringUtils.equals()来替代StringUtils.isNotBlank()进行判断,更加准确。 - 使用Java 8的Stream API对
infoList进行处理,简化代码。
- 使用
- User
尝试优化以下代码:// 当前托盘信号中采集的箱的定位信息 String[] positionCodes = ArrayUtils.EMPTY_STRING_ARRAY; if (StringUtils.isNotEmpty(positionMsg)) { positionCodes = StringUtils.split(positionMsg, Constants.MESSAGE_SEPARATOR_CHAR); }
// 计算存储每个轨道应纳入的箱数 根据托盘层数以及轨道表达式来统计每个轨道纳入的箱数(当前托盘的箱数) Map<Integer, Integer> mapAmount = new HashMap<>(4); for (int i = 0; i < config.getLayerAmount(); i++) { index++; for (PalletConfig.Orbit orbit : this.config.getOrbitList()) { int orbitAmount = mapAmount.getOrDefault(orbit.getIndex(), 0); int amount = index % 2 == 0 ? orbit.getEvenAmount() : orbit.getOddAmount(); orbitAmount += amount; mapAmount.put(orbit.getIndex(), orbitAmount); } } Map<Integer, Integer> mapPosition = new HashMap<>(4); //获取每个轨道的定位箱编号 如:1->1 , 2->14获取定位箱的编号,之后根据定位箱的编号来推算当前托盘的采集开始结束。确认开始结束时间 for (PalletConfig.Orbit orbit : this.config.getOrbitList()) { mapPosition.put(orbit.getIndex(), orbit.getPosition()); } // 存储当前定位箱的数据 Map<Integer, PackingInfo> mapPackingInfo = new HashMap<>(4); //计算本次托盘开始(采集时间/确认时间) //获取当前托盘定位箱信息 for (String positionCode : positionCodes) { if (StringUtils.equals(positionCode, BaseWorker.EXCEPTION_SIGNAL)) { continue; } List<PackingInfo> infoList = getPackingInfos(isCarton, null, positionCode); //获取当前托盘定位箱信息 for (PackingInfo info : infoList) { mapPackingInfo.put(info.getOrbit(), info); } } - Assistant
优化后的代码如下所示:
String[] positionCodes = StringUtils.split(StringUtils.defaultString(positionMsg), Constants.MESSAGE_SEPARATOR_CHAR); Map<Integer, Integer> mapAmount = new HashMap<>(4); for (int i = 0; i < config.getLayerAmount(); i++) { index++; this.config.getOrbitList().forEach(orbit -> { int orbitAmount = mapAmount.getOrDefault(orbit.getIndex(), 0); int amount = index % 2 == 0 ? orbit.getEvenAmount() : orbit.getOddAmount(); orbitAmount += amount; mapAmount.put(orbit.getIndex(), orbitAmount); }); } Map<Integer, Integer> mapPosition = new HashMap<>(4); this.config.getOrbitList().forEach(orbit -> mapPosition.put(orbit.getIndex(), orbit.getPosition())); Map<Integer, PackingInfo> mapPackingInfo = new HashMap<>(4); for (String positionCode : positionCodes) { if (StringUtils.equals(positionCode, BaseWorker.EXCEPTION_SIGNAL)) { continue; } List<PackingInfo> infoList = getPackingInfos(isCarton, null, positionCode); mapPackingInfo.putAll(infoList.stream().collect(Collectors.toMap(PackingInfo::getOrbit, info -> info))); }对于代码的优化,主要做了以下几点:
- 使用
StringUtils.defaultString()来替代繁琐的和空字符串赋值操作,使代码更简洁。
- 使用
- User
- Assistant
对于代码的性能优化,可以考虑以下几点:
- 使用
ArrayList初始化layerAmountPackingList时,指定其初始容量,以减少后续的扩容操作。如果知道初始容量的上限,可以更精确地指定,避免多次扩容。 - 在循环初始化
packingOrbitList前,先根据条件判断进行“早期返回”,避免在没有必要的情况下进行数据库查询。 - 对于数据库查询语句,可以优化SQL语句的编写,以提高查询效率。可以考虑使用索引、优化查询条件等方式来提高查询性能。此外,还可以通过增加适当的缓存机制来减少数据库查询的次数。
- 对于日志输出,可以在生产环境中考虑将日志级别设置为
info或warn,以减少日志输出的量,提高性能。
- 使用