exam.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import 'dart:convert';
  2. import 'package:fis_jsonrpc/rpc.dart';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:vitalapp/database/db.dart';
  5. import 'package:vitalapp/database/entities/defines.dart';
  6. import 'package:vitalapp/global.dart';
  7. import 'package:vitalapp/managers/interfaces/exam.dart';
  8. import 'package:vitalapp/rpc.dart';
  9. import 'package:vitalapp/store/store.dart';
  10. import 'package:fis_common/logger/logger.dart';
  11. class ExamManager implements IExamManager {
  12. String? get userCode => Store.user.userCode;
  13. @override
  14. Future<bool?> createExam(CreateExamRequest examRequest) async {
  15. try {
  16. examRequest.token = Store.user.token;
  17. final examCode = await rpc.vitalExam.createExamAsync(
  18. examRequest,
  19. );
  20. print(jsonEncode(examRequest.toJson()));
  21. if (examCode.isNotEmpty) {
  22. return true;
  23. }
  24. return false;
  25. } catch (e) {
  26. print(jsonEncode(examRequest.toJson()));
  27. logger.e("ExamManager sync user info error.", e);
  28. return null;
  29. }
  30. }
  31. @override
  32. Future<bool?> editExam(UpdateExamRequest examRequest) async {
  33. try {
  34. examRequest.token = Store.user.token;
  35. final result = await rpc.vitalExam.updateExamAsync(
  36. examRequest,
  37. );
  38. return result;
  39. } catch (e) {
  40. logger.e("ExamManager sync user info error.", e);
  41. return null;
  42. }
  43. }
  44. @override
  45. Future<bool?> updateExamByBatchNumberAsync(
  46. UpdateExamByBatchNumberRequest updateExamByBatchNumberRequest) async {
  47. try {
  48. updateExamByBatchNumberRequest.token = Store.user.token;
  49. final result = await rpc.vitalExam.updateExamByBatchNumberAsync(
  50. updateExamByBatchNumberRequest,
  51. );
  52. print(jsonEncode(updateExamByBatchNumberRequest.toJson()));
  53. return result;
  54. } catch (e) {
  55. logger.e("updateExamByBatchNumberAsync sync user info error.", e);
  56. return null;
  57. }
  58. }
  59. @override
  60. Future<List<ExamDTO>?> getExamByBatchNumberAsync(String batchNumber) async {
  61. GetExamByBatchNumberRequest getExamByBatchNumberRequest =
  62. GetExamByBatchNumberRequest();
  63. try {
  64. getExamByBatchNumberRequest.token = Store.user.token;
  65. getExamByBatchNumberRequest.batchNumber = batchNumber;
  66. final result = await rpc.vitalExam.getExamByBatchNumberAsync(
  67. getExamByBatchNumberRequest,
  68. );
  69. print(jsonEncode(getExamByBatchNumberRequest.toJson()));
  70. return result;
  71. } catch (e) {
  72. logger.e("updateExamByBatchNumberAsync sync user info error.", e);
  73. return null;
  74. }
  75. }
  76. @override
  77. Future<List<ExamConclusionDTO>?> getPatientExamByPageAsync(
  78. String patientCode, String examKey) async {
  79. try {
  80. final dtos = <ExamConclusionDTO>[];
  81. if (kIsOnline && !kIsWeb) {
  82. final offlineDtos =
  83. await _getPatientTCMCAllNotUploadedExamList(patientCode);
  84. dtos.addAll(offlineDtos);
  85. }
  86. GetPatientExamByPageRequest request = GetPatientExamByPageRequest(
  87. patientCode: patientCode,
  88. token: Store.user.token,
  89. examKey: examKey,
  90. pageIndex: 1,
  91. pageSize: 9999,
  92. );
  93. final result = await rpc.vitalExam.getPatientExamByPageAsync(request);
  94. if (result.dataCount > 0) {
  95. dtos.addAll(result.pageData!);
  96. }
  97. return dtos;
  98. } catch (e) {
  99. logger.e("getPatientExamByPageAsync sync user info error.", e);
  100. return null;
  101. }
  102. }
  103. @override
  104. Future<bool> checkRecordOfflineCached(String code) async {
  105. try {
  106. final count = await db.repositories.exam.queryable
  107. .where((x) => [
  108. x.isValid.equals(true),
  109. x.userCode.equals(userCode),
  110. x.code.equals(code),
  111. ])
  112. .count();
  113. return count > 0;
  114. } catch (e) {
  115. logger.e("ExamManager checkRecordOfflineCached-$code error", e);
  116. return false;
  117. }
  118. }
  119. Future<List<ExamConclusionDTO>> _getPatientTCMCAllNotUploadedExamList(
  120. String patientCode) async {
  121. final entities = await db.repositories.exam.queryable
  122. .where((x) => [
  123. x.isValid.equals(true),
  124. x.userCode.equals(userCode),
  125. x.patientCode.equals(patientCode),
  126. x.templateKey.equals("HEITCMC"),
  127. x.syncState.notEquals(OfflineDataSyncState.success),
  128. ])
  129. .toList();
  130. if (entities.isEmpty) {
  131. return [];
  132. }
  133. final patient = await db.repositories.patient
  134. .singleByCodeWithUserCode(patientCode, userCode!);
  135. final dtos = entities.map((entity) {
  136. final jsonMap = jsonDecode(entity.dataJson);
  137. return ExamConclusionDTO(
  138. contractedDoctor: "NOT_UPLOADED", // TODO: 先借用一下此字段
  139. code: entity.code,
  140. key: entity.templateKey,
  141. patientCode: entity.patientCode,
  142. patientName: patient?.name ?? "",
  143. examDoctor: Store.user.displayName,
  144. physicalExamNumber: entity.physicalEaxmNumber,
  145. batchNumber: entity.batchNumber,
  146. templateCode: entity.templateCode,
  147. createTime: entity.createTime,
  148. updateTime: entity.updateTime ?? entity.createTime,
  149. examData: entity.dataJson,
  150. tcmConclusion: jsonMap["PhysicalConclusion"],
  151. );
  152. }).toList();
  153. return dtos;
  154. }
  155. }