Java之品优购课程讲义_day16(1)

购物车需求分析与解决方案
1.1 需求分析
用户在商品详细页点击加入购物车,提交商品 SKU 编号和购买数量,添加到购物车。购物车展示页面如下:
Java之品优购课程讲义_day16(1)
1.1 实现思路
购物车数据的存储结构如下:
Java之品优购课程讲义_day16(1)
当用户在未登录的情况下,将此购物车存入 cookies , 在用户登陆的情况下,将购物车数据存入 redis 。如果用户登陆时,cookies 中存在购物车,需要将 cookies 的购物车合并到 redis 中存储.
1.1 工程搭建
(1)创建工程 pinyougou-cart-interface ,依赖 pinyougou-pojo
(2)创 建 工 程 pinyougou-cart-service ( WAR ) , 依 赖 pinyougou-cart-interface 和pinyougou-common 工程 和 spring、 dubbox 等相关依赖, 添加 web.xml 与 spring 配置文件(参照其他 service 工程) tomcat 插件端口设置为 9007 ,dubbo 端口为 20887
(3)创建工程 pinyougou-cart-web ,依赖 pinyougou-cart-interface springsecurity 、 CAS 等。添加 web.xml 与 spring 配置文件(参照其他 web 工程)tomcat 插件端口设置为 9107 , 拷贝 UserDetailServiceImpl.java ,Java之品优购课程讲义_day16(1)

(4)将资源文件夹中 Cookie 工 具 类 拷 贝到 pinyougou-common 工程中。需要在

pinyougou-common 工程引入 servlet-api 依赖

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<scope>provided</scope>

</dependency>

1.1 购物车实体类
在 pinyougou-pojo 的 com.pinyougou.pojogroup 中创建购物车实体类

public  class  Cart  implements  Serializable{

private  String  sellerId;//商家 ID

private  String  sellerName;//商家名称

private  List<TbOrderItem>  orderItemList;//购物车明细

//getter    and  setter    ......

}

这个类是对每个商家的购物车进行的封装

  1. Cookie 存储购物车
    2.1 需求分析
    使用 cookie 存储购物车数据。服务层负责逻辑,控制层负责读写 cookie 。
    2.2 服务接口层
    
    (1)服务层接口        pinyougou-cart-interface 新建 com.pinyougou.cart.service 包,包下建立接口 CartService

/*
购物车服务接口

*@author Administrator

*

*/

public interface CartService {

/**

*添加商品到购物车

*@param cartList

*@param itemId

*@param num

*@return

*/

public List<Cart> addGoodsToCartList(List<Cart> cartList,Long itemId,Integer num );

}

2.1 服务实现层
实现思路:

//1.根据商品 SKU  ID 查询 SKU 商品信息

//2.获取商家 ID

//3.根据商家 ID 判断购物车列表中是否存在该商家的购物车

//4.如果购物车列表中不存在该商家的购物车

//4.1 新建购物车对象

//4.2 将新建的购物车对象添加到购物车列表

//5.如果购物车列表中存在该商家的购物车

// 查询购物车明细列表中是否存在该商品

//5.1.  如果没有,新增购物车明细

//5.2.  如果有,在原购物车明细上添加数量,更改金额

代码实现 pinyougou-cart-service 工程创建 CartServiceImpl.java

/**

*购物车服务实现类

*@author Administrator

*

*/ @Service
public class CartServiceImpl implements CartService {

@Autowired

private TbItemMapper itemMapper;

@Override

public List<Cart> addGoodsToCartList(List<Cart> cartList, Long itemId, Integer num)
{

//1.根据商品 SKU ID 查询 SKU 商品信息

TbItem item = itemMapper.selectByPrimaryKey(itemId);

if(item==null){

throw new RuntimeException("商品不存在");
}

if(!item.getStatus().equals("1")){

throw new RuntimeException("商品状态无效");

}

//2.获取商家 ID

String sellerId = item.getSellerId();

//3.根据商家 ID 判断购物车列表中是否存在该商家的购物车

Cart cart = searchCartBySellerId(cartList,sellerId);

//4.如果购物车列表中不存在该商家的购物车

if(cart==null){

//4.1 新建购物车对象 ,

cart=new Cart(); cart.setSellerId(sellerId); cart.setSellerName(item.getSeller());
TbOrderItem orderItem = createOrderItem(item,num);

List orderItemList=new ArrayList();

orderItemList.add(orderItem);

cart.setOrderItemList(orderItemList);
//4.2 将购物车对象添加到购物车列表

cartList.add(cart);

}else{

//5.如果购物车列表中存在该商家的购物车

// 判断购物车明细列表中是否存在该商品

TbOrderItem orderItem = searchOrderItemByItemId(cart.getOrderItemList(),itemId);

if(orderItem==null){

//5.1. 如果没有,新增购物车明细

orderItem=createOrderItem(item,num); cart.getOrderItemList().add(orderItem);
}else{

//5.2. 如果有,在原购物车明细上添加数量,更改金额
orderItem.setNum(orderItem.getNum()+num); orderItem.setTotalFee(new
BigDecimal(orderItem.getNum()*orderItem.getPrice().doubleValue()) );

//如果数量操作后小于等于 0,则移除

if(orderItem.getNum()<=0){

cart.getOrderItemList().remove(orderItem);//移除购物车明细

}

//如果移除后 cart 的明细数量为 0,则将 cart 移除

if(cart.getOrderItemList().size()==0){
cartList.remove(cart);

}

}

}

return  cartList;

}

/**

*根据商家 ID 查询购物车对象

*@param  cartList

*@param  sellerId

*@return

*/

private  Cart  searchCartBySellerId(List<Cart>  cartList,  String  sellerId){

for(Cart cart:cartList){

if(cart.getSellerId().equals(sellerId)){ return cart;
}

}

return null;

}

/**

*根据商品明细 ID 查询

*@param orderItemList

*@param itemId

*@return

*/

private TbOrderItem searchOrderItemByItemId(List<TbOrderItem> orderItemList ,Long itemId ){

for(TbOrderItem orderItem :orderItemList){

if(orderItem.getItemId().longValue()==itemId.longValue()){ return orderItem;
}

}

return null;

}

/**

*创建订单明细

*@param item

*@param num

*@return

*/

private TbOrderItem createOrderItem(TbItem item,Integer num){

if(num<=0){


throw  new  RuntimeException("数量非法");

}

TbOrderItem  orderItem=new  TbOrderItem(); orderItem.setGoodsId(item.getGoodsId()); orderItem.setItemId(item.getId()); orderItem.setNum(num); orderItem.setPicPath(item.getImage()); orderItem.setPrice(item.getPrice()); orderItem.setSellerId(item.getSellerId()); orderItem.setTitle(item.getTitle());
orderItem.setTotalFee(new  BigDecimal(item.getPrice().doubleValue()*num));

return  orderItem;

}

}

猜你喜欢

转载自blog.51cto.com/13517854/2308267