package com.sh.action;
import java.util.Iterator;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sh.dboperation.BookDAO;
import com.sh.model.Book;
import com.sh.model.Order;
import com.sh.model.User;
public class BookAction extends ActionSupport {
private int b_id;
private String b_name;
private double b_price;
private int b_num;
private int o_num;
public int getB_id() {
return b_id;
}
public void setB_id(int b_id) {
this.b_id = b_id;
}
public String getB_name() {
return b_name;
}
public void setB_name(String b_name) {
this.b_name = b_name;
}
public int getB_num() {
return b_num;
}
public void setB_num(int b_num) {
this.b_num = b_num;
}
public int getO_num() {
return o_num;
}
public void setO_num(int o_num) {
this.o_num = o_num;
}
public double getB_price() {
return b_price;
}
public void setB_price(double b_price) {
this.b_price = b_price;
}
//添加教材action
public String saveAction() throws Exception {
Book book = new Book();
book.setB_name(this.b_name);
book.setB_price(this.b_price);
book.setB_num(this.b_num);
book.setO_num(this.o_num);
BookDAO.save(book);
return SUCCESS;
}
//处理统一订购教材action
public String orderCheckedAction() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
String tem[] = request.getParameterValues("o_num");
int[] o_nums = new int[tem.length];
for(int i=0;i<tem.length;i++)
o_nums[i] = Integer.parseInt(tem[i]);
request.setAttribute("o_nums", o_nums);
Iterator bookList = null;
bookList = BookDAO.ListAllBooks().iterator();
int price = 0;
for(int i=0;i<o_nums.length;i++)
{
Book book = new Book();
book = (Book)bookList.next();
price+=price+o_nums[i]*book.getB_price();
}
request.setAttribute("price", price);
showAction();
return SUCCESS;
}
//提交订单action
public String submitOrderAction() throws Exception {
HttpSession session = ServletActionContext.getRequest().getSession();
int o_nums[] = (int[])session.getAttribute("o_nums");
User user = (User)session.getAttribute("user");;
Iterator bookList = null;
bookList = BookDAO.ListAllBooks().iterator();
Order order = new Order();
String o_detail = "";
int o_price = 0;
for(int i=0;i<o_nums.length;i++)
{
Book book = new Book();
book = (Book)bookList.next();
book.setO_num(o_nums[i]);
book.setB_num(book.getB_num()-o_nums[i]);
BookDAO.updateBook(book);
if(o_nums[i]!=0){
o_detail+=book.getB_name()+book.getO_num()+"本; ";
o_price+=o_price+o_nums[i]*book.getB_price();
}
}
order.setU_name(user.getU_name());
order.setO_detail(o_detail);
order.setO_date(new Date());
order.setO_price(o_price);
BookDAO.saveOrder(order);
showAction();
return SUCCESS;
}
//显示所有教材action
public String showAction() throws Exception {
List bookList = null;
bookList = BookDAO.ListAllBooks();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("bookList", bookList);
return SUCCESS;
}
//显示所有教材action
public String preAddAction() throws Exception {
List bookList = null;
bookList = BookDAO.ListAllBooks();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("bookList", bookList);
return SUCCESS;
}
//添加教材action
public String addAction() throws Exception {
Book book = new Book();
book.setB_name(this.b_name);
book.setB_price(this.b_price);
book.setB_num(this.b_num);
book.setO_num(this.o_num);
BookDAO.save(book);
List bookList = null;
bookList = BookDAO.ListAllBooks();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("bookList", bookList);
return SUCCESS;
}
//预跟新教材action
public String preUpdateAction() throws Exception {
Book book = BookDAO.getBookByB_id(b_id);
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("book", book);
return SUCCESS;
}
//添加教材action
public String update_bookAction() throws Exception {
Book book = BookDAO.getBookByB_id(this.b_id);
book.setB_name(this.b_name);
book.setB_price(this.b_price);
book.setB_num(this.b_num);
BookDAO.updateBook(book);
List bookList = null;
bookList = BookDAO.ListAllBooks();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("bookList", bookList);
return SUCCESS;
}
}