Files
AI-Health/health_app/lib/core/local_database.dart
MingNian 4d213b5a44 feat: 后端架构重构 — Endpoint→Service→Repository分层 + AI确认机制 + 异步任务持久化
- 核心业务拆分为 Endpoint → Application Service → Repository 三层
- AI写入操作必须用户确认后才写库(确认卡片机制)
- 报告/饮食/用药分析改为持久化任务队列(原子领取/重试/重启恢复)
- 运动计划修复: 连续真实日期替代周模板
- 用药提醒去重 + 通知Outbox预留
- 认证收拢到AuthService, 管理员收拢到AdminService
- AI会话加用户归属校验防串号
- 提示词调整为患者视角
- 开发假数据已关闭
- 21/21测试通过, 0警告0错误
2026-06-20 20:41:42 +08:00

62 lines
1.6 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
/// SQLite 本地键值缓存。
///
/// 约定:后端数据库是健康业务数据的唯一事实源。这里仅保存会话、偏好、
/// 草稿和临时缓存不保存健康记录、报告、用药、饮食、AI 对话等核心业务事实。
class LocalDatabase {
static LocalDatabase? _instance;
Database? _db;
LocalDatabase._();
static LocalDatabase get instance => _instance ??= LocalDatabase._();
Future<Database> get database async {
_db ??= await _initDb();
return _db!;
}
Future<Database> _initDb() async {
final dbPath = await getDatabasesPath();
final path = join(dbPath, 'health_app.db');
return openDatabase(
path,
version: 1,
onCreate: (db, version) async {
await db.execute(
'CREATE TABLE kv_store (key TEXT PRIMARY KEY, value TEXT)',
);
},
);
}
Future<void> write(String key, String value) async {
final db = await database;
await db.insert(
'kv_store',
{'key': key, 'value': value},
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
Future<String?> read(String key) async {
final db = await database;
final result =
await db.query('kv_store', where: 'key = ?', whereArgs: [key]);
if (result.isEmpty) return null;
return result.first['value'] as String?;
}
Future<void> delete(String key) async {
final db = await database;
await db.delete('kv_store', where: 'key = ?', whereArgs: [key]);
}
Future<void> deleteAll() async {
final db = await database;
await db.delete('kv_store');
}
}