package cn.railway.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.railway.entity.OrderDetail;
import cn.railway.entity.TblOrder;
import cn.railway.entity.User;
import cn.railway.service.TrainListService;
import cn.railway.service.UserService;
import cn.railway.service.impl.TrainListServiceImpl;
import cn.railway.service.impl.UserServiceImpl;
import cn.railway.utils.Md5Util;
import cn.railway.utils.MobileMessageCheck;
import cn.railway.utils.MobileMessageSend;
@WebServlet("/user/UserServlet")
public class UserServlet extends BaseServlet {
UserService us = null;
public UserServlet() {
us = new UserServiceImpl();
}
public void ajaxRetrun(HttpServletResponse response, int i, String msg) {
PrintWriter out = null;
try {
out = response.getWriter();
if (i > 0) {
out.write(msg);
} else {
out.write("操作失败");
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 跳转展示用户个人信息的页面的请求
*
* @param request
* @param response
*/
public void showUserInfo(HttpServletRequest request, HttpServletResponse response) {
// session中获取 用户的姓名和uid
int uid = (int) request.getSession().getAttribute("uid");
System.out.println(uid);
User user = us.getUserByUid(uid);
request.setAttribute("user", user);
try {
request.getRequestDispatcher("/pages/user/userInfo.jsp").forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 修改该用户的乘客类型
*
* @param request
* @param response
*/
public void updateVisitorType(HttpServletRequest request, HttpServletResponse response) {
// session中获取 用户的姓名和uid
int uid = (int) request.getSession().getAttribute("uid");
System.out.println(uid);
String visitorType = request.getParameter("visitorType");
int i = us.updateVisitorType(uid, visitorType);
// System.out.println(i);
String msg = "修改成功";
this.ajaxRetrun(response, i, msg);
}
public void initUserUpdateTel(HttpServletRequest request, HttpServletResponse response) {
// session中获取 用户的姓名和uid
int uid = (int) request.getSession().getAttribute("uid");
System.out.println(uid);
User user = us.getUserByUid(uid);
String msg = user.getTel();
int i = 1;
this.ajaxRetrun(response, i, msg);
}
/**
* 验证密码是否准确的请求
*
* @param request
* @param response
*/
public void checkPassword(HttpServletRequest request, HttpServletResponse response) {
// session中获取 用户的姓名和uid
int uid = (int) request.getSession().getAttribute("uid");
System.out.println(uid);
String password = request.getParameter("password");
// 对密码加密
String md5Passwrod = Md5Util.md5Password(password);
User user = us.getUserByUid(uid);
String msg = "";
// 比较密码是否准确
if (user.getPassword().equals(md5Passwrod)) {
msg = "已验证";
} else {
msg = "密码不正确";
}
// System.out.println(msg);
int i = 1;
this.ajaxRetrun(response, i, msg);
}
/**
* 修改电话号码的请求
*
* @param request
* @param response
*/
public void updateTel(HttpServletRequest request, HttpServletResponse response) {
// session中获取 用户的姓名和uid
int uid = (int) request.getSession().getAttribute("uid");
System.out.println(uid);
String tel = request.getParameter("tel");
int i = us.updateTel(uid, tel);
String msg = "修改成功";
this.ajaxRetrun(response, i, msg);
}
/**
* 获取验证码的请求
*
* @param request
* @param response
*/
public void getCode(HttpServletRequest request, HttpServletResponse response) {
// session中获取 用户的姓名和uid
int uid = (int) request.getSession().getAttribute("uid");
System.out.println(uid);
// 从前台获取值
String phone = request.getParameter("tel");
User user = us.getUserByUid(uid);
String tel = "";
if (phone == null) {
// 要是没有获取到 采用用户自己的电话号码
tel = user.getTel();
} else {
tel = phone;
}
System.out.println("电话是" + tel);
String msg = "";
try {
msg = MobileMessageSend.sendMsg(tel);
} catch (IOException e) {
e.printStackTrace();
}
// System.out.println(msg);
this.ajaxRetrun(response, 1, msg);
}
/**
* 验证验证码是否正确
*
* @param request
* @param response
*/
public void checkCode(HttpServletRequest request, HttpServletResponse response) {
// session中获取 用户的姓名和uid
int uid = (int) request.getSession().getAttribute("uid");
System.out.println(uid);
// 获取输入的验证码
String code = request.getParameter("code");
String phone = request.getParameter("tel");
User user = us.getUserByUid(uid);
String tel = "";
if (phone == null) {
// 要是没有获取到 采用用户自己的电话号码
tel = user.getTel();
} else {
tel = phone;
}
String msg = "";
try {
msg = MobileMessageCheck.checkMsg(tel, code);
} catch (IOException e) {
e.printStackTrace();
}
// System.out.println(msg);
this.ajaxRetrun(response, 1, msg);
}
/**
* 修改密码的请求
*
* @param request
* @param response
*/
public void updatePassword(HttpServletRequest request, HttpServletResponse response) {
// session中获取 用户的姓名和uid
int uid = (int) request.getSession().getAttribute("uid");
System.out.println(uid);
// 获取新的密码并且加密
String pwd = request.getParameter("password");
String password = Md5Util.md5Password(pwd);
int i = us.updatePassword(uid, password);
String msg = "密码修改成功";
// System.out.println(msg);
this.ajaxRetrun(response, i, msg);
}
/**
* 验证手机号
*
* @param request
* @param response
*/
public void checkTel(HttpServletRequest request, HttpServletResponse response) {
// session中获取 用户的姓名和uid
int uid = (int) request.getSession().getAttribute("uid");
System.out.println(uid);
String tel = request.getParameter("tel");
int i = us.checkTel(tel);
String msg = Integer.toString(i);
// System.out.println(msg);
this.ajaxRetrun(response, 1, msg);
}
/**
* 获取所有的订单信息 分为未使用,和已使用
*
* @param request
* @param response
*/
public void showAllOrder(HttpServletRequest request, HttpServletResponse response) {
// session中获取 用户的姓名和uid
int uid = (int) request.getSession().getAttribute("uid");
System.out.println(uid);
User user = us.getUserByUid(uid);
String statu1 = "已使用";
String statu2 = "未支付";
// 获取使用的订单结果
List<TblOrder> listUsed = us.getALLOrder(uid);
// 获取已使用的订单的详细信息
Map<String, List<OrderDetail>> mapUsed = us.getUsedOrderDetailByOrderNum(uid, statu1);
// 获取未使用订单的详细信息
Map<String, List<OrderDetail>> mapNoUsed = us.getOrderDetailByOrderNum(uid, statu1, statu2);
System.out.println(listUsed.toString());
System.out.println(mapUsed.toString());
System.out.println(mapNoUsed.toString());
request.setAttribute("listOrder", listUsed);
request.setAttribute("mapUsed", mapUsed);
request.setAttribute("mapNoUsed", mapNoUsed);
try {
request.getRequestDispatcher("/pages/user/orderInit.jsp").forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}