package com.tyland.action;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.struts2.interceptor.SessionAware;
import com.tyland.common.CommonService;
import com.tyland.domain.Agent;
import com.tyland.domain.Notice;
import com.tyland.domain.Order;
import com.tyland.domain.OrderItem;
import com.tyland.domain.ProductCode;
import com.tyland.domain.ProductType;
import com.tyland.domain.User;
public class OrderAction implements SessionAware {
private Map<String, Object> session;
private CommonService comService;
private User user;
private List resultList;
private List approvlList=new ArrayList();
private List typeList= new ArrayList();
private List agentList= new ArrayList();
private ProductType proType;
private boolean afterQuery;
private Set productSet;
private ProductCode product;
private ProductCode productCode;
private Integer p_count;
private Set itemSet;
private Order order;
private String message;
private Integer approvl_id;//审批者id
private Integer agent_id;//企业id
private Integer order_id;
private String start_date;
private String end_date;
private String selectedIds;
private String failMsg;
private String deleteCartFlag;
private int type;
private Agent agent;
private ProductType productType;
public Agent getAgent() {
return agent;
}
public void setAgent(Agent agent) {
this.agent = agent;
}
public ProductType getProductType() {
return productType;
}
public void setProductType(ProductType productType) {
this.productType = productType;
}
public String getFailMsg() {
return failMsg;
}
public int getType() {
return type;
}
public void setSelectedIds(String selectedIds) {
this.selectedIds = selectedIds;
}
public void setDeleteCartFlag(String deleteCartFlag) {
this.deleteCartFlag = deleteCartFlag;
}
public Integer getOrder_id() {
return order_id;
}
public List getApprovlList() {
return approvlList;
}
public List getAgentList() {
return agentList;
}
public void setApprovl_id(Integer approvlId) {
approvl_id = approvlId;
}
public void setAgent_id(Integer agentId) {
agent_id = agentId;
}
public void setOrder_id(Integer orderId) {
order_id = orderId;
}
public void setStart_date(String startDate) {
start_date = startDate;
}
public void setEnd_date(String endDate) {
end_date = endDate;
}
public String getMessage() {
return message;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
public ProductCode getProductCode() {
return productCode;
}
public void setItemSet(Set itemSet) {
this.itemSet = itemSet;
}
public void setProductCode(ProductCode productCode) {
this.productCode = productCode;
}
public void setP_count(Integer pCount) {
p_count = pCount;
}
public Set getItemSet() {
return itemSet;
}
public ProductCode getProduct() {
return product;
}
public void setProduct(ProductCode product) {
this.product = product;
}
public void setSession(Map<String, Object> arg0) {
this.session = arg0;
}
public CommonService getComService() {
return comService;
}
public void setComService(CommonService comService) {
this.comService = comService;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List getTypeList() {
return typeList;
}
public void setTypeList(List typeList) {
this.typeList = typeList;
}
public List getResultList() {
return resultList;
}
public void setResultList(List resultList) {
this.resultList = resultList;
}
public ProductType getProType() {
return proType;
}
public void setProType(ProductType proType) {
this.proType = proType;
}
public boolean isAfterQuery() {
return afterQuery;
}
public void setAfterQuery(boolean afterQuery) {
this.afterQuery = afterQuery;
}
public Set getProductSet() {
return productSet;
}
public void setProductSet(Set productSet) {
this.productSet = productSet;
}
public Map<String, Object> getSession() {
return session;
}
/**
*查询已发布给当前用户所属企业的所有产品和产品类别显示在页面
* @return
*/
public String toCreate() {
if (deleteCartFlag!=null && deleteCartFlag.equals("1")) {
}else {
session.remove("cart");
}
typeList = comService.queryAll(ProductType.class);
proType = (ProductType) typeList.get(0);
getProduct(proType);
afterQuery = true;
return "to_create";
}
public String queryProductByType() {
typeList = comService.queryAll(ProductType.class);
proType = (ProductType) comService.queryById(ProductType.class, proType.getId());
getProduct(proType);
afterQuery = true;
return "to_create";
}
/**
*根据产品类别获得已发布产品
*
* @return
*/
private void getProduct(ProductType type) {
user = (User) session.get("sessUser");
Agent agent = user.getAgent();
productSet = agent.getPublishProductSet();
Iterator it = productSet.iterator();
resultList = new ArrayList();
while (it.hasNext()) {
ProductCode p = (ProductCode) it.next();
if (p.getProductType().getId().equals(proType.getId())) {
resultList.add(p);
}
}
}
/**
*跳转到购买产品页面
*
* @return
*/
public String preBuyProduct(){
int productId = product.getId();
product = (ProductCode) comService.queryById(ProductCode.class, productId);
return "pre_buy";
}
/**
*购买产品页面
*
* @return
*/
public String buy(){
int productId = product.getId();
product = (ProductCode) comService.queryById(ProductCode.class, productId);
Map cart = (Map)session.get("cart");
if (null == cart){
cart = new HashMap<Integer,OrderItem>();
session.put("cart", cart);
}
Set<Integer> keys = cart.keySet();
Iterator<Integer> it = keys.iterator();
if (it.hasNext()) {
Integer key = it.next();
OrderItem firstItem =(OrderItem) cart.get(key);
if (null != firstItem) {
int innerProductTypeId = firstItem.getProductCode().getProductType().getId().intValue();
if (innerProductTypeId != product.getProductType().getId().intValue()) {
failMsg = "您不能购买其它类别的产品!";
return "buy_fail";
}
}
}
OrderItem item = (OrderItem) cart.get(productId);
System.out.println("p_count="+p_count);
if (null == item) {
item=new OrderItem();
item.setNumber(p_count);
item.setProductCode(product);
cart.put(productId,item);
} else {
item.setNumber(item.getNumber() + p_count);
}
return "success";
}
/**
*跳转到浏览购物车页面
*
* @return
*/
public String listCart(){
return "suc";
}
/**
*删除购物车中所选产品
*
* @return
*/
public String deleteOrderProduct() {
String[] ids = selectedIds.split("\\,");
for(String id:ids){
Map<Integer, OrderItem> cart = (Map<Integer, OrderItem>) session.get("cart");
if (null != cart) {
cart.remove(Integer.parseInt(id));
}
}
return "suc";
}
/**
*跳转到录入收获地址页面
*
* @return
*/
public String putinAddress(){
//首先判断购物车是否购买过商品
Map<Integer, OrderItem> cart = (Map<Integer, OrderItem>) session
.get("cart");
if (null == cart || cart.size() <= 0) {
message="您还没购买过产品,请先购买产品";
return toCreate();
}
//判断是订单修改还�