同步dbretry接口

This commit is contained in:
Happi (哈比)
2026-03-10 12:49:49 +08:00
parent 1c996075e3
commit 5eaff7ede3
3 changed files with 89 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
import 'package:drift/drift.dart';
import 'package:im_app/data/local/drift/app_database.dart';
import 'package:im_app/domain/entities/api_retry.dart';
import 'package:im_app/domain/enums/retry_status.dart';
import 'package:im_app/domain/repositories/api_retry_repository.dart';
import 'package:storage_sdk/storage_sdk.dart';
@@ -44,6 +45,23 @@ class ApiRetryRepositoryImpl implements ApiRetryRepository {
addIndex: Value(apiRetry.addIndex),
);
// ── rawQuery row → Domain ─────────────────────────────────────────────────
ApiRetry _fromRow(QueryRow row) => ApiRetry(
id: row.read<int>('id'),
uid: row.readNullable<int>('uid'),
apiType: row.read<String>('api_type'),
endPoint: row.read<String>('end_point'),
requestData: row.read<String>('request_data'),
synced: row.readNullable<int>('synced'),
callbackFun: row.read<String>('callback_fun'),
expired: row.readNullable<int>('expired'),
replace: row.readNullable<int>('replace'),
expireTime: row.readNullable<int>('expire_time'),
createTime: row.readNullable<int>('create_time'),
addIndex: row.readNullable<int>('add_index'),
);
// ── 监听 ─────────────────────────────────────────────────────────────────
@override
@@ -76,6 +94,14 @@ class ApiRetryRepositoryImpl implements ApiRetryRepository {
return row != null ? _toEntity(row) : null;
}
@override
Future<ApiRetry?> getByUid(int uid) async {
final row = await _storage.selectFirst<DriftApiRetry, $ApiRetriesTable>(
(t) => t.uid.equals(uid),
);
return row != null ? _toEntity(row) : null;
}
@override
Future<List<ApiRetry>> getPending() async {
final rows = await _storage.selectWhere<DriftApiRetry, $ApiRetriesTable>(
@@ -92,6 +118,24 @@ class ApiRetryRepositoryImpl implements ApiRetryRepository {
return rows.map(_toEntity).toList();
}
@override
Future<List<ApiRetry>> getByEndPointPending(String endPoint) async {
final rows = await _storage.rawQuery(
'SELECT * FROM api_retry WHERE end_point = ? AND synced = 0 ORDER BY create_time ASC',
[endPoint],
);
return rows.map(_fromRow).toList();
}
@override
Future<List<ApiRetry>> getBySynced(int synced) async {
final rows = await _storage.rawQuery(
'SELECT * FROM api_retry WHERE synced = ? ORDER BY create_time ASC',
[synced],
);
return rows.map(_fromRow).toList();
}
// ── 写入 ─────────────────────────────────────────────────────────────────
@override
@@ -145,6 +189,17 @@ class ApiRetryRepositoryImpl implements ApiRetryRepository {
);
}
@override
Future<void> deleteFinished() async {
await _storage
.rawQuery('DELETE FROM api_retry WHERE synced IN (?, ?, ?, ?)', [
RetryStatus.success.value,
RetryStatus.failed.value,
RetryStatus.cancel.value,
RetryStatus.replace.value,
]);
}
@override
Future<void> deleteSynced() async {
await _storage.deleteWhere<DriftApiRetry, $ApiRetriesTable>(

View File

@@ -0,0 +1,21 @@
/// 重试同步状态
///
/// 对应 api_retry 表中 synced 字段的值
enum RetryStatus {
notYet(0),
success(1),
failed(-1),
cancel(2),
replace(3);
const RetryStatus(this.value);
final int value;
static RetryStatus fromValue(int value) {
return RetryStatus.values.firstWhere(
(e) => e.value == value,
orElse: () => RetryStatus.notYet,
);
}
}

View File

@@ -14,12 +14,22 @@ abstract class ApiRetryRepository {
Future<ApiRetry?> getById(int id);
/// 读取指定 uid 的第一条重试任务
Future<ApiRetry?> getByUid(int uid);
/// 读取未同步的重试任务
Future<List<ApiRetry>> getPending();
/// 读取未过期的重试任务
Future<List<ApiRetry>> getActive();
/// 读取指定 endPoint 且未同步的重试任务,按 createTime 升序
Future<List<ApiRetry>> getByEndPointPending(String endPoint);
/// 读取指定同步状态的所有重试任务,按 createTime 升序
/// synced: 0 未同步 / 1 已同步 / -1 失败
Future<List<ApiRetry>> getBySynced(int synced);
// ── 写入 ─────────────────────────────────────────────────────────────────
/// 插入重试任务autoIncrement id无需提供
@@ -41,6 +51,9 @@ abstract class ApiRetryRepository {
Future<void> deleteByUid(int uid);
/// 删除已完成的重试任务synced = success / failed / cancel / replace
Future<void> deleteFinished();
/// 清除已同步的重试任务
Future<void> deleteSynced();