1. /**
    2. * 查询关联产品和其它
    3. * @param depth,commonEntity,nameSpace
    4. * @return
    5. */
    6. private void searchReferences(CommonEntity commonEntity,String nameSpace,int depth) {
    7. Deque<List<Map<String,Object>>> deque = new LinkedList <>();
    8. List<Map<String,Object>> firstList = new ArrayList <>();
    9. if(CollectionUtils.isNotEmpty(commonEntity.getContent())) {
    10. Map <String, Object> contentMap = commonEntity.getContent();
    11. for (Map.Entry <String, Object> entry : contentMap.entrySet()) {
    12. if (entry.getKey().startsWith(FIELD_REFERENCE)) {
    13. processorContentReferenceData(commonEntity.getContent(),entry,firstList,commonEntity,nameSpace);
    14. }
    15. }
    16. deque.push(firstList);
    17. }
    18. //第一层之后 往下 再迭代处理数据 控制深度
    19. while (!deque.isEmpty() && depth > 1){
    20. List<Map <String, Object>> contents = deque.pop();
    21. List<Map<String,Object>> lastList = new ArrayList <>();
    22. for(Map <String, Object> content : contents){
    23. for (Map.Entry <String, Object> entry : content.entrySet()) {
    24. if (entry.getKey().startsWith(FIELD_REFERENCE)) {
    25. processorContentReferenceData(content,entry,lastList,commonEntity,nameSpace);
    26. }
    27. }
    28. }
    29. if(CollectionUtils.isNotEmpty(lastList)){
    30. deque.push(lastList);
    31. }
    32. depth--;
    33. }
    34. }
    35. //处理内部数据
    36. private void processorContentReferenceData(Map <String, Object> contentMap,Map.Entry <String, Object> entry,
    37. List<Map<String,Object>> referenceList,CommonEntity commonEntity,String nameSpace) {
    38. //1. 数组结构的
    39. if(String.valueOf(contentMap.get(entry.getKey())).startsWith("[")){
    40. List <Map <String, Object>> referenceObjects = Stream.of(String.valueOf(contentMap.get(entry.getKey()))
    41. .replace("[","").replace("]","").replace("\"","").split(","))
    42. .map(referenObject -> searchReferenceInner(entry.getKey(),referenObject,commonEntity,nameSpace)).filter(Objects::nonNull).collect(Collectors.toList());
    43. if (CollectionUtils.isNotEmpty(referenceObjects)) {
    44. //直接替换了 查询的数据
    45. contentMap.put(entry.getKey(),referenceObjects);
    46. for(Map <String, Object> referenceObject : referenceObjects){
    47. referenceList.add(referenceObject);
    48. }
    49. }
    50. }else{
    51. //2. 字符串结构的
    52. String referenObject = String.valueOf(contentMap.get(entry.getKey())).replace("\"","");
    53. Map <String, Object> referenceObjectMap = searchReferenceInner(entry.getKey(),referenObject,commonEntity,nameSpace);
    54. if (CollectionUtils.isNotEmpty(referenceObjectMap)) {
    55. //直接替换了 查询的数据
    56. contentMap.put(entry.getKey(),referenceObjectMap);
    57. referenceList.add(referenceObjectMap);
    58. }
    59. }
    60. }
    61. //关联子查询
    62. private Map<String, Object> searchReferenceInner(String referenceStr,String referenValue,CommonEntity commonEntity,String nameSpace) {
    63. String entityType = referenceStr.replace(FIELD_REFERENCE,StringUtils.EMPTY);
    64. //空格变成-
    65. referenValue = CommonEntityDataProcessUtil.formatYiDaField(referenValue);
    66. String detail = commonEntityRepository.findOne(nameSpace,entityType,referenValue,commonEntity.getLanguage());
    67. CommonEntity entity = CommonEntityConvertor.convertStrToEntity(detail);
    68. return entity.getContent();
    69. }

    层层数据查询返回原来数据格式。
    正常查询外层格式,内层不查询,将内层数据存入一个单独的集合,通过拷贝地址,进行查询数据,从而达到内层数据也被查询的目的。