package hotel.controller;
import hotel.model.Order;
import hotel.model.OrderInfo;
import hotel.model.Room;
import hotel.model.User;
import hotel.service.RoomService;
import hotel.service.UserService;
import hotel.utils.Misc;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
*
* 类说明
*
* @author lipeiying
* @version 创建时间:2011-12-31 下午02:46:37
*/
@Controller
public class HotelController {
@Resource(name = "roomService")
private RoomService roomService;
@Resource(name = "userService")
private UserService userService;
@RequestMapping
String index(HttpServletRequest request) {
return login(request);
}
/**
* 用户登陆
*
* @param request
* @return
* @throws Exception
*/
@RequestMapping
String login(HttpServletRequest request) {
String username = request.getParameter("username");
String password = request.getParameter("password");
String role = request.getParameter("role");
int i = Misc.toInt(role, -1);
if (null == username || null == password) {
return "login";
}
User user = userService.login(username.trim(), password.trim(), i);
if (null == user) {
request.setAttribute("errMsg",
"用户名不存在或账户已冻结,登录失败!请确认用户名输入正确,或联系管理员!");
return "login";
}
request.getSession().setAttribute("userLogin", user);
if (User.ADMINISTRATOR == i) {
return manager(request);
} else {
return customer(request);
}
}
/**
* 退出登录
*
* @param request
* @return
*/
@RequestMapping
String logout(HttpServletRequest request) {
request.getSession().setAttribute("userLogin", null);
return "login";
}
/**
* 用户注册
*
* @param request
* @return
* @throws UnsupportedEncodingException
* @throws IOException
*/
@RequestMapping
String register(HttpServletRequest request)
throws UnsupportedEncodingException {
// request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String telephone = request.getParameter("telephone");
if (null == username || null == password || null == telephone) {
return "register";
}
if (userService.isExist(username)) {
request.setAttribute("errMsg", "注册失败!可能该用户名已经存在哦,亲!");
return "register";
}
User user = new User(username, password, telephone, User.CUSTOMER);
userService.createUser(user);
request.setAttribute("errMsg", "恭喜您注册成功啊,亲!");
request.getSession().setAttribute("userLogin", user);
return "customer";
}
/**
* 管理员主页
*
* @param request
* @return
*/
@RequestMapping
String manager(HttpServletRequest request) {
User user = (User) request.getSession().getAttribute("userLogin");
if (null == user || User.CUSTOMER == user.getType()) {
return "login";
}
String username = request.getParameter("username");
List<Order> orders0 = userService.listOrder(username);
List<Order> orders = userService.listOrder();
request.setAttribute("orders0", orders0);
request.setAttribute("orders", orders);
return "manager";
}
/**
* 用户主页
*
* @param request
* @return
*/
@RequestMapping
String customer(HttpServletRequest request) {
User user = (User) request.getSession().getAttribute("userLogin");
if (null == user || User.CUSTOMER != user.getType()) {
return "login";
}
List<Order> list = userService.listOrder(user.getUsername());
request.setAttribute("orders", list);
return "customer";
}
/**
* 房间管理
*
* @param request
* @return
*/
@RequestMapping
String managerroom(HttpServletRequest request) {
User user = (User) request.getSession().getAttribute("userLogin");
if (null == user || User.CUSTOMER == user.getType()) {
return "login";
}
String op = request.getParameter("op");
if ("delete".equals(op)) {
String roomid = request.getParameter("roomid");
if (roomService.deleteRoom(roomid))
request.setAttribute("errMsg", "删除成功!");
else
request.setAttribute("errMsg", "删除失败!");
}
List<Room> list = roomService.listRoom();
request.setAttribute("rooms", list);
return "managerroom";
}
/**
* 修改房间
*
* @param request
* @return
*/
@RequestMapping
String managerupdate(HttpServletRequest request) {
User user = (User) request.getSession().getAttribute("userLogin");
if (null == user || User.CUSTOMER == user.getType()) {
return "login";
}
String proomid = request.getParameter("proomid");
String roomid = request.getParameter("roomid");
Float price = Misc.toFloat(request.getParameter("price"), 0);
int beds = Misc.toInt(request.getParameter("beds"), 0);
int styles = Misc.toInt(request.getParameter("styles"), 0);
if (null == proomid) {
Room r = roomService.getRoom(roomid);
request.setAttribute("room", r);
return "managerupdate";
}
Room room = new Room();
room.setRoomid(roomid);
room.setPrice(price);
room.setBeds(beds);
room.setStyles(styles);
if (roomService.updateRoom(proomid, room))
request.setAttribute("errMsg", "房间信息修改成功!");
else
request.setAttribute("errMsg", "房间信息修改失败!");
return managerroom(request);
}
/**
* 添加房间
*
* @param request
* @return
*/
@RequestMapping
String managercreate(HttpServletRequest request) {
User user = (User) request.getSession().getAttribute("userLogin");
if (null == user || User.CUSTOMER == user.getType()) {
return "login";
}
String roomid = request.getParameter("roomid");
Float price = Misc.toFloat(request.getParameter("price"), 0);
int beds = Misc.toInt(request.getParameter("beds"), 0);
int styles = Misc.toInt(request.getParameter("styles"), 0);
if (null == roomid) {
return "managercreate";
}
Room room = new Room();
room.setRoomid(roomid);
room.setPrice(price);
room.setBeds(beds);
room.setStyles(styles);
room.setStatus(Room.AVAILABLE);
if (roomService.createRoom(room))
request.setAttribute("errMsg", "房间添加成功!");
else
request.setAttribute("errMsg", "房间添加失败,可能房间已存在!");
return managerroom(request);
}
/**
* 查询房间状态
*
* @param request
* @return
*/
@RequestMapping
String managercheck(HttpServletRequest request) {
User user = (User) request.getSession().getAttribute("userLogin");
if (null == user) {
return "login";
}
String roomid = request.getParameter("roomid");
if (null != roomid) {
Room room = roomService.getRoom(roomid);
request.setAttribute("room", room);
}
List<Room> list = roomService.listRoom();
request.setAttribute("rooms", list);
return "managercheck";
}
/**
* 新建订单
*
* @param request
* @return
* @throws IOException
*/
@RequestMapping
String addorder(HttpServletRequest request, HttpServletResponse response)
throws IOException {
User user = (User) request.getSession().getAttribute("userLogin");
if (null == user || User.CUSTOMER != user.getType()) {
return "login";
}
userService.createOrder(user);
response.sendRedirect("customer.do");
return null;
}
/**
* 查看订单明细
*
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping
String vieworder(HttpServletRequest request, HttpServletResponse response)
throws IOException {
User user = (User) request.getSession().getAttribute("userLogin");
if (null == user || User.CUSTOMER != user.getType()) {
return "login";
}
int orderid = Misc.toInt(request.getParameter("orderid"), -1);
String[] roomids = request.getParameterValues("roomids");
// 取消预订
if (null != roomids) {
if (!userService.isOrderCanChange(orderid)) {
request.setAttribute("errMsg", "改订单不能被操作!");
} else {
for (String roomid : roomids) {
userService.cancelRoom(orderid, roomid);
}
}
}
List<OrderInfo> list = userService.listOrderInfo(orderid);
Order order = userService.getOrder(orderid);
for (OrderInfo info : list) {
Room room = roomService.getRoom(info.getRoomNumber());
if (null != room)
info.setRoom(room);
}
request.setAttribute("order", order);
request.setAttribute("rooms", list);
return "vieworder";
}
/**
* 预定房间
*
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping
String add(HttpServletRequest request, HttpServletResponse response)
throws IOException {
User user = (User) request.getSession().getAttribute("userLogin");
if (null == user || User.CUSTOMER != user.getType()) {
return "login";
}
String orderid = request.getParameter("orderid");
String roomid = request.getParameter("roomid");
String days = request.getParameter("days");
int orderidI = Misc.toInt(orderid, -1);
int daysI = Misc.toInt(days, -1);
if (!userService.isOrderCanChange(orderidI)) {
request.setAttribute("orderid", orderid);
request.setAttribute("errMsg", "改订单不能被操作!");
return vieworder(request, response);
}
if (-1 == daysI) {
request.setAttribute("orderid", orderid);
return "add";
}
if (!roomService.isAvailable(roomid)) {
request.setAttribute("orderid", orderid);
request.setAttribute("errMsg", "房间已经被预订或者房间不存在!");
return "add";
}
if (!userService.bookRoom(orderidI, roomid, daysI)) {
request.setAttribute("orderid", orderid);
request.setAttribute("errMsg", "预订失败!");
return "add";
} else {
request.setAttribute("errMsg", "预订成功!");
return vieworder(request, response);
}
}
/**
* 提交订单
*
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping
String submitorder(HttpServletRequest request, HttpServletResponse response)
throws IOException {
User user = (User) request.getSession().getAttribute("userLogin");
if (null == user || User.CUSTOMER != user.getType()) {
return "login";
}
int orderid = Misc.toInt(request.getParameter("orderid"), -1);
if (!userService.isOrderCanChange(orderid)) {
request.setAttribute("errMsg", "订单状态不能改变!");
return vieworder(request, response);
}
if (userService.submitOrder(orderid)) {
request.setAttribute("errMsg", "提交成功!");
} else {
request.setAttribute("errMsg", "提交失败!");
}
return customer(request);
}
/**
* 取消订单
*
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping
String cancelorder(HttpServletRequest request, HttpServletResponse response)
throws IOException {
User user = (User) request.getSession().getAttribute("userLogin");
if (null == user || User.CUSTOMER != user.getType()) {
return "login";
}
int orderid = Misc.toInt(request.getParameter("orderid"), -1);
if (!userService.isOrderCanChange(orderid)) {
request.setAttribute("errMsg", "订单状态不能改变!");
return vieworder(request, response);
}
if (userService.cancelOrder(orderid)) {
request.setAttribute("errMsg", "取消订单成功!");
} else {
request.setAttribute("errMsg", "取消订单失败!");
}
return customer(request);
}
/**
* 管理员新建订单
*
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping
String manageradd(HttpServletRequest request, HttpServletResponse response)
throws IOException {
User user = (User) request.getSession().getAttribute("userLogin");
if (null == user || User.CUSTOMER == user.getType()) {
return "login";
}
String username = request.getParameter("username");
String telephone = request.getParameter("telephone");
if (null == username || null == telephone) {
return "manageradd";
}
User user1 = new User();
user1.setUsername(username);
user1.setTelephone(telephone);
if (!userService.isExist(username)) {
request.setAttribute("errMsg", "该用户不存在!");
return "manageradd";
}
userService.createOrder(user1);
return manager(request);
}
/**
* 管理员查看订单明细
*
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping
String managerview(HttpServletRequest request, HttpServletResponse response)
throws IOException {
User user = (User) request.getSession().getAttribute("userLogin");
if (null == user || User.CUSTOMER == user.getType()) {
return "login";
}
int orderid = Misc.toInt(request.getParameter("orderid"), -1);
String op = request.getParameter("op");
String[] roomids = request.getParameterValues("roomids");
// 取消预订
if (null != roomids) {
if ("cancel".equals(op)) {// 取消预订
if (!userService.isOrderCanChange(orderid)) {
request.setAttribute("errMsg", "该订单不能被操作!");
} else {
for (String roomid : roomids) {
userService.cancelRoom(orderid, roomid);
}
}
} else if ("checkIn".equals(op)) {// 确认入住
boolean flag = true;
for (String roomid : roomids) {
Room r = roomService.getRoom(roomid);
if (r.getStatus() != Room.RESERVE) {
flag = false;
}
if (!flag) {
request.setAttribute("errMsg", "只有已经预订的房间才能入住!");
break;
}
}
if (flag) {
for (String roomid : roomids) {
userService.checkIn(orderid, roomid);
}
request.setAttribute("errMsg", "入住成功");
}
} else if ("checkOut".equals(op)) {// 退房,只有预订中和已入住的房间才能退房,退房后结算金额
boolean flag = true;
for (String roomid : roomids) {
Room r = roomService.getRoom(roomid);
if (r.getStatus() != Room.RESERVE
&& r.getStatus() != Room.CHECKIN) {
flag = false;
}
if (!flag) {
request.setAttribute("errMsg", "只有预订中和已入住的房间才能退房");
break;
}
}
if (flag) {
float totalprices = 0;
for (String roomid : roomids) {
userService.checkOut(orderid, roomid);
List<OrderInfo> infos = userService
.listOrderInfo(orderid);
for (OrderInfo info : infos) {
if (info.getRoomNumber().equals(roomid))
totalprices += info.getTotalprices();
}
}
request.setAttribute("errMsg", "退房成功,应付金额为:" + totalprices
+ "元!");
}
}
}
List<OrderInfo> list = userService.listOrderInfo(orderid);
Order order = userService.getOrder(orderid);
for (OrderInfo info : list) {
Room room = roomService.getRoom(info.getRoomNumber());
if (null != room)
info.setRoom(room);
}
request.setAttribute("order", order);
request.setAttribute("rooms", list);
return "managerview";
}
/**
* 预定房间
*
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping
String managerbook(HttpServletRequest request, HttpServletResponse response)
throws IOException {
User user = (User) request.getSession().getAttribute("userLogin");
if (null == user || User.CUSTOMER == user.getType()) {
return "managerbook";
}
String orderid = request.getParameter("orderid");
String roomid = request.getParameter("roomid");
String days = request.getParameter("days");
int orderidI = Misc.toInt(orderid, -1);
int daysI = Misc.toInt(days, -1);
if (!userService.isOrderCanChange(orderidI)) {
request.setAttribute("orderid", orderid);
request.setAttribute("errMsg", "改订单不能被操作!");
return managerview(request, response);
}
if (-1 == daysI) {
request.setAttribute("orderid", orderid);
return "managerbook";
}
if (!roomService.isAvailable(roomid)) {
request.setAttribute("orderid", orderid);
request.setAttribute("errMsg", "房间已经被预订或者房间不存在!");
return "managerbook";
}
if (!userService.bookRoom(orderidI, roomid, daysI)) {
request.setAttribute("orderid", orderid);
request.setAttribute("errMsg", "预订失败!");
return "managerbook";
} else {
request.setAttribute("errMsg", "预订成功!");
return managerview(request, response);
}
}
/**
* 管理员取消订单
*
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping
String managercancelorder(HttpServletRequest request,
HttpServletResponse response) throws IOException {
User user = (User) request.getSession().getAttribute("userLogin");
if (null == user || User.CUSTOMER == user.getType()) {
return "login";
}
int orderid = Misc.toInt(request.getParameter("orderid"), -1);
if (!userService.isOrderCanChange(orderid)) {
request.setAttribute("errMsg", "订单状态不能改变!");
return managerview(request, response);
}
if (userService.cancelOrder(orderid)) {
request.setAttribute("errMsg", "取消订单成功!");
} else {
request.setAttribute("errMsg", "取消订单失败!");
}
return managerview(request, response);
}
/**
* 管理员提交订单
*
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping
String managersubmitorder(HttpServletRequest request,
HttpServletResponse response) throws IOException {
User user = (User) request.getSession().getAttribute("userLogin");
if (null == user || User.CUSTOMER == user.getType()) {
return "login";
}
int orderid = Misc.toInt(request.getParameter("orderid"), -1);
if (!userService.isOrderCanChange(orderid)) {
request.setAttribute("errMsg", "订单状态不能改变!");
return managerview(request, response);
}
if (userService.submitOrder(orderid)) {
request.setAttribute("errMsg", "提交成功!");
} else {
request.setAttribute("errMsg", "提交失败!");
}
return managerview(request, response);
}
/**
* 直接入住
*
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping
String managerdirectin(HttpServletRequest request,
HttpServletResponse response) throws IOException {
User user = (User) request.getSession().getAttribute("userLogin");
if (null == user || User.CUSTOMER == user.getType()) {
return "login";
}
String orderid = request.getParameter("orderid");
String roomid = request.getParameter("roomid");
int days = Misc.toInt(request.getParameter("days"), -1);
if (null == roomid || -1 == days) {
request.setAttribute("orderid", orderid);
return "managerdirectin";
} else {
if (!roomService.isAvailable(roomid)) {
request.setAttribute("errMsg",
"该房间已经被预订或者已经有人入住或者订单里面已经存在这个房间!");
request.setAttribute("orderid", orderid);
return "managerdirectin";
}
List<OrderInfo> o = userService.listOrderInfo(Misc.toInt(orderid,
-1));
boolean flag = true;
for (OrderInfo i : o) {
if (i.getRoomNumber().equals(roomid))
flag = false;
}
if (!flag) {
request.setAttribute("errMsg", "该房间已经被预订或者已经有人入住!");
request.setAttribute("orderid", orderid);
return "managerdirectin";
}
userService.directIn(Misc.toInt(orderid, -1), roomid, days);
return managerview(request, response);
}
}
/**
* 结束订单
*
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping
String manageroverorder(HttpServletRequest request,
HttpServletResponse response) throws IOException {
User user = (User) request.getSession().getAttribute("userLogin");
if (null == user || User.CUSTOMER == user.getType()) {
return "login";
}
int orderid = Misc.toInt(request.getParameter("orderid"), -1);
List<OrderInfo> infos = userService.listOrderInfo(orderid);
boolean flag = true;
for (OrderInfo info : infos) {
if (info.getAction() != OrderInfo.CHECKOUT)
flag = false;
}
if (flag) {
userService.overOrder(orderid);
request.setAttribute("errMsg", "成功结束订单!");
return managerview(request, response);
} else {
request.setAttribute("errMsg", "操作失败,请先把房间全退了才能结束订单!");
return managerview(request, response);
}
}
}