Files
data_manage/bin/repository/RoomRepository.dart
2026-01-20 09:26:55 +08:00

303 lines
8.5 KiB
Dart

import 'package:EasyDartModule/EasyDartModule.dart';
import '../model/Room.dart';
import '../const/MessageConstants.dart';
class RoomRepository {
// 获取所有房间列表
Future<List> fetchRoomList(Room query,
{int page = 1, int pageSize = 10}) async {
var db = EasyDartModule.dataBase;
// 构建查询条件
SelectorBuilder select = SelectorBuilder();
// 添加查询条件
if (query.id != null) {
select.eq('_id', ObjectId.fromHexString(query.id!));
}
if (query.tid != null) {
select.eq('oid', query.tid);
}
if (query.level != null) {
select.gte('data_level', query.level);
}
if (query.room_name != null) {
select.match('room_name', query.room_name!, caseInsensitive: true);
}
if (query.room_type != null) {
List<String> roomTypes = query.room_type!.split(',');
select.oneFrom('room_type', roomTypes);
}
if (query.room_desc != null) {
select.eq('room_desc', query.room_desc);
}
if (query.bed_num != null) {
select.eq('bed_num', query.bed_num);
}
if (query.bed_num_use != null) {
select.eq('bed_num_use', query.bed_num_use);
}
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.created_at != null) {
select.gte('created_at', query.created_at);
}
if (query.updated_at != null) {
select.lte('updated_at', query.updated_at);
}
if (query.area_id != null) {
select.eq('area_id', query.area_id);
}
select.eq('deleted', 0);
// 设置分页
select.skip((page - 1) * pageSize).limit(pageSize);
// 设置排序
select.sortBy("created_at"); // 按创建时间排序
// 执行查询
var result = await db.query(
'bus_room_info',
condition: select, // 查询条件 + 分页 + 排序
);
if (result.isEmpty) {
return [];
}
return result;
}
// 获取单个房间信息
Future<Room?> fetchRoomById(int roomId) async {
var db = EasyDartModule.dataBase;
// 构建查询条件
var condition = {'room_id': roomId};
// 执行查询
var result = await db.query(
'bus_room_info',
condition: condition,
);
if (result.isNotEmpty) {
return Room.fromJson(result.first);
}
return null;
}
// 添加房间
Future<bool> insertRoom(Room room) async {
var db = EasyDartModule.dataBase;
// 执行插入操作
var result = await db.insert('bus_room_info', room.toJson());
// 返回是否插入成功
return true;
}
// 更新房间信息
Future<String> updateRoom(String roomId, Room updatedRoom) async {
String message = "";
var db = EasyDartModule.dataBase;
var str = updatedRoom.toJson();
str.remove("_id");
var updateCondition = {
'_id': ObjectId.fromHexString(roomId),
};
try {
// var result = await db.update('bus_room_type', update, condition);
var result = await db.update('bus_room_info', str, updateCondition);
return message; // 返回受影响的行数
} catch (e) {
message = MessageConstants.UPDATE_ERROR;
return message;
}
}
// 删除房间 (逻辑删除)
Future<String> deleteRoom(String roomId) async {
var db = EasyDartModule.dataBase;
// 构建查询条件
var condition = {'room_id': roomId};
String message = "";
// try {
try {
var roomIdList = roomId.split(',');
var objectIdList =
roomIdList.map((id) => ObjectId.fromHexString(id)).toList();
var condition = {
'_id': {'\$in': objectIdList}
}; // 使用 \$in 操作符查询多个 ID
var update = {
'\$set': {'deleted': 1}
}; // 逻辑删除,只更新 deleted 字段
var result = await db.update('bus_room_info', update, condition);
return message; // 返回受影响的行数
} catch (e) {
print("Error during deleteRoomType: $e");
message = MessageConstants.DELETE_ERROR;
return message;
}
}
// 获取房间数量
Future<int> getRoomCount(Room 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_type != null) {
select.eq('room_type', query.room_type);
}
if (query.room_desc != null) {
select.eq('room_desc', query.room_desc);
}
if (query.bed_num != null) {
select.eq('bed_num', query.bed_num);
}
if (query.bed_num_use != null) {
select.eq('bed_num_use', query.bed_num_use);
}
// 执行查询并返回计数
var result = await db.query(
'bus_room_info',
condition: select,
);
return result.length;
}
//办理预约入住
addOrderCheckIn(data) async {
var db = EasyDartModule.dataBase;
// 执行插入操作
var result = await db.insert('bus_room_check_info', data);
// 返回是否插入成功
final pipeline = AggregationPipelineBuilder()
// .addStage(Match(where.eq('type', 1).map['\$query']))
// 关联房间表
.addStage(AddFields({
'data.room_info.id': {'\$toObjectId': '\$data.room_info.id'},
'data.bed_info.id': {'\$toObjectId': '\$data.bed_info.id'}
}))
.addStage(Lookup(
from: 'bus_room_info',
localField: 'data.room_info.id',
foreignField: '_id',
as: 'room_info'))
// 关联床位表
.addStage(Lookup(
from: 'bus_bed_info',
localField: 'data.bed_info.id',
foreignField: '_id',
as: 'bed_info'))
// 选择需要的字段
.addStage(Project({
'_id': 1, // 预约表的 _id
'level': 1, // 预约人姓名
'appointment_date': 1, // 预约时间
'status': 1, // 预约状态
'data': 1,
'room_id': {
'\$arrayElemAt': ['\$room_info._id', 0]
}, // 取房间ID
'room_name': {
'\$arrayElemAt': ['\$room_info.room_name', 0]
}, // 取房间名称
'bed_id': {
'\$arrayElemAt': ['\$bed_info._id', 0]
}, // 取床位ID
'bed_name': {
'\$arrayElemAt': ['\$bed_info.bed_name', 0]
} // 取床位名称
}))
.build();
try {
// final db1 = Db('mongodb://192.168.1.80:27017/test');
const host = "192.168.1.80:27017";
const userName = "root";
const password = "123456";
const dataBase = "test";
// 创建连接字符串
final connectionString =
"mongodb://$userName:$password@$host/$dataBase?authSource=admin";
var db1 = await Db.create(connectionString);
await db1.open();
final result1 = await DbCollection(db1, 'bus_message')
.aggregateToStream(pipeline)
.toList();
print(result1);
} catch (e) {
print(e);
}
return result;
}
//删除预约入住
delOrderCheckIn(data) async {
var db = EasyDartModule.dataBase;
// 构建查询条件
// var condition = {'room_id': roomId};
String message = "";
// try {
try {
var roomIdList = data['id'].split(',');
var objectIdList =
roomIdList.map((id) => ObjectId.fromHexString(id)).toList();
var condition = {
'_id': {'\$in': objectIdList}
}; // 使用 \$in 操作符查询多个 ID
var update = {
'\$set': {'deleted': 1}
}; // 逻辑删除,只更新 deleted 字段
var result = await db.update('bus_room_check_info', update, condition,
multiUpdate: true);
return message; // 返回受影响的行数
} catch (e) {
print("Error during deleteRoomType: $e");
message = MessageConstants.DELETE_ERROR;
return message;
}
}
Future<void> addOrderCheckInRuZhuRecord(data) async {
try {
var db = EasyDartModule.dataBase;
// 执行插入操作
var result = await db.insert('bus_ruzhu_info', data);
} catch (e) {
print(e);
}
}
Future<void> addCheckOutRecord(data) async {
try {
var db = EasyDartModule.dataBase;
// 执行插入操作
var result = await db.insert('bus_checkout_info', data);
} catch (e) {
print(e);
}
}
}