package org.web;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import org.dao.IBookDao;
import org.dao.IOrderDao;
import org.dao.impl.BookDaoImpl;
import org.dao.impl.OrderDaoImpl;
import org.model.Bookinfo;
import org.model.Order;
import org.model.Userinfo;
import org.web.interceptor.AuthenticationInterceptor;
import com.opensymphony.xwork2.ActionSupport;
public class CartAction extends ActionSupport implements SessionAware{
//实例化dao
IBookDao dao = new BookDaoImpl();
IOrderDao orderDao = new OrderDaoImpl();
private Map sessionMap = new HashMap();
private Integer bookid;
private String address;
private String postcode;
private List<Bookinfo> books;
private Order order;
private Integer orderID;
//把书添加到购物车
public String add()throws Exception{
Bookinfo book = dao.getByID(bookid);
//从session中取出list
books = (List)sessionMap.get("cart");
if(books==null){
books = new ArrayList<Bookinfo>();
}
books.add(book);
//存到session中
sessionMap.put("cart", books);
return "add";
}
//浏览购物车
public String view()throws Exception{
//从购物车取出所有的书
books = (List)sessionMap.get("cart");
//从登录用户会话中提取用户注册时候的地址和邮编
Userinfo user = (Userinfo)sessionMap.get(AuthenticationInterceptor.USER_SESSION_KEY);
if (user!=null){
setAddress(user.getAddress());
setPostcode(user.getPostcode());
}
return "view";
}
//生成订单
public String newOrder()throws Exception{
books = (List)sessionMap.get("cart");
// 将一份订单中的相同书籍记录合并成一条,并使数量加1
Hashtable result = combineBook(books);
// 将订单写入数据库
Enumeration enu = result.keys();
// 循环合并记录后的哈希表
while (enu.hasMoreElements()) {
Order order = new Order();
order.setAddress(address);
order.setPostcode(postcode);
//设置状态为已付款
order.setStatus((short)1);
order.setOrderdate(new Date(System.currentTimeMillis()));
Userinfo user = (Userinfo)sessionMap.get(AuthenticationInterceptor.USER_SESSION_KEY);
//设置哪个用户下的订单
order.setUserinfo(user);
//以下三行代码获得合并数量后的订单书本数量
Integer bookid = (Integer) enu.nextElement();
Integer number = (Integer) result.get(bookid);
order.setNumber(number.shortValue());
//getBookinfo方法:将book列表中,根据bookid返回符合的book记录
//设置订单是哪本书
order.setBookinfo(getBookinfo(books,bookid));
orderDao.save(order);
}
//结帐后清空购物车
sessionMap.remove("cart");
return SUCCESS;
}
/**
* 将book列表中,根据bookid返回符合的book记录
* @param booklist
* @param bookid
* @return
*/
private Bookinfo getBookinfo(List<Bookinfo> booklist,Integer bookid){
for (Bookinfo book: booklist){
//如果存在相等的bookid,返回这个book对象.
if (book.getId().equals(bookid))
return book;
}
return null;
}
/**
* 将一份订单中的相同书籍记录合并成一条,并使数量加1
* @param books
* @return
*/
private Hashtable combineBook(List books) {
ListIterator iter = books.listIterator();
//构造一个哈希表,以book的id为键,值为book的数量
Hashtable result = new Hashtable();
while (iter.hasNext()) {
Bookinfo book = (Bookinfo) iter.next();
if (result.get(book.getId()) == null) {
//第一次放入,数量初始化为1
result.put(book.getId(), new Integer(1));
} else {
Integer now = (Integer) result.get(book.getId());
result.remove(book.getId());
//第二次以上放入,数量为上一次统计的数量加一
result.put(book.getId(), new Integer(now.intValue() + 1));
}
}
return result;
}
public String viewOrder()throws Exception{
order = orderDao.getByID(orderID);
return "order";
}
public Integer getBookid() {
return bookid;
}
public void setBookid(Integer bookid) {
this.bookid = bookid;
}
public List<Bookinfo> getBooks() {
return books;
}
public void setBooks(List<Bookinfo> books) {
this.books = books;
}
public void setSession(Map map) {
this.sessionMap = map;
}
public Map getSessionMap() {
return sessionMap;
}
public void setSessionMap(Map sessionMap) {
this.sessionMap = sessionMap;
}
public String getPostcode() {
return postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
public Integer getOrderID() {
return orderID;
}
public void setOrderID(Integer orderID) {
this.orderID = orderID;
}
}
- 1
- 2
前往页