package com.servlet;
import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.bean.Good;
import com.dao.Dao;
import com.dao.GoodDao;
@WebServlet("/GoodServlet")
public class GoodServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private GoodDao goodDao=null;
private List<Good> carList =null;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
carList = (List<Good>) session.getAttribute("carList");
if(carList==null){
List<Good> list=new ArrayList<Good>();
session.setAttribute("carList", list);
}
//获取goodDao
goodDao=(GoodDao) session.getAttribute("goodDao");
if(goodDao==null){
goodDao=new GoodDao();
session.setAttribute("goodDao", goodDao);
}
request.setCharacterEncoding("utf-8");
String method = request.getParameter("method");
if(method.equals("findAll")){
findAll(request,response);
}
if(method.equals("buy")){
buy(request,response);
}
if(method.equals("shopCar")){
shopCar(request,response);
}
if(method.equals("remove")){
remove(request,response);
}
if(method.equals("removeAll")){
removeAll(request,response);
}
}
//向购物车中添加商品
protected void addShopCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
// List<Good> carList = (List<Good>) session.getAttribute("carList");
String id = request.getParameter("id");
Good good = goodDao.getOne(id);
//购物车为空的时候
if(good!=null){
if(carList.size()==0){
Good temp=new Good();
temp.setName(good.getName());
System.out.println(good.getPrice());
temp.setNum(1);
temp.setPrice(good.getPrice());
carList.add(temp);
}else{
int i=0;
//存在添加商品
for(;i<carList.size();i++){
Good temp=(Good) carList.get(i);
if(temp.getName().equals(good.getName())){
temp.setNum(temp.getNum()+1);
break;
}
}
//不存在添加商品
if(i>=carList.size()){
Good temp=new Good();
temp.setName(good.getName());
temp.setNum(1);
temp.setPrice(good.getPrice());
carList.add(temp);
}
}
}
}
//读取数据库中所有图书信息
protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Good> goodList = goodDao.getAllGood();
HttpSession session = request.getSession();
session.setAttribute("goodList", goodList);
request.getRequestDispatcher("/WEB-INF/pages/list.jsp").forward(request, response);
}
//购买商品后,修改库存
protected void buy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Object obj = session.getAttribute("user");
//判断用户是否登录
if(obj==null){
request.getRequestDispatcher("login.jsp").forward(request, response);
}else{
String id = request.getParameter("id");
String sql="update good set num=num-1 where id=?";
goodDao.update(id,sql);
addShopCar(request,response);
findAll(request,response);
}
}
//跳转到购物车页面
protected void shopCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/pages/shopCar.jsp").forward(request, response);
}
//移出购物车中商品,修改数据库中的数据
protected void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//修改数据库中的数据
String name = (String) request.getParameter("name");
System.out.println(name);
String sql="update good set num=num+1 where name=?";
goodDao.update(name, sql);
//修改Sessio中List的数据
HttpSession session = request.getSession();
List<Good> carList = (List<Good>) session.getAttribute("carList");
for(int i=0;i<carList.size();i++){
Good temp=(Good)carList.get(i);
if(temp.getName().equals(name)){
if(temp.getNum()>1){
temp.setNum(temp.getNum()-1);
break;
}else if(temp.getNum()==1){
carList.remove(i);
}
}
}
request.getRequestDispatcher("/WEB-INF/pages/shopCar.jsp").forward(request, response);
}
//移出购物车中的所有商品
protected void removeAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//修改Sessio中List的数据
HttpSession session = request.getSession();
String sql=null;
// List<Good> carList = (List<Good>) session.getAttribute("carList");
for(int i=0;i<carList.size();i++){
sql="update good set num=num+"+carList.get(i).getNum()+" where name=?";
goodDao.update(carList.get(i).getName(), sql);
}
session.removeAttribute("carList");
request.getRequestDispatcher("/WEB-INF/pages/shopCar.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}