spring+springmvc+hibernate製作一個簡單的電商網站(3)購物車
購物車是電商網站重要的模組之一,也是有一定難度的模組。
這裡主要是教大家大概的實現思路,我還是不會把原始碼寫出來讓各位Ctrl CV ,因為專案是保密的而且只會Ctrl CV的不算是開發工程師
一,購物車
購物車這個模組因為需求不同,各個網站的購物車功能也不同,有的網站讓你登陸後才可以新增購物車,有的網站是可以不登陸就新增,在這裡就說說登陸後新增的購物車吧。
資料庫中有兩張表:StoreCart GoodsCart
StoreCart是用來建立購物車的表裡面存的是使用者和店鋪
GoodsCart是用來儲存購物車裡面所有商品的資訊的與StoreCart關聯,多對1
新增購物車
@RequsetMapping(value = "/xxx/xxx/add_goods_cart")
public void add_goods_cart(HttpServletRequest request, HttpServletResponse response, String id, String count, String pirce, String gsp, String msg){
List<StoreCart> cart = new ArrayList<StoreCart>();
List<StoreCart> user_cart = new ArrayList<StoreCart>();//登陸狀態的購物車
//這裡可以判斷一下庫存是否充足,我就不寫了
User user = LoginContext.getSesssionValueByLogin(request);
Map map = new HashMap();
StoreCart sc;
if(user != null){
if(user.getStore != null){ //這裡要判斷使用者是否有自己的店鋪,防止使用者自己購買自己的東西
if(this.goodsService.getObjById(Long.parseLong(id)).getGoods_store().getStore_name().equals(user.getStore().getStore_name())){
return;
}
map.clear();
map.put("user_id",user.getId());
map.put("sc_status",Integer.valueOf(0));
user_cart = this.storeCartService.query()
}
}else{return;}
//遍歷買家登陸後的購物車,你需要獲取到買家購物車裡是否有商品防止新增相同的。
for(StoreCart sc12 : user_cart){
boolean sc_add = true;
for(StoreCart sc1 : cart){
if(sc1.getStore().getId().equals(sc12.getStore().getId)){
sc_add = false;
}
}
if(sc_add){cart.add(sc12);}
}
//上面那些是為了得到買家在沒新增商品之前的購物車資料,下面開始買家選擇的商品規格
String[] gsp_ids = gsp.split(",");
Arrays.sort(gsp_ids);
boolean add = true;
double total_price = 0.0D;
int total_count = 0;
String[] gsp_ids1;//已有購物車內的商品規格
//遍歷購物車去判斷購物車內是否包含當前所選規格的商品。
for(StoreCart sc1 : cart){
for(GoodsCart gc : sc1.getGcs()){
if((gsp_ids != null) && (gsp_ids.length > 0) && (gc.getGsps() != null) && (gc.getGsps().size()) > 0){//這裡有一個商品的規格類如何判斷就靠自己了}
}
}
if(//所選規格可以新增購物車時候){
Goods goods = this.goodsService.getObjById(Long.parseLong(id));
//這裡在遍歷一遍,檢視是要更新購物車內的商品,還是增加商品 , update / save;
StoreCart sc33 = new StoreCart();
for(StroeCart sc1 : cart){sc33 = sc1;}
this.storeCartService.save(sc33 ); / this.storeCartService.update(sc33);
//處理購物車的明細
GoodsCart obj = new GoodsCart();
obj.setAddTime(new Date());
obj.setCount(Integer.parseInt(count));
obj.setPrice(Double.parseDouble(price));
obj.setGoods(goods);
//解析一下商品的SKU,這裡就不寫了,自己設計吧。
GoodsSpecProperty spec; ...
obj.setSc(sc33);
this.goodsCartService.save(obj);
sc33.getGcs().add(obj);//將購物車商品明細新增到購物車內
//然後計算一下總價格,最後更新一下購物車就OK了
}
}
檢視購物車
檢視購物車就比較簡單了
在確保使用者登陸的時候,使用使用者id在StoreCart表中查詢資料,在查詢的時候也要確保一下使用者是否是賣家並且是否購買了自己家的商品,這些都沒問題就把查詢到的購物車資訊返回給前臺處理。
@RequestMapping(value = "goods_cart1")
public void goods_cart1(HttpServeltRequest request, HttpServeltResponse response){
Map map = new HashMap();
map.clear();
map.put("user_id",user.getId());
map.put("sc_status",Integer.valueOf(0));
List<StoreCart> cart = this.storeCartService.query("select obj from StoreCart obj where obj.user.id=:user_id and obj.sc_status=:sc_status",map);
if(cart != null){
Store store = ((User)LoginContext.getSessionValueByLogin(request)).getStore != null ? ((User)LoginContext.getSessionValueByLogin(request)).getStore != null : null;
for(StoreCart sc : cart){
if(sc.getStore().getId().equals(store.getId())){
for(GoodsCart gc : sc.getGcs())){
gc.getGsps().clear();
this.goodsCartService.delete(gc.getId());
}
sc.getGcs().clear();
this.storeCartService.delete(sc.getId());
}
}
}
//返回使用者購物車的資訊就可以了
}