198 lines
6.5 KiB
Dart
198 lines
6.5 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:floor/floor.dart';
|
|
import 'package:pj_supermarket_cloud/common/index.dart';
|
|
|
|
import 'package:sqflite/sqflite.dart' as sqflite;
|
|
|
|
part 'database.g.dart';
|
|
|
|
const entities = <Type>[
|
|
BroadcastEntity,
|
|
OrderEntity,
|
|
OrderHoldBillEntity,
|
|
CanteenSpuEntity,
|
|
SupermarketSkuEntity,
|
|
SupermarketSpuEntity,
|
|
SupermarketCodeEntity,
|
|
ShopCardEntity,
|
|
ClassifyEntity,
|
|
PrintHistoryEntity,
|
|
UserEntity,
|
|
FaceEntity,
|
|
SettingEntity,
|
|
];
|
|
|
|
const typeConverters = <Type>[
|
|
DateTimeConverter,
|
|
DateTimeEmptyConverter,
|
|
OrderStatusConverter,
|
|
OrderTypeConverter,
|
|
OrderPayTypeConverter,
|
|
SysTypeConverter,
|
|
PrintStatusConverter,
|
|
SupermarketNatureConverter,
|
|
ListIntConverter,
|
|
BoolConverter,
|
|
];
|
|
|
|
@Database(version: 3, entities: entities)
|
|
@TypeConverters(typeConverters)
|
|
abstract class AppDatabase extends FloorDatabase {
|
|
BroadcastDao get getBroadcastDao;
|
|
OrderDao get getOrderDao;
|
|
OrderHoldBillDao get getOrderHoldBillDao;
|
|
CanteenSpuDao get getCanteenSpuDao;
|
|
SupermarketSkuDao get getSupermarketSkuDao;
|
|
SupermarketSpuDao get getSupermarketSpuDao;
|
|
SupermarketCodeDao get getSupermarketCodeDao;
|
|
ShopCardDao get getShopCardDao;
|
|
ClassifyDao get getClassifyDao;
|
|
PrintHistoryDao get getPrintHistoryDao;
|
|
UserDao get getUserDao;
|
|
FaceDao get getFaceDao;
|
|
SettingDao get getSettingDao;
|
|
|
|
// 获取数据库迁移列表
|
|
static List<Migration> _getDatabaseMigrations() {
|
|
return [
|
|
// 从版本1到版本2的迁移:添加productNamePy字段
|
|
Migration(1, 2, (database) async {
|
|
// 先检查列是否存在,不存在才添加
|
|
final result = await database.rawQuery(
|
|
"PRAGMA table_info(supermarket_spu)",
|
|
);
|
|
bool columnExists = false;
|
|
for (var column in result) {
|
|
if (column['name'] == 'productNamePy') {
|
|
columnExists = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!columnExists) {
|
|
await database.execute(
|
|
'ALTER TABLE supermarket_spu ADD COLUMN productNamePy TEXT');
|
|
}
|
|
}),
|
|
Migration(2, 3, (database) async {
|
|
// 如果表不存在则创建,存在则校验列并补齐缺失项
|
|
final tables = await database.rawQuery(
|
|
"SELECT name FROM sqlite_master WHERE type='table' AND name='order_hold_bill'",
|
|
);
|
|
if (tables.isEmpty) {
|
|
await database.execute('''
|
|
CREATE TABLE IF NOT EXISTS order_hold_bill (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
sysType TEXT NOT NULL,
|
|
carListJson TEXT,
|
|
userJson TEXT,
|
|
price INTEGER DEFAULT 0 NOT NULL,
|
|
extraPrice INTEGER DEFAULT 0 NOT NULL,
|
|
payPrice INTEGER DEFAULT 0 NOT NULL,
|
|
createTime TEXT NOT NULL
|
|
)
|
|
''');
|
|
await database.execute(
|
|
"CREATE INDEX IF NOT EXISTS idx_order_hold_bill_sysType ON order_hold_bill(sysType)",
|
|
);
|
|
await database.execute(
|
|
"CREATE INDEX IF NOT EXISTS idx_order_hold_bill_createTime ON order_hold_bill(createTime)",
|
|
);
|
|
} else {
|
|
final columns =
|
|
await database.rawQuery("PRAGMA table_info(order_hold_bill)");
|
|
bool hasSysType = false;
|
|
bool hasCarListJson = false;
|
|
bool hasOrderListJson = false;
|
|
bool hasUserJson = false;
|
|
bool hasPrice = false;
|
|
bool hasExtraPrice = false;
|
|
bool hasPayPrice = false;
|
|
bool hasCreateTime = false;
|
|
for (final c in columns) {
|
|
final name = c['name'];
|
|
if (name == 'sysType') hasSysType = true;
|
|
if (name == 'carListJson') hasCarListJson = true;
|
|
if (name == 'orderListJson') hasOrderListJson = true;
|
|
if (name == 'userJson') hasUserJson = true;
|
|
if (name == 'price') hasPrice = true;
|
|
if (name == 'extraPrice') hasExtraPrice = true;
|
|
if (name == 'payPrice') hasPayPrice = true;
|
|
if (name == 'createTime') hasCreateTime = true;
|
|
}
|
|
if (!hasSysType) {
|
|
await database.execute(
|
|
'ALTER TABLE order_hold_bill ADD COLUMN sysType TEXT',
|
|
);
|
|
}
|
|
if (!hasCarListJson) {
|
|
await database.execute(
|
|
'ALTER TABLE order_hold_bill ADD COLUMN carListJson TEXT',
|
|
);
|
|
if (hasOrderListJson) {
|
|
// 旧列迁移到新列
|
|
await database.execute(
|
|
"UPDATE order_hold_bill SET carListJson = orderListJson WHERE carListJson IS NULL",
|
|
);
|
|
}
|
|
}
|
|
if (!hasUserJson) {
|
|
await database.execute(
|
|
'ALTER TABLE order_hold_bill ADD COLUMN userJson TEXT',
|
|
);
|
|
}
|
|
if (!hasPrice) {
|
|
await database.execute(
|
|
'ALTER TABLE order_hold_bill ADD COLUMN price INTEGER DEFAULT 0 NOT NULL',
|
|
);
|
|
}
|
|
if (!hasExtraPrice) {
|
|
await database.execute(
|
|
'ALTER TABLE order_hold_bill ADD COLUMN extraPrice INTEGER DEFAULT 0 NOT NULL',
|
|
);
|
|
}
|
|
if (!hasPayPrice) {
|
|
await database.execute(
|
|
'ALTER TABLE order_hold_bill ADD COLUMN payPrice INTEGER DEFAULT 0 NOT NULL',
|
|
);
|
|
}
|
|
if (!hasCreateTime) {
|
|
await database.execute(
|
|
'ALTER TABLE order_hold_bill ADD COLUMN createTime TEXT DEFAULT "" NOT NULL',
|
|
);
|
|
}
|
|
await database.execute(
|
|
"CREATE INDEX IF NOT EXISTS idx_order_hold_bill_sysType ON order_hold_bill(sysType)",
|
|
);
|
|
await database.execute(
|
|
"CREATE INDEX IF NOT EXISTS idx_order_hold_bill_createTime ON order_hold_bill(createTime)",
|
|
);
|
|
}
|
|
}),
|
|
];
|
|
}
|
|
|
|
static Future<AppDatabase> getDataBase() async {
|
|
final dbPath = await AppFileTool.getDatabasePath();
|
|
final dataBase = await $FloorAppDatabase
|
|
.databaseBuilder("$dbPath/${Constants.databaseName}")
|
|
.addCallback(
|
|
Callback(
|
|
onCreate: (database, version) async {
|
|
logger.i('数据库已创建,版本: $version');
|
|
},
|
|
onOpen: (database) async {
|
|
logger.i('数据库已打开');
|
|
},
|
|
onUpgrade: (database, startVersion, endVersion) {
|
|
logger.i('数据库已升级,版本: $startVersion -> $endVersion');
|
|
},
|
|
),
|
|
)
|
|
.addMigrations(_getDatabaseMigrations())
|
|
.build();
|
|
return dataBase;
|
|
}
|
|
}
|