diagnosis.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. //
  46. }
  47. return diagnosisAggregationRecord;
  48. }
  49. @override
  50. Future<List<DiagnosisAggregationRecordModel>?> getListByPatientCode(
  51. String patientCode) async {
  52. try {
  53. List<DiagnosisAggregationRecordModel> records =
  54. <DiagnosisAggregationRecordModel>[];
  55. List<DiagnosisEntity> localRecords = [];
  56. if (kIsOnline) {
  57. localRecords = await db.repositories.diagnosis
  58. .getNotUploadedListByPatientCode(patientCode, Store.user.userCode!);
  59. } else {
  60. localRecords = await db.repositories.diagnosis
  61. .getListByPatientCode(patientCode, userCode: Store.user.userCode!);
  62. }
  63. var currentPatient = Store.user.currentSelectPatientInfo!;
  64. for (var element in localRecords) {
  65. Map<String, dynamic> data = jsonDecode(element.dataJson);
  66. List<DiagnosisAggregationData> list = [];
  67. for (var key in data.keys) {
  68. list.add(DiagnosisAggregationData(
  69. key: key, diagnosisData: jsonEncode(data[key])));
  70. }
  71. records.add(DiagnosisAggregationRecordModel(
  72. appDataId: element.code,
  73. patientCode: element.patientCode,
  74. patientName: currentPatient.patientName,
  75. doctorName: Store.user.displayName,
  76. diagnosisTime: element.createTime,
  77. diagnosisAggregationData: list,
  78. isExistLocalData: element.syncState != OfflineDataSyncState.success,
  79. ));
  80. }
  81. return records;
  82. } catch (e) {
  83. return null;
  84. }
  85. }
  86. /// 创建健康检测
  87. @override
  88. Future<bool> submitDiagnosisAsync(SubmitDiagnosisRequest request) async {
  89. request.token = Store.user.token;
  90. var result = await rpc.vitalDiagnosis.submitDiagnosisAsync(request);
  91. return result;
  92. }
  93. /// 创建健康检测(在线)
  94. @override
  95. Future<bool> syncPatientAndDiagnosisData(
  96. SyncPatientAndDiagnosisDataRequest request) async {
  97. try {
  98. var result = await rpc.vitalDiagnosis.syncPatientAndDiagnosisDataAsync(
  99. request,
  100. );
  101. if (result) {
  102. final code = request.cardNo!;
  103. var existEntity = await _checkPatientExist(code);
  104. if (existEntity != null) {
  105. existEntity.syncState = OfflineDataSyncState.success;
  106. existEntity.diagnosisCount -= 1; // 待上传检测条数减一
  107. if (existEntity.diagnosisCount == 0 &&
  108. existEntity.examCount == 0 &&
  109. existEntity.gxyFollowUpCount == 0 &&
  110. existEntity.tnbFollowUpCount == 0) {
  111. // 要其他数据也上传完成,才能更新overallSyncState为成功
  112. existEntity.overallSyncState = OfflineDataSyncState.success;
  113. }
  114. await db.repositories.patient.update(existEntity);
  115. }
  116. }
  117. return result;
  118. } catch (e) {
  119. logger.e(
  120. "syncPatientAndDiagnosisData - Check patient exist error. Code: ${request.cardNo}.",
  121. e);
  122. return false;
  123. }
  124. }
  125. Future<PatientEntity?> _checkPatientExist(String code) async {
  126. try {
  127. final entity = await db.repositories.patient
  128. .singleByCodeWithUserCode(code, Store.user.userCode!);
  129. return entity;
  130. } catch (e) {
  131. logger.e(
  132. "PatientServiceMock - Check patient exist error. Code: $code.", e);
  133. }
  134. return null;
  135. }
  136. ///获取所有userCode为空的数据,并赋值
  137. @override
  138. Future<void> resettingUsercodeIsEmptyData() async {
  139. if (kIsWeb) return;
  140. var version = await db.database.getVersion();
  141. if (version > 0) {
  142. final list = await db.repositories.diagnosis.queryable.where((x) {
  143. final List<IDbColumnCondition> arr = [];
  144. arr.add(x.isValid.equals(true));
  145. arr.add(x.userCode.equals(''));
  146. return arr;
  147. }).toList();
  148. logger.w(
  149. "DiagnosisManager resettingUsercodeIsEmptyData list.count:${list.length}.");
  150. for (var element in list) {
  151. if (element.syncState == OfflineDataSyncState.success) {
  152. var result = await getDiagnosisAggregationPageAsync(
  153. element.patientCode, 1, 1000);
  154. if (result != null && result.pageData != null) {
  155. var diagnosisAggregationRecord = result.pageData!
  156. .firstWhereOrNull((x) => x.appDataId == element.code);
  157. if (diagnosisAggregationRecord != null) {
  158. element.userCode = diagnosisAggregationRecord.doctorCode ?? '';
  159. }
  160. }
  161. } else {
  162. element.userCode = Store.user.userCode!;
  163. }
  164. await db.repositories.diagnosis.update(element);
  165. }
  166. }
  167. }
  168. @override
  169. Future<List<List<String>>> getTableData(DiagnosisAggregationRecordModel dto,
  170. {bool isLastRecord = false}) async {
  171. var currentDiagnosis = <List<String>>[];
  172. var index = 1;
  173. for (var element in dto.diagnosisAggregationData!) {
  174. if (element.diagnosisData != "null") {
  175. var jsonData = json.decode(element.diagnosisData!);
  176. List<String> keys = [];
  177. if (jsonData is Map) {
  178. for (var key in jsonData.keys) {
  179. keys.add(key);
  180. }
  181. }
  182. List<DictionaryWithUnitDTO>? dtos = [];
  183. if (!kIsOnline) {
  184. for (var key in keys) {
  185. dtos.add(DictionaryWithUnitDTO(
  186. key: key,
  187. name: DiagnosisTranslator.reportTr(key),
  188. unit: DiagnosisTranslator.getExamUnit(key)));
  189. }
  190. } else {
  191. dtos = await Get.find<IDictionaryManager>()
  192. .getDictionaryNameAndUnitByKeysAsync(keys) ??
  193. [];
  194. }
  195. for (var key in keys) {
  196. if (key == "ECG_POINT" || key == "ECG_POINT12" || key == "ECG12") {
  197. continue;
  198. }
  199. var dto = dtos.firstWhereOrNull((item) => item.key == key);
  200. final value = jsonData[key].toString();
  201. if (value.isEmpty) {
  202. // 不展示空值
  203. continue;
  204. }
  205. currentDiagnosis.add([
  206. if (!isLastRecord) (index++).toString(),
  207. dto?.name ?? '',
  208. value,
  209. dto?.unit ?? '',
  210. ]);
  211. }
  212. }
  213. }
  214. return currentDiagnosis;
  215. }
  216. @override
  217. Future<bool> removeDiagnosis(String appDataId) async {
  218. try {
  219. bool result = false;
  220. final entity = await db.repositories.diagnosis.singleByCode(appDataId);
  221. if (entity != null) {
  222. result = await db.repositories.diagnosis.delete(entity.id);
  223. if (!result) {
  224. return false;
  225. }
  226. }
  227. if (kIsOnline) {
  228. result = await rpc.vitalDiagnosis.removeDiagnosisByAppDataIdAsync(
  229. RemoveDiagnosisByAppDataIdRequest(
  230. token: Store.user.token,
  231. appDataId: appDataId,
  232. ),
  233. );
  234. }
  235. return result;
  236. } catch (e) {
  237. logger.e("DiagnosisManager remove diagnosis-$appDataId error.", e);
  238. return false;
  239. }
  240. }
  241. @override
  242. Future<DiagnosisAggregationRecordModel?> getLastRecordInfo() async {
  243. try {
  244. if (Store.user.currentSelectPatientInfo?.code != null) {
  245. List<DiagnosisAggregationRecordModel> getRecords = [];
  246. logger.i('onReadLastRecordInfo: 开始获取 最近检测记录.');
  247. /// 这是在线的
  248. if (kIsOnline) {
  249. var getLastRecords = await getDiagnosisAggregationPageAsync(
  250. Store.user.currentSelectPatientInfo!.code!, 1, 10);
  251. if (getLastRecords != null) {
  252. getRecords.addAll(getLastRecords.pageData ?? []);
  253. logger.i(
  254. 'PatientDetailController onReadLastRecordInfo getLastRecords.length:${getLastRecords.dataCount}.');
  255. } else {
  256. logger.i(
  257. 'PatientDetailController onReadLastRecordInfo no online data available.');
  258. }
  259. }
  260. /// 这是离线的
  261. var listRecords = await getListByPatientCode(
  262. Store.user.currentSelectPatientInfo!.code!);
  263. if (listRecords != null) {
  264. getRecords.addAll(listRecords);
  265. logger.i(
  266. 'PatientDetailController onReadLastRecordInfo listRecords.length:${listRecords.length}.');
  267. } else {
  268. logger.i(
  269. 'PatientDetailController onReadLastRecordInfo no local data available.');
  270. }
  271. if (getRecords.isNotEmpty) {
  272. var lastRecordInfo = getRecords.reduce((curr, next) =>
  273. (curr.diagnosisTime!).isAfter(next.diagnosisTime!) ? curr : next);
  274. logger.i('onReadLastRecordInfo: ${lastRecordInfo.toJson()}');
  275. return lastRecordInfo;
  276. } else {
  277. logger.i('onReadLastRecordInfo: empty');
  278. }
  279. }
  280. } catch (e) {
  281. logger.e('PatientDetailController onReadLastRecordInfo.', e);
  282. }
  283. return null;
  284. }
  285. }