List<OutStoreDetailEntity> items = outStore.getItems(); for (OutStoreDetailEntity item : items){ Long planDetailId = item.getPlanDetailId(); PlanDetailEntity planDetailEntity = planDetailDao.selectById(planDetailId); if(!(planDetailEntity.getIssuedCount()<=planDetailEntity.getCount())&&!((planDetailEntity.getIssuedCount()+item.getCount()) <= planDetailEntity.getIssuedCount())){ planDetailEntity.setIssuedCount(item.getCount()+planDetailEntity.getIssuedCount()); planDetailDao.updateById(planDetailEntity); } } 优化这段按代码
时间: 2024-03-12 09:46:52 浏览: 17
这段代码的优化方案如下:
```java
List<OutStoreDetailEntity> items = outStore.getItems();
List<PlanDetailEntity> planDetails = new ArrayList<>(items.size());
// 预处理需要更新的计划明细
for (OutStoreDetailEntity item : items) {
PlanDetailEntity planDetailEntity = planDetailDao.selectById(item.getPlanDetailId());
planDetailEntity.setIssuedCount(item.getCount() + planDetailEntity.getIssuedCount());
planDetails.add(planDetailEntity);
}
// 批量更新计划明细
planDetailDao.updateBatchById(planDetails);
// 判断是否超出计划数量
for (PlanDetailEntity planDetailEntity : planDetails) {
if (planDetailEntity.getIssuedCount() > planDetailEntity.getCount()) {
// 处理超出计划数量的情况
}
}
```
优化方案的具体流程如下:
1. 预处理需要更新的计划明细,将其存储在一个列表中。
```java
List<PlanDetailEntity> planDetails = new ArrayList<>(items.size());
for (OutStoreDetailEntity item : items) {
PlanDetailEntity planDetailEntity = planDetailDao.selectById(item.getPlanDetailId());
planDetailEntity.setIssuedCount(item.getCount() + planDetailEntity.getIssuedCount());
planDetails.add(planDetailEntity);
}
```
2. 批量更新计划明细。
```java
planDetailDao.updateBatchById(planDetails);
```
3. 对于每个更新后的计划明细,判断是否超出计划数量。如果超出,则需要进行相应的处理。
```java
for (PlanDetailEntity planDetailEntity : planDetails) {
if (planDetailEntity.getIssuedCount() > planDetailEntity.getCount()) {
// 处理超出计划数量的情况
}
}
```
通过对计划明细进行批量更新,可以减少与数据库的交互次数,提高效率。同时,将判断超出计划数量的逻辑移到了最后,避免了多次判断的重复操作。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)