/**
* 查询关联产品和其它
* @param depth,commonEntity,nameSpace
* @return
*/
private void searchReferences(CommonEntity commonEntity,String nameSpace,int depth) {
Deque<List<Map<String,Object>>> deque = new LinkedList <>();
List<Map<String,Object>> firstList = new ArrayList <>();
if(CollectionUtils.isNotEmpty(commonEntity.getContent())) {
Map <String, Object> contentMap = commonEntity.getContent();
for (Map.Entry <String, Object> entry : contentMap.entrySet()) {
if (entry.getKey().startsWith(FIELD_REFERENCE)) {
processorContentReferenceData(commonEntity.getContent(),entry,firstList,commonEntity,nameSpace);
}
}
deque.push(firstList);
}
//第一层之后 往下 再迭代处理数据 控制深度
while (!deque.isEmpty() && depth > 1){
List<Map <String, Object>> contents = deque.pop();
List<Map<String,Object>> lastList = new ArrayList <>();
for(Map <String, Object> content : contents){
for (Map.Entry <String, Object> entry : content.entrySet()) {
if (entry.getKey().startsWith(FIELD_REFERENCE)) {
processorContentReferenceData(content,entry,lastList,commonEntity,nameSpace);
}
}
}
if(CollectionUtils.isNotEmpty(lastList)){
deque.push(lastList);
}
depth--;
}
}
//处理内部数据
private void processorContentReferenceData(Map <String, Object> contentMap,Map.Entry <String, Object> entry,
List<Map<String,Object>> referenceList,CommonEntity commonEntity,String nameSpace) {
//1. 数组结构的
if(String.valueOf(contentMap.get(entry.getKey())).startsWith("[")){
List <Map <String, Object>> referenceObjects = Stream.of(String.valueOf(contentMap.get(entry.getKey()))
.replace("[","").replace("]","").replace("\"","").split(","))
.map(referenObject -> searchReferenceInner(entry.getKey(),referenObject,commonEntity,nameSpace)).filter(Objects::nonNull).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(referenceObjects)) {
//直接替换了 查询的数据
contentMap.put(entry.getKey(),referenceObjects);
for(Map <String, Object> referenceObject : referenceObjects){
referenceList.add(referenceObject);
}
}
}else{
//2. 字符串结构的
String referenObject = String.valueOf(contentMap.get(entry.getKey())).replace("\"","");
Map <String, Object> referenceObjectMap = searchReferenceInner(entry.getKey(),referenObject,commonEntity,nameSpace);
if (CollectionUtils.isNotEmpty(referenceObjectMap)) {
//直接替换了 查询的数据
contentMap.put(entry.getKey(),referenceObjectMap);
referenceList.add(referenceObjectMap);
}
}
}
//关联子查询
private Map<String, Object> searchReferenceInner(String referenceStr,String referenValue,CommonEntity commonEntity,String nameSpace) {
String entityType = referenceStr.replace(FIELD_REFERENCE,StringUtils.EMPTY);
//空格变成-
referenValue = CommonEntityDataProcessUtil.formatYiDaField(referenValue);
String detail = commonEntityRepository.findOne(nameSpace,entityType,referenValue,commonEntity.getLanguage());
CommonEntity entity = CommonEntityConvertor.convertStrToEntity(detail);
return entity.getContent();
}
层层数据查询返回原来数据格式。
正常查询外层格式,内层不查询,将内层数据存入一个单独的集合,通过拷贝地址,进行查询数据,从而达到内层数据也被查询的目的。