package com.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dao.GoodsDao;
import com.dao.OrderformDao;
import com.dao.ShopcarDao;
import com.dao.TempDao;
import com.toolsbean.StringHandler;
import com.valuebean.OrderformBean;
import com.valuebean.ShopcarBean;
import com.valuebean.UserBean;
public class ShopcarServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String servletpath=request.getServletPath();
if("/buy".equals(servletpath))
buy(request,response);
else if("/showshopcar".equals(servletpath))
showshopcar(request,response);
else if("/remove".equals(servletpath))
remove(request,response);
else if("/clearshopcar".equals(servletpath))
clear(request,response);
else if("/submitshopcar".equals(servletpath))
submitDispatcher(request,response);
else if("/createorderform".equals(servletpath))
createorderform(request,response);
}
/** @功能:购买商品 */
protected void buy(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String message="";
Integer buygoodsid=StringHandler.strToint(request.getParameter("buygoodsId"));
if(buygoodsid==null){
message="<li>商品ID值错误!</li>";
message+="<a href='window.history.go(-1)'>返回</a>";
}
else{
try {
Date now=new Date(); //获取当前时间
TempDao tempDao=new TempDao();
String shopcarid=seeshopcarcookie(request,response); //查询客户端Cookie中是否保存了一个购物车ID值
if(shopcarid==null||shopcarid.equals("")||!tempDao.isexist(shopcarid)){ //没有保存
shopcarid=addshopcarcookie(request,response,now); //生成一个购物车ID保存到客户端Cookie中,并返回该ID值
tempDao.saveShopcarCreateTime(shopcarid, StringHandler.timeTostr(now)); //记录该购物车ID和创建时间到数据表中
}
int i=-1;
ShopcarDao shopcarDao=new ShopcarDao();
Object[] params={shopcarid,buygoodsid};
if(shopcarDao.isBuy(params)) //如果已经购买了该商品
i=shopcarDao.addBuyNum(params); //更新商品购买数量
else{ //没有购买该商品
params=new Object[]{shopcarid,buygoodsid,1};
i=shopcarDao.addBuyGoods(params); //添加该商品到购物车(tb_shopcar数据表)中
}
shopcarDao.closed();
if(i<=0)
message="<li>添加商品到购物车失败!</li><br>";
else
message="<li>添加商品到购物车成功!</li><br>";
message+="<a href='javascript:window.history.go(-1)'>返回</a>";
message+=" ";
message+="<a href='showshopcar'>查看购物车<a/>";
} catch (SQLException e) {
e.printStackTrace();
}
}
request.setAttribute("message",message);
RequestDispatcher rd=request.getRequestDispatcher("/message.jsp");
rd.forward(request,response);
}
private String seeshopcarcookie(HttpServletRequest request,HttpServletResponse response){
String webname=request.getContextPath();
webname=webname.substring(1);
Cookie[] coks=request.getCookies();
String shopcarid="";
int i=0;
for(;i<coks.length;i++){
Cookie icok=coks[i];
if(icok.getName().equals(webname+".usershopcar")){
shopcarid=icok.getValue();
break;
}
}
return shopcarid;
}
private String addshopcarcookie(HttpServletRequest request,HttpServletResponse response,Date date){
String webname=request.getContextPath();
webname=webname.substring(1);
String shopcarid="car"+StringHandler.getSerial(date);
Cookie shopcar=new Cookie(webname+".usershopcar",shopcarid);
shopcar.setPath("/");
int maxage=60*60*24*3; //设置cookie的有效期为3天
shopcar.setMaxAge(maxage);
response.addCookie(shopcar);
return shopcarid;
}
protected void showshopcar(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String shopcarid=seeshopcarcookie(request,response);
if(shopcarid!=null&&!shopcarid.equals("")){
ShopcarBean shopcar=new ShopcarDao().getShopcar(shopcarid);
request.setAttribute("shopcar",shopcar);
}
RequestDispatcher rd=request.getRequestDispatcher("/showShopcar.jsp");
rd.forward(request,response);
}
protected void remove(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
Integer goodsId=StringHandler.strToint(request.getParameter("goodsId"));
String shopcarid=seeshopcarcookie(request,response);
if(shopcarid!=null&&!shopcarid.equals("")&&goodsId!=null){
ShopcarDao shopcarDao=new ShopcarDao();
int i=shopcarDao.deleteGoods(shopcarid,goodsId);
if(i<=0)
request.setAttribute("message","●删除商品失败!");
else
request.setAttribute("message","●删除商品成功!");
shopcarDao.closed();
}
showshopcar(request,response);
}
protected void clear(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String shopcarid=seeshopcarcookie(request,response);
if(shopcarid!=null&&!shopcarid.equals("")){
ShopcarDao shopcarDao=new ShopcarDao();
int i=shopcarDao.clearShopcar(shopcarid);
if(i<=0)
request.setAttribute("message","●清空购物车失败!");
else
request.setAttribute("message","●清空购物车成功!");
shopcarDao.closed();
}
RequestDispatcher rd=request.getRequestDispatcher("/showShopcar.jsp");
rd.forward(request,response);
}
protected void submitDispatcher(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
String whichsubmit=request.getParameter("whichsubmit");
if(whichsubmit==null)
payforMoney(request,response);
else
updatebuyNum(request,response);
}
private void payforMoney(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
if(shopcar_validateBuyNum(request, response)){
String buygoodsids=StringHandler.ArrayToString(request.getParameterValues("buygoodsids"));
String buygoodsnum=StringHandler.ArrayToString(request.getParameterValues("buygoodsnum"));
request.setAttribute("buygoodsids", buygoodsids);
request.setAttribute("buygoodsnum", buygoodsnum);
RequestDispatcher rd=request.getRequestDispatcher("/fillOrderform.jsp");
rd.forward(request,response);
}
else
showshopcar(request,response);
}
protected void updatebuyNum(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
shopcar_validateBuyNum(request, response);
showshopcar(request,response);
}
private boolean shopcar_validateBuyNum(HttpServletRequest request,HttpServletResponse response){
boolean mark=true;
String[] goodsStoreNums=request.getParameterValues("buygoodsstorenum");
String[] buyNums=request.getParameterValues("buygoodsnum");
String[] goodsIds=request.getParameterValues("buygoodsids");
String shopcarid=seeshopcarcookie(request,response);
if(goodsIds!=null&&goodsIds.length!=0&&shopcarid!=null){
Map messages=new HashMap();
Object[] params=new Object[3];
ShopcarDao shopcarDao=new ShopcarDao();
for(int i=0;i<goodsIds.length;i++){
int int_buyNum=Integer.parseInt(buyNums[i]);
int int_goodsStoreNums=Integer.parseInt(goodsStoreNums[i]);
if(int_buyNum>int_goodsStoreNums){
mark=false;
messages.