经营信息字段回显功能:用map优化多余for循环,查询数据库一步到位,减少对同一张表数据字典用户表的多次查询。 直接组装所有数据字典为map,包括MapString, List, MapString, SysDataUserDi
经营信息字段回显功能:用map优化多余for循环,查询数据库一步到位,减少对同一张表数据字典用户表的多次查询。
直接组装所有数据字典为map,包括Map<String, List>, Map<String, SysDataUserDict> allDataMap,map可以考虑用TreeMap, LinkedHashMap, HashMap
1、抽出集合list到某一个字段list
List<SysDataUserDict> secondList = userDictGroupMap.get(sysDataUserDict.getDataKey());
List<String> collect = new LinkedList<>();
if (CollectionUtil.isNotEmpty(secondList)) {
collect = secondList.stream().map(SysDataUserDict::getDataKey).collect(Collectors.toList());
}
2、详细代码
CollectionItemContentResp operationResp = new CollectionItemContentResp();
operationResp.setDataKey(CollectShopConstant.COLLECT_SHOP_OPERATION);
operationResp.setDataText(CollectShopConstant.COLLECT_SHOP_OPERATION_TEXT);
Map<String, List<SysDataUserDict>> userDictGroupMap =
sysDataUserDictRemoteService.getDataGroupByMerchantId(configReq.getMerchantId()).getData();
//一级
List<SysDataUserDict> firstList = userDictGroupMap.get(CollectShopConstant.COLLECT_SHOP_OPERATION);
List<String> operationList = new LinkedList<>();
if (CollectionUtil.isNotEmpty(firstList)) {
firstList.forEach(sysDataUserDict -> {
//二级
List<SysDataUserDict> secondList = userDictGroupMap.get(sysDataUserDict.getDataKey());
List<String> collect = new LinkedList<>();
if (CollectionUtil.isNotEmpty(secondList)) {
collect = secondList.stream().map(SysDataUserDict::getDataKey).collect(Collectors.toList());
}
operationList.addAll(collect);
});
}
//所有data_key SysDataUserDict
Map<String, SysDataUserDict> allDataMap =
sysDataUserDictRemoteService.getMerchantDictMap(configReq.getMerchantId()).getData();
List<CollectionItemContentResp> child = new LinkedList<>();
if (CollectionUtil.isNotEmpty(operationList)) {
operationList.forEach(operation -> {
SysDataUserDict sysDataUserDict = allDataMap.get(operation);
CollectionItemContentResp collectionItemContentResp = new CollectionItemContentResp();
BeanUtils.copyProperties(sysDataUserDict, collectionItemContentResp);
child.add(collectionItemContentResp);
});
}
operationResp.setChild(child);
if(CollectionUtil.isNotEmpty(child)) {
modelProperty.add(operationResp);
}
3、如果要用其它表,改起来很快的
//加上经营信息
CollectionItemContentResp operationResp = new CollectionItemContentResp();
operationResp.setDataKey(CollectShopConstant.COLLECT_SHOP_OPERATION);
operationResp.setDataText(CollectShopConstant.COLLECT_SHOP_OPERATION_TEXT);
Map<String, List<SysDataDict>> userDictGroupMap = sysDictDataUtils.getGroupDataMap();
// 一级
List<SysDataDict> firstList = userDictGroupMap.get(CollectShopConstant.COLLECT_SHOP_OPERATION);
List<String> operationList = new LinkedList<>();
if (CollectionUtil.isNotEmpty(firstList)) {
firstList.forEach(sysDataUserDict -> {
// 二级
List<SysDataDict> secondList = userDictGroupMap.get(sysDataUserDict.getDataKey());
List<String> collect = new LinkedList<>();
if (CollectionUtil.isNotEmpty(secondList)) {
collect = secondList.stream().map(SysDataDict::getDataKey).collect(Collectors.toList());
}
operationList.addAll(collect);
});
}
// 所有data_key
Map<String, SysDataDict> allDataMap = sysDictDataUtils.getAllDataMap();
List<CollectionItemContentResp> child = new LinkedList<>();
if (CollectionUtil.isNotEmpty(operationList)) {
operationList.forEach(operation -> {
SysDataDict sysDataUserDict = allDataMap.get(operation);
CollectionItemContentResp collectionItemContentResp = new CollectionItemContentResp();
BeanUtils.copyProperties(sysDataUserDict, collectionItemContentResp);
collectionItemContentResp.setDataType(sysDataUserDict.getDictType());
child.add(collectionItemContentResp);
});
}
operationResp.setChild(child);
if(CollectionUtil.isNotEmpty(child)) {
modelProperty.add(operationResp);
}