徐振升 2 weeks ago
parent
commit
c9db8d83bc
  1. 50
      lib/app/features/enterprise/data/datasources/enterprise_local_data_source.dart
  2. 18
      lib/app/features/enterprise/data/model/enterprise_model.dart
  3. 7
      lib/app/features/enterprise/data/repositories_impl/enterprise_repository_impl.dart
  4. 3
      lib/app/features/enterprise/domain/repositories/enterprise_repository.dart
  5. 10
      lib/app/features/enterprise/domain/usecases/get_unsynced_enterprises_usecase.dart

50
lib/app/features/enterprise/data/datasources/enterprise_local_data_source.dart

@ -15,6 +15,8 @@ abstract class EnterpriseLocalDataSource {
DateTime? endDate, DateTime? endDate,
}); });
Future<List<EnterpriseModel>> getUnsyncedEnterprises();
/// ///
} }
@ -25,11 +27,13 @@ class EnterpriseLocalDataSourceImpl implements EnterpriseLocalDataSource {
required DatabaseService databaseService, required DatabaseService databaseService,
}) : _databaseService = databaseService; }) : _databaseService = databaseService;
static const String _tableName = 'enterprises';
@override @override
Future<void> addEnterprise(EnterpriseModel enterprise) async { Future<void> addEnterprise(EnterpriseModel enterprise) async {
final db = await _databaseService.database; final db = await _databaseService.database;
await db.insert( await db.insert(
"enterprises", _tableName,
enterprise.toMap(), enterprise.toMap(),
// conflictAlgorithm (id) // conflictAlgorithm (id)
// ConflictAlgorithm.replace ID // ConflictAlgorithm.replace ID
@ -42,7 +46,7 @@ class EnterpriseLocalDataSourceImpl implements EnterpriseLocalDataSource {
Future<void> updateEnterprise(EnterpriseModel enterprise) async { Future<void> updateEnterprise(EnterpriseModel enterprise) async {
final db = await _databaseService.database; final db = await _databaseService.database;
await db.update( await db.update(
'enterprises', _tableName,
enterprise.toMap(), enterprise.toMap(),
where: 'id = ?', // ID where: 'id = ?', // ID
whereArgs: [enterprise.id], whereArgs: [enterprise.id],
@ -113,7 +117,7 @@ class EnterpriseLocalDataSourceImpl implements EnterpriseLocalDataSource {
COUNT(CASE WHEN p.syncStatus != $pendingCreateIndex THEN 1 ELSE NULL END) AS uploadedProblems, COUNT(CASE WHEN p.syncStatus != $pendingCreateIndex THEN 1 ELSE NULL END) AS uploadedProblems,
COUNT(CASE WHEN p.syncStatus == $pendingCreateIndex THEN 1 ELSE NULL END) AS pendingProblems COUNT(CASE WHEN p.syncStatus == $pendingCreateIndex THEN 1 ELSE NULL END) AS pendingProblems
FROM FROM
enterprises e $_tableName e
LEFT JOIN LEFT JOIN
problems p ON e.id = p.enterpriseId problems p ON e.id = p.enterpriseId
$whereString $whereString
@ -133,4 +137,44 @@ class EnterpriseLocalDataSourceImpl implements EnterpriseLocalDataSource {
return EnterpriseListItemModel.fromMap(maps[i]); return EnterpriseListItemModel.fromMap(maps[i]);
}); });
} }
@override
Future<List<EnterpriseModel>> getUnsyncedEnterprises() async {
final db = await _databaseService.database;
// 1. [] 使
// SyncStatus
const List<SyncStatus> unsyncedStatuses = [
SyncStatus.pendingCreate,
SyncStatus.pendingUpdate,
SyncStatus.pendingDelete,
];
// 2. []
//
final List<int> statusIndexes = unsyncedStatuses
.map((status) => status.index)
.toList();
// 3. [] SQL '?'
// 使 unsyncedStatuses
final String placeholders = List.filled(
statusIndexes.length,
'?',
).join(',');
// 4.
final List<Map<String, dynamic>> maps = await db.query(
_tableName,
where: 'syncStatus IN ($placeholders)', // e.g., 'syncStatus IN (?,?,?)'
whereArgs: statusIndexes, // e.g., [2, 3, 4]
);
if (maps.isEmpty) {
return [];
}
// 5. [] 使 fromMap
return maps.map((map) => EnterpriseModel.fromMap(map)).toList();
}
} }

18
lib/app/features/enterprise/data/model/enterprise_model.dart

@ -89,4 +89,22 @@ class EnterpriseModel extends Enterprise {
'majorHazardsDescription': majorHazardsDescription, 'majorHazardsDescription': majorHazardsDescription,
}; };
} }
Enterprise toEntity() {
// EnterpriseModel Enterprise
// Enterprise
return Enterprise(
id: id,
syncStatus: syncStatus,
lastModifiedTime: lastModifiedTime,
creationTime: creationTime,
name: name,
type: type,
address: address,
scale: scale,
contactPerson: contactPerson,
contactPhone: contactPhone,
majorHazardsDescription: majorHazardsDescription,
);
}
} }

7
lib/app/features/enterprise/data/repositories_impl/enterprise_repository_impl.dart

@ -72,4 +72,11 @@ class EnterpriseRepositoryImpl implements EnterpriseRepository {
// //
await localDataSource.updateEnterprise(enterpriseModel); await localDataSource.updateEnterprise(enterpriseModel);
} }
@override
Future<List<Enterprise>> getUnsyncedEnterprises() async {
final List<EnterpriseModel> enterpriseModels = await localDataSource
.getUnsyncedEnterprises();
return enterpriseModels.map((model) => model.toEntity()).toList();
}
} }

3
lib/app/features/enterprise/domain/repositories/enterprise_repository.dart

@ -17,4 +17,7 @@ abstract class EnterpriseRepository implements SyncableRepository<Enterprise> {
DateTime? startDate, DateTime? startDate,
DateTime? endDate, DateTime? endDate,
}); });
///
Future<List<Enterprise>> getUnsyncedEnterprises();
} }

10
lib/app/features/enterprise/domain/usecases/get_unsynced_enterprises_usecase.dart

@ -0,0 +1,10 @@
import 'package:problem_check_system/app/features/enterprise/domain/entities/enterprise.dart';
import 'package:problem_check_system/app/features/enterprise/domain/repositories/enterprise_repository.dart';
class GetUnsyncedEnterprisesUsecase {
final EnterpriseRepository enterpriseRepository;
const GetUnsyncedEnterprisesUsecase({required this.enterpriseRepository});
Future<List<Enterprise>> call() async {
return await enterpriseRepository.getUnsyncedEnterprises();
}
}
Loading…
Cancel
Save