SSM民宿预订系统:入住退房模块开发详解
入住退房模块是民宿预订管理系统中非常重要的一个模块,它关系到整个预订流程的顺畅进行。下面我们就来介绍一下如何基于SSM框架来实现入住退房模块。
- 数据库设计
我们需要在数据库中创建一个表来存储入住退房的相关信息,如下所示:
CREATE TABLE `checkinout` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`room_id` int(11) NOT NULL COMMENT '房间ID',
`user_id` int(11) NOT NULL COMMENT '用户ID',
`checkin_time` datetime NOT NULL COMMENT '入住时间',
`checkout_time` datetime DEFAULT NULL COMMENT '退房时间',
`status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '状态(1:入住中,2:已退房)',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
- 后台实现
在后台实现中,我们需要创建一个CheckInOutController类,该类中包含入住和退房的相关方法。
(1) 入住方法
@RequestMapping(value = "/checkin", method = RequestMethod.POST)
public ResponseEntity<String> checkin(@RequestBody CheckInOut checkInOut) {
try {
// 保存入住信息
checkInOutService.save(checkInOut);
// 更新房间状态
Room room = new Room();
room.setId(checkInOut.getRoomId());
room.setStatus(2);
roomService.update(room);
// 返回成功信息
return ResponseEntity.ok("入住成功!");
} catch (Exception e) {
e.printStackTrace();
// 返回失败信息
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("入住失败,请稍后重试!");
}
}
(2) 退房方法
@RequestMapping(value = "/checkout/{id}", method = RequestMethod.PUT)
public ResponseEntity<String> checkout(@PathVariable("id") Integer id) {
try {
// 根据ID查询入住信息
CheckInOut checkInOut = checkInOutService.getById(id);
// 更新退房时间和状态
checkInOut.setCheckoutTime(new Date());
checkInOut.setStatus(2);
checkInOutService.update(checkInOut);
// 更新房间状态
Room room = new Room();
room.setId(checkInOut.getRoomId());
room.setStatus(1);
roomService.update(room);
// 返回成功信息
return ResponseEntity.ok("退房成功!");
} catch (Exception e) {
e.printStackTrace();
// 返回失败信息
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("退房失败,请稍后重试!");
}
}
- 前台实现
在前台实现中,我们需要在入住和退房页面中添加相应的按钮,以便用户进行操作。具体实现方式如下:
(1) 入住页面
<div class="form-group">
<label for="room_id">房间号</label>
<select class="form-control" id="room_id" name="room_id">
<option value="">请选择房间号</option>
<!-- 动态生成房间号下拉框选项 -->
<c:forEach items="${roomList}" var="room">
<option value="${room.id}">${room.roomNo}</option>
</c:forEach>
</select>
</div>
<div class="form-group">
<label for="user_id">用户ID</label>
<input type="text" class="form-control" id="user_id" name="user_id" placeholder="请输入用户ID">
</div>
<button type="button" class="btn btn-primary" onclick="checkin()">入住</button>
<script>
function checkin() {
var room_id = $("#room_id").val();
var user_id = $("#user_id").val();
// 发送AJAX请求进行入住
$.ajax({
type: "POST",
url: "/checkinout/checkin",
data: JSON.stringify({
roomId: room_id,
userId: user_id
}),
contentType: "application/json;charset=UTF-8",
success: function(data) {
alert(data);
// 刷新页面
location.reload();
},
error: function() {
alert("入住失败,请稍后重试!");
}
});
}
</script>
(2) 退房页面
<table class="table table-bordered">
<thead>
<tr>
<th>房间号</th>
<th>用户ID</th>
<th>入住时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<!-- 动态生成入住信息表格 -->
<c:forEach items="${checkInOutList}" var="checkInOut">
<tr>
<td>${checkInOut.roomNo}</td>
<td>${checkInOut.userId}</td>
<td>${checkInOut.checkinTime}</td>
<td>
<!-- 生成退房按钮 -->
<button type="button" class="btn btn-danger" onclick="checkout(${checkInOut.id})">退房</button>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<script>
function checkout(id) {
// 发送AJAX请求进行退房
$.ajax({
type: "PUT",
url: "/checkinout/checkout/" + id,
success: function(data) {
alert(data);
// 刷新页面
location.reload();
},
error: function() {
alert("退房失败,请稍后重试!");
}
});
}
</script>
至此,基于SSM框架的民宿预订管理系统入住退房模块就实现了。通过这个模块,用户可以方便地进行入住和退房操作,管理员也可以方便地管理房间和入住信息。
原文地址: https://www.cveoy.top/t/topic/nJ6V 著作权归作者所有。请勿转载和采集!