这段代码的功能是根据传入的参数获取符合条件的物料清单,并返回前五个物料清单中不符合条件的物料。

优化建议:

1.使用Lambda表达式简化代码。

2.使用Java 8的Stream API对集合进行操作,使代码更加简洁高效。

3.使用方法引用和静态导入,让代码更加简洁易读。

4.使用Optional类避免空指针异常。

5.将常量提取为静态变量,避免魔法值。

6.使用StringBuilder拼接字符串,避免字符串的频繁拼接。

优化后的代码如下:

private static final int LIMIT_COUNT = 40; private static final String LIMIT_COUNTRY = "中国"; private static final String LIMIT_SUPPLIER = "XG";

public Map<String, Object> getBOM1(@RequestBody String requestBody) { JSONObject params = JSON.parseObject(requestBody); List materialList = materialService.list(Wrappers.lambdaQuery() .eq(Material::getProcessId, params.getString("processId")) .orderByAsc(Material::getSerialNumber));

List<Material> newMaterialList = materialList.stream()
        .limit(5)
        .filter(i -> Optional.ofNullable(i.getLimit())
                .map(JSONArray::parseArray)
                .map(limit -> limit.stream()
                        .map(Object::toString)
                        .map(JSON::parseObject)
                        .map(limitItem -> {
                            String key = limitItem.getString("key");
                            String value = limitItem.getString("value");
                            switch (key) {
                                case "count":
                                    return Integer.parseInt(value) < LIMIT_COUNT;
                                case "country":
                                    return !value.equals(LIMIT_COUNTRY);
                                case "supplier":
                                    return !value.equals(LIMIT_SUPPLIER);
                                default:
                                    return true;
                            }
                        })
                        .allMatch(j -> j))
                .orElse(true))
        .collect(Collectors.toList());

return new HashMap<String, Object>() {{
    put("code", 0);
    put("message", "操作成功");
    put("bom", newMaterialList);
}};
public MapString Object getBOM1RequestBody String requestBody JSONObject params = JSONparseObjectrequestBody; ListMaterial materialList = materialServicelist WrappersMate

原文地址: https://www.cveoy.top/t/topic/eEJ1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录