首页>代码>高仿京东商城SSH源代码下载>/京东源码/JingDongSSH/src/com/softeem/jingdong/action/GoodsAction.java
package com.softeem.jingdong.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.softeem.jingdong.entity.GoodsBean;
import com.softeem.jingdong.entity.ShoppingCarBean;
import com.softeem.jingdong.entity.UsersBean;
import com.softeem.jingdong.service.GoodsService;
import com.softeem.jingdong.service.ShoppingCarService;

/***
 * 商品信息
 * @author Administrator
 *
 */
public class GoodsAction extends ActionSupport {

	private static final long serialVersionUID = 8979942382300596390L;
	private GoodsService goodsService;
	private ShoppingCarService scs;
	private int id;
	private List<GoodsBean> list = new ArrayList<GoodsBean>();
	private GoodsBean goods = new GoodsBean();
	private String key;
	
	/**
	 * 用户首页商品列表
	 */
	public String getGoodsList(){
		//调用方法获取商品集合
		list = goodsService.getAllGoods();
		return SUCCESS; 
	}
	
	/**
	 * 查看商品详细信息
	 * @return
	 */
	public String getGoodsInfo(){
		goods = goodsService.getGoodsById(id);
		return SUCCESS;
	}
	
	/**
	 * 购买商品
	 * @return
	 */
	public String buyGoods(){
		
		//获取 Session 
		HttpSession session = ServletActionContext.getRequest().getSession(true);
		
		//获取用户
		UsersBean users = (UsersBean)session.getAttribute("users");
		
		//获取用户的购物车信息
		ShoppingCarBean shoppingCar = users.getShoppingCar();
		
		//将商品添加到购物车里面
		GoodsBean goods = scs.addGoodsToShoppingCar(id);
		shoppingCar.getList().add(goods);
		
		//设置购物车总价
		shoppingCar.setSumPrice( this.getShoppingCarSumPrice( shoppingCar.getList() ) );
		
		//设置商品总数量
		shoppingCar.setNumber( shoppingCar.getList().size() );
		
		return SUCCESS;
	}
	
	/**
	 * 求购物车总价
	 */
	public double getShoppingCarSumPrice(List<GoodsBean> list){
		
		double sum = 0;
		
		for(GoodsBean g : list){
			sum += g.getPrice();
		}
		
		return sum;
	}
	
	/**
	 * 模糊查询商品
	 * @return
	 */
	public String getSearchGoodsList(){
		list = goodsService.getSearchList(key);
		return SUCCESS;
	}

	
	public List<GoodsBean> getList() {
		return list;
	}
	public void setList(List<GoodsBean> list) {
		this.list = list;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public GoodsBean getGoods() {
		return goods;
	}
	public void setGoods(GoodsBean goods) {
		this.goods = goods;
	}

	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}
	public void setScs(ShoppingCarService scs) {
		this.scs = scs;
	}
	public GoodsService getGoodsService() {
		return goodsService;
	}
	public ShoppingCarService getScs() {
		return scs;
	}
	
}
最近下载更多
baoligeer  LV5 2022年6月6日
任星浩  LV1 2021年11月18日
giantinheart  LV3 2021年5月23日
wusiyin  LV14 2020年12月17日
曾学成  LV2 2020年12月14日
refreshbird  LV1 2020年12月14日
方立亮  LV1 2020年11月6日
渺无人烟  LV11 2020年7月31日
cpemy_abc  LV10 2020年6月9日
远大帅比  LV6 2020年5月19日
最近浏览更多
czx5450  LV16 4月1日
linlin01  LV1 1月2日
2486624997  LV1 2023年12月22日
2385649653  LV7 2023年12月12日
limin123  LV6 2023年11月20日
heqian  LV16 2023年10月31日
zhaozhiqi  LV5 2023年10月21日
微信网友_6680567232876544  LV8 2023年10月10日
mengmeng12323423  LV3 2023年9月8日
543539666  LV7 2023年8月30日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友