尝试优化以下代码效率:public void modifyPalletPacking(ProductLineSms sms) { Calendar calendar =…
- User
尝试优化以下代码效率: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 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()); } } } } - Assistant
- User
尝试优化以下代码效率: public void processExceptionPallet(List packingInfos) { //查托盘明细表 if (CollectionUtils.isNotEmpty(packingInfos)) { packingInfos.forEach(packingInfo -> { LogisticsCellList logisticsCellList = logisticsCellListMapper.selectByPackingEpc(packingInfo.getEpc()); if (logisticsCellList != null) { //修改托盘表 Long id = logisticsCellList.getId(); LogisticsCellInfo logisticsCellInfo = logisticsCellInfoMapper.selectByPrimaryKey(id); if (logisticsCellInfo != null) { logisticsCellInfo.setCodeAmount(logisticsCellInfo.getCodeAmount() - 1); logisticsCellInfo.setRealAmount(logisticsCellInfo.getRealAmount() - 1); logisticsCellInfo.setSurplusAmount(logisticsCellInfo.getSurplusAmount() - 1); logisticsCellInfoMapper.updateAmount(logisticsCellInfo); // 删除托盘明细表 log.info("处理异常托盘,循环删除,id:{}", id); logisticsCellListMapper.deleteByPrimaryKey(id); } } }); } }
- Assistant
该代码的优化主要从以下几个方面考虑:
- 减少数据库查询次数:
- 可以将查询到的
logisticsCellInfo保存在一个Map中,使用logisticsCellList.getId()作为key,这样在循环中可以直接通过key获取数据,而不需要每次重新查询数据库。
- 批量更新数据库:
- 可以将需要更新的
logisticsCellInfo对象保存在一个List中,在循环结束后使用批量更新的方式将数据更新到数据库,避免每次都进行单个更新。
以下是根据上述优化思路进行的代码优化:
public void processExceptionPallet(List<PackingInfo> packingInfos) { if (CollectionUtils.isNotEmpty(packingInfos)) { Map<Long, LogisticsCellInfo> logisticsCellInfoMap = new HashMap<>(); List<Long> idsToDelete = new ArrayList<>(); packingInfos.forEach(packingInfo -> { LogisticsCellList logisticsCellList = logisticsCellListMapper.selectByPackingEpc(packingInfo.getEpc()); if (logisticsCellList != null) { //修改
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.