123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- import 'dart:convert';
- import 'package:fis_jsonrpc/rpc.dart';
- import 'package:flutter/foundation.dart';
- import 'package:get/get.dart';
- import 'package:vital_local_database/core/index.dart';
- import 'package:vitalapp/consts/diagnosis.dart';
- import 'package:vitalapp/database/db.dart';
- import 'package:vitalapp/database/entities/defines.dart';
- import 'package:vitalapp/database/entities/diagnosis.dart';
- import 'package:vitalapp/database/entities/patient.dart';
- import 'package:vitalapp/global.dart';
- import 'package:vitalapp/managers/interfaces/diagnosis.dart';
- import 'package:vitalapp/managers/interfaces/dictionary.dart';
- import 'package:vitalapp/managers/interfaces/models/diagnosis_aggregation_record_model.dart';
- import 'package:vitalapp/rpc.dart';
- import 'package:vitalapp/store/store.dart';
- import 'package:fis_common/logger/logger.dart';
- class DiagnosisManager implements IDiagnosisManager {
- DiagnosisManager();
- @override
- Future<PageCollection<DiagnosisAggregationRecordModel>?>
- getDiagnosisAggregationPageAsync(
- String patientCode, int pageIndex, int pageSize) async {
- PageCollection<DiagnosisAggregationRecordModel> diagnosisAggregationRecord =
- PageCollection<DiagnosisAggregationRecordModel>();
- try {
- var request = DiagnosisPageRequest(
- token: Store.user.token,
- pageIndex: pageIndex,
- pageSize: pageSize,
- patientCode: patientCode,
- );
- diagnosisAggregationRecord.pageData = [];
- var result =
- await rpc.vitalDiagnosis.getDiagnosisAggregationPageAsync(request);
- diagnosisAggregationRecord.pageIndex = result.pageIndex;
- diagnosisAggregationRecord.pageSize = result.pageSize;
- diagnosisAggregationRecord.dataCount = result.dataCount;
- for (var element in result.pageData!) {
- var record = DiagnosisAggregationRecordModel.fromJson(element.toJson());
- record.isExistLocalData = false;
- diagnosisAggregationRecord.pageData!.add(record);
- }
- } catch (e) {
- //
- }
- return diagnosisAggregationRecord;
- }
- @override
- Future<List<DiagnosisAggregationRecordModel>?> getListByPatientCode(
- String patientCode) async {
- try {
- List<DiagnosisAggregationRecordModel> records =
- <DiagnosisAggregationRecordModel>[];
- List<DiagnosisEntity> localRecords = [];
- if (kIsOnline) {
- localRecords = await db.repositories.diagnosis
- .getNotUploadedListByPatientCode(patientCode, Store.user.userCode!);
- } else {
- localRecords = await db.repositories.diagnosis
- .getListByPatientCode(patientCode, userCode: Store.user.userCode!);
- }
- var currentPatient = Store.user.currentSelectPatientInfo!;
- for (var element in localRecords) {
- Map<String, dynamic> data = jsonDecode(element.dataJson);
- List<DiagnosisAggregationData> list = [];
- for (var key in data.keys) {
- list.add(DiagnosisAggregationData(
- key: key, diagnosisData: jsonEncode(data[key])));
- }
- records.add(DiagnosisAggregationRecordModel(
- appDataId: element.code,
- patientCode: element.patientCode,
- patientName: currentPatient.patientName,
- doctorName: Store.user.displayName,
- diagnosisTime: element.createTime,
- diagnosisAggregationData: list,
- isExistLocalData: element.syncState != OfflineDataSyncState.success,
- ));
- }
- return records;
- } catch (e) {
- return null;
- }
- }
- /// 创建健康检测
- @override
- Future<bool> submitDiagnosisAsync(SubmitDiagnosisRequest request) async {
- request.token = Store.user.token;
- var result = await rpc.vitalDiagnosis.submitDiagnosisAsync(request);
- return result;
- }
- /// 创建健康检测(在线)
- @override
- Future<bool> syncPatientAndDiagnosisData(
- SyncPatientAndDiagnosisDataRequest request) async {
- try {
- var result = await rpc.vitalDiagnosis.syncPatientAndDiagnosisDataAsync(
- request,
- );
- if (result) {
- final code = request.cardNo!;
- var existEntity = await _checkPatientExist(code);
- if (existEntity != null) {
- existEntity.syncState = OfflineDataSyncState.success;
- existEntity.diagnosisCount -= 1; // 待上传检测条数减一
- if (existEntity.diagnosisCount == 0 &&
- existEntity.examCount == 0 &&
- existEntity.gxyFollowUpCount == 0 &&
- existEntity.tnbFollowUpCount == 0) {
- // 要其他数据也上传完成,才能更新overallSyncState为成功
- existEntity.overallSyncState = OfflineDataSyncState.success;
- }
- await db.repositories.patient.update(existEntity);
- }
- }
- return result;
- } catch (e) {
- logger.e(
- "syncPatientAndDiagnosisData - Check patient exist error. Code: ${request.cardNo}.",
- e);
- return false;
- }
- }
- Future<PatientEntity?> _checkPatientExist(String code) async {
- try {
- final entity = await db.repositories.patient
- .singleByCodeWithUserCode(code, Store.user.userCode!);
- return entity;
- } catch (e) {
- logger.e(
- "PatientServiceMock - Check patient exist error. Code: $code.", e);
- }
- return null;
- }
- ///获取所有userCode为空的数据,并赋值
- @override
- Future<void> resettingUsercodeIsEmptyData() async {
- if (kIsWeb) return;
- var version = await db.database.getVersion();
- if (version > 0) {
- final list = await db.repositories.diagnosis.queryable.where((x) {
- final List<IDbColumnCondition> arr = [];
- arr.add(x.isValid.equals(true));
- arr.add(x.userCode.equals(''));
- return arr;
- }).toList();
- logger.w(
- "DiagnosisManager resettingUsercodeIsEmptyData list.count:${list.length}.");
- for (var element in list) {
- if (element.syncState == OfflineDataSyncState.success) {
- var result = await getDiagnosisAggregationPageAsync(
- element.patientCode, 1, 1000);
- if (result != null && result.pageData != null) {
- var diagnosisAggregationRecord = result.pageData!
- .firstWhereOrNull((x) => x.appDataId == element.code);
- if (diagnosisAggregationRecord != null) {
- element.userCode = diagnosisAggregationRecord.doctorCode ?? '';
- }
- }
- } else {
- element.userCode = Store.user.userCode!;
- }
- await db.repositories.diagnosis.update(element);
- }
- }
- }
- @override
- Future<List<List<String>>> getTableData(DiagnosisAggregationRecordModel dto,
- {bool isLastRecord = false}) async {
- var currentDiagnosis = <List<String>>[];
- var index = 1;
- for (var element in dto.diagnosisAggregationData!) {
- if (element.diagnosisData != "null") {
- var jsonData = json.decode(element.diagnosisData!);
- List<String> keys = jsonData.keys.toList();
- List<DictionaryWithUnitDTO>? dtos = [];
- if (!kIsOnline) {
- for (var key in keys) {
- dtos.add(DictionaryWithUnitDTO(
- key: key,
- name: DiagnosisTranslator.reportTr(key),
- unit: DiagnosisTranslator.getExamUnit(key)));
- }
- } else {
- dtos = await Get.find<IDictionaryManager>()
- .getDictionaryNameAndUnitByKeysAsync(keys) ??
- [];
- }
- for (var key in keys) {
- if (key == "ECG_POINT" || key == "ECG_POINT12" || key == "ECG12") {
- continue;
- }
- var dto = dtos.firstWhereOrNull((item) => item.key == key);
- final value = jsonData[key].toString();
- if (value.isEmpty) {
- // 不展示空值
- continue;
- }
- currentDiagnosis.add([
- if (!isLastRecord) (index++).toString(),
- dto?.name ?? '',
- value,
- dto?.unit ?? '',
- ]);
- }
- }
- }
- return currentDiagnosis;
- }
- @override
- Future<bool> removeDiagnosis(String appDataId) async {
- try {
- bool result = false;
- final entity = await db.repositories.diagnosis.singleByCode(appDataId);
- if (entity != null) {
- result = await db.repositories.diagnosis.delete(entity.id);
- if (!result) {
- return false;
- }
- }
- if (kIsOnline) {
- result = await rpc.vitalDiagnosis.removeDiagnosisByAppDataIdAsync(
- RemoveDiagnosisByAppDataIdRequest(
- token: Store.user.token,
- appDataId: appDataId,
- ),
- );
- }
- return result;
- } catch (e) {
- logger.e("DiagnosisManager remove diagnosis-$appDataId error.", e);
- return false;
- }
- }
- @override
- Future<DiagnosisAggregationRecordModel?> getLastRecordInfo() async {
- try {
- if (Store.user.currentSelectPatientInfo?.code != null) {
- List<DiagnosisAggregationRecordModel> getRecords = [];
- logger.i('onReadLastRecordInfo: 开始获取 最近检测记录.');
- /// 这是在线的
- if (kIsOnline) {
- var getLastRecords = await getDiagnosisAggregationPageAsync(
- Store.user.currentSelectPatientInfo!.code!, 1, 10);
- if (getLastRecords != null) {
- getRecords.addAll(getLastRecords.pageData ?? []);
- logger.i(
- 'PatientDetailController onReadLastRecordInfo getLastRecords.length:${getLastRecords.dataCount}.');
- } else {
- logger.i(
- 'PatientDetailController onReadLastRecordInfo no online data available.');
- }
- }
- /// 这是离线的
- var listRecords = await getListByPatientCode(
- Store.user.currentSelectPatientInfo!.code!);
- if (listRecords != null) {
- getRecords.addAll(listRecords);
- logger.i(
- 'PatientDetailController onReadLastRecordInfo listRecords.length:${listRecords.length}.');
- } else {
- logger.i(
- 'PatientDetailController onReadLastRecordInfo no local data available.');
- }
- if (getRecords.isNotEmpty) {
- var lastRecordInfo = getRecords.reduce((curr, next) =>
- (curr.diagnosisTime!).isAfter(next.diagnosisTime!) ? curr : next);
- logger.i('onReadLastRecordInfo: ${lastRecordInfo.toJson()}');
- return lastRecordInfo;
- } else {
- logger.i('onReadLastRecordInfo: empty');
- }
- }
- } catch (e) {
- logger.e('PatientDetailController onReadLastRecordInfo.', e);
- }
- return null;
- }
- }
|