feat:数据库迁移文件

This commit is contained in:
dzc committed 2026-03-29 16:20:32 +08:00
1 parent e730b6923c
commit e5e24231e7
10 files changed
+54 -11

No files matched your search

@@ -15,7 +15,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_PATH = "pj_cashier/database";
private static final String DATABASE_NAME = "cash_register.db";
private static final int DATABASE_VERSION = 5;
private static final int DATABASE_VERSION = 6;
public DatabaseHelper(Context context) {
super(context, getCustomDatabasePath(context), null, DATABASE_VERSION);
@@ -75,6 +75,10 @@ class ServiceProduct {
/// 菜品上架日期
DateTime? saleDate;
/// 是否涨价
@JsonKey(fromJson: JsonTool.stringToBoolean, toJson: JsonTool.booleanToString)
bool? isPriceIncreased;
ServiceProduct({
this.id,
this.spuId,
@@ -41,7 +41,10 @@ ServiceProduct _$ServiceProductFromJson(Map<String, dynamic> json) =>
saleDate: json['saleDate'] == null
? null
: DateTime.parse(json['saleDate'] as String),
)..price = (json['price'] as num?)?.toInt();
)
..price = (json['price'] as num?)?.toInt()
..isPriceIncreased =
JsonTool.stringToBoolean(json['isPriceIncreased'] as String);
Map<String, dynamic> _$ServiceProductToJson(ServiceProduct instance) =>
<String, dynamic>{
@@ -55,6 +58,7 @@ Map<String, dynamic> _$ServiceProductToJson(ServiceProduct instance) =>
'sort': instance.sort,
'categoryType': instance.categoryType,
'saleDate': instance.saleDate?.toIso8601String(),
'isPriceIncreased': JsonTool.booleanToString(instance.isPriceIncreased),
};
ServiceSupermarketSpu _$ServiceSupermarketSpuFromJson(
+2 -2
View File
@@ -3,7 +3,7 @@ import 'dart:io';
import 'package:floor/floor.dart';
import 'package:pj_supermarket_cloud/common/index.dart';
import 'package:pj_supermarket_cloud/common/services/database/databaseMigration.dart';
import 'package:pj_supermarket_cloud/common/services/database/database_migration.dart';
import 'package:sqflite/sqflite.dart' as sqflite;
@@ -40,7 +40,7 @@ const typeConverters = <Type>[
BoolConverter,
];
@Database(version: 5, entities: entities)
@Database(version: 6, entities: entities)
@TypeConverters(typeConverters)
abstract class AppDatabase extends FloorDatabase {
BroadcastDao get getBroadcastDao;
+11 -5
View File
@@ -126,7 +126,7 @@ class _$AppDatabase extends AppDatabase {
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 NOT NULL, `extraPrice` INTEGER NOT NULL, `payPrice` INTEGER NOT NULL, `createTime` TEXT NOT NULL)');
await database.execute(
'CREATE TABLE IF NOT EXISTS `canteen_spu` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `serviceUniqueCode` TEXT NOT NULL, `classifyServiceUniqueCode` TEXT NOT NULL, `hourType` INTEGER, `saleDate` TEXT NOT NULL, `name` TEXT NOT NULL, `price` INTEGER NOT NULL, `image` TEXT, `isDefault` INTEGER NOT NULL, `status` INTEGER NOT NULL, `sort` INTEGER NOT NULL, `createTime` TEXT NOT NULL, `updateTime` TEXT NOT NULL)');
'CREATE TABLE IF NOT EXISTS `canteen_spu` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `serviceUniqueCode` TEXT NOT NULL, `classifyServiceUniqueCode` TEXT NOT NULL, `hourType` INTEGER, `saleDate` TEXT NOT NULL, `name` TEXT NOT NULL, `price` INTEGER NOT NULL, `image` TEXT, `isDefault` INTEGER NOT NULL, `status` INTEGER NOT NULL, `sort` INTEGER NOT NULL, `isPriceIncreased` INTEGER NOT NULL, `createTime` TEXT NOT NULL, `updateTime` TEXT NOT NULL)');
await database.execute(
'CREATE TABLE IF NOT EXISTS `supermarket_sku` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `serviceUniqueCode` TEXT NOT NULL, `spuId` TEXT NOT NULL, `barCode` TEXT NOT NULL, `spec` TEXT NOT NULL, `unit` TEXT NOT NULL, `packageNum` INTEGER NOT NULL, `purchasePrice` INTEGER NOT NULL, `retailPrice` INTEGER NOT NULL, `promotionPrice` INTEGER NOT NULL)');
await database.execute(
@@ -734,6 +734,8 @@ class _$CanteenSpuDao extends CanteenSpuDao {
'isDefault': _boolConverter.encode(item.isDefault),
'status': item.status,
'sort': item.sort,
'isPriceIncreased':
_boolConverter.encode(item.isPriceIncreased),
'createTime': _dateTimeConverter.encode(item.createTime),
'updateTime': _dateTimeConverter.encode(item.updateTime)
}),
@@ -753,6 +755,8 @@ class _$CanteenSpuDao extends CanteenSpuDao {
'isDefault': _boolConverter.encode(item.isDefault),
'status': item.status,
'sort': item.sort,
'isPriceIncreased':
_boolConverter.encode(item.isPriceIncreased),
'createTime': _dateTimeConverter.encode(item.createTime),
'updateTime': _dateTimeConverter.encode(item.updateTime)
}),
@@ -772,6 +776,8 @@ class _$CanteenSpuDao extends CanteenSpuDao {
'isDefault': _boolConverter.encode(item.isDefault),
'status': item.status,
'sort': item.sort,
'isPriceIncreased':
_boolConverter.encode(item.isPriceIncreased),
'createTime': _dateTimeConverter.encode(item.createTime),
'updateTime': _dateTimeConverter.encode(item.updateTime)
});
@@ -826,7 +832,7 @@ class _$CanteenSpuDao extends CanteenSpuDao {
) async {
return _queryAdapter.query(
'SELECT * FROM canteen_spu WHERE serviceUniqueCode = ?1 AND (?2 = \'-1\' OR strftime(\'%Y-%m-%d\', saleDate) = strftime(\'%Y-%m-%d\', ?2)) LIMIT 1',
mapper: (Map<String, Object?> row) => CanteenSpuEntity(id: row['id'] as int?, serviceUniqueCode: row['serviceUniqueCode'] as String, classifyServiceUniqueCode: row['classifyServiceUniqueCode'] as String, hourType: row['hourType'] == null ? null : ProductHourType.values[row['hourType'] as int], saleDate: _dateTimeConverter.decode(row['saleDate'] as String), name: row['name'] as String, price: row['price'] as int, image: row['image'] as String?, isDefault: _boolConverter.decode(row['isDefault'] as int), status: row['status'] as int, sort: row['sort'] as int, createTime: _dateTimeConverter.decode(row['createTime'] as String), updateTime: _dateTimeConverter.decode(row['updateTime'] as String)),
mapper: (Map<String, Object?> row) => CanteenSpuEntity(id: row['id'] as int?, serviceUniqueCode: row['serviceUniqueCode'] as String, classifyServiceUniqueCode: row['classifyServiceUniqueCode'] as String, hourType: row['hourType'] == null ? null : ProductHourType.values[row['hourType'] as int], saleDate: _dateTimeConverter.decode(row['saleDate'] as String), name: row['name'] as String, price: row['price'] as int, image: row['image'] as String?, isDefault: _boolConverter.decode(row['isDefault'] as int), status: row['status'] as int, sort: row['sort'] as int, isPriceIncreased: _boolConverter.decode(row['isPriceIncreased'] as int), createTime: _dateTimeConverter.decode(row['createTime'] as String), updateTime: _dateTimeConverter.decode(row['updateTime'] as String)),
arguments: [code, saleDate]);
}
@@ -838,7 +844,7 @@ class _$CanteenSpuDao extends CanteenSpuDao {
) async {
return _queryAdapter.queryList(
'SELECT canteen_spu.* FROM canteen_spu LEFT JOIN classify ON canteen_spu.classifyServiceUniqueCode = classify.serviceUniqueCode AND classify.delFlag = 0 WHERE (?1 = \'-1\' OR canteen_spu.classifyServiceUniqueCode = ?1) AND (?2 = \'-1\' OR INSTR(\',\' || hourType || \',\', \',\' || ?2 || \',\') > 0) AND (?3 = \'-1\' OR strftime(\'%Y-%m-%d\', saleDate) = strftime(\'%Y-%m-%d\', ?3)) ORDER BY classify.sort,canteen_spu.sort desc,canteen_spu.price',
mapper: (Map<String, Object?> row) => CanteenSpuEntity(id: row['id'] as int?, serviceUniqueCode: row['serviceUniqueCode'] as String, classifyServiceUniqueCode: row['classifyServiceUniqueCode'] as String, hourType: row['hourType'] == null ? null : ProductHourType.values[row['hourType'] as int], saleDate: _dateTimeConverter.decode(row['saleDate'] as String), name: row['name'] as String, price: row['price'] as int, image: row['image'] as String?, isDefault: _boolConverter.decode(row['isDefault'] as int), status: row['status'] as int, sort: row['sort'] as int, createTime: _dateTimeConverter.decode(row['createTime'] as String), updateTime: _dateTimeConverter.decode(row['updateTime'] as String)),
mapper: (Map<String, Object?> row) => CanteenSpuEntity(id: row['id'] as int?, serviceUniqueCode: row['serviceUniqueCode'] as String, classifyServiceUniqueCode: row['classifyServiceUniqueCode'] as String, hourType: row['hourType'] == null ? null : ProductHourType.values[row['hourType'] as int], saleDate: _dateTimeConverter.decode(row['saleDate'] as String), name: row['name'] as String, price: row['price'] as int, image: row['image'] as String?, isDefault: _boolConverter.decode(row['isDefault'] as int), status: row['status'] as int, sort: row['sort'] as int, isPriceIncreased: _boolConverter.decode(row['isPriceIncreased'] as int), createTime: _dateTimeConverter.decode(row['createTime'] as String), updateTime: _dateTimeConverter.decode(row['updateTime'] as String)),
arguments: [classifyServiceUniqueCode, hourType, saleDate]);
}
@@ -851,7 +857,7 @@ class _$CanteenSpuDao extends CanteenSpuDao {
) async {
return _queryAdapter.queryList(
'SELECT canteen_spu.* FROM canteen_spu LEFT JOIN classify ON canteen_spu.classifyServiceUniqueCode = classify.serviceUniqueCode AND classify.delFlag = 0 WHERE (?1 = \'-1\' OR canteen_spu.classifyServiceUniqueCode = ?1) AND (?2 = \'-1\' OR INSTR(\',\' || canteen_spu.hourType || \',\', \',\' || ?2 || \',\') > 0) AND strftime(\'%Y-%m-%d\', saleDate) = strftime(\'%Y-%m-%d\', \'now\', \'localtime\') ORDER BY classify.sort, classify.id, canteen_spu.price LIMIT ?3 OFFSET ?4',
mapper: (Map<String, Object?> row) => CanteenSpuEntity(id: row['id'] as int?, serviceUniqueCode: row['serviceUniqueCode'] as String, classifyServiceUniqueCode: row['classifyServiceUniqueCode'] as String, hourType: row['hourType'] == null ? null : ProductHourType.values[row['hourType'] as int], saleDate: _dateTimeConverter.decode(row['saleDate'] as String), name: row['name'] as String, price: row['price'] as int, image: row['image'] as String?, isDefault: _boolConverter.decode(row['isDefault'] as int), status: row['status'] as int, sort: row['sort'] as int, createTime: _dateTimeConverter.decode(row['createTime'] as String), updateTime: _dateTimeConverter.decode(row['updateTime'] as String)),
mapper: (Map<String, Object?> row) => CanteenSpuEntity(id: row['id'] as int?, serviceUniqueCode: row['serviceUniqueCode'] as String, classifyServiceUniqueCode: row['classifyServiceUniqueCode'] as String, hourType: row['hourType'] == null ? null : ProductHourType.values[row['hourType'] as int], saleDate: _dateTimeConverter.decode(row['saleDate'] as String), name: row['name'] as String, price: row['price'] as int, image: row['image'] as String?, isDefault: _boolConverter.decode(row['isDefault'] as int), status: row['status'] as int, sort: row['sort'] as int, isPriceIncreased: _boolConverter.decode(row['isPriceIncreased'] as int), createTime: _dateTimeConverter.decode(row['createTime'] as String), updateTime: _dateTimeConverter.decode(row['updateTime'] as String)),
arguments: [classifyServiceUniqueCode, hourType, pageSize, offset]);
}
@@ -859,7 +865,7 @@ class _$CanteenSpuDao extends CanteenSpuDao {
Future<List<CanteenSpuEntity>> getDefault(String hourType) async {
return _queryAdapter.queryList(
'SELECT * FROM canteen_spu WHERE isDefault = 1 AND (?1 = \'-1\' OR hourType = ?1) AND strftime(\'%Y-%m-%d\', saleDate) = strftime(\'%Y-%m-%d\', \'now\', \'localtime\')',
mapper: (Map<String, Object?> row) => CanteenSpuEntity(id: row['id'] as int?, serviceUniqueCode: row['serviceUniqueCode'] as String, classifyServiceUniqueCode: row['classifyServiceUniqueCode'] as String, hourType: row['hourType'] == null ? null : ProductHourType.values[row['hourType'] as int], saleDate: _dateTimeConverter.decode(row['saleDate'] as String), name: row['name'] as String, price: row['price'] as int, image: row['image'] as String?, isDefault: _boolConverter.decode(row['isDefault'] as int), status: row['status'] as int, sort: row['sort'] as int, createTime: _dateTimeConverter.decode(row['createTime'] as String), updateTime: _dateTimeConverter.decode(row['updateTime'] as String)),
mapper: (Map<String, Object?> row) => CanteenSpuEntity(id: row['id'] as int?, serviceUniqueCode: row['serviceUniqueCode'] as String, classifyServiceUniqueCode: row['classifyServiceUniqueCode'] as String, hourType: row['hourType'] == null ? null : ProductHourType.values[row['hourType'] as int], saleDate: _dateTimeConverter.decode(row['saleDate'] as String), name: row['name'] as String, price: row['price'] as int, image: row['image'] as String?, isDefault: _boolConverter.decode(row['isDefault'] as int), status: row['status'] as int, sort: row['sort'] as int, isPriceIncreased: _boolConverter.decode(row['isPriceIncreased'] as int), createTime: _dateTimeConverter.decode(row['createTime'] as String), updateTime: _dateTimeConverter.decode(row['updateTime'] as String)),
arguments: [hourType]);
}
@@ -151,7 +151,29 @@ abstract class DatabaseMigration {
}
});
static Migration migration5To6 = Migration(5, 6, (database) async {
final columns = await database.rawQuery("PRAGMA table_info(canteen_spu)");
bool hasIsPriceIncreased = false;
for (final c in columns) {
if (c['name'] == 'isPriceIncreased') {
hasIsPriceIncreased = true;
break;
}
}
if (!hasIsPriceIncreased) {
await database.execute(
"ALTER TABLE canteen_spu ADD COLUMN isPriceIncreased INTEGER NOT NULL DEFAULT 0",
);
}
});
static List<Migration> getDatabaseMigrations() {
return [migration1To2, migration2To3, migration3To4];
return [
migration1To2,
migration2To3,
migration3To4,
migration4To5,
migration5To6
];
}
}
@@ -43,6 +43,9 @@ class CanteenSpuEntity extends Equatable {
/// 排序 倒序排列
int sort;
/// 是否涨价
bool isPriceIncreased;
/// 创建时间
DateTime createTime;
@@ -61,6 +64,7 @@ class CanteenSpuEntity extends Equatable {
required this.isDefault,
required this.status,
required this.sort,
required this.isPriceIncreased,
required this.createTime,
required this.updateTime,
});
@@ -19,6 +19,7 @@ CanteenSpuEntity _$CanteenSpuEntityFromJson(Map<String, dynamic> json) =>
isDefault: json['isDefault'] as bool,
status: (json['status'] as num).toInt(),
sort: (json['sort'] as num).toInt(),
isPriceIncreased: json['isPriceIncreased'] as bool,
createTime: DateTime.parse(json['createTime'] as String),
updateTime: DateTime.parse(json['updateTime'] as String),
);
@@ -36,6 +37,7 @@ Map<String, dynamic> _$CanteenSpuEntityToJson(CanteenSpuEntity instance) =>
'isDefault': instance.isDefault,
'status': instance.status,
'sort': instance.sort,
'isPriceIncreased': instance.isPriceIncreased,
'createTime': instance.createTime.toIso8601String(),
'updateTime': instance.updateTime.toIso8601String(),
};
@@ -316,6 +316,7 @@ abstract class SyncProductTool {
hourType: ProductHourType.values.fromCode(v.hourType),
saleDate: v.saleDate ?? DateTime.now(),
isDefault: false,
isPriceIncreased: v.isPriceIncreased ?? false,
);
if (!ObjectUtil.isEmptyList(productList)) {
final targetProduct = productList.firstWhereOrNull((i) =>
+1 -1
View File
@@ -12,7 +12,7 @@ class Constants {
/// 服务 url 线上
// static const apiUrl = 'https://stgl.pj.gov.cn/api';
static const apiUrl = 'https://rj4jv5rwqs.by.takin.cc';
static const apiUrl = 'https://dzc-test-1.fy.takin.cc';
/// 静态资源 url
static const ossUrl = apiUrl;