当前位置: 首页 > news >正文

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": "蒋大辉"
      }
    ]
  }  
  ''';
}

相关文章:

  • 使用 ADO.NET 访问 Oracle 9i 存储过程
  • 判断一个Flutter的包是plugin还是package
  • 发现苹果的MacOS支持图片OCR文字识别
  • Flutter elevation属性名称的含义
  • ORACLE函数大全
  • IntelliJ、Android Studio 支持多个字符的快捷键 Second stroke
  • DataTable,DataView和DataGrid中一些容易混淆的概念
  • Flutter将工程迁移到空安全的常用代码修改方式
  • DataView能对DataTable的内容做筛选和排序工作
  • Flutter bezier_chart库支持null safe
  • 用dataview过滤datagird中的数据例子
  • Flutter 转 null safe时报错: The argument type ‘Object‘ can‘t be assigned to the parameter type XXX
  • ADO.NET DataSet、DataView 和 DataViewManager 对象指南
  • flutter 旧有V1项目升级V2
  • 在Apple M1 CPU的电脑上设置应用以rosetta方式运行的原理
  • [Vue CLI 3] 配置解析之 css.extract
  • iOS小技巧之UIImagePickerController实现头像选择
  • RxJS 实现摩斯密码(Morse) 【内附脑图】
  • SSH 免密登录
  • vue.js框架原理浅析
  • 道格拉斯-普克 抽稀算法 附javascript实现
  • 后端_MYSQL
  • 你对linux中grep命令知道多少?
  • Hibernate主键生成策略及选择
  • linux 淘宝开源监控工具tsar
  • Python 之网络式编程
  • 你学不懂C语言,是因为不懂编写C程序的7个步骤 ...
  • 说说我为什么看好Spring Cloud Alibaba
  • 选择阿里云数据库HBase版十大理由
  • # 数论-逆元
  • #NOIP 2014# day.2 T2 寻找道路
  • (delphi11最新学习资料) Object Pascal 学习笔记---第2章第五节(日期和时间)
  • (差分)胡桃爱原石
  • (动手学习深度学习)第13章 计算机视觉---图像增广与微调
  • (二)WCF的Binding模型
  • (翻译)Quartz官方教程——第一课:Quartz入门
  • (三)Honghu Cloud云架构一定时调度平台
  • (十八)三元表达式和列表解析
  • (学习日记)2024.01.09
  • (一)Java算法:二分查找
  • (原)记一次CentOS7 磁盘空间大小异常的解决过程
  • (原創) 是否该学PetShop将Model和BLL分开? (.NET) (N-Tier) (PetShop) (OO)
  • (转)Android学习系列(31)--App自动化之使用Ant编译项目多渠道打包
  • (转)http-server应用
  • (转)Linux NTP配置详解 (Network Time Protocol)
  • (转)nsfocus-绿盟科技笔试题目
  • (转)VC++中ondraw在什么时候调用的
  • .bat批处理(一):@echo off
  • .net core 6 集成 elasticsearch 并 使用分词器
  • .Net Memory Profiler的使用举例
  • .net 程序发生了一个不可捕获的异常
  • .net 无限分类
  • .NET/C# 使用 #if 和 Conditional 特性来按条件编译代码的不同原理和适用场景
  • ::什么意思
  • @Query中countQuery的介绍