import 'dart:collection'; import 'package:fis_jsonrpc/rpc.dart'; import 'package:get/get.dart'; import 'package:vnoteapp/architecture/defines.dart'; import 'package:vnoteapp/managers/interfaces/label.dart'; /// 人群分类标签控制器 class CrowdLabelsController extends FControllerBase { final state = CrowdLablesState(); final _manager = Get.find(); @override void onReady() { super.onReady(); _loadOptions(); } void onNormalSelectedChanged(List codes) { state.selectedNormalCodes = codes; } void onDiseaseSelectedChanged(List codes) { state.selectedDiseaseCodes = codes; } void onSpecialCareSelectedChanged(List codes) { state.selectedSpecialCareCodes = codes; } void onItemCheckChanged(String code) { bool result = false; result = _updateNormalChecked(code); if (result) return; result = _updateDiseaseChecked(code); if (result) return; result = _updateSpecialCareChecked(code); } bool _updateNormalChecked(code) { final item = state.normalOptions.firstWhereOrNull((e) => e.code == code); if (item == null) return false; final arr = state.selectedNormalCodes.toList(); if (!arr.contains(code)) { if (arr.contains(code) == false) { arr.add(code); } } else { arr.remove(code); } state.selectedNormalCodes = arr; return true; } bool _updateDiseaseChecked(code) { final item = state.diseaseOptions.firstWhereOrNull((e) => e.code == code); if (item == null) return false; final arr = state.selectedDiseaseCodes.toList(); if (!arr.contains(code)) { if (arr.contains(code) == false) { arr.add(code); } } else { arr.remove(code); } state.selectedDiseaseCodes = arr; return true; } bool _updateSpecialCareChecked(code) { final item = state.specialCareOptions.firstWhereOrNull((e) => e.code == code); if (item == null) return false; final arr = state.selectedSpecialCareCodes.toList(); if (!arr.contains(code)) { if (arr.contains(code) == false) { arr.add(code); } } else { arr.remove(code); } state.selectedSpecialCareCodes = arr; return true; } Future _loadOptions() async { // TODO: Future.await() state.normalOptions = await _manager.getNormalCrowdLabels(); // TODO: state.diseaseOptions = (await _manager.getDiseaseCrowdLabels()) ..removeLast(); // state.diseaseOptions = await _manager.getDiseaseCrowdLabels(); state.specialCareOptions = await _manager.getSpecialCareCrowdLabels(); } } /// 人群分类标签状态 class CrowdLablesState { final RxList _normalOptions = RxList(); final RxList _diseaseOptions = RxList(); final RxList _specialCareOptions = RxList(); final RxList _selectedNormalCodes = RxList(); final RxList _selectedDiseaseCodes = RxList(); final RxList _selectedSpecialCareCodes = RxList(); /// 一般人群分类选项集合 List get normalOptions => UnmodifiableListView(_normalOptions.toList()); set normalOptions(List val) => _normalOptions.value = val; /// 疾病人群分类选项集合 List get diseaseOptions => UnmodifiableListView(_diseaseOptions.toList()); set diseaseOptions(List val) => _diseaseOptions.value = val; /// 特殊照顾人群分类选项集合 List get specialCareOptions => UnmodifiableListView(_specialCareOptions.toList()); set specialCareOptions(List val) => _specialCareOptions.value = val; /// 一般人群分类 已选择Code集合 List get selectedNormalCodes => UnmodifiableListView(_selectedNormalCodes.toList()); set selectedNormalCodes(List val) => _selectedNormalCodes.value = val; /// 疾病人群分类 已选择Code集合 List get selectedDiseaseCodes => UnmodifiableListView(_selectedDiseaseCodes.toList()); set selectedDiseaseCodes(List val) => _selectedDiseaseCodes.value = val; /// 特殊照顾人群分类 已选择Code集合 List get selectedSpecialCareCodes => UnmodifiableListView(_selectedSpecialCareCodes.toList()); set selectedSpecialCareCodes(List val) => _selectedSpecialCareCodes.value = val; /// 所有 已选择Code集合 List get selectedCodes { final array = []; array.addAll(_selectedNormalCodes); array.addAll(_selectedDiseaseCodes); array.addAll(_selectedSpecialCareCodes); return array; } /// 所有 已选择标签名称集合 List get selectedNames { final array = []; for (var code in _selectedNormalCodes) { array.add(_getNameByCode(_normalOptions, code)!); } for (var code in _selectedDiseaseCodes) { array.add(_getNameByCode(_diseaseOptions, code)!); } for (var code in _selectedSpecialCareCodes) { array.add(_getNameByCode(_specialCareOptions, code)!); } return array; } static String? _getNameByCode(List source, String code) { final dto = source.firstWhereOrNull((e) => e.code == code); return dto?.labelName; } }