diagnosis.dart 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import 'dart:convert';
  2. import 'package:fis_jsonrpc/rpc.dart';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:get/get.dart';
  5. import 'package:vital_local_database/core/index.dart';
  6. import 'package:vitalapp/consts/diagnosis.dart';
  7. import 'package:vitalapp/database/db.dart';
  8. import 'package:vitalapp/database/entities/defines.dart';
  9. import 'package:vitalapp/database/entities/diagnosis.dart';
  10. import 'package:vitalapp/database/entities/patient.dart';
  11. import 'package:vitalapp/global.dart';
  12. import 'package:vitalapp/managers/interfaces/diagnosis.dart';
  13. import 'package:vitalapp/managers/interfaces/dictionary.dart';
  14. import 'package:vitalapp/managers/interfaces/models/diagnosis_aggregation_record_model.dart';
  15. import 'package:vitalapp/rpc.dart';
  16. import 'package:vitalapp/store/store.dart';
  17. import 'package:fis_common/logger/logger.dart';
  18. class DiagnosisManager implements IDiagnosisManager {
  19. DiagnosisManager();
  20. @override
  21. Future<PageCollection<DiagnosisAggregationRecordModel>?>
  22. getDiagnosisAggregationPageAsync(
  23. String patientCode, int pageIndex, int pageSize) async {
  24. PageCollection<DiagnosisAggregationRecordModel> diagnosisAggregationRecord =
  25. PageCollection<DiagnosisAggregationRecordModel>();
  26. try {
  27. var request = DiagnosisPageRequest(
  28. token: Store.user.token,
  29. pageIndex: pageIndex,
  30. pageSize: pageSize,
  31. patientCode: patientCode,
  32. );
  33. diagnosisAggregationRecord.pageData = [];
  34. var result =
  35. await rpc.vitalDiagnosis.getDiagnosisAggregationPageAsync(request);
  36. diagnosisAggregationRecord.pageIndex = result.pageIndex;
  37. diagnosisAggregationRecord.pageSize = result.pageSize;
  38. diagnosisAggregationRecord.dataCount = result.dataCount;
  39. for (var element in result.pageData!) {
  40. var record = DiagnosisAggregationRecordModel.fromJson(element.toJson());
  41. record.isExistLocalData = false;
  42. diagnosisAggregationRecord.pageData!.add(record);
  43. }
  44. } catch (e) {}
  45. return diagnosisAggregationRecord;
  46. }
  47. @override
  48. Future<List<DiagnosisAggregationRecordModel>?> getListByPatientCode(
  49. String patientCode) async {
  50. try {
  51. List<DiagnosisAggregationRecordModel> records =
  52. <DiagnosisAggregationRecordModel>[];
  53. List<DiagnosisEntity> localRecords = [];
  54. localRecords = await db.repositories.diagnosis
  55. .getNotUploadedListByPatientCode(patientCode, Store.user.userCode!);
  56. if (!kIsOnline) {
  57. localRecords = await db.repositories.diagnosis
  58. .getListByPatientCode(patientCode, userCode: Store.user.userCode!);
  59. }
  60. var currentPatient = Store.user.currentSelectPatientInfo!;
  61. for (var element in localRecords) {
  62. Map<String, dynamic> data = jsonDecode(element.dataJson);
  63. List<DiagnosisAggregationData> list = [];
  64. for (var key in data.keys) {
  65. list.add(DiagnosisAggregationData(
  66. key: key, diagnosisData: jsonEncode(data[key])));
  67. }
  68. records.add(DiagnosisAggregationRecordModel(
  69. patientCode: element.patientCode,
  70. patientName: currentPatient.patientName,
  71. doctorName: Store.user.displayName,
  72. diagnosisTime: element.createTime,
  73. diagnosisAggregationData: list,
  74. isExistLocalData: element.syncState != OfflineDataSyncState.success,
  75. ));
  76. }
  77. return records;
  78. } catch (e) {}
  79. return null;
  80. }
  81. /// 创建健康检测
  82. @override
  83. Future<bool> submitDiagnosisAsync(SubmitDiagnosisRequest request) async {
  84. request.token = Store.user.token;
  85. var result = await rpc.vitalDiagnosis.submitDiagnosisAsync(request);
  86. return result;
  87. }
  88. /// 创建健康检测(在线)
  89. @override
  90. Future<bool> syncPatientAndDiagnosisData(
  91. SyncPatientAndDiagnosisDataRequest request) async {
  92. try {
  93. var result = await rpc.vitalDiagnosis.syncPatientAndDiagnosisDataAsync(
  94. request,
  95. );
  96. if (result) {
  97. final code = request.cardNo!;
  98. var existEntity = await _checkPatientExist(code);
  99. if (existEntity != null) {
  100. existEntity.syncState = OfflineDataSyncState.success;
  101. existEntity.diagnosisCount -= 1; // 待上传检测条数减一
  102. if (existEntity.diagnosisCount == 0 &&
  103. existEntity.examCount == 0 &&
  104. existEntity.followUpCount == 0) {
  105. // 要其他数据也上传完成,才能更新overallSyncState为成功
  106. existEntity.overallSyncState = OfflineDataSyncState.success;
  107. }
  108. await db.repositories.patient.update(existEntity);
  109. }
  110. }
  111. return result;
  112. } catch (e) {
  113. logger.e(
  114. "syncPatientAndDiagnosisData - Check patient exist error. Code: ${request.cardNo}.",
  115. e);
  116. return false;
  117. }
  118. }
  119. Future<PatientEntity?> _checkPatientExist(String code) async {
  120. try {
  121. final entity = await db.repositories.patient.singleByCode(code);
  122. return entity;
  123. } catch (e) {
  124. logger.e(
  125. "PatientServiceMock - Check patient exist error. Code: $code.", e);
  126. }
  127. return null;
  128. }
  129. ///获取所有userCode为空的数据,并赋值
  130. @override
  131. Future<void> resettingUsercodeIsEmptyData() async {
  132. if (kIsWeb) return;
  133. var version = await db.database.getVersion();
  134. if (version > 0) {
  135. final list = await db.repositories.diagnosis.queryable.where((x) {
  136. final List<IDbColumnCondition> arr = [];
  137. arr.add(x.isValid.equals(true));
  138. arr.add(x.userCode.equals(''));
  139. return arr;
  140. }).toList();
  141. logger.w(
  142. "DiagnosisManager resettingUsercodeIsEmptyData list.count:${list.length}.");
  143. for (var element in list) {
  144. if (element.syncState == OfflineDataSyncState.success) {
  145. var result = await getDiagnosisAggregationPageAsync(
  146. element.patientCode, 1, 1000);
  147. if (result != null && result.pageData != null) {
  148. var diagnosisAggregationRecord = result.pageData!
  149. .firstWhereOrNull((x) => x.appDataId == element.code);
  150. if (diagnosisAggregationRecord != null) {
  151. element.userCode = diagnosisAggregationRecord.doctorCode ?? '';
  152. }
  153. }
  154. } else {
  155. element.userCode = Store.user.userCode!;
  156. }
  157. await db.repositories.diagnosis.update(element);
  158. }
  159. }
  160. }
  161. @override
  162. Future<List<List<String>>> getTableData(DiagnosisAggregationRecordModel dto,
  163. {bool isLastRecord = false}) async {
  164. var currentDiagnosis = <List<String>>[];
  165. var index = 1;
  166. for (var element in dto.diagnosisAggregationData!) {
  167. if (element.diagnosisData != "null") {
  168. var jsonData = json.decode(element.diagnosisData!);
  169. List<String> keys = jsonData.keys.toList();
  170. List<DictionaryWithUnitDTO>? dtos = [];
  171. if (!kIsOnline) {
  172. for (var key in keys) {
  173. dtos.add(DictionaryWithUnitDTO(
  174. key: key,
  175. name: DiagnosisTranslator.reportTr(key),
  176. unit: DiagnosisTranslator.getExamUnit(key)));
  177. }
  178. } else {
  179. dtos = await Get.find<IDictionaryManager>()
  180. .getDictionaryNameAndUnitByKeysAsync(keys) ??
  181. [];
  182. }
  183. for (var key in keys) {
  184. if (key == "ECG_POINT" || key == "ECG_POINT12") {
  185. continue;
  186. }
  187. var dto = dtos.firstWhereOrNull((item) => item.key == key);
  188. final value = jsonData[key].toString();
  189. if (value.isEmpty) {
  190. // 不展示空值
  191. continue;
  192. }
  193. currentDiagnosis.add([
  194. if (!isLastRecord) (index++).toString(),
  195. dto?.name ?? '',
  196. value,
  197. dto?.unit ?? '',
  198. ]);
  199. }
  200. }
  201. }
  202. return currentDiagnosis;
  203. }
  204. }