diff --git a/lib/common/models/api/hard_api_models.dart b/lib/common/models/api/hard_api_models.dart index 38e612c..044904b 100644 --- a/lib/common/models/api/hard_api_models.dart +++ b/lib/common/models/api/hard_api_models.dart @@ -434,6 +434,9 @@ class ServiceBroadcastToday { /// 支付价格 int payPrice; + /// 创建时间 + String createTime; + ServiceBroadcastToday({ required this.id, required this.userId, @@ -451,6 +454,7 @@ class ServiceBroadcastToday { this.hourType, required this.productDesc, required this.payPrice, + required this.createTime, }); factory ServiceBroadcastToday.fromJson(Map json) => diff --git a/lib/common/models/api/hard_api_models.g.dart b/lib/common/models/api/hard_api_models.g.dart index b78f925..94cd64a 100644 --- a/lib/common/models/api/hard_api_models.g.dart +++ b/lib/common/models/api/hard_api_models.g.dart @@ -265,6 +265,7 @@ ServiceBroadcastToday _$ServiceBroadcastTodayFromJson( hourType: json['hourType'] as String?, productDesc: json['productDesc'] as String, payPrice: (json['payPrice'] as num).toInt(), + createTime: json['createTime'] as String, ); Map _$ServiceBroadcastTodayToJson( @@ -286,6 +287,7 @@ Map _$ServiceBroadcastTodayToJson( 'hourType': instance.hourType, 'productDesc': instance.productDesc, 'payPrice': instance.payPrice, + 'createTime': instance.createTime, }; ServiceBroadcastTodayPage _$ServiceBroadcastTodayPageFromJson( diff --git a/lib/common/services/database/dao/broadcast_dao.dart b/lib/common/services/database/dao/broadcast_dao.dart index 511cedc..abd5c6d 100644 --- a/lib/common/services/database/dao/broadcast_dao.dart +++ b/lib/common/services/database/dao/broadcast_dao.dart @@ -7,13 +7,14 @@ import 'package:pj_supermarket_cloud/common/index.dart'; // broadcast @dao abstract class BroadcastDao { - Future toggleEnable(bool enable) async { + Future toggleEnable(bool enable, ProductHourType hourType) async { final app = Get.find(); final database = app.dataBase.database; final sql = ''' UPDATE broadcast SET enable = ${enable ? 1 : 0} WHERE isDone = 0 + AND (:hourType = '-1' OR hourType = :hourType) AND strftime('%Y-%m-%d', createTime) = strftime('%Y-%m-%d', 'now', 'localtime') '''; return await database.rawUpdate(sql, []); @@ -35,6 +36,17 @@ abstract class BroadcastDao { return await database.rawDelete(sql, [...codes]); } + Future delAll() async { + final appC = Get.find(); + final database = appC.dataBase.database; + final sql = ''' + DELETE FROM broadcast + WHERE + strftime('%Y-%m-%d', createTime) = strftime('%Y-%m-%d', 'now', 'localtime') + '''; + return await database.rawDelete(sql, []); + } + @insert Future save(BroadcastEntity entity); @@ -58,9 +70,10 @@ abstract class BroadcastDao { isDone = 0 AND enable = 1 AND broadcastCount < broadcastMaxCount + AND (:hourType = '-1' OR hourType = :hourType) AND strftime('%Y-%m-%d', createTime) = strftime('%Y-%m-%d', 'now', 'localtime') """) - Future> getAll(); + Future> getAll(String hourType); @Query(""" SELECT * FROM broadcast diff --git a/lib/common/services/database/database.g.dart b/lib/common/services/database/database.g.dart index 2b11c75..d73f4dc 100644 --- a/lib/common/services/database/database.g.dart +++ b/lib/common/services/database/database.g.dart @@ -282,27 +282,11 @@ class _$BroadcastDao extends BroadcastDao { } @override - Future> getAll() async { + Future> getAll(String hourType) async { return _queryAdapter.queryList( - 'SELECT * FROM broadcast WHERE isDone = 0 AND enable = 1 AND broadcastCount < broadcastMaxCount AND strftime(\'%Y-%m-%d\', createTime) = strftime(\'%Y-%m-%d\', \'now\', \'localtime\')', - mapper: (Map row) => BroadcastEntity( - id: row['id'] as int?, - content: row['content'] as String, - broadcastCount: row['broadcastCount'] as int, - broadcastMaxCount: row['broadcastMaxCount'] as int, - isBroadcast: _boolConverter.decode(row['isBroadcast'] as int), - isDone: _boolConverter.decode(row['isDone'] as int), - createTime: _dateTimeConverter.decode(row['createTime'] as String), - serviceCode: row['serviceCode'] as String, - userId: row['userId'] as String, - username: row['username'] as String, - userMobile: row['userMobile'] as String, - productDesc: row['productDesc'] as String, - payPrice: row['payPrice'] as int, - hourType: row['hourType'] == null - ? null - : ProductHourType.values[row['hourType'] as int], - enable: _boolConverter.decode(row['enable'] as int))); + 'SELECT * FROM broadcast WHERE isDone = 0 AND enable = 1 AND broadcastCount < broadcastMaxCount AND (?1 = \'-1\' OR hourType = ?1) AND strftime(\'%Y-%m-%d\', createTime) = strftime(\'%Y-%m-%d\', \'now\', \'localtime\')', + mapper: (Map row) => BroadcastEntity(id: row['id'] as int?, content: row['content'] as String, broadcastCount: row['broadcastCount'] as int, broadcastMaxCount: row['broadcastMaxCount'] as int, isBroadcast: _boolConverter.decode(row['isBroadcast'] as int), isDone: _boolConverter.decode(row['isDone'] as int), createTime: _dateTimeConverter.decode(row['createTime'] as String), serviceCode: row['serviceCode'] as String, userId: row['userId'] as String, username: row['username'] as String, userMobile: row['userMobile'] as String, productDesc: row['productDesc'] as String, payPrice: row['payPrice'] as int, hourType: row['hourType'] == null ? null : ProductHourType.values[row['hourType'] as int], enable: _boolConverter.decode(row['enable'] as int)), + arguments: [hourType]); } @override diff --git a/lib/common/utils/sync/sync_check_tool.dart b/lib/common/utils/sync/sync_check_tool.dart index 27270ee..c819954 100644 --- a/lib/common/utils/sync/sync_check_tool.dart +++ b/lib/common/utils/sync/sync_check_tool.dart @@ -1,5 +1,6 @@ import 'dart:async'; +import 'package:get/get.dart'; import 'package:mqtt_client/mqtt_client.dart'; import 'package:pj_supermarket_cloud/common/index.dart'; @@ -117,7 +118,10 @@ class SyncCheckTool { if (!isNetwork) return; logger.i("|$_tag|$logMsg,$inSeconds秒一次,开始执行"); try { - await SyncTakeFoodBroadcastTool.doCheckBroadcast(); + final setting = await AppSettingTool.getSysSetting(); + await SyncTakeFoodBroadcastTool.doCheckBroadcast( + setting.hourType ?? ProductHourType.TOTAL, + ); } catch (e) { logger.i("|$_tag|$logMsg,$inSeconds秒一次,执行失败,error:$e"); } finally { diff --git a/lib/common/utils/sync/sync_take_food_broadcast_tool.dart b/lib/common/utils/sync/sync_take_food_broadcast_tool.dart index cd8142d..06a5cce 100644 --- a/lib/common/utils/sync/sync_take_food_broadcast_tool.dart +++ b/lib/common/utils/sync/sync_take_food_broadcast_tool.dart @@ -35,11 +35,14 @@ abstract class SyncTakeFoodBroadcastTool { ); await broadcastDao.save(saveObj); logger.i("|$_tag|添加到广播记录到本地数据库,json:|${saveObj.toJson()}|"); - await SyncTakeFoodBroadcastTool.doCheckBroadcast(); + final setting = await AppSettingTool.getSysSetting(); + await SyncTakeFoodBroadcastTool.doCheckBroadcast( + setting.hourType ?? ProductHourType.TOTAL, + ); } // 检查广播 - static Future doCheckBroadcast() async { + static Future doCheckBroadcast(ProductHourType hourType) async { if (_isBroadcast) { logger.i("|$_tag|检查打印,正在广播中"); return; @@ -48,7 +51,7 @@ abstract class SyncTakeFoodBroadcastTool { final appC = Get.find(); final broadcastDao = appC.dataBase.getBroadcastDao; try { - final list = await broadcastDao.getAll(); + final list = await broadcastDao.getAll(hourType.code); if (ObjectUtil.isEmptyList(list)) { _isBroadcast = false; logger.i("|$_tag|检查广播,没有需要广播的订单信息"); @@ -65,10 +68,16 @@ abstract class SyncTakeFoodBroadcastTool { : list.length; // 获取当前组的元素 final currentGroup = list.sublist(start, end); - String msg = "请手机尾号,"; + // 使用 Set 去重 + final Set uniqueContents = {}; for (final v in currentGroup) { - msg += "${v.content},"; + uniqueContents.add(v.content); } + String msg = "请手机尾号"; + for (final content in uniqueContents) { + msg += "$content,"; + } + // 逗号会影响语音播报断句 msg = msg.substring(0, msg.length - 1); msg += "的用户,尽快取餐"; // 播报当前组 @@ -121,13 +130,13 @@ abstract class SyncTakeFoodBroadcastTool { if (ObjectUtil.isEmptyList(serviceList.records)) return false; final appC = Get.find(); final broadcastDao = appC.dataBase.getBroadcastDao; - final serviceCodes = - serviceList.records?.map((e) => e.id.toString()).toList(); - if (ObjectUtil.isEmptyList(serviceCodes)) { - return result; - } + // final serviceCodes = + // serviceList.records?.map((e) => e.id.toString()).toList(); + // if (ObjectUtil.isEmptyList(serviceCodes)) { + // return result; + // } // 根据服务码查询本地数据库 - await broadcastDao.delByServiceCodes(serviceCodes!); + await broadcastDao.delAll(); final saveList = serviceList.records! .map((e) => _convertServiceBroadcastTodayToBroadcastEntity(e)) .toList(); @@ -154,7 +163,7 @@ abstract class SyncTakeFoodBroadcastTool { hourType: ProductHourType.values.fromCode(obj.hourType) ?? ProductHourType.NOON, enable: true, - createTime: DateTime.now(), + createTime: DateUtil.getDateTime(obj.createTime) ?? DateTime.now(), ); } } diff --git a/lib/pages/canteen_reserve/controller.dart b/lib/pages/canteen_reserve/controller.dart index 05de3dc..4fd2f89 100644 --- a/lib/pages/canteen_reserve/controller.dart +++ b/lib/pages/canteen_reserve/controller.dart @@ -128,30 +128,40 @@ class CanteenReserveController extends GetxController { maskType: EasyLoadingMaskType.black, ); // 立即执行一次广播 - SyncTakeFoodBroadcastTool.doCheckBroadcast(); + SyncTakeFoodBroadcastTool.doCheckBroadcast(hourType.value); EasyLoading.dismiss(); } /// 关闭全部广播 Future closeBroadcast() async { + final isSure = await appMessageDialog( + Get.context!, + "是否关闭所有${hourType.value.title}广播", + ); + if (!isSure) return; EasyLoading.show( status: '关闭中...', maskType: EasyLoadingMaskType.black, ); final broadcastDao = appC.dataBase.getBroadcastDao; - await broadcastDao.toggleEnable(false); + await broadcastDao.toggleEnable(false, hourType.value); await _getReservePage(); EasyLoading.dismiss(); } /// 打开全部广播 Future openBroadcast() async { + final isSure = await appMessageDialog( + Get.context!, + "是否打卡所有${hourType.value.title}广播", + ); + if (!isSure) return; EasyLoading.show( status: '打开中...', maskType: EasyLoadingMaskType.black, ); final broadcastDao = appC.dataBase.getBroadcastDao; - await broadcastDao.toggleEnable(true); + await broadcastDao.toggleEnable(true, hourType.value); await _getReservePage(); EasyLoading.dismiss(); }