优化这段代码:public void fixedSpProduct(List<CrmSpProduct> crmSpProductList,Long spApplyId) { if (CollectionUtils.isNotEmpty(crmSpProductList)) { Map<String,Object> map = new HashMap<>(); map.put("SP_APPLY_ID",spApplyId); List<CrmSpProduct> list = this.getBaseMapper().selectByMap(map); if (CollectionUtils.isNotEmpty(list)){ crmSpProductMapper.deleteByMap(map); } for (CrmSpProduct c : crmSpProductList){ c.setSpApplyId(spApplyId); } super.saveOrUpdateBatch(crmSpProductList); } else { throw new BusinessRuntimeException("当前没有任何产品清单,操作失败"); } }
时间: 2023-06-02 11:03:22 浏览: 90
可以考虑使用批量操作来优化代码:
```
public void fixedSpProduct(List<CrmSpProduct> crmSpProductList,Long spApplyId) {
if (CollectionUtils.isEmpty(crmSpProductList)) {
throw new BusinessRuntimeException("当前没有任何产品清单,操作失败");
}
Map<String,Object> map = new HashMap<>();
map.put("SP_APPLY_ID",spApplyId);
crmSpProductMapper.deleteByMap(map);
for (CrmSpProduct c : crmSpProductList){
c.setSpApplyId(spApplyId);
}
super.saveOrUpdateBatch(crmSpProductList);
}
```
相关问题
List<Map> smallClassMap = super.getQueryBySql(sql); List<String> smallClassList = new ArrayList<>(); if(CollectionUtils.isNotEmpty(smallClassMap)){ for (Map map : smallClassMap) { smallClassList.add(map.get("NAME").toString()); } }
这段代码主要是从数据库中查询出一组数据,并将其中的某个字段值("NAME")提取出来,放入一个新的列表中(smallClassList)。
首先,通过调用父类的getQueryBySql方法,传入sql语句进行查询操作,并将结果保存在smallClassMap变量中(假设该方法返回一个List<Map>类型的结果)。
然后,判断smallClassMap是否为空,如果不为空,则遍历smallClassMap中的每个Map对象。对于每个Map对象,通过调用get("NAME")方法获取其"NAME"字段的值,并将其转换为String类型,然后将其添加到smallClassList列表中。
最终,如果查询结果不为空,smallClassList将包含所有查询结果中"NAME"字段的值。
需要注意的是,在使用这段代码时,需要保证sql语句的正确性,并且确保查询结果中包含"NAME"字段。另外,还需要确保所依赖的工具类(如CollectionUtils)已正确引入。
private List<ChannelTypeTenantVO> filterUseType(List<ChannelTypeTenantVO> channelTypes, List<String> filterList){ if(CollectionUtils.isEmpty(channelTypes) || CollectionUtils.isEmpty(filterList)) { return channelTypes; } return channelTypes.stream().filter(v -> { if(StringUtils.isEmpty(v.getUseCondition())) { return true; } ChannelTypeVO.UseConditionVO useConditionVO = JsonUtils.readValueQuietly(v.getUseCondition(), ChannelTypeVO.UseConditionVO.class); if(BooleanUtils.isNotTrue(useConditionVO.getUseTypeFlag())) { return true; } List<String> useTypeList = useConditionVO.getUseTypeList(); if(CollectionUtils.isNotEmpty(useTypeList) && !Collections.disjoint(useTypeList, filterList)) { return true; } return false; }).collect(Collectors.toList()); } private List<ChannelTypeVO> filterUseType1(List<ChannelTypeVO> channelTypes, List<String> filterList){ if(CollectionUtils.isEmpty(channelTypes) || CollectionUtils.isEmpty(filterList)) { return channelTypes; } return channelTypes.stream().filter(v -> { if(StringUtils.isEmpty(v.getUseCondition())) { return true; } ChannelTypeVO.UseConditionVO useConditionVO = JsonUtils.readValueQuietly(v.getUseCondition(), ChannelTypeVO.UseConditionVO.class); if(BooleanUtils.isNotTrue(useConditionVO.getUseTypeFlag())) { return true; } List<String> useTypeList = useConditionVO.getUseTypeList(); if(CollectionUtils.isNotEmpty(useTypeList) && !Collections.disjoint(useTypeList, filterList)) { return true; } return false; }).collect(Collectors.toList()); }
这段代码是两个方法,它们的方法名和实现相同,只是所处理的数据类型不同。第一个方法是 `filterUseType`,它接受一个 `List<ChannelTypeTenantVO>` 类型的参数 `channelTypes` 和一个 `List<String>` 类型的参数 `filterList`,返回一个经过过滤后的 `List<ChannelTypeTenantVO>` 类型的列表。第二个方法是 `filterUseType1`,它接受一个 `List<ChannelTypeVO>` 类型的参数 `channelTypes` 和一个 `List<String>` 类型的参数 `filterList`,返回一个经过过滤后的 `List<ChannelTypeVO>` 类型的列表。这样的重复代码是不必要的,可以将它们合并为一个方法,使用泛型来处理不同类型的数据。
可以将这两个方法合并为一个泛型方法,它接受一个 `List<T>` 类型的参数 `channelTypes`,一个 `List<String>` 类型的参数 `filterList`,以及一个 `Class<T>` 类型的参数 `clazz`,用于指定 `channelTypes` 中元素的类型。其中的逻辑实现和原来的方法是相同的。例如:
```
private <T> List<T> filterUseType(List<T> channelTypes, List<String> filterList, Class<T> clazz) {
if(CollectionUtils.isEmpty(channelTypes) || CollectionUtils.isEmpty(filterList)) {
return channelTypes;
}
return channelTypes.stream().filter(v -> {
if(StringUtils.isEmpty(v.getUseCondition())) {
return true;
}
ChannelTypeVO.UseConditionVO useConditionVO = JsonUtils.readValueQuietly(v.getUseCondition(), ChannelTypeVO.UseConditionVO.class);
if(BooleanUtils.isNotTrue(useConditionVO.getUseTypeFlag())) {
return true;
}
List<String> useTypeList = useConditionVO.getUseTypeList();
if(CollectionUtils.isNotEmpty(useTypeList) && !Collections.disjoint(useTypeList, filterList)) {
return true;
}
return false;
}).map(clazz::cast).collect(Collectors.toList());
}
```
这样,你就可以在调用方法时传入不同类型的数据,而不需要重复编写相似的代码。例如,你可以这样调用该方法:
```
List<ChannelTypeTenantVO> channelTypes = ...;
List<String> filterList = ...;
List<ChannelTypeTenantVO> result1 = filterUseType(channelTypes, filterList, ChannelTypeTenantVO.class);
List<ChannelTypeVO> channelTypes2 = ...;
List<String> filterList2 = ...;
List<ChannelTypeVO> result2 = filterUseType(channelTypes2, filterList2, ChannelTypeVO.class);
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)