package com.yzp.action;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import com.yzp.dao.hbnImpl.ShoppingCarDaoHbnImpl;
import com.yzp.entity.Cart;
import com.yzp.entity.Item;
import com.yzp.entity.Order;
import com.yzp.entity.User;
import com.yzp.service.impl.ShoppingCarServiceHbnImpl;
public class CartAction extends MappingDispatchAction{
public ActionForward modifyNumber(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception{
//修改数量
Cart cart=(Cart)request.getSession().getAttribute("cart");
if(cart==null){
cart=new Cart();
}
System.out.println("modifyNumber");
String[] number=request.getParameterValues("number");
String[] product=request.getParameterValues("items");
if(number!=null){
for(int i=0;i<number.length;i++){
Item item=cart.findByProductId(Integer.parseInt(product[i]));
System.out.println("********");
item.setNumber(Integer.parseInt(number[i]));
cart.modifyItemNumber(item);//调用Cart.modifyItemNumber(item)
}
}
request.getSession().setAttribute("cart",cart);
return mapping.findForward("modifyNumber");
}
public ActionForward deleteSelectItem(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception{
//删除所选项Item
Cart cart=(Cart)request.getSession().getAttribute("cart");
if(cart==null){
cart=new Cart();
}
System.out.println("deleteSelectItem");
String[] product=request.getParameterValues("item");
if(product!=null){
for(int i=0;i<product.length;i++){
cart.removeItemByProductId(Integer.parseInt(product[i]));
}
}
request.getSession().setAttribute("cart",cart);
return mapping.findForward("deleteSelectItem");
}
public ActionForward clearCart(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception{
//清空购物车
Cart cart=(Cart)request.getSession().getAttribute("cart");
if(cart==null){
cart=new Cart();
request.getSession().setAttribute("cart",cart);
}
cart.clear();
System.out.println(cart+"cart#########");
return mapping.findForward("clearCart");
}
public ActionForward operateOrder(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception{
//在cart.jsp中,点击结帐
User user=(User)request.getSession().getAttribute("user");
if(user==null){//没有登录
return mapping.findForward("unlogin");
}else{
Cart cart=(Cart)request.getSession().getAttribute("cart");
if(cart==null){//购物车空,
cart=new Cart();
request.getSession().setAttribute("fail",cart);
}
if(cart.isEmpty()){//购物车不空,
return mapping.findForward("fail");
}
return mapping.findForward("order");
}
}
public ActionForward generagetOrder(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception{
//生成一个Order
Cart cart=(Cart)request.getSession().getAttribute("cart");
User user=(User)request.getSession().getAttribute("user");
Order order=new Order();
order.setUser(user);
order.setStatus(1);
order.setCost(cart.getCost());
Set<Item> items=cart.getItems();
order.setItem(items);
ShoppingCarServiceHbnImpl sh=new ShoppingCarServiceHbnImpl();
sh.generateOrder(order);//保存到数据库
request.getSession().setAttribute("order", order);
return mapping.findForward("success");
}
}