名字真难改 2024-03-08 10:48 采纳率: 0%
浏览 3

判断级联数据是否合法,是否有更好的办法?

现在遇到了一个业务问题,假设存在一个仓库和位置的主子表关系
例如:仓库1 --- 01 02 03 仓库2 --- 01 02 03
判断前端传入的值是否符合规则(仓库和位置的值匹配,仓库值不为空,库位可以为空)
目前我用的是list.contain()方法做的判断,代码如下

//inboundItems 是数据集合
 public boolean checkItem(List<InboundItem> inboundItems) {
        int count = 0;
        if (CollUtil.isNotEmpty(inboundItems)) {
            //通过sql获取仓库和位置匹配的list
            List<CabinAndStorePlaceVO> result = inboundReceiptService.getCabinAndStorePlace();
            for (InboundItem inboundItem : inboundItems) {
                if (StringUtils.isNotBlank(inboundItem.getStorePlaceId())) {
                    if (result.contains(new CabinAndStorePlaceVO().setCabinId(inboundItem.getStoreId()).setStorePlaceId(inboundItem.getStorePlaceId()))) {
                        count++;
                    }
                }
            }
        } else {
            return true;
        }
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

还有什么更加简单方便的办法吗?

  • 写回答

4条回答 默认 最新

  • 百锦再@新空间 优质创作者: 编程框架技术领域 2024-03-08 10:48
    关注

    public boolean check(List<
    InboundItem>
    inboundItems) {int count = 0;

    if (CollUtil.isNotEmpty(inboundItems)) {List<
    CabinAndStorePlaceVO>
    result = inboundReceiptService.getCabinAndStorePlace();

    for (InboundItem inboundItem : inboundItems) {if (StringUtils.isNotBlank(inboundItem.getStorePlaceId())) {CabinAndStorePlaceVO cabinAndStorePlaceVO = new CabinAndStorePlaceVO(inboundItem.getStoreId(), inboundItem.getStorePlaceId());
    if (result.contains(cabinAndStorePlaceVO)) {count++;
    }}}
    if (count == inboundItems.size()) {return true;
    } else {return false;
    }} else {return false;
    }}

    在提供的代码中,使用了list.contains()方法来判断前端传入的值是否符合规则。如果仓库和位置匹配,并且仓库值不为空,则计数器count加一。最后比较计数器的值和数据集合的大小是否相等,如果相等则返回true,表示符合规则,否则返回false。如果有更加简单方便的方法,可进行代码举例说明。


    有问题你别着急,评论留言都可以,看到马上就回复,尽量及时补充齐
    评论

报告相同问题?

问题事件

  • 创建了问题 3月8日