一对多关联的两张表,确定关联属性

本文介绍了一种根据子表状态更新主表状态的方法。通过遍历子表中的每一项记录,并统计不同状态的数量,最终确定主表的状态。适用于一对多关联的数据表结构。

 

说明:对于一对多关联的两张表(a --> b 是1 --> n),根据b表的某个属性来确定a表的相关属性

示例:header --> details(1 --> n),根据detailsLineStatus来确定headerStatus 

public void updateHeaderStatus(Header header) { 
        //查询明细
        DetailsSearch search = new DetailsSearch();
        search.setHeaderNo(header.getHeaderNo());
        List<Details> list = detailsMapper.find(search);

        //数量临时变量
        int countMax = 0;//最大状态的单据行的个数
        int countAll = list.size();//单据行数
        String statusMax = Consts.ASN_STATUS_00;//初始化最大状态

        //遍历所有明细
        for (Details d : list) {
            
            //统计状态个数
            String detailsLineStatus = d.getDetailsLineStatus();
            if (statusMax.compareTo(detailsLineStatus) == 0) {
                countMax++;
            }
            if (statusMax.compareTo(detailsLineStatus) < 0) {
                statusMax = detailsLineStatus;
                countMax = 1;
            }
        }

        //计算头状态
        String headerStatus = null;
        if (countAll == countMax) {//所有明细相同状态
            headerStatus = statusMax;
        } else {//明细状态各不相同
            if (v1.equals(statusMax)) {//明细某一完全状态
                asnStatus = 'v1降一级';//设置头部为某一非完全状态
            } else {//其他状态
                headerStatus = statusMax;
            }
        }
        
        header.setHeaderStatus(headerStatus);
        headerMapper.updateByPrimaryKeySelective(header);
}

 

转载于:https://my.oschina.net/u/3496297/blog/1590407

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值