diagnosis.dart 7.3 KB

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