125 lines
3.8 KiB
Dart
125 lines
3.8 KiB
Dart
import 'package:EasyDartModule/EasyDartModule.dart';
|
|
|
|
import '../model/Bed.dart';
|
|
import '../model/Person.dart';
|
|
import '../model/Reservation.dart';
|
|
import '../repository/BedRepository.dart';
|
|
import '../enum/DeviceStatus.dart';
|
|
|
|
class BedService {
|
|
final BedRepository bedRepository = BedRepository();
|
|
|
|
BedService();
|
|
|
|
// 获取床位列表,支持查询参数
|
|
Future<List> getBedList(Bed query) async {
|
|
if (query.device_status != null && query.device_status!.isNotEmpty) {
|
|
List<String> status = query.device_status!.split(",");
|
|
if (status.length != 2) {
|
|
// 查询所有的设备
|
|
var allDevices =
|
|
await EasyDartModule.redis.get("oid_${query.tid}_devices");
|
|
if (allDevices != null && allDevices.isNotEmpty) {
|
|
List<String> deviceList = allDevices.split(',');
|
|
|
|
if (status[0] == '0') {
|
|
// 查询离线设备
|
|
List<String> offlineDevices = [];
|
|
for (var deviceId in deviceList) {
|
|
var isOnline = await EasyDartModule.redis.get("oid_${deviceId}");
|
|
if (isOnline == null) {
|
|
offlineDevices.add(deviceId);
|
|
}
|
|
}
|
|
query.deviceIds = offlineDevices;
|
|
}
|
|
if (status[0] == '1') {
|
|
// 查询在线设备
|
|
List<String> onlineDevices = [];
|
|
for (var deviceId in deviceList) {
|
|
var isOnline = await EasyDartModule.redis.get("oid_${deviceId}");
|
|
if (isOnline != null) {
|
|
onlineDevices.add(deviceId);
|
|
}
|
|
}
|
|
query.deviceIds = onlineDevices;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
List bedList = await bedRepository.fetchBedList(query);
|
|
if (bedList.isNotEmpty) {
|
|
fillDeviceStatus(bedList);
|
|
}
|
|
return bedList;
|
|
}
|
|
|
|
// 获取单个床位信息
|
|
Future<Map?> getBedById(String bedId) async {
|
|
return await bedRepository.fetchBedById(bedId);
|
|
}
|
|
|
|
// 创建床位
|
|
Future<bool> createBed(Bed bed) async {
|
|
// 直接将异常抛给调用者
|
|
bed.created_at = DateTime.now().millisecondsSinceEpoch;
|
|
bed.deleted = 0;
|
|
return await bedRepository.insertBed(bed);
|
|
}
|
|
|
|
// 更新床位信息
|
|
Future<String> updateBed(String bedId, Bed updatedBed) async {
|
|
// 直接将异常抛给调用者
|
|
updatedBed.updated_at = DateTime.now().millisecondsSinceEpoch;
|
|
return await bedRepository.updateBed(bedId, updatedBed);
|
|
}
|
|
|
|
// 删除床位
|
|
Future<String> deleteBed(String bedId) async {
|
|
// 直接将异常抛给调用者
|
|
if (bedId == null || bedId.isEmpty) {
|
|
return "ID不能为空";
|
|
}
|
|
return await bedRepository.deleteBed(bedId);
|
|
}
|
|
|
|
Future<int> getBedCount(Bed query) async {
|
|
return await bedRepository.getBedCount(query);
|
|
}
|
|
|
|
//查询预约入住信息
|
|
getOrderCheckIn(Reservation query) async {
|
|
return await bedRepository.getOrderCheckInList(query);
|
|
}
|
|
|
|
//设备在离线
|
|
void fillDeviceStatus(List bedList) {
|
|
try {
|
|
if (bedList.isNotEmpty) {
|
|
bedList.forEach((bed) async {
|
|
if (bed['device_id'] != null) {
|
|
String deviceId = bed['device_id'];
|
|
if (deviceId != null && deviceId.isNotEmpty) {
|
|
var deviceStatus =
|
|
await EasyDartModule.redis.get("mac_$deviceId");
|
|
if (deviceStatus != null) {
|
|
bed['device_status'] = DeviceStatus.ONLINE.code;
|
|
} else {
|
|
bed['device_status'] = DeviceStatus.OFFLONE.code;
|
|
}
|
|
bed['map_device_status'] = deviceStatus;//地图设备状态
|
|
}
|
|
}
|
|
});
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
}
|
|
}
|
|
|
|
//更新床位状态
|
|
Future<void> updateBedStatus(String bed_id, int code, Person person) async {
|
|
await bedRepository.updateBedStatus(bed_id, code, person);
|
|
}
|
|
}
|