controller.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. import 'package:flutter/material.dart';
  2. import 'package:flyinsono/architecture/router/desktop/names.dart';
  3. import 'package:flyinsono/architecture/services/modules/config_language.dart';
  4. import 'package:flyinsono/architecture/utils/common_util.dart';
  5. import 'package:flyinsono/architecture/utils/prompt_box.dart';
  6. import 'package:flyinsono/lab/color/lab_colors.dart';
  7. import 'package:flyinsono/lab/components/classify_data_selector/classify_data_selector.dart';
  8. import 'package:flyinsono/lab/components/lab_dialog.dart';
  9. import 'package:flyinsono/lab/manager/interfaces/project.dart';
  10. import 'package:flyinsono/lab/manager/page_manager.dart';
  11. import 'package:flyinsono/lab/mixin/tab_hook_mixin.dart';
  12. import 'package:flyinsono/lab/mock_data/mock_rpc.dart';
  13. import 'package:flyinsono/lab/pages/lab_batch_analysis/controller.dart';
  14. import 'package:flyinsono/lab/pages/lab_image/index.dart';
  15. import 'package:flyinsono/lab/router/lab_route_names.dart';
  16. import 'package:fis_jsonrpc/services/index.dart';
  17. import 'package:flyinsono/managers/interfaces/language.dart';
  18. import 'package:get/get.dart';
  19. import 'package:fis_measure/configs/patient.dart';
  20. import 'package:fis_measure/interfaces/enums/species.dart';
  21. import 'package:fis_common/helpers/encrypt.dart';
  22. import 'index.dart';
  23. class LabDataController extends GetxController with TabHookMixin {
  24. LabDataController({
  25. required this.tabHashCode,
  26. });
  27. @override
  28. final int tabHashCode;
  29. final state = LabDataState();
  30. /// 数据页分为以下五个部分
  31. static const dataSourceSelectorId = "data_source_selector"; // 数据源选择器
  32. static const patientListId = "patient_list"; // 病人/样本列表
  33. static const recordListId = "record_list"; // 检查记录列表
  34. static const imageListId = "image_list"; // 图片列表
  35. /// 数据源
  36. List<ProjectModel> dataSourceList = [];
  37. String selectedDataSourceId = "";
  38. /// 数据源搜索栏输入控制器
  39. final dataSourceSearchInputController = TextEditingController();
  40. /// 病人列表
  41. List<ClassifyData<PatientModel>> patientClassifyData = [];
  42. final languageConfigManager = Get.find<ILanguageConfigManager>();
  43. int selectedClassifyIndex = 0;
  44. int selectedPatientIndex = 0;
  45. bool isPatientListLoading = false;
  46. /// 检查记录列表
  47. List<RecordModel> recordList = [];
  48. bool isRecordListLoading = false;
  49. /// 图片列表
  50. List<ImageModel> imageList = [];
  51. bool isImageListLoading = false;
  52. /// 当前项目的病人类型
  53. OrganizationPatientTypeEnum patientType = OrganizationPatientTypeEnum.Person;
  54. List<PatientModel> get selectedPatientList =>
  55. [patientClassifyData[selectedClassifyIndex].data[selectedPatientIndex]];
  56. // 获取当前选中的病人
  57. PatientModel get selectedPatient =>
  58. patientClassifyData[selectedClassifyIndex].data[selectedPatientIndex];
  59. // 获取当前选中的检查记录
  60. RecordModel get selectedRecord => recordList[state.selectedRecordIndex];
  61. /// 当前无数据
  62. // bool get isNoData => dataSourceList.length == 0;
  63. /// 项目接口
  64. final projectService = Get.find<IProjectManager>();
  65. /// 语言服务
  66. final languageService = Get.find<LanguageService>();
  67. // 查看/编辑
  68. void editRecord() {
  69. print("查看/编辑 $selectedRecord");
  70. PageManager.ins.openNewTab(
  71. LabRouteNames.RecordInfo,
  72. "查看/编辑",
  73. multiple: false,
  74. args: selectedRecord,
  75. );
  76. }
  77. // 删除选中的检查记录
  78. void deleteRecord() async {
  79. dynamic result = await Get.dialog(
  80. LabDialog(
  81. title: "删除检查",
  82. content: "确定要删除当前所选检查?",
  83. ),
  84. barrierColor: LabColors.base100.withOpacity(0.1),
  85. );
  86. if (result is bool && result) {
  87. bool? deleteRecord = await projectService.deleteRecordAsync(
  88. recordCode: selectedRecord.id,
  89. projectCode: selectedDataSourceId,
  90. );
  91. if (deleteRecord ?? false) {
  92. await updatePatientList(selectedDataSourceId);
  93. PromptBox.toast('删除完成');
  94. print("删除检查记录 $selectedRecord");
  95. }
  96. }
  97. }
  98. void clickRecordItem(int index) {
  99. state.selectedRecordIndex = index;
  100. updateImageList(recordList[index].images);
  101. }
  102. void clickPatientItem(int classifyIndex, int index) {
  103. selectedClassifyIndex = classifyIndex;
  104. selectedPatientIndex = index;
  105. updateRecordList(patientClassifyData[classifyIndex].data[index].code);
  106. update([patientListId]);
  107. }
  108. ///删除病人
  109. Future<void> deleteCurrentPatient() async {
  110. String patientCode = patientClassifyData[selectedClassifyIndex]
  111. .data[selectedPatientIndex]
  112. .code;
  113. dynamic result = await Get.dialog(
  114. LabDialog(
  115. title: "删除样本",
  116. content: "确定要删除当前所选样本?",
  117. ),
  118. barrierColor: LabColors.base100.withOpacity(0.1),
  119. );
  120. if (result is bool && result) {
  121. bool deleteRecord =
  122. await projectService.deleteSample(selectedDataSourceId, patientCode);
  123. if (deleteRecord) {
  124. await updatePatientList(selectedDataSourceId);
  125. PromptBox.toast('删除完成');
  126. }
  127. }
  128. }
  129. // 根据病人 Id 选中病人
  130. void selectPatientByCode(String patientCode) {
  131. bool isExist = false;
  132. for (int i = 0; i < patientClassifyData.length; i++) {
  133. for (int j = 0; j < patientClassifyData[i].data.length; j++) {
  134. if (patientClassifyData[i].data[j].code == patientCode) {
  135. selectedClassifyIndex = i;
  136. selectedPatientIndex = j;
  137. isExist = true;
  138. break;
  139. }
  140. }
  141. if (isExist) break;
  142. }
  143. if (!isExist) return;
  144. updateRecordList(patientCode);
  145. update([patientListId]);
  146. }
  147. void clickImage(int index) {
  148. var currentProject = dataSourceList
  149. .firstWhereOrNull((element) => element.id == selectedDataSourceId);
  150. PageManager.ins.openTab(
  151. LabRouteNames.Image,
  152. args: LabImageActiveArguments(
  153. patient: selectedPatient,
  154. record: selectedRecord,
  155. images: imageList,
  156. selectedImageIndex: index,
  157. patientType: patientType,
  158. projectCode: currentProject?.id,
  159. ),
  160. );
  161. }
  162. /// 空图像测试入口
  163. void debugForEmptyImageList() async {
  164. final images = await MockRPC.getImageListByRecordId("");
  165. PageManager.ins.openTab(
  166. LabRouteNames.Image,
  167. args: LabImageActiveArguments(
  168. patient: selectedPatient,
  169. record: selectedRecord,
  170. images: images,
  171. selectedImageIndex: 0,
  172. ),
  173. );
  174. }
  175. /// 进入批量分析页面
  176. void enterBatchAnalysis() {
  177. PageManager.ins.openNewTab(
  178. LabRouteNames.BatchAnalysis,
  179. "批量分析",
  180. args: LabBatchAnalysisInitArguments(
  181. patient: selectedPatient,
  182. record: selectedRecord,
  183. images: imageList,
  184. projectCode: selectedDataSourceId,
  185. ),
  186. );
  187. }
  188. /// 初始化数据源列表
  189. void initDataSourceList() async {
  190. List<ResearchProjectBaseDTO>? researchProjectList =
  191. await projectService.getResearchProjectListAsync(
  192. keyWord: "",
  193. searchType: ResearchProjectSearchTypeEnum.Undefined,
  194. );
  195. List<ProjectModel> projects = [];
  196. researchProjectList?.forEach((element) {
  197. projects.add(ProjectModel(
  198. id: element.code ?? '',
  199. name: element.name ?? '',
  200. type: ProjectType.created,
  201. projectIntroduction: element.introduction ?? '',
  202. organizationPatientType: element.patientType,
  203. ));
  204. });
  205. dataSourceList = projects;
  206. selectedDataSourceId = dataSourceList[0].id;
  207. await updatePatientList(selectedDataSourceId);
  208. update([dataSourceSelectorId]);
  209. }
  210. /// 更新数据源列表
  211. void updateDataSourceList() async {
  212. List<ResearchProjectBaseDTO>? researchProjectList =
  213. await projectService.getResearchProjectListAsync(
  214. keyWord: "",
  215. searchType: ResearchProjectSearchTypeEnum.Undefined,
  216. );
  217. List<ProjectModel> projects = [];
  218. researchProjectList?.forEach((element) {
  219. projects.add(ProjectModel(
  220. id: element.code ?? '',
  221. name: element.name ?? '',
  222. type: ProjectType.created,
  223. projectIntroduction: element.introduction ?? '',
  224. organizationPatientType: element.patientType,
  225. ));
  226. });
  227. dataSourceList = projects;
  228. state.noData = dataSourceList.length == 0;
  229. if (!dataSourceList.map((e) => e.id).contains(selectedDataSourceId)) {
  230. if (dataSourceList.length > 0) {
  231. selectedDataSourceId = dataSourceList.first.id;
  232. } else {
  233. selectedDataSourceId = '';
  234. }
  235. await updatePatientList(selectedDataSourceId);
  236. }
  237. update([dataSourceSelectorId]);
  238. }
  239. // 选择数据源
  240. Future<void> selectDataSource(String? projectId) async {
  241. if (projectId == null) return;
  242. selectedDataSourceId = projectId;
  243. await updatePatientList(projectId);
  244. update([dataSourceSelectorId]);
  245. }
  246. /// 获取项目详情
  247. Future<void> getResearchProjectDetailAsync(String? projectId) async {
  248. if (projectId == null) return;
  249. ResearchProjectDTO? detail = await projectService.getResearchProjectDetail(
  250. projectCode: projectId,
  251. );
  252. patientType = detail?.patientType ?? OrganizationPatientTypeEnum.Person;
  253. await getResearchProjectData(projectId);
  254. }
  255. Future<void> getResearchProjectData(
  256. String projectId,
  257. ) async {
  258. PageCollection<ResearchProjectDataDTO>? result =
  259. await projectService.getResearchProjectDataPagesAsync(
  260. projectCode: projectId,
  261. keyWord: dataSourceSearchInputController.text,
  262. pageIndex: 1,
  263. pageSize: 1000,
  264. sort: 0,
  265. );
  266. List<PatientModel> patients = [];
  267. result?.pageData?.forEach((element) {
  268. String patientName =
  269. FEncryptHelper.decryptBase64(element.patientName ?? '');
  270. if (patientName.isEmpty) {
  271. patientName = "未命名";
  272. }
  273. patients.add(PatientModel(
  274. id: element.devicePatientId ?? '',
  275. code: element.patientCode ?? '',
  276. name: patientName,
  277. isExistUrmData: element.isUrm,
  278. fileSize: CommonUtil.convertBytesToMegabytesOrGigabytes(
  279. int.parse(
  280. element.fileSize ?? '0',
  281. ),
  282. ),
  283. ));
  284. });
  285. if (result?.pageData?.isEmpty ?? true) {
  286. patientClassifyData = [];
  287. isPatientListLoading = false;
  288. state.noPatient = true;
  289. update([patientListId]);
  290. return;
  291. } else {
  292. patientClassifyData = [
  293. ClassifyData(
  294. classifyName: patientType == OrganizationPatientTypeEnum.Person
  295. ? "实验样本"
  296. : "实验样本",
  297. data: patients,
  298. showActionButton: true,
  299. showItemActionButton: true,
  300. ),
  301. ];
  302. state.noPatient = false;
  303. }
  304. if (patients.length > 0) updateRecordList(selectedPatient.code);
  305. isPatientListLoading = false;
  306. update([patientListId]);
  307. }
  308. // 根据数据源更新样本列表(病人列表)
  309. Future<void> updatePatientList(String projectId) async {
  310. isPatientListLoading = true;
  311. isRecordListLoading = true;
  312. isImageListLoading = true;
  313. update([patientListId, recordListId, imageListId]);
  314. if (projectId.isEmpty) {
  315. return;
  316. }
  317. await getResearchProjectDetailAsync(projectId);
  318. isPatientListLoading = false;
  319. update([patientListId, imageListId]);
  320. }
  321. // 根据病人 code 更新检查记录列表
  322. void updateRecordList(String patientCode) async {
  323. isRecordListLoading = true;
  324. isImageListLoading = true;
  325. update([recordListId, imageListId]);
  326. List<RecordModel> result = await projectService.getRecordDetail(
  327. patientCode: patientCode,
  328. patientType: patientType,
  329. selectedPatient: selectedPatient,
  330. );
  331. recordList = result;
  332. if (result.isEmpty) {
  333. state.noRecord = true;
  334. return;
  335. } else {
  336. state.selectedRecordIndex = 0;
  337. updateImageList(result.first.images);
  338. state.noRecord = false;
  339. }
  340. isRecordListLoading = false;
  341. update([recordListId]);
  342. }
  343. /// 切换测量公式
  344. void changeMeasureCalcuators() {
  345. List<DataItemDTO> patientInfo = selectedRecord.patientInfo;
  346. if (patientInfo.isNotEmpty) {
  347. DataItemDTO? dataItem = patientInfo.firstWhereOrNull((element) =>
  348. element.key == "Species" &&
  349. (element.value == "Mouse" || element.value == "Rat"));
  350. if (dataItem != null) {
  351. GlobalPatientConfig.switchSpecies(SpeciesType.mouse);
  352. } else {
  353. GlobalPatientConfig.switchSpecies(SpeciesType.animals);
  354. }
  355. }
  356. }
  357. // 根据检查记录 Id 更新图片列表
  358. void updateImageList(List<ImageModel> resultImage) {
  359. changeMeasureCalcuators();
  360. isImageListLoading = true;
  361. update([imageListId]);
  362. imageList = resultImage;
  363. isImageListLoading = false;
  364. update([imageListId]);
  365. }
  366. // 更新当前选中的检查下的图像列表
  367. void updateImageListByCurrRecord() async {
  368. // 判断当前是否选中了检查记录,如果有则获取检查下的图像列表
  369. if (recordList.isNotEmpty) {
  370. // 取当前选中的检查code,获取详情数据
  371. List<RecordModel> result = await projectService.getRecordDetail(
  372. patientCode: selectedPatient.code,
  373. patientType: patientType,
  374. selectedPatient: selectedPatient,
  375. );
  376. recordList = result;
  377. if (result.isEmpty) {
  378. state.noRecord = true;
  379. return;
  380. } else {
  381. if (result.length > state.selectedRecordIndex) {
  382. updateImageList(result[state.selectedRecordIndex].images);
  383. }
  384. state.noRecord = false;
  385. }
  386. }
  387. }
  388. void updateByLabDataActiveArguments(LabDataActiveArguments arguments) async {
  389. if (selectedDataSourceId != arguments.projectCode) {
  390. // selectedClassifyIndex = 0;
  391. selectedPatientIndex = 0;
  392. }
  393. print("🥓🥓🥓🥓🥓");
  394. print(arguments.projectCode);
  395. print(arguments.patientCode);
  396. print("🥓🥓🥓🥓🥓");
  397. await selectDataSource(arguments.projectCode);
  398. selectPatientByCode(arguments.patientCode);
  399. }
  400. @override
  401. void onSleep() {
  402. // cancelTimer();
  403. print("Lab Data onSleep: $tabHashCode");
  404. }
  405. @override
  406. void onActive(arguments) {
  407. updateDataSourceList();
  408. print("Lab Data onActive: $tabHashCode arguments: $arguments");
  409. if (arguments is LabDataActiveArguments) {
  410. updateByLabDataActiveArguments(arguments);
  411. } else {
  412. try {
  413. updateImageListByCurrRecord();
  414. } catch (e) {
  415. print(e);
  416. }
  417. }
  418. }
  419. /// 在 widget 内存中分配后立即调用。
  420. @override
  421. void onInit() {
  422. super.onInit();
  423. initDataSourceList();
  424. /// 初始化语言包
  425. languageService.init();
  426. }
  427. /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  428. @override
  429. void onReady() {
  430. super.onReady();
  431. }
  432. /// 在 [onDelete] 方法之前调用。
  433. @override
  434. void onClose() {
  435. super.onClose();
  436. }
  437. /// dispose 释放内存
  438. @override
  439. void dispose() {
  440. super.dispose();
  441. }
  442. void debugForHomePage() {
  443. Get.offNamed(RouteNames.Lab.Login);
  444. }
  445. }
  446. class LabDataActiveArguments {
  447. LabDataActiveArguments({
  448. required this.projectCode,
  449. required this.patientCode,
  450. });
  451. final String projectCode;
  452. final String patientCode;
  453. }