111
This commit is contained in:
1 parent
d3b818a660
commit
917e7b4e83
5 files changed
+73
-32
No files matched your search
@@ -207,7 +207,9 @@ abstract class SyncProductTool {
|
||||
}
|
||||
}
|
||||
|
||||
/// 同步商品
|
||||
/// 按终端业态执行常规商品同步。
|
||||
///
|
||||
/// 食堂常规同步保留今天起连续七天的缓存范围;MQTT 食堂变更必须改用 [syncTodayCanteenProduct]。
|
||||
static Future<void> syncProduct() async {
|
||||
final sysSetting = await AppSettingTool.getSysSetting();
|
||||
final sysType = sysSetting.sysType;
|
||||
@@ -217,34 +219,11 @@ abstract class SyncProductTool {
|
||||
final DateTime now = DateTime.now();
|
||||
for (int i = 0; i < 7; i++) {
|
||||
final DateTime date = now.add(Duration(days: i));
|
||||
final String day = DateUtil.formatDate(
|
||||
date,
|
||||
format: DateFormats.y_mo_d,
|
||||
final success = await _syncCanteenProductForDay(
|
||||
date: date,
|
||||
merchantId: sysSetting.serviceMerchantId,
|
||||
);
|
||||
final dayDatetime = DateUtil.getDateTime(day);
|
||||
final pa = {
|
||||
"day": day,
|
||||
"hourType": null,
|
||||
"categoryId": null,
|
||||
"merchantId": sysSetting.serviceMerchantId,
|
||||
};
|
||||
final canteenList = await ProductApi.canteenProduct(pa);
|
||||
if (canteenList == null) return;
|
||||
if (ObjectUtil.isEmptyList(canteenList)) {
|
||||
// 空数组只代表当天没有可售菜品,不能清空其他日期已经同步的商品。
|
||||
final app = Get.find<AppControl>();
|
||||
await app.dataBase.getCanteenSpuDao.replaceByDay(
|
||||
day,
|
||||
const [],
|
||||
const [],
|
||||
);
|
||||
continue;
|
||||
}
|
||||
for (var v in canteenList) {
|
||||
v.saleDate = dayDatetime;
|
||||
}
|
||||
// 更新数据
|
||||
await _batchUpdateCaneenProduct(list: canteenList, dayStr: day);
|
||||
if (!success) return;
|
||||
}
|
||||
} else if (sysType == SysType.SUPERMARKET) {
|
||||
// 超市
|
||||
@@ -294,6 +273,62 @@ abstract class SyncProductTool {
|
||||
}
|
||||
}
|
||||
|
||||
/// 仅同步终端本地时区的当天食堂菜品,并在成功后记录食堂商品同步时间。
|
||||
///
|
||||
/// MQTT 菜品变更使用此入口,确保请求和本地替换都不会扩展到未来一周。
|
||||
static Future<bool> syncTodayCanteenProduct() async {
|
||||
final today = DateTime.now();
|
||||
final sysSetting = await AppSettingTool.getSysSetting();
|
||||
if (sysSetting.sysType != SysType.CANTEEN) return false;
|
||||
try {
|
||||
final success = await _syncCanteenProductForDay(
|
||||
date: today,
|
||||
merchantId: sysSetting.serviceMerchantId,
|
||||
);
|
||||
if (!success) return false;
|
||||
sysSetting.canteenProductSyncTime = DateTime.now();
|
||||
await AppSettingTool.setSysSetting(sysSetting);
|
||||
return true;
|
||||
} catch (e, stack) {
|
||||
logger.e("|$_tag|同步当天食堂商品信息意外的错误error:|$e|,stack:|$stack|");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 请求并原子替换指定自然日的食堂菜品快照。
|
||||
///
|
||||
/// 返回 `false` 表示接口没有有效响应,本地当天旧快照会保留;空数组属于有效响应,会清空当天快照。
|
||||
static Future<bool> _syncCanteenProductForDay({
|
||||
required DateTime date,
|
||||
required String? merchantId,
|
||||
}) async {
|
||||
final day = DateUtil.formatDate(date, format: DateFormats.y_mo_d);
|
||||
final dayDatetime = DateUtil.getDateTime(day);
|
||||
final canteenList = await ProductApi.canteenProduct({
|
||||
"day": day,
|
||||
"hourType": null,
|
||||
"categoryId": null,
|
||||
"merchantId": merchantId,
|
||||
});
|
||||
if (canteenList == null) return false;
|
||||
if (ObjectUtil.isEmptyList(canteenList)) {
|
||||
// 空数组只代表指定日期没有可售菜品,不能清空其他日期已经同步的商品。
|
||||
final app = Get.find<AppControl>();
|
||||
await app.dataBase.getCanteenSpuDao.replaceByDay(
|
||||
day,
|
||||
const [],
|
||||
const [],
|
||||
);
|
||||
return true;
|
||||
}
|
||||
for (final product in canteenList) {
|
||||
// 接口按 day 查询,统一覆盖供应日期可防止异常响应把其他日期写进当天同步结果。
|
||||
product.saleDate = dayDatetime;
|
||||
}
|
||||
await _batchUpdateCaneenProduct(list: canteenList, dayStr: day);
|
||||
return true;
|
||||
}
|
||||
|
||||
// **************************************************************************
|
||||
// 同步食堂商品 spu start
|
||||
// **************************************************************************
|
||||
|
||||
Reference in new issue
Block a user