package com.yinhe.shoppingCar.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yinhe.shoppingCar.dao.BuyGoodsCar;
import com.yinhe.shoppingCar.dao.OrdersBo;
import com.yinhe.shoppingCar.dao.ParticularOrderBo;
import com.yinhe.shoppingCar.tool.Time;
import com.yinhe.shoppingCar.vo.GoodsVo;
import com.yinhe.shoppingCar.vo.OrdersVo;
import com.yinhe.shoppingCar.vo.ParticularOrderVo;
import com.yinhe.shoppingCar.vo.UserVo;
public class OrderAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private OrdersBo ob ; // 对简单信息的操作类
private ParticularOrderBo pob;//对详细信息的操作类
private HttpServletRequest request ;
private HttpSession session;
private ArrayList<GoodsVo> buyGoodsList;//用户购买所购买的商品集合
private int ordersId; //接收由上一个界面传来的订单ID
@SuppressWarnings("unchecked")
public OrderAction(){
ob = new OrdersBo();
pob = new ParticularOrderBo();
request = ServletActionContext.getRequest();
session = request.getSession();
buyGoodsList = (ArrayList<GoodsVo>)session.getAttribute("buyGoodsList");
}
//成员属性的get和set方法
public int getOrdersId() {
return ordersId;
}
public void setOrdersId(int ordersId) {
this.ordersId = ordersId;
}
//返回配置信息
//添加订单
public String addOrders(){
String message = this.writeOrders(this.getBuyGoodsMap());
String[] showMessage = this.getShowMessage();
request.setAttribute("showMessage", showMessage);
request.setAttribute("message", message);
return SUCCESS;
}
//显示订单
public String showOrders(){
String[] showMessage = this.getShowMessage();
request.setAttribute("showMessage", showMessage);
return SUCCESS;
}
//显示详细订单
public String showParticularOrders(){
ArrayList<String[]> particularMessages =
this.getParticularMessage(buyGoodsList.size(),this.getBuyGoodsMap());
// this.getParticularMessage(pob.getNumById(ordersId),this.getBuyGoodsMap());
request.setAttribute("particularMessages", particularMessages);
return SUCCESS;
}
//安全退出时的操作
public String clearGoods(){
HashMap<Integer,Integer> carMessage = this.getBuyGoodsMap();
if(carMessage != null){
this.getBuyGoodsMap().clear();
}
session.setAttribute("buyGoodsList", null);
session.setAttribute("user", null);
session.setAttribute("selectOk", null);
return SUCCESS;
}
//获取当前用户的对象
private UserVo getCurrentUser(){
return (UserVo)session.getAttribute("user");
}
//获取购物车中的map信息
private HashMap<Integer,Integer> getBuyGoodsMap(){
BuyGoodsCar car = (BuyGoodsCar)session.getAttribute("car");
if(car != null){
return (HashMap<Integer, Integer>) car.getBuyGoodsCar();
}
return null;
}
//设置订单页面显示的信息
private String[] getShowMessage(){
UserVo currentUser = this.getCurrentUser();
double totalPrice = (Double)session.getAttribute("totalPrice");
String[] message = new String[6];
message[0]=ob.getMaxId()+"";
message[1]=Time.getSystemTime();
message[2]=currentUser.getUserName();
message[3]=currentUser.getUserEmail();
message[4]=currentUser.getUserAddress();
message[5]=(totalPrice/10000)+"万元";
return message;
}
//将用户的购买物品添加到数据库中,分简略的添加和详细的添加
private String writeOrders(HashMap<Integer,Integer> buyNumList){
int check = 0;
//添加简略的数据到数据库
int ordersId = ob.getMaxId()+1;
double totalPrice = (Double)session.getAttribute("totalPrice");
OrdersVo orders = new OrdersVo(ordersId,
this.getCurrentUser().getUserId(),
Time.getSystemTime(), totalPrice);
ob.addOrders(orders);
// 添加详细的数据到数据库
ParticularOrderVo p_order = null;
Iterator<Integer> it = buyNumList.keySet().iterator();
int n = 0;
while(it.hasNext()){
int goodsId = (Integer) it.next();
int goodsNum = (Integer)buyNumList.get(goodsId);
double partTotal = goodsNum*buyGoodsList.get(n).getGoodsPrice();
n++;
p_order = new ParticularOrderVo(ordersId, goodsId,
goodsNum, partTotal);
if(pob.addParticularOrders(p_order)){
check++;
}
}
if(check==buyNumList.size())
return "添加完成";
else
return "添加不完整";
}
//获取订单的详细信息
private ArrayList<String[]> getParticularMessage(int num,HashMap<Integer,Integer> buyNumList){
ArrayList<String[]> particularMessages = new ArrayList<String[]>();
String[] message = null;
for (int i = 0; i < num; i++) {
int goodsId = buyGoodsList.get(i).getGoodsId();
double price = buyGoodsList.get(i).getGoodsPrice();
int goodsNum = buyNumList.get(goodsId);
message = new String[5];
message[0]=goodsId+"";
message[1]=buyGoodsList.get(i).getGoodsName();
message[2]=price+"元";
message[3]=goodsNum+"";
message[4]=price*goodsNum+"";
// System.out.println(price*goodsNum);
particularMessages.add(message);
}
return (ArrayList<String[]>)particularMessages;
}
}