337 lines
11 KiB
Dart
337 lines
11 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:EasyDartModule/EasyDartModule.dart';
|
|
|
|
import '../model/Room.dart';
|
|
import '../repository/RoomRepository.dart';
|
|
import '../service/BedService.dart';
|
|
import '../service/PersonService.dart';
|
|
|
|
import '../model/Bed.dart';
|
|
import '../model/Person.dart';
|
|
|
|
import '../const/ServiceConstant.dart';
|
|
import '../const/MessageType.dart';
|
|
import '../const/RuzhuMessageType.dart';
|
|
import '../const/Constants.dart';
|
|
import '../enum/BedStatus.dart';
|
|
import '../enum/MessageStatus.dart';
|
|
|
|
class RoomService {
|
|
final RoomRepository roomRepository = RoomRepository();
|
|
final BedService bedService = BedService();
|
|
final PersonService personService = PersonService();
|
|
|
|
RoomService();
|
|
|
|
// 获取房间列表
|
|
Future<List> getRoomList(Room query,
|
|
{int page = 1, int pageSize = 10,bool fill = true,}) async {
|
|
var roomList = await roomRepository.fetchRoomList(query,
|
|
page: page, pageSize: pageSize);
|
|
if (roomList != null && roomList.isNotEmpty && fill) {
|
|
await fillRoomInfo(roomList);
|
|
}
|
|
return roomList;
|
|
}
|
|
|
|
// 获取房间数量
|
|
Future<int> getRoomCount(Room query) async {
|
|
return await roomRepository.getRoomCount(query);
|
|
}
|
|
|
|
// 添加房间
|
|
Future<String> addRoom(Room room) async {
|
|
if (room.room_type == null || room.room_type!.isEmpty) {
|
|
return "请选择房间类型";
|
|
}
|
|
int currentTime = DateTime.now().millisecondsSinceEpoch;
|
|
room.created_at = currentTime;
|
|
room.deleted = 0;
|
|
await roomRepository.insertRoom(room);
|
|
return "";
|
|
}
|
|
|
|
// 更新房间
|
|
Future<String> updateRoom(String roomId, Room updatedRoom) {
|
|
int currentTime = DateTime.now().millisecondsSinceEpoch;
|
|
updatedRoom.updated_at = currentTime;
|
|
return roomRepository.updateRoom(roomId, updatedRoom);
|
|
}
|
|
|
|
// 删除房间
|
|
Future<String> deleteRoom(String roomId) async {
|
|
if (roomId == null || roomId.isEmpty) {
|
|
return "ID不能为空";
|
|
}
|
|
return await roomRepository.deleteRoom(roomId);
|
|
}
|
|
|
|
//办理预约入住
|
|
addOrderCheckIn(data, Map<String, dynamic> jwt) {
|
|
int currentTime = DateTime.now().millisecondsSinceEpoch;
|
|
data['create_at'] = currentTime;
|
|
data['deleted'] = 0;
|
|
data['create_user_id'] = jwt['uid'];
|
|
return roomRepository.addOrderCheckIn(data);
|
|
}
|
|
|
|
//删除预约入住
|
|
delOrderCheckIn(data) {
|
|
return roomRepository.delOrderCheckIn(data);
|
|
}
|
|
|
|
//填充床位信息
|
|
Future<String> fillRoomInfo(List roomList) async {
|
|
try {
|
|
if (roomList == null || roomList.isEmpty) {
|
|
return "";
|
|
}
|
|
for (var item in roomList) {
|
|
String roomId = item['_id'].toHexString();
|
|
//根据roomid查询床位数
|
|
Bed bedQuery = Bed();
|
|
bedQuery.room_id = roomId;
|
|
bedQuery.limit = Constants.default_limit_max.toString();
|
|
List bedList = await bedService.getBedList(bedQuery);
|
|
if (bedList == null || bedList.isEmpty) {
|
|
item['bed_count'] = 0; //总床位数
|
|
item['bed_use_count'] = 0; //可使用床位数
|
|
} else {
|
|
item['bed_count'] = bedList.length;
|
|
//根据床位id查询使用数
|
|
try {
|
|
String bedIdsString = bedList
|
|
.map((bed) {
|
|
var id = bed['_id'];
|
|
return id is ObjectId ? id.toHexString() : id.toString();
|
|
}) // 提取 _id 字段并转换为字符串
|
|
.where((id) => id != null) // 过滤掉 null 的值
|
|
.join(','); // 用逗号连接成一个字符串
|
|
Person personQuery = Person();
|
|
personQuery.bed_ids = bedIdsString;
|
|
personQuery.limit = Constants.default_limit_max.toString();
|
|
var persons = await personService.getPersonList(personQuery,
|
|
page: 1, pageSize: Constants.default_limit_max);
|
|
if (persons == null || persons.isEmpty) {
|
|
item['bed_use_count'] = item['bed_count'];
|
|
} else {
|
|
item['bed_use_count'] = item['bed_count'] - persons.length;
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
}
|
|
}
|
|
}
|
|
return "";
|
|
} catch (e) {
|
|
print(e);
|
|
return "";
|
|
}
|
|
}
|
|
|
|
//添加预约消息
|
|
Future<void> addReservationMessage(data, Map<String, dynamic> jwt) async {
|
|
//todo 根据jwt取登录用户信息
|
|
data['ruzhu_msg_type'] = RuzhuMessageType.reservation.code;
|
|
String serviceAddress = ServiceConstant.service_address;
|
|
String serviceName = ServiceConstant.message_service;
|
|
String serviceApi = ServiceConstant.addMessage;
|
|
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
|
var createTime = DateTime.now().millisecondsSinceEpoch;
|
|
var status = MessageStatus.notify.code; //消息状态 0:无需处理 1:待处理 2:已处理
|
|
var info = {
|
|
'room_id': data['room_id'],
|
|
'bed_id': data['bed_id'],
|
|
'start_time': data['start_time'].toString(),
|
|
'end_time': data['end_time'].toString(),
|
|
'rel_name': data['rel_name'],
|
|
'ship': data['ship'],
|
|
'ruzhu_msg_type': data['ruzhu_msg_type'],
|
|
'create_user_id': jwt['uid'],
|
|
'bed_name':data['bed_name'],
|
|
'room_name':data['room_name'],
|
|
};
|
|
var pushData = {
|
|
'level': 1,
|
|
'createTime': createTime,
|
|
'type': MessageType.check_in.code,
|
|
'status': status,
|
|
'tid': jwt['tid'],
|
|
'data': info,
|
|
};
|
|
try {
|
|
var res =
|
|
await EasyDartModule.dio.post(queryUrl, data: jsonEncode(pushData));
|
|
print(res);
|
|
} catch (e) {
|
|
print(e);
|
|
}
|
|
}
|
|
|
|
//办理入住信息
|
|
addRuzhuCheckIn(data, Map<String, dynamic> jwt) async {
|
|
//添加人员信息
|
|
//todo 如果是已有人员信息??
|
|
int currentTime = DateTime.now().millisecondsSinceEpoch;
|
|
Person person = Person();
|
|
person.room_id = data['room_id'];
|
|
person.room_name = data['room_name'];
|
|
person.bed_id = data['bed_id'];
|
|
person.bed_name = data['bed_name'];
|
|
person.check_in_start_time = data['start_time'];
|
|
person.check_in_end_time = data['end_time'];
|
|
person.contact_name = data['rel_name'];
|
|
person.person_name = data['person_name'];
|
|
person.gender = data['gender'];
|
|
person.ethnicity = data['minzu'];
|
|
person.person_type_id = data['person_type'];
|
|
person.person_type_name = data['person_type_name'];
|
|
person.id_card_number = data['cardNo'];
|
|
person.phone_number = data['phone'];
|
|
person.service_level = data['service_level'];
|
|
person.height = data['height'];
|
|
person.weight = data['weight'];
|
|
person.tid = jwt['tid'];
|
|
person.level = jwt['level'];
|
|
person.age = data['age'];
|
|
person.desc = data['desc'];
|
|
person.contact_relationship = data['ship'];
|
|
person.contact_phone = data['tel'];
|
|
person.oid = jwt['tid'];
|
|
person.data_level = jwt['level'];
|
|
personService.addPerson(person);
|
|
//更改床位状态
|
|
await bedService.updateBedStatus(
|
|
data['bed_id'], BedStatus.using.code, person);
|
|
//发送入住消息
|
|
addRuzhuMessage(data, jwt);
|
|
//添加入住记录
|
|
addRuzhuRecord(data, jwt);
|
|
}
|
|
|
|
//添加入住消息
|
|
Future<void> addRuzhuMessage(data, Map<String, dynamic> jwt) async {
|
|
data['ruzhu_msg_type'] = RuzhuMessageType.check_in.code;
|
|
data['create_user_id'] = jwt['uid'];
|
|
data['start_time'] = data['start_time'].toString();
|
|
data['end_time'] = data['end_time'].toString();
|
|
String serviceAddress = ServiceConstant.service_address;
|
|
String serviceName = ServiceConstant.message_service;
|
|
String serviceApi = ServiceConstant.addMessage;
|
|
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
|
var createTime = DateTime.now().millisecondsSinceEpoch;
|
|
var status = MessageStatus.notify.code; //消息状态 0:无需处理 1:待处理 2:已处理
|
|
// var info = {
|
|
// 'room_id': data['room_id'],
|
|
// 'bed_id': data['bed_id'],
|
|
// 'start_time': data['start_time'],
|
|
// 'end_time': data['end_time'],
|
|
// 'rel_name': data['rel_name'],
|
|
// 'ship': data['ship'],
|
|
// 'ruzhu_msg_type': data['ruzhu_msg_type'],
|
|
// };
|
|
var pushData = {
|
|
'level': 1,
|
|
'createTime': createTime,
|
|
'type': MessageType.check_in.code,
|
|
'status': status,
|
|
'tid': jwt['tid'],
|
|
'data': data,
|
|
};
|
|
try {
|
|
var res =
|
|
await EasyDartModule.dio.post(queryUrl, data: jsonEncode(pushData));
|
|
print(res);
|
|
} catch (e) {
|
|
print(e);
|
|
}
|
|
}
|
|
|
|
//办理退住
|
|
checkout(data, Map<String, dynamic> jwt) async {
|
|
// 1.更新人员信息 当前正在该床位入住的人员??有什么需要更新,正在入住的房间,床位?
|
|
// 2.添加退住记录
|
|
// 3.发送退住消息
|
|
// 4.更新床位状态
|
|
String bedId = data['bed_id'];
|
|
data['create_user_id'] = jwt['uid'];
|
|
//更新床位状态
|
|
Person person = Person();
|
|
await bedService.updateBedStatus(bedId, BedStatus.no_using.code, person);
|
|
person.bed_id = bedId;
|
|
List personlist = await personService.getPersonList(person);
|
|
try {
|
|
if (personlist != null && personlist.isNotEmpty) {
|
|
var personSelect = personlist[0];
|
|
// 添加退住记录
|
|
await addCheckOutRecord(data, jwt, personSelect);
|
|
//发送退住消息
|
|
await addCheckOutMessage(personSelect, jwt,data);
|
|
//更新人员信息
|
|
await personService.updatePerson(
|
|
personSelect['_id'].toHexString(), person);
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
}
|
|
}
|
|
|
|
//添加入住记录
|
|
void addRuzhuRecord(data, Map<String, dynamic> jwt) {
|
|
//问题 记录人员入住,怎么查询人员入住历史
|
|
int currentTime = DateTime.now().millisecondsSinceEpoch;
|
|
data['create_at'] = currentTime;
|
|
data['deleted'] = 0;
|
|
data['create_user_id'] = jwt['uid'];
|
|
roomRepository.addOrderCheckInRuZhuRecord(data);
|
|
}
|
|
|
|
//添加退住消息
|
|
Future<void> addCheckOutMessage(data, Map<String, dynamic> jwt, bedInfo) async {
|
|
Map info = {};
|
|
info['ruzhu_msg_type'] = RuzhuMessageType.check_out.code;
|
|
info['bed_id'] = bedInfo['bed_id'];
|
|
info['bed_name'] = bedInfo['bed_name'];
|
|
info['room_name'] = bedInfo['room_name'];
|
|
info['room_id'] = bedInfo['room_id'];
|
|
info['person_id'] = data['_id'].toHexString();
|
|
info['person_name'] = data['person_name'];
|
|
String serviceAddress = ServiceConstant.service_address;
|
|
String serviceName = ServiceConstant.message_service;
|
|
String serviceApi = ServiceConstant.addMessage;
|
|
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
|
var createTime = DateTime.now().millisecondsSinceEpoch;
|
|
var status = MessageStatus.notify.code; //消息状态 0:无需处理 1:待处理 2:已处理
|
|
var pushData = {
|
|
'level': 1,
|
|
'createTime': createTime,
|
|
'type': MessageType.check_in.code,
|
|
'status': status,
|
|
'tid': jwt['tid'],
|
|
'data': info,
|
|
};
|
|
try {
|
|
var res =
|
|
await EasyDartModule.dio.post(queryUrl, data: jsonEncode(pushData));
|
|
print(res);
|
|
} catch (e) {
|
|
print(e);
|
|
}
|
|
}
|
|
|
|
//添加退住记录
|
|
Future<void> addCheckOutRecord(
|
|
data, Map<String, dynamic> jwt, personSelect) async {
|
|
//问题 记录人员入住,怎么查询人员入住历史
|
|
int currentTime = DateTime.now().millisecondsSinceEpoch;
|
|
data['create_at'] = currentTime;
|
|
data['deleted'] = 0;
|
|
data['create_user_id'] = jwt['uid'];
|
|
data['person_id'] = personSelect['_id'];
|
|
data['person_name'] = personSelect['person_name'];
|
|
//查询当前使用该床位的人员
|
|
await roomRepository.addCheckOutRecord(data);
|
|
}
|
|
}
|