From 321ae3378aa39af0aa4cab9b0baec2429c6a42b6 Mon Sep 17 00:00:00 2001 From: dzc <2388224167@qq.com> Date: Mon, 17 Mar 2025 12:29:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=B4=AD=E4=B9=B0mixin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../database/dao/canteen_spu_dao.dart | 19 +++--- .../database/dao/supermarket_sku_dao.dart | 1 + .../database/dao/supermarket_spu_dao.dart | 14 +++- lib/common/services/database/database.dart | 4 +- lib/common/services/database/database.g.dart | 64 +++++++++++++++++-- lib/common/utils/sync/sync_product_tool.dart | 4 +- .../widgets/order/mixin/order_mixin.dart | 25 ++++++-- lib/pages/canteen_product/controller.dart | 2 +- lib/pages/canteen_product/view.dart | 4 +- .../setting/widgets/setting_product.dart | 4 +- lib/pages/supermarket_order/view.dart | 4 +- 11 files changed, 113 insertions(+), 32 deletions(-) diff --git a/lib/common/services/database/dao/canteen_spu_dao.dart b/lib/common/services/database/dao/canteen_spu_dao.dart index 308767f..1251ef5 100644 --- a/lib/common/services/database/dao/canteen_spu_dao.dart +++ b/lib/common/services/database/dao/canteen_spu_dao.dart @@ -5,7 +5,7 @@ import 'package:get/get.dart'; import 'package:pj_supermarket_cloud/common/index.dart'; @dao -abstract class ProductDao { +abstract class CanteenSpuDao { @insert Future save(CanteenSpuEntity entity); @@ -18,7 +18,9 @@ abstract class ProductDao { final database = app.dataBase.database; final placeholders = List.generate(codes.length, (index) => '?').join(', '); final sql = ''' - UPDATE canteen_spu SET delFlag = 1 WHERE serviceUniqueCode IN ($placeholders) + UPDATE canteen_spu SET delFlag = 1 + WHERE + serviceUniqueCode IN ($placeholders) AND strftime('%Y-%m-%d', saleDate) = strftime('%Y-%m-%d', 'now', 'localtime') '''; return await database.rawUpdate(sql, [...codes]); @@ -63,21 +65,22 @@ abstract class ProductDao { FROM canteen_spu LEFT JOIN classify ON canteen_spu.classifyServiceUniqueCode = classify.serviceUniqueCode WHERE - canteen_spu.delFlag = 0 AND - (:classifyServiceUniqueCode = '-1' OR canteen_spu.classifyServiceUniqueCode = :classifyServiceUniqueCode) AND - (:hourType = '-1' OR INSTR(',' || hourType || ',', ',' || :hourType || ',') > 0) AND + canteen_spu.delFlag = 0 + AND (:classifyServiceUniqueCode = '-1' OR canteen_spu.classifyServiceUniqueCode = :classifyServiceUniqueCode) + AND (:hourType = '-1' OR INSTR(',' || hourType || ',', ',' || :hourType || ',') > 0) ORDER BY classify.sort, canteen_spu.price ''') Future> getAll( - String classifyServiceUniqueCode, String hourType); + String classifyServiceUniqueCode, + String hourType, + ); - // GROUP BY canteen_spu.serviceUniqueCode @Query(''' SELECT canteen_spu.* FROM canteen_spu LEFT JOIN classify ON canteen_spu.classifyServiceUniqueCode = classify.serviceUniqueCode WHERE - canteen_spu.delFlag = 0 AND + canteen_spu.delFlag = 0 AND (:classifyServiceUniqueCode = '-1' OR canteen_spu.classifyServiceUniqueCode = :classifyServiceUniqueCode) AND (:hourType = '-1' OR INSTR(',' || canteen_spu.hourType || ',', ',' || :hourType || ',') > 0) AND strftime('%Y-%m-%d', saleDate) = strftime('%Y-%m-%d', 'now', 'localtime') diff --git a/lib/common/services/database/dao/supermarket_sku_dao.dart b/lib/common/services/database/dao/supermarket_sku_dao.dart index fbd010b..d360fb1 100644 --- a/lib/common/services/database/dao/supermarket_sku_dao.dart +++ b/lib/common/services/database/dao/supermarket_sku_dao.dart @@ -2,5 +2,6 @@ import 'package:floor/floor.dart'; import 'package:pj_supermarket_cloud/common/index.dart'; +// supermarket_sku @dao abstract class SupermarketSkuDao {} diff --git a/lib/common/services/database/dao/supermarket_spu_dao.dart b/lib/common/services/database/dao/supermarket_spu_dao.dart index 6a6639f..7369027 100644 --- a/lib/common/services/database/dao/supermarket_spu_dao.dart +++ b/lib/common/services/database/dao/supermarket_spu_dao.dart @@ -2,5 +2,17 @@ import 'package:floor/floor.dart'; import 'package:pj_supermarket_cloud/common/index.dart'; +// supermarket_spu @dao -abstract class SupermarketSpuDao {} +abstract class SupermarketSpuDao { + @Query(''' + SELECT supermarket_spu.* + FROM supermarket_spu + LEFT JOIN classify ON supermarket_spu.classifyServiceUniqueCode = classify.serviceUniqueCode + WHERE + supermarket_spu.delFlag = 0 + AND (:classifyServiceUniqueCode = '-1' OR supermarket_spu.classifyServiceUniqueCode = :classifyServiceUniqueCode) + ORDER BY classify.sort, supermarket_spu.price + ''') + Future> getAll(String classifyServiceUniqueCode); +} diff --git a/lib/common/services/database/database.dart b/lib/common/services/database/database.dart index 879e30e..c1ab665 100644 --- a/lib/common/services/database/database.dart +++ b/lib/common/services/database/database.dart @@ -36,7 +36,9 @@ const typeConverters = [ @TypeConverters(typeConverters) abstract class AppDatabase extends FloorDatabase { OrderDao get getOrderDao; - ProductDao get getProductDao; + CanteenSpuDao get getCanteenSpuDao; + SupermarketSkuDao get getSupermarketSkuDao; + SupermarketSpuDao get getSupermarketSpuDao; ShopCardDao get getShopCardDao; ClassifyDao get getClassifyDao; PrintHistoryDao get getPrintHistoryDao; diff --git a/lib/common/services/database/database.g.dart b/lib/common/services/database/database.g.dart index c06cb79..836ebf6 100644 --- a/lib/common/services/database/database.g.dart +++ b/lib/common/services/database/database.g.dart @@ -74,7 +74,11 @@ class _$AppDatabase extends AppDatabase { OrderDao? _getOrderDaoInstance; - ProductDao? _getProductDaoInstance; + CanteenSpuDao? _getCanteenSpuDaoInstance; + + SupermarketSkuDao? _getSupermarketSkuDaoInstance; + + SupermarketSpuDao? _getSupermarketSpuDaoInstance; ShopCardDao? _getShopCardDaoInstance; @@ -142,8 +146,21 @@ class _$AppDatabase extends AppDatabase { } @override - ProductDao get getProductDao { - return _getProductDaoInstance ??= _$ProductDao(database, changeListener); + CanteenSpuDao get getCanteenSpuDao { + return _getCanteenSpuDaoInstance ??= + _$CanteenSpuDao(database, changeListener); + } + + @override + SupermarketSkuDao get getSupermarketSkuDao { + return _getSupermarketSkuDaoInstance ??= + _$SupermarketSkuDao(database, changeListener); + } + + @override + SupermarketSpuDao get getSupermarketSpuDao { + return _getSupermarketSpuDaoInstance ??= + _$SupermarketSpuDao(database, changeListener); } @override @@ -442,8 +459,8 @@ class _$OrderDao extends OrderDao { } } -class _$ProductDao extends ProductDao { - _$ProductDao( +class _$CanteenSpuDao extends CanteenSpuDao { + _$CanteenSpuDao( this.database, this.changeListener, ) : _queryAdapter = QueryAdapter(database), @@ -557,7 +574,7 @@ class _$ProductDao extends ProductDao { String hourType, ) async { return _queryAdapter.queryList( - 'SELECT canteen_spu.* FROM canteen_spu LEFT JOIN classify ON canteen_spu.classifyServiceUniqueCode = classify.serviceUniqueCode WHERE canteen_spu.delFlag = 0 AND (?1 = \'-1\' OR canteen_spu.classifyServiceUniqueCode = ?1) AND (?2 = \'-1\' OR INSTR(\',\' || hourType || \',\', \',\' || ?2 || \',\') > 0) AND ORDER BY classify.sort, canteen_spu.price', + 'SELECT canteen_spu.* FROM canteen_spu LEFT JOIN classify ON canteen_spu.classifyServiceUniqueCode = classify.serviceUniqueCode WHERE canteen_spu.delFlag = 0 AND (?1 = \'-1\' OR canteen_spu.classifyServiceUniqueCode = ?1) AND (?2 = \'-1\' OR INSTR(\',\' || hourType || \',\', \',\' || ?2 || \',\') > 0) ORDER BY classify.sort, canteen_spu.price', mapper: (Map 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: _dateTimeEmptyConverter.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), delFlag: _boolConverter.decode(row['delFlag'] as int)), arguments: [classifyServiceUniqueCode, hourType]); } @@ -570,7 +587,7 @@ class _$ProductDao extends ProductDao { int offset, ) async { return _queryAdapter.queryList( - 'SELECT canteen_spu.* FROM canteen_spu LEFT JOIN classify ON canteen_spu.classifyServiceUniqueCode = classify.serviceUniqueCode WHERE canteen_spu.delFlag = 0 AND AND (?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', + 'SELECT canteen_spu.* FROM canteen_spu LEFT JOIN classify ON canteen_spu.classifyServiceUniqueCode = classify.serviceUniqueCode WHERE canteen_spu.delFlag = 0 AND (?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 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: _dateTimeEmptyConverter.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), delFlag: _boolConverter.decode(row['delFlag'] as int)), arguments: [classifyServiceUniqueCode, hourType, pageSize, offset]); } @@ -607,6 +624,39 @@ class _$ProductDao extends ProductDao { } } +class _$SupermarketSkuDao extends SupermarketSkuDao { + _$SupermarketSkuDao( + this.database, + this.changeListener, + ); + + final sqflite.DatabaseExecutor database; + + final StreamController changeListener; +} + +class _$SupermarketSpuDao extends SupermarketSpuDao { + _$SupermarketSpuDao( + this.database, + this.changeListener, + ) : _queryAdapter = QueryAdapter(database); + + final sqflite.DatabaseExecutor database; + + final StreamController changeListener; + + final QueryAdapter _queryAdapter; + + @override + Future> getAll( + String classifyServiceUniqueCode) async { + return _queryAdapter.queryList( + 'SELECT supermarket_spu.* FROM supermarket_spu LEFT JOIN classify ON supermarket_spu.classifyServiceUniqueCode = classify.serviceUniqueCode WHERE supermarket_spu.delFlag = 0 AND (?1 = \'-1\' OR supermarket_spu.classifyServiceUniqueCode = ?1) ORDER BY classify.sort, supermarket_spu.price', + mapper: (Map row) => SupermarketSpuEntity(id: row['id'] as int?), + arguments: [classifyServiceUniqueCode]); + } +} + class _$ShopCardDao extends ShopCardDao { _$ShopCardDao( this.database, diff --git a/lib/common/utils/sync/sync_product_tool.dart b/lib/common/utils/sync/sync_product_tool.dart index a437967..8c8c6d9 100644 --- a/lib/common/utils/sync/sync_product_tool.dart +++ b/lib/common/utils/sync/sync_product_tool.dart @@ -179,7 +179,7 @@ abstract class SyncProductTool { }) async { final app = Get.find(); final dataBase = app.dataBase; - final productDao = dataBase.getProductDao; + final productDao = dataBase.getCanteenSpuDao; final productList = await productDao.getAll( "-1", "-1", @@ -225,7 +225,7 @@ abstract class SyncProductTool { if (ObjectUtil.isEmpty(list)) return; final app = Get.find(); final dataBase = app.dataBase; - final productDao = dataBase.getProductDao; + final productDao = dataBase.getCanteenSpuDao; List changeList = []; for (final v in list) { CanteenSpuEntity item = CanteenSpuEntity( diff --git a/lib/common/widgets/order/mixin/order_mixin.dart b/lib/common/widgets/order/mixin/order_mixin.dart index 24402b0..11dc91b 100644 --- a/lib/common/widgets/order/mixin/order_mixin.dart +++ b/lib/common/widgets/order/mixin/order_mixin.dart @@ -27,8 +27,11 @@ mixin OrderMixin { // 分类 RxList classifyList = RxList([]); - // 商品 - RxList productList = RxList([]); + // 食堂商品 + RxList canteenSpuDao = RxList([]); + + // 超市商品 + RxList supermarketSpuDao = RxList([]); // 总价 RxInt totalPrice = RxInt(0); @@ -112,10 +115,20 @@ mixin OrderMixin { // 获取商品 Future getProduct({String classifyCode = "-1"}) async { - final list = await appController.dataBase.getProductDao - .getAll(classifyCode, hourType.value.code); - productList.value = list; - productList.refresh(); + if (_sysType == SysType.SUPERMARKET) { + final list = await appController.dataBase.getSupermarketSpuDao.getAll( + classifyCode, + ); + supermarketSpuDao.value = list; + supermarketSpuDao.refresh(); + } else if (_sysType == SysType.CANTEEN) { + final list = await appController.dataBase.getCanteenSpuDao.getAll( + classifyCode, + hourType.value.code, + ); + canteenSpuDao.value = list; + canteenSpuDao.refresh(); + } classifyList.refresh(); } diff --git a/lib/pages/canteen_product/controller.dart b/lib/pages/canteen_product/controller.dart index 676a7d0..9fe5c95 100644 --- a/lib/pages/canteen_product/controller.dart +++ b/lib/pages/canteen_product/controller.dart @@ -247,7 +247,7 @@ class CanteenProductController extends GetxController with OrderMixin { // 获取默认商品 Future _getDefaultProduct() async { final sysType = appSetting.sysType.name; - final productDao = appC.dataBase.getProductDao; + final productDao = appC.dataBase.getCanteenSpuDao; List list = await productDao.getDefault(sysType); if (ObjectUtil.isEmptyList(list)) return; for (final CanteenSpuEntity v in list) { diff --git a/lib/pages/canteen_product/view.dart b/lib/pages/canteen_product/view.dart index 6603c8d..3c58a46 100644 --- a/lib/pages/canteen_product/view.dart +++ b/lib/pages/canteen_product/view.dart @@ -68,13 +68,13 @@ class CanteenProductPage extends GetView { itemCount: controller.classifyList.length, itemBuilder: (context, index) { if (ObjectUtil.isEmpty(controller.classifyList) || - ObjectUtil.isEmpty(controller.productList)) { + ObjectUtil.isEmpty(controller.canteenSpuDao)) { return const SizedBox.shrink(); } final current = controller.classifyList[index].serviceUniqueCode; final currentStr = controller.classifyList .firstWhereOrNull((v) => v.serviceUniqueCode == current); - final currentList = controller.productList + final currentList = controller.canteenSpuDao .where((i) => i.classifyServiceUniqueCode == current) .toList(); if (ObjectUtil.isEmpty(currentList)) { diff --git a/lib/pages/setting/widgets/setting_product.dart b/lib/pages/setting/widgets/setting_product.dart index cbc78cf..26d7672 100644 --- a/lib/pages/setting/widgets/setting_product.dart +++ b/lib/pages/setting/widgets/setting_product.dart @@ -78,7 +78,7 @@ class ProductManageController extends GetxController { try { final app = Get.find(); final sysSetting = await AppSettingTool.getSysSetting(); - final productDao = app.dataBase.getProductDao; + final productDao = app.dataBase.getCanteenSpuDao; final currnetHourType = hourType.code; String sysType = sysSetting.sysType.name; final findCount = await productDao.getCount( @@ -120,7 +120,7 @@ class ProductManageController extends GetxController { maskType: EasyLoadingMaskType.black, ); final app = Get.find(); - final productDao = app.dataBase.getProductDao; + final productDao = app.dataBase.getCanteenSpuDao; final editObj = CanteenSpuEntity.fromJson(v.toJson()); editObj.isDefault = !editObj.isDefault; await productDao.edit( diff --git a/lib/pages/supermarket_order/view.dart b/lib/pages/supermarket_order/view.dart index 7042781..b41a1a2 100644 --- a/lib/pages/supermarket_order/view.dart +++ b/lib/pages/supermarket_order/view.dart @@ -99,13 +99,13 @@ class SupermarketOrderPage extends GetView { itemCount: controller.classifyList.length, itemBuilder: (context, index) { if (ObjectUtil.isEmpty(controller.classifyList) || - ObjectUtil.isEmpty(controller.productList)) { + ObjectUtil.isEmpty(controller.canteenSpuDao)) { return const SizedBox.shrink(); } final current = controller.classifyList[index].serviceUniqueCode; final currentStr = controller.classifyList .firstWhereOrNull((v) => v.serviceUniqueCode == current); - final currentList = controller.productList + final currentList = controller.canteenSpuDao .where((i) => i.classifyServiceUniqueCode == current) .toList(); if (ObjectUtil.isEmpty(currentList)) {