|
|
package cn.itbaizhan.action;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import javax.annotation.Resource;
|
|
|
import org.springframework.context.annotation.Scope;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import cn.itbaizhan.po.Commodity;
|
|
|
import cn.itbaizhan.po.CommodityClass;
|
|
|
import cn.itbaizhan.service.CommodityClassService;
|
|
|
import cn.itbaizhan.service.CommodityService;
|
|
|
import com.opensymphony.xwork2.ActionContext;
|
|
|
import com.opensymphony.xwork2.ActionSupport;
|
|
|
@SuppressWarnings("serial")
|
|
|
@Component("addToCarAction")
|
|
|
@Scope("prototype")//多例,每个请求生成一个新的action
|
|
|
public class AddToCarAction extends ActionSupport {
|
|
|
// 业务逻辑层,用于处理商品相关的业务操作
|
|
|
private CommodityService commodityService; // 商品服务对象
|
|
|
private Commodity commodity; // 商品对象
|
|
|
// 购买的商品数量
|
|
|
private Integer comamount;
|
|
|
// 商品的总价格
|
|
|
// 商品服务类的实例,用于处理商品相关的业务逻辑
|
|
|
private double totalPrice=0;
|
|
|
}
|
|
|
// 获取商品服务实例的方法
|
|
|
public CommodityService getCommodityService() {
|
|
|
// 返回商品服务实例
|
|
|
return commodityService;
|
|
|
}
|
|
|
@Resource
|
|
|
// 设置商品服务
|
|
|
// 设置商品服务实例的方法
|
|
|
public void setCommodityService(CommodityService commodityService) {
|
|
|
this.commodityService = commodityService;
|
|
|
}
|
|
|
// 设置商品的方法
|
|
|
// 设置商品信息的方法
|
|
|
public void setCommodity(Commodity commodity) {
|
|
|
this.commodity = commodity;
|
|
|
}
|
|
|
// 获取商品信息的方法
|
|
|
public Commodity getCommodity() {
|
|
|
// 返回商品对象
|
|
|
return commodity;
|
|
|
}
|
|
|
// 设置商品数量的方法
|
|
|
// 设置商品数量的方法
|
|
|
public void setComamount(Integer comamount) {
|
|
|
this.comamount = comamount;
|
|
|
}
|
|
|
// 获取商品数量的方法
|
|
|
public Integer getComamount() {
|
|
|
// 返回商品数量的方法
|
|
|
return comamount;
|
|
|
}
|
|
|
public void setTotalPrice(double totalPrice) {
|
|
|
// 设置总价的方法
|
|
|
this.totalPrice = totalPrice;
|
|
|
}
|
|
|
// 获取购物车中所有商品的总价
|
|
|
public double getTotalPrice() {
|
|
|
// 返回总价
|
|
|
return totalPrice;
|
|
|
}
|
|
|
@SuppressWarnings("unchecked")
|
|
|
public String execute() throws Exception {
|
|
|
// 获取商品ID
|
|
|
int commodityId= commodity.getCommodityId();
|
|
|
System.out.println("商品id:"+commodityId);
|
|
|
// 获取session对象
|
|
|
Map session =(Map) ActionContext.getContext().getSession();
|
|
|
// 根据商品ID查找商品信息
|
|
|
Commodity commoditys = commodityService.findCommodityById(commodityId);
|
|
|
// 如果商品库存为0,提示商品无货并返回错误信息
|
|
|
if(commoditys.getCommodityLeaveNum()==0){
|
|
|
// 将“商品已无货!”的信息存入当前会话中
|
|
|
ActionContext.getContext().getSession().put("comnull","商品已无货!");
|
|
|
return "error";
|
|
|
}
|
|
|
else{
|
|
|
// 初始化购物车
|
|
|
List<Commodity> car = null; //声明一个购物车
|
|
|
// 从session中获取购物车,如果不存在则创建一个新的购物车
|
|
|
if(session.get("car") == null) {
|
|
|
car = new ArrayList<Commodity>(); //新建一个ArrayList实例
|
|
|
car.add(commoditys);//将商品添加到购物车中
|
|
|
// 更新商品的剩余库存数量,减去1
|
|
|
commoditys.setCommodityLeaveNum(commoditys.getCommodityLeaveNum()-1);
|
|
|
// 更新商品信息
|
|
|
commodityService.update(commoditys);
|
|
|
}
|
|
|
else {
|
|
|
car = (List<Commodity>)session.get("car"); //取得购物车
|
|
|
// 遍历购物车,检查商品是否已存在
|
|
|
for(int i = car.size(); i > 0; i--){
|
|
|
Commodity com = car.get(i-1); //获取商品
|
|
|
if(com.getCommodityId() == commodityId) { //商品已经存在
|
|
|
// 商品已存在,无需重复添加
|
|
|
} else { //商品不存在
|
|
|
car.add(commoditys); //将商品添加到购物车中
|
|
|
commoditys.setCommodityLeaveNum(commoditys.getCommodityLeaveNum()-1);
|
|
|
```
|
|
|
```
|
|
|
/**
|
|
|
* 此方法用于将商品的剩余数量减少一个
|
|
|
*/
|
|
|
// 更新商品信息
|
|
|
commodityService.update(commoditys);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
// 计算购物车中商品的总价
|
|
|
// 获取购物车中商品的迭代器
|
|
|
// 创建一个迭代器来遍历购物车中的商品
|
|
|
Iterator<Commodity> it = car.iterator();
|
|
|
// 使用 for 循环遍历购物车中的商品,并在每次循环结束时减少计数器 i 的值
|
|
|
for(int i = car.size();it.hasNext();i--){
|
|
|
Commodity com = it.next();
|
|
|
// 将当前商品的价格累加到总价中
|
|
|
totalPrice+=com.getCommodityPrice();
|
|
|
}
|
|
|
// 将购物车和总价保存在session中
|
|
|
session.put("car", car);//将购物车保存在session中
|
|
|
session.put("totalPrice", totalPrice);//将总价保存在session中
|
|
|
// 返回操作成功的字符串标识
|
|
|
return "success";
|
|
|
}
|
|
|
}
|
|
|
@SuppressWarnings("unchecked")
|
|
|
public String deleteFromCar(){
|
|
|
// 获取商品ID
|
|
|
int commodityId= commodity.getCommodityId();
|
|
|
System.out.println("商品id:"+commodityId);
|
|
|
// 根据商品ID查找商品信息并恢复其库存
|
|
|
// 根据商品ID查找商品
|
|
|
Commodity commoditys = commodityService.findCommodityById(commodityId);
|
|
|
// 增加商品的库存数量
|
|
|
commoditys.setCommodityLeaveNum(commoditys.getCommodityLeaveNum()+1);
|
|
|
// 更新商品信息
|
|
|
commodityService.update(commoditys);
|
|
|
// 获取session对象和购物车
|
|
|
Map session = ActionContext.getContext().getSession();//获得session对象
|
|
|
List<Commodity> car = (List<Commodity>)session.get("car");//取得购物车
|
|
|
// 遍历购物车,删除指定的商品
|
|
|
//此代码片段用于遍历购物车中的商品,并根据给定的商品ID删除特定商品。
|
|
|
Iterator<Commodity> it = car.iterator();
|
|
|
// 获取购物车中商品的迭代器,以便遍历购物车中的商品项。
|
|
|
// 遍历购物车中的商品,反向迭代
|
|
|
for(int i = car.size();it.hasNext();i--){
|
|
|
Commodity com = it.next();
|
|
|
//此循环用于遍历购物车中的商品,每次迭代从商品总数中减一,并获取下一个商品。
|
|
|
// 检查当前商品的ID是否与传入的商品ID一致
|
|
|
if(com.getCommodityId() == commodityId) {
|
|
|
it.remove(); //这行代码是关键。删除该商品
|
|
|
}
|
|
|
}
|
|
|
// 计算购物车中商品的总价
|
|
|
// 获取购物车中商品的迭代器
|
|
|
Iterator<Commodity> is = car.iterator();
|
|
|
// 遍历购物车中的所有商品,计算总价
|
|
|
for(int i = car.size();is.hasNext();i--){
|
|
|
// 从输入流中读取下一个商品对象
|
|
|
Commodity com = is.next();
|
|
|
// 将当前商品的价格累加到总价格中
|
|
|
totalPrice+=com.getCommodityPrice();
|
|
|
}
|
|
|
// 遍历购物车中的商品,从后向前迭代
|
|
|
// 对于每个商品,获取其价格并累加到总价中
|
|
|
// 将更新后的购物车和总价保存在session中
|
|
|
session.put("car",car);//将重新购物车保存到session中
|
|
|
return "delete";
|
|
|
}
|
|
|
} |