Browse Source

1、优化检测离线提交逻辑

guanxinyi 1 year ago
parent
commit
fbf9513649
1 changed files with 44 additions and 62 deletions
  1. 44 62
      lib/pages/medical/controller.dart

+ 44 - 62
lib/pages/medical/controller.dart

@@ -165,7 +165,7 @@ class MedicalController extends FControllerBase with BluetoothAndLocationMixin {
   ) async {
     try {
       List<DiagnosisItem> diagnosisItems = await recordDataCacheManager
-          .convertDiagnosisDataToList(submitDiagnosisDataValue);
+          .verifyDiagnosisDataList(submitDiagnosisDataValue);
 
       state.currentTab = '-1';
       await recordDataCacheManager.saveRecordData(
@@ -173,97 +173,79 @@ class MedicalController extends FControllerBase with BluetoothAndLocationMixin {
         patientCode,
         submitDiagnosisDataValue,
       );
+      state.currentTab = state.medicalMenuList[0].key;
       if (kIsOnline) {
-        SubmitDiagnosisRequest submitDiagnosisRequest = SubmitDiagnosisRequest(
-          appDataId: appDataId,
-          patientCode: patientCode,
-          diagnosisItems: diagnosisItems,
-          diagnosisTime: DateTime.now().toUtc(),
-        );
-        print(submitDiagnosisRequest.toJson());
         busy = true;
+        diagnosisItems = await recordDataCacheManager
+            .convertDiagnosisDataToList(submitDiagnosisDataValue);
         bool result;
-        if (kIsOnline) {
-          result = await _diagnosisManager.syncPatientAndDiagnosisData(
-            SyncPatientAndDiagnosisDataRequest(
-              patientCode: patientCode,
-              patientName:
-                  Store.user.currentSelectPatientInfo?.patientName ?? '',
-              patientAddress:
-                  Store.user.currentSelectPatientInfo?.patientAddress ?? '',
-              patientGender:
-                  Store.user.currentSelectPatientInfo?.patientGender ??
-                      GenderEnum.Unknown,
-              permanentResidenceAddress: Store
-                  .user.currentSelectPatientInfo?.permanentResidenceAddress,
-              phone: Store.user.currentSelectPatientInfo?.phone,
-              cardNo: Store.user.currentSelectPatientInfo?.cardNo,
-              nationality: Store.user.currentSelectPatientInfo?.nationality,
-              birthday: Store.user.currentSelectPatientInfo?.birthday,
-              crowdLabels: Store.user.currentSelectPatientInfo?.crowdLabels,
-              cardType: Store.user.currentSelectPatientInfo?.cardType ??
-                  CardTypeEnum.Identity,
-              contractedDoctor:
-                  Store.user.currentSelectPatientInfo?.contractedDoctor,
-              appDataId: appDataId,
-              diagnosisTime: DateTime.now(),
-              token: Store.user.token,
-              diagnosisItems: diagnosisItems,
-            ),
-          );
-          if (result) {
-            //如果在线,则提交历史数据
-            var submitSuccess = await _submitHistory();
-            if (submitSuccess) {
-              logger.i('当前数据提交成功,且历史记录:$patientCode 提交成功');
-            }
-          }
-        } else {
-          result = await _diagnosisManager
-              .submitDiagnosisAsync(submitDiagnosisRequest);
-        }
+        result = await _diagnosisManager.syncPatientAndDiagnosisData(
+          SyncPatientAndDiagnosisDataRequest(
+            patientCode: patientCode,
+            patientName: Store.user.currentSelectPatientInfo?.patientName ?? '',
+            patientAddress:
+                Store.user.currentSelectPatientInfo?.patientAddress ?? '',
+            patientGender: Store.user.currentSelectPatientInfo?.patientGender ??
+                GenderEnum.Unknown,
+            permanentResidenceAddress:
+                Store.user.currentSelectPatientInfo?.permanentResidenceAddress,
+            phone: Store.user.currentSelectPatientInfo?.phone,
+            cardNo: Store.user.currentSelectPatientInfo?.cardNo,
+            nationality: Store.user.currentSelectPatientInfo?.nationality,
+            birthday: Store.user.currentSelectPatientInfo?.birthday,
+            crowdLabels: Store.user.currentSelectPatientInfo?.crowdLabels,
+            cardType: Store.user.currentSelectPatientInfo?.cardType ??
+                CardTypeEnum.Identity,
+            contractedDoctor:
+                Store.user.currentSelectPatientInfo?.contractedDoctor,
+            appDataId: appDataId,
+            diagnosisTime: DateTime.now(),
+            token: Store.user.token,
+            diagnosisItems: diagnosisItems,
+          ),
+        );
+        busy = false;
         if (result) {
-          busy = false;
+          //如果在线,则提交历史数据
+          var submitSuccess = await _submitHistory();
+          if (submitSuccess) {
+            logger.i('当前数据提交成功,且历史记录:$patientCode 提交成功');
+          }
           recordDataCacheManager.recordSyncStateChange(appDataId);
           PromptBox.toast('提交成功');
-          await saveCachedAppDataId();
-
-          await deleteDirectory();
-          await initReadCached();
+          await initRecordDataState();
 
           ///提交之后,测试结果清空
           diagnosisDataValue.clear();
-          Future.delayed(const Duration(milliseconds: 10), () {
-            state.currentTab = state.medicalMenuList[0].key;
-          });
         } else {
           PromptBox.toast('提交失败');
           logger.i('提交失败:$patientCode');
         }
-        if (busy) {
-          busy = false;
-        }
       } else {
         Future.delayed(const Duration(milliseconds: 10), () {
           state.currentTab = state.medicalMenuList[0].key;
         });
         PromptBox.toast("已缓存至本地");
         logger.i('已缓存至本地:$patientCode');
-        await saveCachedAppDataId();
-
-        await deleteDirectory();
-        await initReadCached();
+        await initRecordDataState();
 
         ///提交之后,测试结果清空
         diagnosisDataValue.clear();
         Get.back();
       }
     } catch (err) {
+      busy = false;
       state.currentTab = state.medicalMenuList[0].key;
       logger.e('submitDiagnosis error: ${err.toString()}');
     }
   }
 
+  Future<void> initRecordDataState() async {
+    await saveCachedAppDataId();
+    await deleteDirectory();
+    await initReadCached();
+  }
+
   Future<void> createDiagnosis() async {
     busy = true;
     try {