名字真难改 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日

悬赏问题

  • ¥15 VS2022 C++的相关代码问题咨询
  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标
  • ¥15 人大金仓下载,有人知道怎么解决吗
  • ¥15 一个小问题,本人刚入门,哪位可以help
  • ¥30 python安卓开发
  • ¥15 使用R语言GD包一直不出结果
  • ¥15 计算机微处理器与接口技术相关问题,求解答图片的这个问题,有多少个端口,端口地址和解答问题的方法和思路,不要AI作答
  • ¥15 如何根据一个截图编写对应的HTML代码