【实习笔记】数据回显时字段的反查

问题描述

页面要展示数据库主表数据,但是主表中只存储了子表的id,如果直接展示主表的数据的话,相应子表的字段展示不出来。

解决方式:

通常在涉及数据展示的时候,我们需要展示的是有意义的数据,如果是单表的话可以直接展示,如果是主子表的话,要拿外键到相对的外表查询对应的数据项,再从数据项中取字段。
【实习笔记】数据回显时字段的反查_第1张图片

具体代码:

private List getDisList(int companyId, String language, List list, boolean isDis) {
        if (list.size() == 0 || !isDis) {
            return list;
        }
        List pivDTOs = baseParamInfoService.selTableSelectvl(language, companyId,    "payhrcSalcycle,idtype,");
        List payhrcSalcycleList = pivDTOs.get(0).paramList;
        List idtypeList = pivDTOs.get(1).paramList;
        StringBuffer companyIds = new StringBuffer();
        StringBuffer mupdNames = new StringBuffer();
        StringBuffer mupdPaylegals = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            if(StringUtils.isNotBlank(list.get(i).companyId)) companyIds.append("," + list.get(i).companyId);
            if(StringUtils.isNotBlank(list.get(i).mupdName)) mupdNames.append("," + list.get(i).mupdName);
            if(StringUtils.isNotBlank(list.get(i).mupdPaylegal)) mupdPaylegals.append("," + list.get(i).mupdPaylegal);
        }
        Map baseCompanyMap = new HashMap();
        if(companyIds.length() > 0) {
            baseCompanyMap = baseCompanyService.getMapByIds(companyId, language, companyIds.substring(1),  false);
        }
        Map empInfoMap = new HashMap();
        if(mupdNames.length() > 0) {
            empInfoMap = empInfoService.getMapByIds(companyId, language, mupdNames.substring(1),  false);
        }
        Map orgLegaletyMap = new HashMap<>();
        if (mupdPaylegals.length() > 0) {
            orgLegaletyMap = orgLegaletyService.getMapByIds(companyId, language, mupdPaylegals.substring(1), false);
        }
        for (int i = 0; i < list.size(); i++) {
            if (baseCompanyMap.get(list.get(i).companyId)!=null) {
                list.get(i).companyIdDis = baseCompanyMap.get(list.get(i).companyId).compFnameCnDis;
            }
            list.get(i).mupdCutoffDis =  PTUtils.getParamValueDesc(list.get(i).mupdCutoff, payhrcSalcycleList);
            list.get(i).mupdSalcycleDis = list.get(i).mupdSalcycle;
            if (empInfoMap.get(list.get(i).mupdName)!=null) {
                list.get(i).mupdNameDis = empInfoMap.get(list.get(i).mupdName).empName;
            }
            if (orgLegaletyMap.get(list.get(i).mupdPaylegal) != null) {
                list.get(i).mupdPaylegalDis = orgLegaletyMap.get(list.get(i).mupdPaylegal).legtName;
            }
            list.get(i).mupdIdtypeDis =  PTUtils.getParamValueDesc(list.get(i).mupdIdtype, idtypeList);
            TsPubFuns.formatObjectForDisplay(list.get(i));
            }

        return list;
    }

代码解析:

【实习笔记】数据回显时字段的反查_第2张图片

第一步:拿到主表数据

通过getDisList方法拿到主表PayMongsupd中所有的数据,返回list

private List getDisList(int companyId, String language, List list, boolean isDis) {
        if (list.size() == 0 || !isDis) {
            return list;
        }
第二步:对主表数据进行处理

① 主表数据项中存储着可以直接显示的有意义字段和子表对应字段id(companyId,mupdName,mupdPaylegal),通过定义StringBuffer(companyIds,mupdNames,mupdPaylegals)接受list中所有的对应id

for (int i = 0; i < list.size(); i++) {
            if(StringUtils.isNotBlank(list.get(i).companyId)) companyIds.append("," + list.get(i).companyId);
            if(StringUtils.isNotBlank(list.get(i).mupdName)) mupdNames.append("," + list.get(i).mupdName);
            if(StringUtils.isNotBlank(list.get(i).mupdPaylegal)) mupdPaylegals.append("," + list.get(i).mupdPaylegal);
        }

② 通过Map( baseCompanyMap),Map( empInfoMap),Map(Map)接收(BaseCompany,EmpInfo,OrgLegalety)中根据对应id查询出的数据项

Map baseCompanyMap = new HashMap();
        if(companyIds.length() > 0) {
            baseCompanyMap = baseCompanyService.getMapByIds(companyId, language, companyIds.substring(1),  false);
        }
        Map empInfoMap = new HashMap();
        if(mupdNames.length() > 0) {
            empInfoMap = empInfoService.getMapByIds(companyId, language, mupdNames.substring(1),  false);
        }
        Map orgLegaletyMap = new HashMap<>();
        if (mupdPaylegals.length() > 0) {
            orgLegaletyMap = orgLegaletyService.getMapByIds(companyId, language, mupdPaylegals.substring(1), false);
        }

③ 对要展示的字段用虚拟字段接收

for (int i = 0; i < list.size(); i++) {
            if (baseCompanyMap.get(list.get(i).companyId)!=null) {
                list.get(i).companyIdDis = baseCompanyMap.get(list.get(i).companyId).compFnameCnDis;
            }
            list.get(i).mupdCutoffDis =  PTUtils.getParamValueDesc(list.get(i).mupdCutoff, payhrcSalcycleList);
            list.get(i).mupdSalcycleDis = list.get(i).mupdSalcycle;
            if (empInfoMap.get(list.get(i).mupdName)!=null) {
                list.get(i).mupdNameDis = empInfoMap.get(list.get(i).mupdName).empName;
            }
            if (orgLegaletyMap.get(list.get(i).mupdPaylegal) != null) {
                list.get(i).mupdPaylegalDis = orgLegaletyMap.get(list.get(i).mupdPaylegal).legtName;
            }
            
第三步:返回List
return list;
    }

你可能感兴趣的:(java)