135 lines
4.5 KiB
Dart
135 lines
4.5 KiB
Dart
import 'dart:convert';
|
||
|
||
import 'package:animated_tree_view/animated_tree_view.dart';
|
||
import 'package:animated_tree_view/tree_view/tree_node.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:starwork_flutter/api/model/team/request/create_new_depart_request.dart';
|
||
import 'package:starwork_flutter/api/model/team/request/get_depart_list_request.dart';
|
||
import 'package:starwork_flutter/api/model/team/response/depart_list_reponse.dart';
|
||
import 'package:starwork_flutter/api/model/user/response/user_info_response.dart';
|
||
import 'package:starwork_flutter/api/service/team_api_service.dart';
|
||
import 'package:starwork_flutter/base/app_logger.dart';
|
||
import 'package:starwork_flutter/base/base_controller.dart';
|
||
import 'package:starwork_flutter/common/constant/cache_keys.dart';
|
||
import 'package:starwork_flutter/common/utils/shared_preferences_utils.dart';
|
||
|
||
class PersonnelManageController extends BaseController {
|
||
final teamApi = Get.find<TeamApiService>();
|
||
final Rx<TreeNode?> treeData = Rx<TreeNode?>(null);
|
||
TreeViewController? treeViewController;
|
||
var selectedDepartItem = DepartItem().obs; // 当前选中的组织
|
||
var cacheUserInfo = UserInfoResponse().obs; // 缓存的登录用户信息
|
||
final RxInt totalPersonCount = 0.obs;
|
||
|
||
@override
|
||
void onReady() {
|
||
super.onReady();
|
||
requestDepartList();
|
||
getCacheUserInfo();
|
||
}
|
||
|
||
requestDepartList() async {
|
||
var response = await teamApi.requestDepartList(
|
||
request: GetDepartListRequest(departNo: ''),
|
||
);
|
||
if (response.isSuccess) {
|
||
// 转换数据为树形结构
|
||
final tree = _convertToTree(response.data!.departList ?? []);
|
||
treeData.value = tree;
|
||
|
||
treeData.refresh();
|
||
// 统计总人数
|
||
totalPersonCount.value = _calculateTotalPersonCount(response.data!.departList ?? []);
|
||
|
||
treeViewController?.expandAllChildren(treeData.value ?? TreeNode.root());
|
||
}
|
||
}
|
||
|
||
// 计算总人数
|
||
int _calculateTotalPersonCount(List<DepartItem> departList) {
|
||
int total = 0;
|
||
for (final depart in departList) {
|
||
total += depart.personNum ?? 0;
|
||
}
|
||
return total;
|
||
}
|
||
|
||
TreeNode _convertToTree(List<DepartItem> departList) {
|
||
// 创建根节点
|
||
final root = TreeNode.root();
|
||
|
||
// 如果列表为空,直接返回根节点
|
||
if (departList.isEmpty) {
|
||
return root;
|
||
}
|
||
|
||
// 创建一个映射来存储所有部门节点,方便查找
|
||
final Map<int, TreeNode> departNodeMap = {};
|
||
|
||
// 先创建所有部门节点
|
||
for (final depart in departList) {
|
||
final node = TreeNode(
|
||
key: depart.id.toString(),
|
||
data: depart,
|
||
);
|
||
departNodeMap[depart.id!] = node;
|
||
}
|
||
|
||
// 建立部门间的父子关系
|
||
for (final depart in departList) {
|
||
final currentNode = departNodeMap[depart.id];
|
||
if (currentNode == null) continue;
|
||
|
||
// 建立部门间的父子关系
|
||
if (depart.parentId == -1) {
|
||
// 顶级部门添加到根节点下
|
||
root.add(currentNode);
|
||
} else {
|
||
// 子部门添加到对应父部门下
|
||
final parentNode = departNodeMap[depart.parentId];
|
||
parentNode?.add(currentNode);
|
||
}
|
||
}
|
||
|
||
// 将人员节点添加到对应的部门节点下(在所有部门结构建立完成后再添加)
|
||
for (final depart in departList) {
|
||
final currentNode = departNodeMap[depart.id];
|
||
if (currentNode == null) continue;
|
||
|
||
// 将该部门下的所有人员作为子节点添加
|
||
if (depart.persons != null && depart.persons!.isNotEmpty) {
|
||
// 对人员按某种规则排序(例如按姓名或ID)
|
||
final sortedPersons = List<PersonItem>.from(depart.persons!);
|
||
// 这里可以根据需要添加排序逻辑,例如按姓名排序:
|
||
// sortedPersons.sort((a, b) => (a.personName ?? '').compareTo(b.personName ?? ''));
|
||
|
||
for (final person in sortedPersons) {
|
||
final personNode = TreeNode(
|
||
key: 'person_${person.id}',
|
||
data: person,
|
||
);
|
||
currentNode.add(personNode);
|
||
}
|
||
}
|
||
}
|
||
|
||
return root;
|
||
}
|
||
|
||
|
||
|
||
void getCacheUserInfo() async {
|
||
String? cachedJson = await SharedPreferencesUtils.getString(CacheKeys.userAccountInfo);
|
||
if (cachedJson != null) {
|
||
try {
|
||
Map<String, dynamic> jsonMap = jsonDecode(cachedJson);
|
||
UserInfoResponse userInfoResponse = UserInfoResponse.fromJson(jsonMap);
|
||
cacheUserInfo.value = userInfoResponse;
|
||
cacheUserInfo.refresh();
|
||
} catch (e) {
|
||
AppLogger.error('JSON 解析错误: $e');
|
||
}
|
||
}
|
||
}
|
||
}
|