482 lines
16 KiB
Dart
482 lines
16 KiB
Dart
import 'package:EasyDartModule/EasyDartModule.dart';
|
|
|
|
import '../const/MessageConstants.dart';
|
|
import '../model/Bed.dart';
|
|
import '../model/Person.dart';
|
|
import '../model/Reservation.dart';
|
|
import '../enum/DeviceStatus.dart';
|
|
import '../const/Constants.dart';
|
|
|
|
class BedRepository {
|
|
// 获取床位列表,支持查询条件
|
|
Future<List> fetchBedList(Bed query) async {
|
|
var db = EasyDartModule.dataBase;
|
|
SelectorBuilder select = SelectorBuilder();
|
|
// 添加查询条件
|
|
if (query.id != null) {
|
|
select.eq('_id', query.id);
|
|
}
|
|
if (query.tid != null) {
|
|
select.eq('oid', query.tid);
|
|
}
|
|
if (query.level != null) {
|
|
select.gte('data_level', query.level);
|
|
}
|
|
if (query.room_id != null) {
|
|
// List<String> roomTypes = query.roomIds!.split(',');
|
|
// select.oneFrom('room_id', roomTypes);
|
|
select.eq('room_id', query.room_id);
|
|
}
|
|
if (query.mapping != null) {
|
|
if (query.mapping!.length == 1) {
|
|
if (query.mapping == "1") {
|
|
select.nin('mapped_coordinates', [null, '']);
|
|
} else if (query.mapping == "0") {
|
|
select.oneFrom('mapped_coordinates', [null, '']);
|
|
}
|
|
}
|
|
}
|
|
if (query.device_status != null) {
|
|
List deviceIdList = [];
|
|
if (query.device_status!.length == 1) {
|
|
var allDevices =
|
|
await EasyDartModule.redis.get("oid_${query.tid}_devices");
|
|
if (allDevices != null) {
|
|
if (query.device_status == DeviceStatus.ONLINE.code.toString()) {
|
|
if (allDevices != null && allDevices.isNotEmpty) {
|
|
List<String> deviceIds = allDevices.split(',');
|
|
for (var deviceId in deviceIds) {
|
|
var deviceStatus =
|
|
await EasyDartModule.redis.get("mac_$deviceId");
|
|
if (deviceStatus != null && deviceStatus.isNotEmpty) {
|
|
deviceIdList.add(deviceId);
|
|
}
|
|
}
|
|
}
|
|
if (deviceIdList.isNotEmpty) {
|
|
select.oneFrom('device_id', deviceIdList);
|
|
} else {
|
|
return [];
|
|
}
|
|
} else if (query.device_status ==
|
|
DeviceStatus.OFFLONE.code.toString()) {
|
|
if (allDevices != null && allDevices.isNotEmpty) {
|
|
List<String> deviceIds = allDevices.split(',');
|
|
for (var deviceId in deviceIds) {
|
|
var deviceStatus =
|
|
await EasyDartModule.redis.get("mac_$deviceId");
|
|
if (deviceStatus == null || deviceStatus.isEmpty) {
|
|
deviceIdList.add(deviceId);
|
|
}
|
|
}
|
|
if (deviceIdList.isNotEmpty) {
|
|
select.oneFrom('device_id', deviceIdList);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (query.areaIds != null) {
|
|
List<String> areaIds = query.areaIds!.split(',');
|
|
SelectorBuilder newSelect = SelectorBuilder();
|
|
var newquery = newSelect.oneFrom('area_id', areaIds);
|
|
var result = await db.query(
|
|
'bus_room_info',
|
|
condition: newquery,
|
|
);
|
|
if (result.isNotEmpty) {
|
|
List<String> roomIds = [];
|
|
for (var item in result) {
|
|
roomIds.add(item['_id'].toHexString());
|
|
}
|
|
select.oneFrom('room_id', roomIds);
|
|
}
|
|
}
|
|
if (query.room_id != null) {
|
|
select.eq('room_id', query.room_id);
|
|
}
|
|
if (query.bed_type_id != null) {
|
|
// select.eq('bed_type_id', query.bed_type_id);
|
|
List<String> roomTypes = query.bed_type_id!.split(',');
|
|
select.oneFrom('bed_type_id', roomTypes);
|
|
}
|
|
if (query.bed_type_name != null) {
|
|
select.match('bed_type_name', query.bed_type_name!,
|
|
caseInsensitive: true);
|
|
}
|
|
if (query.bed_name != null) {
|
|
select.match('bed_name', query.bed_name!, caseInsensitive: true);
|
|
}
|
|
if (query.is_mapped != null) {
|
|
select.eq('is_mapped', query.is_mapped == true ? 1 : 0);
|
|
}
|
|
if (query.device_model != null) {
|
|
select.match('device_model', query.device_model!, caseInsensitive: true);
|
|
}
|
|
if (query.device_name != null) {
|
|
select.match('device_name', query.device_name!, caseInsensitive: true);
|
|
}
|
|
if (query.device_id != null) {
|
|
select.match('device_id', query.device_id!, caseInsensitive: false);
|
|
}
|
|
if (query.status != null) {
|
|
select.eq('status', query.status);
|
|
}
|
|
if (query.created_by_id != null) {
|
|
select.eq('created_by_id', query.created_by_id);
|
|
}
|
|
if (query.updated_by_id != null) {
|
|
select.eq('updated_by_id', query.updated_by_id);
|
|
}
|
|
if (query.oid != null) {
|
|
select.eq('oid', query.oid);
|
|
}
|
|
if (query.start_time != null) {
|
|
select.gte('created_at', query.start_time);
|
|
}
|
|
if (query.end_time != null) {
|
|
select.lte('created_at', query.end_time);
|
|
}
|
|
if (query.deviceIds != null && query.deviceIds!.isNotEmpty) {
|
|
select.oneFrom('device_id', query.deviceIds!);
|
|
}
|
|
select.eq('deleted', 0);
|
|
// 设置分页
|
|
select
|
|
.skip(((int.tryParse(query.page ?? Constants.default_page.toString()) ??
|
|
Constants.default_page) -
|
|
1) *
|
|
(int.tryParse(query.limit ?? Constants.default_limit.toString()) ??
|
|
Constants.default_limit))
|
|
.limit(
|
|
int.tryParse(query.limit ?? Constants.default_limit.toString()) ??
|
|
Constants.default_limit);
|
|
|
|
// 设置排序
|
|
select.sortBy("created_at"); // 按创建时间排序
|
|
// 执行查询
|
|
var result = await db.query('bus_bed_info', condition: select);
|
|
|
|
// 如果查询结果为空,返回空列表
|
|
return result.isEmpty ? [] : result;
|
|
}
|
|
|
|
// 获取单个床位信息
|
|
Future<Map?> fetchBedById(String bedId) async {
|
|
var db = EasyDartModule.dataBase;
|
|
var condition = {'_id': ObjectId.fromHexString(bedId)}; // 使用 bed_id 作为条件
|
|
var result = await db.query('bus_bed_info', condition: condition);
|
|
return result.isNotEmpty ? result.first : null;
|
|
}
|
|
|
|
// 插入床位信息
|
|
Future<bool> insertBed(Bed bed) async {
|
|
var db = EasyDartModule.dataBase;
|
|
int currentTime = DateTime.now().millisecondsSinceEpoch;
|
|
bed.created_at = currentTime;
|
|
bed.updated_at = currentTime;
|
|
bed.deleted = 0; // 初始化为未删除
|
|
var result = await db.insert('bus_bed_info', bed.toJson());
|
|
return true;
|
|
}
|
|
|
|
// 更新床位信息
|
|
Future<String> updateBed(String bedId, Bed updatedBed) async {
|
|
var db = EasyDartModule.dataBase;
|
|
String message = "";
|
|
updatedBed.updated_at = DateTime.now().millisecondsSinceEpoch;
|
|
var condition = {'bed_id': bedId}; // 使用 bed_id 作为条件
|
|
try {
|
|
var result =
|
|
await db.update('bus_bed_info', updatedBed.toJson(), condition);
|
|
message = MessageConstants.UPDATE_SUCCESS;
|
|
} catch (e) {
|
|
message = MessageConstants.UPDATE_ERROR;
|
|
}
|
|
return message;
|
|
}
|
|
|
|
// 删除床位(逻辑删除)
|
|
Future<String> deleteBed(String bedId) async {
|
|
var db = EasyDartModule.dataBase;
|
|
String message = "";
|
|
var condition = {'bed_id': bedId}; // 使用 bed_id 作为查询条件
|
|
var result = await db.query('bus_bed_info', condition: condition);
|
|
|
|
if (result.isNotEmpty) {
|
|
// 标记为已删除
|
|
var update = {'deleted': 1};
|
|
try {
|
|
var updateResult = await db.update('bus_bed_info', update, condition);
|
|
message = MessageConstants.DELETE_SUCCESS;
|
|
} catch (e) {
|
|
message = MessageConstants.DELETE_ERROR;
|
|
}
|
|
} else {
|
|
message = "未找到床位";
|
|
}
|
|
return message;
|
|
}
|
|
|
|
Future<int> getBedCount(Bed query) async {
|
|
var db = EasyDartModule.dataBase;
|
|
SelectorBuilder select = SelectorBuilder();
|
|
|
|
// 添加查询条件
|
|
if (query.id != null) {
|
|
select.eq('_id', query.id);
|
|
}
|
|
if (query.tid != null) {
|
|
select.eq('oid', query.tid);
|
|
}
|
|
if (query.level != null) {
|
|
select.gte('data_level', query.level);
|
|
}
|
|
if (query.room_id != null) {
|
|
// List<String> roomTypes = query.roomIds!.split(',');
|
|
// select.oneFrom('room_id', roomTypes);
|
|
select.eq('room_id', query.room_id);
|
|
}
|
|
if (query.mapping != null) {
|
|
if (query.mapping!.length == 1) {
|
|
if (query.mapping == "1") {
|
|
select.nin('mapped_coordinates', [null, '']);
|
|
} else if (query.mapping == "0") {
|
|
select.oneFrom('mapped_coordinates', [null, '']);
|
|
}
|
|
}
|
|
}
|
|
if (query.device_status != null) {
|
|
List deviceIdList = [];
|
|
if (query.device_status!.length == 1) {
|
|
var allDevices =
|
|
await EasyDartModule.redis.get("oid_${query.tid}_devices");
|
|
if (allDevices != null) {
|
|
if (query.device_status == DeviceStatus.ONLINE.code.toString()) {
|
|
if (allDevices != null && allDevices.isNotEmpty) {
|
|
List<String> deviceIds = allDevices.split(',');
|
|
for (var deviceId in deviceIds) {
|
|
var deviceStatus =
|
|
await EasyDartModule.redis.get("mac_$deviceId");
|
|
if (deviceStatus != null && deviceStatus.isNotEmpty) {
|
|
deviceIdList.add(deviceId);
|
|
}
|
|
}
|
|
}
|
|
if (deviceIdList.isNotEmpty) {
|
|
select.oneFrom('device_id', deviceIdList);
|
|
} else {
|
|
return 0;
|
|
}
|
|
} else if (query.device_status ==
|
|
DeviceStatus.OFFLONE.code.toString()) {
|
|
if (allDevices != null && allDevices.isNotEmpty) {
|
|
List<String> deviceIds = allDevices.split(',');
|
|
for (var deviceId in deviceIds) {
|
|
var deviceStatus =
|
|
await EasyDartModule.redis.get("mac_$deviceId");
|
|
if (deviceStatus == null || deviceStatus.isEmpty) {
|
|
deviceIdList.add(deviceId);
|
|
}
|
|
}
|
|
if (deviceIdList.isNotEmpty) {
|
|
select.oneFrom('device_id', deviceIdList);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (query.areaIds != null) {
|
|
List<String> areaIds = query.areaIds!.split(',');
|
|
SelectorBuilder newSelect = SelectorBuilder();
|
|
var newquery = newSelect.oneFrom('area_id', areaIds);
|
|
var result = await db.query(
|
|
'bus_room_info',
|
|
condition: newquery,
|
|
);
|
|
if (result.isNotEmpty) {
|
|
List<String> roomIds = [];
|
|
for (var item in result) {
|
|
roomIds.add(item['_id'].toHexString());
|
|
}
|
|
select.oneFrom('room_id', roomIds);
|
|
}
|
|
}
|
|
if (query.room_id != null) {
|
|
select.eq('room_id', query.room_id);
|
|
}
|
|
if (query.bed_type_id != null) {
|
|
// select.eq('bed_type_id', query.bed_type_id);
|
|
List<String> roomTypes = query.bed_type_id!.split(',');
|
|
select.oneFrom('bed_type_id', roomTypes);
|
|
}
|
|
if (query.bed_type_name != null) {
|
|
select.match('bed_type_name', query.bed_type_name!,
|
|
caseInsensitive: true);
|
|
}
|
|
if (query.bed_name != null) {
|
|
select.match('bed_name', query.bed_name!, caseInsensitive: true);
|
|
}
|
|
if (query.is_mapped != null) {
|
|
select.eq('is_mapped', query.is_mapped == true ? 1 : 0);
|
|
}
|
|
if (query.device_model != null) {
|
|
select.match('device_model', query.device_model!, caseInsensitive: true);
|
|
}
|
|
if (query.device_name != null) {
|
|
select.match('device_name', query.device_name!, caseInsensitive: true);
|
|
}
|
|
if (query.device_id != null) {
|
|
select.match('device_id', query.device_id!, caseInsensitive: false);
|
|
}
|
|
if (query.status != null) {
|
|
select.eq('status', query.status);
|
|
}
|
|
if (query.created_by_id != null) {
|
|
select.eq('created_by_id', query.created_by_id);
|
|
}
|
|
if (query.updated_by_id != null) {
|
|
select.eq('updated_by_id', query.updated_by_id);
|
|
}
|
|
if (query.oid != null) {
|
|
select.eq('oid', query.oid);
|
|
}
|
|
if (query.start_time != null) {
|
|
select.gte('created_at', query.start_time);
|
|
}
|
|
if (query.end_time != null) {
|
|
select.lte('created_at', query.end_time);
|
|
}
|
|
if (query.deviceIds != null && query.deviceIds!.isNotEmpty) {
|
|
select.oneFrom('device_id', query.deviceIds!);
|
|
}
|
|
select.eq('deleted', 0);
|
|
|
|
// 执行查询,获取符合条件的记录数量
|
|
var result = await db.query(
|
|
'bus_bed_info',
|
|
condition: select, // 查询条件
|
|
);
|
|
|
|
// 如果查询结果为空,返回 0
|
|
if (result.isEmpty) {
|
|
return 0;
|
|
}
|
|
|
|
// 返回计数结果
|
|
return result.length;
|
|
}
|
|
|
|
Future<List> getOrderCheckInList(Reservation query) async {
|
|
var db = EasyDartModule.dataBase;
|
|
SelectorBuilder select = SelectorBuilder();
|
|
|
|
// 添加查询条件
|
|
if (query.id != null) {
|
|
select.eq('_id', query.id);
|
|
}
|
|
if (query.room_id != null) {
|
|
select.eq('room_id', query.room_id);
|
|
}
|
|
if (query.bed_id != null) {
|
|
select.eq('bed_id', query.bed_id!);
|
|
}
|
|
if (query.start_time != null) {
|
|
select.gte('start_time', query.start_time);
|
|
}
|
|
if (query.end_time != null) {
|
|
select.lte('end_time', query.end_time);
|
|
}
|
|
if (query.total_days != null) {
|
|
select.eq('total_days', query.total_days);
|
|
}
|
|
if (query.contact_name != null) {
|
|
select.match('contact_name', query.contact_name!, caseInsensitive: true);
|
|
}
|
|
if (query.relationship_type != null) {
|
|
select.match('relationship_type', query.relationship_type!,
|
|
caseInsensitive: true);
|
|
}
|
|
if (query.phone_number != null) {
|
|
select.match('phone_number', query.phone_number!, caseInsensitive: true);
|
|
}
|
|
if (query.oid != null) {
|
|
select.eq('oid', query.oid);
|
|
}
|
|
if (query.desc != null) {
|
|
select.match('desc', query.desc!, caseInsensitive: true);
|
|
}
|
|
|
|
// 添加默认查询条件
|
|
select.eq('deleted', 0);
|
|
|
|
// 执行查询
|
|
var result = await db.query('bus_room_check_info', condition: select);
|
|
|
|
// 如果查询结果为空,返回空列表
|
|
return result.isEmpty ? [] : result;
|
|
}
|
|
|
|
Future<void> updateBedStatus(String bed_id, int code, Person person) async {
|
|
var start_time = person.check_in_start_time;
|
|
var end_time = person.check_in_end_time;
|
|
var db = EasyDartModule.dataBase;
|
|
var condition = {'_id': ObjectId.fromHexString(bed_id)}; // 使用 bed_id 作为条件
|
|
var update = {
|
|
'\$set': {
|
|
'status': code,
|
|
'updated_at': DateTime.now().millisecondsSinceEpoch,
|
|
'start_time': start_time,
|
|
'end_time': end_time,
|
|
},
|
|
};
|
|
try {
|
|
var result = await db.update('bus_bed_info', update, condition);
|
|
} catch (e) {
|
|
print(e);
|
|
}
|
|
}
|
|
|
|
//存储统计信息
|
|
Future<void> addStatistics(
|
|
Map result, int code, int queryStartTime, int queryEndTime) async {
|
|
var db = EasyDartModule.dataBase;
|
|
try {
|
|
if (result != null && result.isNotEmpty) {
|
|
result.forEach((tid, statistics) async {
|
|
Map<String, dynamic> info = {};
|
|
info['tid'] = tid;
|
|
info['type'] = code;
|
|
info['start_time'] = queryStartTime;
|
|
info['end_time'] = queryEndTime;
|
|
info['data'] = statistics;
|
|
var result = await db.insert('bus_statistics_info', info);
|
|
print("object");
|
|
});
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
}
|
|
}
|
|
|
|
findResultByQuery(Map query) async {
|
|
var db = EasyDartModule.dataBase;
|
|
SelectorBuilder select = SelectorBuilder();
|
|
select.eq('type', query['type']);
|
|
select.gte('start_time', query['startTime']);
|
|
select.lte('end_time', query['endTime']);
|
|
select.eq('tid', query['tid']);
|
|
var result = await db.query(
|
|
'bus_statistics_info',
|
|
condition: select,
|
|
);
|
|
|
|
if (result.isEmpty) {
|
|
return [];
|
|
}
|
|
return result;
|
|
}
|
|
}
|