|
|
@ -1,60 +1,76 @@
|
|
|
|
package com.cn.servlet;
|
|
|
|
package com.cn.servlet; // 定义Servlet所在的包名
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException; // 导入IOException,用于处理输入输出异常
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.io.PrintWriter; // 导入PrintWriter,用于向客户端发送字符文本数据
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
import javax.servlet.ServletException; // 导入ServletException,用于处理Servlet运行时的异常
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
import javax.servlet.http.HttpServlet; // 导入HttpServlet,是所有HTTP servlet的父类
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletRequest; // 导入HttpServletRequest,代表客户端的请求信息
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import javax.servlet.http.HttpServletResponse; // 导入HttpServletResponse,代表服务器对客户端的响应信息
|
|
|
|
|
|
|
|
|
|
|
|
import com.cn.domain.Train;
|
|
|
|
import com.cn.domain.Train; // 导入Train类,该类定义了车次的数据结构
|
|
|
|
import com.cn.service.PrepService;
|
|
|
|
import com.cn.service.PrepService; // 导入PrepService接口,该接口定义了订单服务的方法
|
|
|
|
import com.cn.service.TrainService;
|
|
|
|
import com.cn.service.TrainService; // 导入TrainService接口,该接口定义了车次服务的方法
|
|
|
|
import com.cn.service.impl.PrepServiceImpl;
|
|
|
|
import com.cn.service.impl.PrepServiceImpl; // 导入PrepService接口的实现类,用于具体的订单业务操作
|
|
|
|
import com.cn.service.impl.TrainServiceImpl;
|
|
|
|
import com.cn.service.impl.TrainServiceImpl; // 导入TrainService接口的实现类,用于具体的车次业务操作
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* DeletePrepServlet类,用于处理删除订单(退票)的请求。
|
|
|
|
* @ClassName: DeletePrepServlet
|
|
|
|
* @ClassName: DeletePrepServlet 类名:DeletePrepServlet
|
|
|
|
* @Description: 删除订单
|
|
|
|
* @Description: 删除订单,即退票功能
|
|
|
|
* @author: ljy
|
|
|
|
* @author: ljy Servlet的作者
|
|
|
|
* @date: 2019年9月28日 下午7:25:16
|
|
|
|
* @date: 2019年9月28日 下午7:25:16 Servlet创建的日期和时间
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class DeletePrepServlet extends HttpServlet {
|
|
|
|
public class DeletePrepServlet extends HttpServlet {
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
private static final long serialVersionUID = 1L; // 用于序列化
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 处理GET请求的方法,用于执行删除订单(退票)的操作。
|
|
|
|
|
|
|
|
* @param request HttpServletRequest对象,包含客户端的请求信息
|
|
|
|
|
|
|
|
* @param response HttpServletResponse对象,包含服务器对客户端的响应信息
|
|
|
|
|
|
|
|
* @throws ServletException 抛出Servlet异常
|
|
|
|
|
|
|
|
* @throws IOException 抛出输入输出异常
|
|
|
|
|
|
|
|
*/
|
|
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
|
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
|
|
|
throws ServletException, IOException {
|
|
|
|
throws ServletException, IOException {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建订单服务对象
|
|
|
|
PrepService prepService = new PrepServiceImpl();
|
|
|
|
PrepService prepService = new PrepServiceImpl();
|
|
|
|
|
|
|
|
// 创建车次服务对象
|
|
|
|
TrainService trainService = new TrainServiceImpl();
|
|
|
|
TrainService trainService = new TrainServiceImpl();
|
|
|
|
|
|
|
|
// 初始化记录数变量,用于存储删除操作的结果
|
|
|
|
int recordNumber = 0;
|
|
|
|
int recordNumber = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// 这是从界面过来的,实现退票业务
|
|
|
|
// 从请求中获取订单ID,并转换为Integer类型
|
|
|
|
Integer prepId = Integer.valueOf(request.getParameter("prepId"));
|
|
|
|
Integer prepId = Integer.valueOf(request.getParameter("prepId"));
|
|
|
|
|
|
|
|
|
|
|
|
// 将订单信息中的trainID先拉取出来,下面更改车次座位数需要
|
|
|
|
// 根据订单ID获取车次ID,用于后续更改车次座位数
|
|
|
|
Integer trainId = prepService.getById(prepId).getTrainId();
|
|
|
|
Integer trainId = prepService.getById(prepId).getTrainId();
|
|
|
|
|
|
|
|
|
|
|
|
// 删除订单
|
|
|
|
// 调用PrepService的delete方法删除订单,并返回影响的记录数
|
|
|
|
recordNumber = prepService.delete(prepId);
|
|
|
|
recordNumber = prepService.delete(prepId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取PrintWriter对象,用于向客户端发送响应
|
|
|
|
PrintWriter out = response.getWriter();
|
|
|
|
PrintWriter out = response.getWriter();
|
|
|
|
if (recordNumber == 1) {
|
|
|
|
if (recordNumber == 1) {
|
|
|
|
// 座位数+1
|
|
|
|
// 如果订单删除成功,更新车次的座位数
|
|
|
|
|
|
|
|
// 根据车次ID获取车次对象
|
|
|
|
Train train = trainService.getById(trainId);
|
|
|
|
Train train = trainService.getById(trainId);
|
|
|
|
|
|
|
|
// 座位数加1
|
|
|
|
train.setSeatNumber(train.getSeatNumber() + 1);
|
|
|
|
train.setSeatNumber(train.getSeatNumber() + 1);
|
|
|
|
|
|
|
|
// 更新车次对象
|
|
|
|
trainService.update(train);
|
|
|
|
trainService.update(train);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 向客户端发送JavaScript代码,弹出提示并跳转到我的订单页面
|
|
|
|
out.write("<script>alert('退票成功!');" + "window.location.href='MyPrepServlet'</script>");
|
|
|
|
out.write("<script>alert('退票成功!');" + "window.location.href='MyPrepServlet'</script>");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
out.write("<script>alert('很抱歉,退票失败!');" + "window.location.href='MyPrepServlet'</script>");
|
|
|
|
// 如果订单删除失败,向客户端发送JavaScript代码,弹出提示并跳转到我的订单页面
|
|
|
|
|
|
|
|
out.write("<script>alert('很抱歉,退票失败!');" + "window.location.href='MyPrepServlet'</script>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 关闭PrintWriter对象
|
|
|
|
out.close();
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|