package cn.cwnu.tourism.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.cwnu.tourism.po.MyPlane;
import cn.cwnu.tourism.po.MyRoom;
import cn.cwnu.tourism.po.MyTicket;
import cn.cwnu.tourism.po.Scenic;
import cn.cwnu.tourism.po.Ticket;
import cn.cwnu.tourism.po.User;
import cn.cwnu.tourism.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
/**
* 向用户登录页面跳转
*/
@RequestMapping(value="/toLogin")
public String toLogin() {
return "login";
}
/**
* 向用户注册页面跳转
*/
@RequestMapping(value="/toRegister")
public String toRegister() {
return "register";
}
/**
* 向用户主页面跳转
*/
@RequestMapping(value="/toMain")
public String toMain(Model model) {
/*数据库获取主页面的景点信息(只获取8个)*/
//List<Scenic> scenicList = userService.getPicture();
// 查询出所有景点票信息(只获取8个)
List<Ticket> listTicket = userService.getTicket();
List<Scenic> scenicList = new ArrayList();
//由获取到的门票信息查询景点信息(只获取8个)
for (int i = 0; i < listTicket.size(); i++) {
//由门票名查询景点名
Scenic scenic = userService.getScenicByTicket(listTicket.get(i).getTicketName());
scenic.setTicket(listTicket.get(i));
//System.out.println(scenic);
scenicList.add(scenic);
}
model.addAttribute("data", scenicList);
/*通过遍历的景点名获取到景点的门票价钱*/
return "index";
}
/*验证注册用户是否已经存在*/
@RequestMapping(value = "/isRegister")
@ResponseBody
public String isRegister(String username){
//获得要校验的用户名
String userName = username;
//传递username到service
boolean isExist=userService.checkUsername(userName);
return "{\"isExist\":"+isExist+"}";
}
/*实现新用户注册*/
@RequestMapping(value = "/register")
public String insetUser(String username,String password,HttpSession session,Model model){
//System.out.println(username);
boolean isExist=userService.checkUsername(username);
if(isExist){
//用户存在,则注册失败
model.addAttribute("msg", "用户名已经存在,请更改用户名!");
return "forward:/toRegister";
}else {
//新用户则注册成功,并在后台记录
User user = new User();
user.setUserName(username);
user.setPassword(password);
userService.insertUser(user);
session.setAttribute("USER_SESSION", username);
//return "redirect:index.jsp";
return "redirect:/toMain";
}
}
/*退出登录*/
@RequestMapping(value = "/outLogin")
public String outLogin(HttpSession session){
// 清除Session
session.invalidate();
// 重定向到登录页面的跳转方法
//return "redirect:index.jsp";
return "redirect:/toMain";
}
/*用户登录*/
/*验证用户登录是否正确*/
@RequestMapping(value = "/login")
public String login(String username,String password,Model model,HttpSession session) {
// 获取用户名和密码,并包装成用户User
User user = new User();
user.setUserName(username);
user.setPassword(password);
// 从数据库中获取用户名和密码后进行判断
int row = userService.selectUser(user);
//System.out.println(row);
if(row >= 1){
// 将用户对象添加到Session
session.setAttribute("USER_SESSION", username);
// 重定向到主页面的跳转方法
//return "redirect:index.jsp";
return "redirect:/toMain";
}
model.addAttribute("msg", "用户名或密码错误,请重新登录!");
return "forward:/toLogin";
}
/*从此处实现用户管理界面*/
@RequestMapping(value = "/showUser")
public String showUser(HttpSession session, Model model){
/*试试用request获取从充值controller方法中重定向传来的值
充值成功的提示信息
String msg = (String)(request.getAttribute("msg"));*/
String userName = (String)session.getAttribute("USER_SESSION");
/*根据当前登录用户名查询myRoom,myTicket,myPlane,user表的信息,并传送回user.jsp页面*/
List<MyRoom> listRoom = userService.selectMyRoomByUserName(userName);
int roomNumber = listRoom.size();
List<MyTicket> listTicket = userService.selectMyTicketByUserName(userName);
int ticketNumber = listTicket.size();
List<MyPlane> listPlane = userService.selectMyPlaneByUserName(userName);
int planeNumber = listPlane.size();
User user = userService.selectUserByUserName(userName);
Integer money = user.getUserMoney();
/*信息送回前台*/
model.addAttribute("roomNumber", roomNumber);
model.addAttribute("ticketNumber", ticketNumber);
model.addAttribute("planeNumber", planeNumber);
model.addAttribute("money", money);
/*充值成功的提示信息
model.addAttribute("msg", msg);*/
return "user/user";
}
/*user界面,通过展开按钮异步通过Ajax返回局部更新的信息*/
@RequestMapping(value = "/showMyPlane")
@ResponseBody
public List<MyPlane> showMyPlane(HttpSession session){
String userName = (String)session.getAttribute("USER_SESSION");
/*根据当前登录用户名查询myPlane表的信息,并传送回user.jsp页面*/
List<MyPlane> listPlane = userService.selectMyPlaneByUserName(userName);
return listPlane;
}
@RequestMapping(value = "/showMyTicket")
@ResponseBody
public List<MyTicket> showMyTicket(HttpSession session){
String userName = (String)session.getAttribute("USER_SESSION");
/*根据当前登录用户名查询myTicket表的信息,并传送回user.jsp页面*/
List<MyTicket> listTicket = userService.selectMyTicketByUserName(userName);
return listTicket;
}
@RequestMapping(value = "/showMyRoom")
@ResponseBody
public List<MyRoom> showMyRoom(HttpSession session){
String userName = (String)session.getAttribute("USER_SESSION");
/*根据当前登录用户名查询myRoom表的信息,并传送回user.jsp页面*/
List<MyRoom> listRoom = userService.selectMyRoomByUserName(userName);
return listRoom;
}
/*充值页面*/
@RequestMapping(value = "/moneyRecharge")
public String moneyRecharge(Integer money, Model model){
Integer nMoney = money;
//充值前的余额
model.addAttribute("money", nMoney);
return "user/user_recharge";
}
/*充值*/
@RequestMapping(value = "/moneyChage")
public String moneyChage(Integer userMoney, HttpSession session){
/*本来的钱,余额
System.out.println(money);//并没有从user_recharge.jsp页面传过来
准备充值的钱
System.out.println(userMoney);*/
String userName = (String)session.getAttribute("USER_SESSION");
/*根据用户名查询余额*/
User user = userService.selectUserByUserName(userName);
Integer money = user.getUserMoney();
/*改变用户余额*/
user.setUserMoney(money+userMoney);
/*更改用户(余额)*/
userService.updateUserMoney(user);
//request.setAttribute("msg", "充值成功!");
/*想办法跳转回另一个controller方法*/
//@RequestMapping(value = "/showUser")
return "redirect:/showUser";
}
/*退订一张机票*/
@RequestMapping(value = "/updateOnePlane")
@ResponseBody
public String updateOnePlane(String myPlaneId, HttpSession session){
/*通过航班订单号删除(修改)一条记�
评论0