Quellcode durchsuchen

Fix 修复优化离线数据上传出错停止上传的问题

Yue vor 1 Monat
Ursprung
Commit
217b4e9098

+ 9 - 9
UI/CF.APP/chicken_farm/lib/apis/breeding/_submit.dart

@@ -36,7 +36,7 @@ class BreedSubmitApi {
       }).toList();
       ResultModel result = ResultModel.offline();
       if (!AppConfig.isOffline &&
-          await _breedingDataService.queryCount(TableConfig.chicken) == 0) {
+          await _breedingDataService.queryTotalCount() == 0) {
         result = await ApiService().post(
           bindChickenUrl,
           data: list,
@@ -69,7 +69,7 @@ class BreedSubmitApi {
       }).toList();
       ResultModel result = ResultModel.offline();
       if (!AppConfig.isOffline &&
-          await _breedingDataService.queryCount(TableConfig.chicken) == 0) {
+          await _breedingDataService.queryTotalCount() == 0) {
         result = await ApiService().post(
           cageChangeUrl,
           data: list,
@@ -100,7 +100,7 @@ class BreedSubmitApi {
       List<Map<String, dynamic>> list = [weightData].toList();
       ResultModel result = ResultModel.offline();
       if (!AppConfig.isOffline &&
-          await _breedingDataService.queryCount(TableConfig.chicken) == 0) {
+          await _breedingDataService.queryTotalCount() == 0) {
         result = await ApiService().post(
           weightUrl,
           data: list,
@@ -130,7 +130,7 @@ class BreedSubmitApi {
       ];
       ResultModel result = ResultModel.offline();
       if (!AppConfig.isOffline &&
-          await _breedingDataService.queryCount(TableConfig.chicken) == 0) {
+          await _breedingDataService.queryTotalCount() == 0) {
         result = await ApiService().post(
           cullUrl,
           data: list,
@@ -161,7 +161,7 @@ class BreedSubmitApi {
       }).toList();
       ResultModel result = ResultModel.offline();
       if (!AppConfig.isOffline &&
-          await _breedingDataService.queryCount(TableConfig.chicken) == 0) {
+          await _breedingDataService.queryTotalCount() == 0) {
         result = await ApiService().post(
           cullUrl,
           data: list,
@@ -200,13 +200,13 @@ class BreedSubmitApi {
   String getUrl(String type) {
     switch (type) {
       case BreedConfig.chicken:
-        return '$apiPrefix/bindChicken';
+        return '$apiPrefix/addChicken';
       case BreedConfig.cageChange:
-        return '$apiPrefix/cageChange';
+        return '$apiPrefix/changeCage';
       case BreedConfig.weight:
-        return '$apiPrefix/weight';
+        return '$apiPrefix/chickenWeight';
       case BreedConfig.cull:
-        return '$apiPrefix/cull';
+        return '$apiPrefix/cullChicken';
       default:
         return '';
     }

+ 11 - 5
UI/CF.APP/chicken_farm/lib/core/services/upload_service.dart

@@ -9,7 +9,7 @@ import 'package:chicken_farm/core/utils/service_checker.dart';
 import 'package:flutter_riverpod/flutter_riverpod.dart';
 
 typedef UploadProgressCallback =
-    void Function(int uploaded, int total, String status);
+    void Function(int uploaded, int error, int total, String status);
 typedef UploadCompleteCallback = void Function();
 typedef UploadErrorCallback = void Function(String error, {bool isAuthError});
 
@@ -29,6 +29,7 @@ class UploadService {
   bool isAuthErr = false;
   int totalOperations = 0;
   int uploadedCount = 0;
+  int errorCount = 0;
   UploadProgressCallback? _onProgress;
   UploadCompleteCallback? _onComplete;
   UploadErrorCallback? _onError;
@@ -45,7 +46,7 @@ class UploadService {
 
     // 如果上传正在进行中,则立即通知新的进度回调
     if (_isUploading) {
-      _onProgress?.call(uploadedCount, totalOperations, '上传中...');
+      _onProgress?.call(uploadedCount, errorCount, totalOperations, '上传中...');
     }
   }
 
@@ -83,7 +84,7 @@ class UploadService {
       }
 
       logger.i('开始上传 $totalOperations 个离线操作');
-      _onProgress?.call(uploadedCount, totalOperations, '上传中...');
+      _onProgress?.call(uploadedCount, errorCount, totalOperations, '上传中...');
 
       bool flag = true;
       // 上传个体绑定数据
@@ -151,9 +152,10 @@ class UploadService {
         logger.i('$breedConfigType 类型数据 id:${data['id']} 上传成功');
         _onProgress?.call(
           uploadedCount,
+          errorCount,
           totalOperations,
           count == dataList.length
-              ? '${BreedConfig.getName(breedConfigType)}已上传完成!}'
+              ? '${BreedConfig.getName(breedConfigType)}已上传完成!'
               : '上传中...',
         );
       } else if (result.isNetError == true) {
@@ -172,8 +174,12 @@ class UploadService {
         logger.e(
           '$breedConfigType 类型数据 id:${data['id']} 上传失败: ${result.message}',
         );
+        await _bdService.delete(tableName, 'id = ?', [data['id']]);
+        uploadedCount++;
+        errorCount++;
+        count++;
         _onError?.call(result.message);
-        return false;
+        _onProgress?.call(uploadedCount, errorCount, totalOperations, '上传中...');
       }
     }
     return true;

+ 17 - 11
UI/CF.APP/chicken_farm/lib/pages/upload/upload_page.dart

@@ -24,8 +24,10 @@ class UploadStatus {
 
 class _UploadPageState extends State<UploadPage> with WidgetsBindingObserver {
   late UploadService _uploadService;
-  int _totalOperations = 0;
-  int _uploadedOperations = 0;
+  int _totalCount = 0;
+  int _uploadedCount = 0;
+  int _errorCount = 0;
+  String _error = "";
   int _status = UploadStatus.preparing; // 改为int类型
   bool _isUploading = false;
   bool _uploadCompleted = false;
@@ -71,7 +73,7 @@ class _UploadPageState extends State<UploadPage> with WidgetsBindingObserver {
       case UploadStatus.completed:
         return '上传完成';
       case UploadStatus.error:
-        return '上传出错,请稍后再试';
+        return '上传出错,请稍后再试($_error)';
       case UploadStatus.exception:
         return '上传异常,请稍后再试';
       default:
@@ -130,8 +132,8 @@ class _UploadPageState extends State<UploadPage> with WidgetsBindingObserver {
       setState(() {
         _isUploading = true;
         _status = UploadStatus.uploading; // 使用状态常量
-        _uploadedOperations = 0;
-        _totalOperations = 0;
+        _uploadedCount = 0;
+        _totalCount = 0;
         _uploadCompleted = false;
       });
     }
@@ -140,11 +142,12 @@ class _UploadPageState extends State<UploadPage> with WidgetsBindingObserver {
     logger.d('开始上传,$t');
     // 注册回调
     _uploadService.registerCallbacks(
-      onProgress: (uploaded, total, status) {
+      onProgress: (uploaded, error, total, status) {
         if (mounted) {
           setState(() {
-            _uploadedOperations = uploaded;
-            _totalOperations = total;
+            _uploadedCount = uploaded;
+            _totalCount = total;
+            _errorCount = error;
           });
         }
         logger.d('上传进度 $t:$uploaded/$total');
@@ -176,6 +179,7 @@ class _UploadPageState extends State<UploadPage> with WidgetsBindingObserver {
         if (mounted) {
           setState(() {
             _isUploading = false;
+            _error = message;
             _status = UploadStatus.error; // 使用状态常量
           });
         }
@@ -215,13 +219,15 @@ class _UploadPageState extends State<UploadPage> with WidgetsBindingObserver {
                             textAlign: TextAlign.center,
                           ),
                           const SizedBox(height: 30),
-                          if (_totalOperations > 0) ...[
+                          if (_totalCount > 0) ...[
                             LinearProgressIndicator(
-                              value: _uploadedOperations / _totalOperations,
+                              value: _uploadedCount / _totalCount,
                               minHeight: 10,
                             ),
                             const SizedBox(height: 10),
-                            Text('$_uploadedOperations / $_totalOperations'),
+                            Text(
+                              '$_uploadedCount / $_totalCount [$_errorCount 错误]',
+                            ),
                             const SizedBox(height: 30),
                           ],
                           if (_isUploading) ...[