?package com.oracle.xm.action;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
import com.oracle.xm.biz.ProductInfoBiz;
import com.oracle.xm.biz.XiaomiAddressBiz;
import com.oracle.xm.biz.XmOrderBiz;
import com.oracle.xm.biz.XmOrderdetailBiz;
import com.oracle.xm.entity.Car;
import com.oracle.xm.entity.ProductInfo;
import com.oracle.xm.entity.XiaomiAddress;
import com.oracle.xm.entity.XiaomiUser;
import com.oracle.xm.entity.XmOrder;
import com.oracle.xm.entity.XmOrderdetail;
@ParentPackage("struts-default")
@Namespace("/prod")
@Controller
@Scope("prototype")
public class ProductInfoAction extends ActionSupport
implements SessionAware,RequestAware {
Map<String, Object> request;
Map<String, Object> session;
//商品业务逻辑层的对象
@Autowired@Qualifier("ProductInfoBiz")
ProductInfoBiz pbiz;
@Autowired@Qualifier("XiaomiAddressBiz")
XiaomiAddressBiz abiz;
@Autowired@Qualifier("XmOrderBiz")
XmOrderBiz obiz;
@Autowired@Qualifier("XmOrderdetailBiz")
XmOrderdetailBiz dbiz;
private Long pid;
//创建与地址相关的数据变量
private String showadd;
private String beizhu;
private Long total;
//新地址变量
private String newaddress;
private String cnee;
private String phone;
//旧地址变量
private int rdoAddress;
private String rdocnee;
private String rdophone;
//选中商品的id
private Long pids[];
@Action(value="showall",results={
@Result(name="success",location="/main.jsp")
})
public String showall(){
List<ProductInfo> list=pbiz.findAll();
List<ProductInfo> haiList=new ArrayList<ProductInfo>();
for(int i=0;i<4;i++){
haiList.add(list.get(i));
}
session.put("haiList",haiList);
return SUCCESS;
}
@Action(value="myorder",results={
@Result(name="success",location="/myorder.jsp")
})
public String myorder(){
//谁登录查谁的订单
XiaomiUser user=(XiaomiUser) session.get("user");
//取出该用户的所有订单
List<XmOrder> orderList=obiz.findByProperty("xiaomiUser.UId", user.getUId());
//从库中查询每个订单的订单明细
for(XmOrder order:orderList){
List<XmOrderdetail> detaillist=dbiz.findByProperty("xmOrder.OId",order.getOId());
order.setXmOrderdetails(detaillist);
//取得订单明细中的商品的全部数据
for(XmOrderdetail orderDetail:detaillist){
Long pid=orderDetail.getProductInfo().getPId();
ProductInfo p=(ProductInfo) pbiz.findById(pid);
orderDetail.setProductInfo(p);
}
}
request.put("orderList",orderList);
return SUCCESS;
}
@Action(value="pay",results={
@Result(name="success",type="redirect", location="/myorder.jsp")
})
public String pay(){
String tempaddress="";
String tempcnee="";
String tempphone="";
//取出用户对象
XiaomiUser user=(XiaomiUser) session.get("user");
if("newa".equals(showadd)){
//启用新地址
XiaomiAddress address=new XiaomiAddress();
address.setAddress(newaddress);
address.setCnee(cnee);
address.setPhone(phone);
address.setXiaomiUser(user);
abiz.save(address);
tempaddress=newaddress;
tempcnee=cnee;
tempphone=phone;
}else {
List<XiaomiAddress> list = (List<XiaomiAddress>) session
.get("addressList");
XiaomiAddress a = list.get(rdoAddress);
tempaddress = a.getAddress();
tempphone = a.getPhone();
tempcnee = a.getCnee();
}
//添加新订单
XmOrder order=new XmOrder();
order.setAddress(tempaddress);
order.setBeizhu(beizhu);
order.setCnee(tempcnee);
order.setODate(new Timestamp(new Date().getTime()));
order.setPhone(tempphone);
order.setStatus("待发货");
order.setTotal(total);
order.setXiaomiUser(user);
//添加新订单明细
List<XmOrderdetail> odList=new ArrayList<XmOrderdetail>();
Map<Long,Car> carMap=(Map<Long, Car>) session.get("carMap");
if(pids!=null){
for(Long pid:pids){
Car car=carMap.get(pid);//取出购物车集合中对应的购物车
XmOrderdetail od=new XmOrderdetail();
od.setODate(new Timestamp(new Date().getTime()));
od.setPNumber(car.getNumber());
od.setProductInfo(car.getProduct());
od.setTotal(car.getProduct().getPPrice()*car.getNumber());
odList.add(od);
}
}
obiz.addOrderInfo(order, odList);
//订单生成后,清空购物车中被结算过的商品
if(pids!=null){
for(Long pid:pids){
carMap.remove(pid);
}
}
session.put("msg","支付成功!订单将尽快发货!");
return SUCCESS;
}
@Action(value="jiesuan",results={
@Result(name="success",location="/jiesuan.jsp")
})
public String jiesuan(){
XiaomiUser u=(XiaomiUser) session.get("user");
List<XiaomiAddress> addresslist=abiz.findByProperty("xiaomiUser.UId",u.getUId());
session.put("addressList",addresslist);
return SUCCESS;
}
@Action(value="byid",results={
@Result(name="success",location="/productdetail.jsp")
})
public String getById(){
ProductInfo pinfo=(ProductInfo) pbiz.findById(pid);
request.put("product",pinfo);
return SUCCESS;
}
@Action(value="jiancarshop",results={
@Result(name="success",type="redirect",location="/carshop.jsp")
})
public String jiancarshop(){
Map<Long ,Car> carMap=(Map<Long, Car>) session.get("carMap");
//有的话数量减1
Car car=carMap.get(pid);
if(car.getNumber()>1)
car.setNumber(car.getNumber()-1);
return SUCCESS;
}
@Action(value="addcarshop",results={
@Result(name="success", type="redirect", location="/carshop.jsp")
})
public String addCarShop(){
ProductInfo product=(ProductInfo) pbiz.findById(pid);
//从session中取出购物车集合
Map<Long ,Car> carMap=(Map<Long, Car>) session.get("carMap");
//放入购物车判断
if(carMap.containsKey(product.getPId())){
//数量加1
Car car=carMap.get(pid);
car.setNumber(car.getNumber()+1);
}else{
//重新放入
Car car=new Car();
car.setProduct(product);
car.setNumber(1l);
carMap.put(pid,car);
}
session.put("carMap",carMap);
return SUCCESS;
}
@Action(value="delete")
public String deletecarshop(){
//取出session中的购物车集合,直接使用pid删除
Map<Long,Car> carMap=(Map<Long, Car>) session.get("carMap");
carMap.remove(pid);
session.put("carMap",carMap);
return null;
}
public Long getPid() {
return pid;
}
public void setPid(Long pid) {
this.pid = pid;
}
@Override
public void setRequest(Map<String, Object> arg0) {
this.request=arg0;
}
@Override
public void setSession(Map<String, Object> arg0) {
this.session=arg0;
}
public String getShowadd() {
return showadd;
}
public void setShowadd(String showadd) {
this.showadd = showadd;
}
public String getBeizhu() {
return beizhu;
}
public void setBeizhu(String beizhu) {
this.beizhu = beizhu;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public String getNewaddress() {
return newaddress;
}
public void setNewaddress(String newaddress) {
this.newaddress = newaddress;
}
pu