Flutter 公司组织结构实体类递归构造 递归查询

        服务端返回的公司组织结构实体对象的结构是树状的,客户端在构建实现对象时,需要递归的把各级的实体类都构建出来。在查询的时候,也需要用递归的方式来查询。

void main() {
  doFind();
}

void doFind() {
  String jsonString = getJsonString();
  Map<String, dynamic> rootMap = jsonDecode(jsonString);
  OrganizationModel rootOrg = OrganizationModel.fromJson(rootMap);

  List foundUsers = [];
  searchUser(rootOrg, '张', foundUsers);

  print('found user count:${foundUsers.length}');
}

///按用户名查询,只查询用户,不查组织。
void searchUser(OrganizationModel rootOrganizationModel, String keyWord, List resultUsers) {
  //用户组织结构中的所有用户(不包含组织),不仅限于本级。用于查询。
  List<OrganizationModel> allUsers = [];
  recursiveFoundAllUser(rootOrganizationModel, allUsers);

  for (OrganizationModel user in allUsers) {
    if (user.name != null && user.name.contains(keyWord)) {
      resultUsers.add(user);
    }
  }
}

///递归的从组织结构树中把用户都取出来,只取用户,不取部门。用于组织结构页面中的查询功能。
///查到的结果通过引用参数foundUsers返回。
void recursiveFoundAllUser(OrganizationModel currentOrganizationModel, List foundUsers) {
  List users = currentOrganizationModel.users;
  if (users != null) {
    for (OrganizationModel user in users) {
      //重建对象,防止引用对象产生修改问题。
      OrganizationModel copiedUser = OrganizationModel.empty();
      copiedUser.id = user.id;
      copiedUser.name = user.name;

      foundUsers.add(copiedUser);
    }
  }

  List organizations = currentOrganizationModel.organizations;
  if (organizations == null || organizations.length == 0) {
    return;
  }

  for (OrganizationModel childOrganization in organizations) {
    //递归
    recursiveFoundAllUser(childOrganization, foundUsers);
  }
}

class OrganizationModel {
  int id;
  String name;
  List<OrganizationModel> organizations;
  List<OrganizationModel> users;

  OrganizationModel(
    this.id,
    this.name,
    this.organizations,
    this.users,
  );

  OrganizationModel.empty();

  factory OrganizationModel.fromJson(Map<String, dynamic> json) {
    return OrganizationModel(
      json['id'] as int,
      json['name'] as String,
      (json['organizations'] as List)?.map((e) => e == null ? null : OrganizationModel.fromJson(e as Map<String, dynamic>))?.toList(),
      (json['users'] as List)?.map((e) => e == null ? null : OrganizationModel.fromJson(e as Map<String, dynamic>))?.toList(),
    );
  }
}

String getJsonString() {
  return '''
  {
    "id": 6636142779154173952,
    "name": "北京世纪教育科技有限公司",
    "organizations": [
      {
        "id": 6636240725304545280,
        "name": "战略合作中心",
        "organizations": null,
        "users": [
          {
            "id": 6637575629174870016,
            "name": "张大继"
          },
          {
            "id": 6637293257711816704,
            "name": "金大瑞"
          }
        ]
      },
      {
        "id": 6636522476308402176,
        "name": "市场中心",
        "organizations": [
          {
            "id": 6769480335790051328,
            "name": "环京战区",
            "organizations": [
              {
                "id": 6637566159619231744,
                "name": "大连分公司",
                "organizations": [
                  {
                    "id": 6829963307508699136,
                    "name": "总监一部",
                    "organizations": [
                      {
                        "id": 6829999798028996608,
                        "name": "经理一部",
                        "organizations": null,
                        "users": [
                          {
                            "id": 6776071027786125312,
                            "name": "张大阳"
                          },
                          {
                            "id": 6677831655157272576,
                            "name": "吴大煜"
                          }
                        ]
                      }
                    ],
                    "users": [
                      {
                        "id": 6853959974348001280,
                        "name": "崔大伟"
                      },
                      {
                        "id": 6776071292794834944,
                        "name": "吴大涵"
                      },
                      {
                        "id": 6776071223655927808,
                        "name": "张大国"
                      }
                    ]
                  }
                ],
                "users": [
                  {
                    "id": 6785359176228016128,
                    "name": "张白云"
                  }
                ]
              }
            ],
            "users": null
          }
        ],
        "users": null
      },
      {
        "id": 6637251187156586496,
        "name": "财务部",
        "organizations": [
          {
            "id": 6769483913367785472,
            "name": "总部财务",
            "organizations": null,
            "users": [
              {
                "id": 6637325183000645632,
                "name": "张大楠"
              },
              {
                "id": 6637320375967551488,
                "name": "王大连"
              }
            ]
          }
        ],
        "users": null
      },
      {
        "id": 6637251450378522624,
        "name": "总裁办",
        "organizations": null,
        "users": [
          {
            "id": 6637247835978993664,
            "name": "李大佳"
          },
          {
            "id": 6637214779612205056,
            "name": "李大东"
          }
        ]
      },
      {
        "id": 6636522498269777920,
        "name": "技术中心",
        "organizations": [
          {
            "id": 6637620686112296960,
            "name": "总部技术中心",
            "organizations": null,
            "users": [
              {
                "id": 6840561320513900544,
                "name": "方大勇"
              },
              {
                "id": 6637556098842890240,
                "name": "李大霏"
              }
            ]
          },
          {
            "id": 6637620745000325120,
            "name": "北京大区",
            "organizations": null,
            "users": [
              {
                "id": 6637288574620405760,
                "name": "周大媛"
              }
            ]
          },
          {
            "id": 6637620810527936512,
            "name": "东北大区",
            "organizations": null,
            "users": [
              {
                "id": 6637292718395625472,
                "name": "池大霁"
              },
              {
                "id": 6722713638320345088,
                "name": "曹大强"
              }
            ]
          },
          {
            "id": 6637620880916746240,
            "name": "山东大区",
            "organizations": null,
            "users": [
              {
                "id": 6637293160861143040,
                "name": "伊大帅"
              },
              {
                "id": 6637266416984788992,
                "name": "曾大成"
              },
              {
                "id": 6637626476143448064,
                "name": "迟一礼"
              }
            ]
          }
        ],
        "users": null
      },
      {
        "id": 6637251507035181056,
        "name": "人力资源中心",
        "organizations": null,
        "users": [
          {
            "id": 6695878785629294592,
            "name": "闫大京"
          },
          {
            "id": 6709718214345101312,
            "name": "梁大芬"
          }
        ]
      }
    ],
    "users": [
      {
        "id": 6868841707459448832,
        "name": "张大丹"
      },
      {
        "id": 6864502026701770752,
        "name": "蒋大辉"
      }
    ]
  }  
  ''';
}

    原文作者:gaoyp
    原文地址: https://blog.csdn.net/gaoyp/article/details/122681828
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞