Java生鮮電商平台-購物車模塊的設計與架構
說明:任何一個電商無論是B2C還是B2B都有一個購物車模塊,其中最重要的原因就是客戶需要的東西放在一起,形成一個購物清單,確認是否有問題,然后再進行下單與付款.
1. 購物車數據庫設計:

說明:業務需求:
1》購物車里面應該存放,那個買家,買了那個菜品的什么規格,有多少數量,然后這個菜品的加工方式如何。(如果存在加工方式的話,就會在這里顯示處理。)
2》買家存在購物起送價。也就是用戶放入購物車的商品的總價格如果低於配置的起送價,那么這個提交按鈕就是灰色的。(不可能你點一個洋蔥我們就送過去,成本太高。)
系統設計:
1. 購物車在買家APP上進行,這個時候是不需要跟后端API交互的,因為體驗很差勁,用戶在APP頁面中不停的點擊加菜以及菜的數量,如果根據后端進行交互,哪怕是每次請求是100ms,頁面會存在很嚴重的抖動行為,速度快的話會出現卡頓,這個是不行的。
2. 在用戶確定完成后,確認下單的時候,提交購物車,讓后端可以存儲用戶購物車的數據。(確認下單過程)
3. 在用戶去付款的時候,也就是提交了訂單。這個時候再清理購物車。
相關后端代碼如下:
/**
* 購物車
*/
@RestController
@RequestMapping("/buyer/goodsCart")
public class GoodsCartController extends BaseController{
private static final Logger logger = LoggerFactory.getLogger(GoodsCartController.class);
@Autowired
private GoodsCartService goodsCartService;
@Autowired
private GoodsFormatService goodsFormatService;
/**
* 生成購物車;
* 1:先刪除歷史購物車;
* 2:新增購物車數據;
*/
@RequestMapping(value="/commitCart",method={RequestMethod.GET,RequestMethod.POST})
public JsonResult commitCar(HttpServletRequest request, HttpServletResponse response,@RequestBody GoodsListVo goodsListVo)
{
try
{
List<GoodsCart> list = goodsListVo.getList();
if(null == goodsListVo.getUserId() || null == list){
return new JsonResult(JsonResultCode.FAILURE, "請求參數異常", "");
}
for (int i = 0; i < list.size(); i++) {
if(null ==list.get(i).getBuyerId() || null == list.get(i).getFormatId()){
return new JsonResult(JsonResultCode.FAILURE, "請求參數異常", "");
}
}
int result = goodsCartService.commitCart(list,goodsListVo.getUserId());
//更新購物車為空 則直接返回;
if(list.size()<1){
return new JsonResult(JsonResultCode.SUCCESS, "更新成功", new ArrayList<>());
}
Long buyerId = list.get(0).getBuyerId();
List<GoodsCartVo> goodsCartBuyers = goodsCartService.getGoodsCartBuyerId(buyerId);
if (result == list.size())
{
return new JsonResult(JsonResultCode.SUCCESS, "新增成功", goodsCartBuyers);
}
return new JsonResult(JsonResultCode.SUCCESS, "有下架商品", goodsCartBuyers);
}catch(Exception e){
logger.error("[GoodsCartController][commitCart]",e);
return new JsonResult(JsonResultCode.FAILURE, "系統錯誤,請稍后重試","");
}
}
/**
* 對單個數據進行修改 新增 刪除;
* @param request
* @param response
* @param goodsCart
* @return
*/
@RequestMapping(value="/updateCart",method={RequestMethod.GET,RequestMethod.POST})
public JsonResult updateCart(HttpServletRequest request, HttpServletResponse response,@RequestBody GoodsCart goodsCart)
{
try
{
BigDecimal goodsNumber=goodsCart.getGoodsNumber();
Long formartId=goodsCart.getFormatId();
BigDecimal count = goodsFormatService.getGoodsFormatById(formartId).getFormatPrice().multiply(goodsNumber);
goodsCart.setCreateTime(new Date());
int result = goodsCartService.updateGoodsCart(goodsCart);
if (result > 0)
{
return new JsonResult(JsonResultCode.SUCCESS, "操作成功", count);
}
return new JsonResult(JsonResultCode.FAILURE, "操作失敗", count);
}catch(Exception e){
logger.error("[GoodsCartController][updateCart]",e);
return new JsonResult(JsonResultCode.FAILURE, "系統錯誤,請稍后重試","");
}
}
/**
*
* @param request
* @param response
* @param goodsCart
* @return
*/
@RequestMapping(value="/insertCart",method={RequestMethod.GET,RequestMethod.POST})
public JsonResult insertCart(HttpServletRequest request, HttpServletResponse response,@RequestBody GoodsCart goodsCart)
{
try
{
BigDecimal goodsNumber=goodsCart.getGoodsNumber();
Long formartId=goodsCart.getFormatId();
BigDecimal count = goodsFormatService.getGoodsFormatById(formartId).getFormatPrice().multiply(goodsNumber);
goodsCart.setCreateTime(new Date());
int result = goodsCartService.insertGoodsCart(goodsCart);
if (result > 0)
{
return new JsonResult(JsonResultCode.SUCCESS, "操作成功", count);
}
return new JsonResult(JsonResultCode.FAILURE, "操作失敗", count);
}catch(Exception e){
logger.error("[GoodsCartController][insertCart]",e);
return new JsonResult(JsonResultCode.FAILURE, "系統錯誤,請稍后重試","");
}
}
/**
* 根據cartId 刪除單個購物車項;
* @param request
* @param response
* @param cartId
* @return
*/
@RequestMapping(value="/deleteCart",method={RequestMethod.GET,RequestMethod.POST})
public JsonResult deleteCar(HttpServletRequest request, HttpServletResponse response, Long cartId)
{
try
{
if(null == cartId){
return new JsonResult(JsonResultCode.FAILURE, "請求參數異常", "");
}
int result = goodsCartService.deleteGoodsCart(cartId);
if (result > 0)
{
return new JsonResult(JsonResultCode.SUCCESS, "刪除成功", "");
} else
{
return new JsonResult(JsonResultCode.FAILURE, "數據已不存在", "");
}
}catch(Exception e){
logger.error("[GoodsCartController][deleteCart]",e);
return new JsonResult(JsonResultCode.FAILURE, "系統錯誤,請稍后重試","");
}
}
@RequestMapping(value="/showCart",method={RequestMethod.GET,RequestMethod.POST})
public JsonResult showCart(HttpServletRequest request, HttpServletResponse response,@RequestBody GoodsListVo goodsListVo) {
try
{
List<GoodsCart> list = goodsListVo.getList();
Long buyerId = goodsListVo.getUserId();
Long sellerId = goodsListVo.getSellerId();
if(null == buyerId ||null == sellerId){
return new JsonResult(JsonResultCode.FAILURE, "請求參數異常", "");
}
goodsCartService.commitCartByBuyerIdSellerId(list,buyerId, sellerId);
List<ProductVo> productVos = goodsCartService.getGoodsCartListBySellerId(buyerId, sellerId);
return new JsonResult(JsonResultCode.SUCCESS, "查詢成功", productVos);
}catch(Exception e){
logger.error("[GoodsCartController][showCart]",e);
return new JsonResult(JsonResultCode.FAILURE, "系統錯誤,請稍后重試","");
}
}
}
基本業務功能:
1.購物車可以清空。
2. 購物車可以提交。
3. 購物車可以更新。
4. 購物車也可以查詢。
補充說明:那么APP端提交給后端的API的對象應該是怎么樣的呢?
以下貼出代碼與講解:
public class GoodsCart implements Serializable{
private static final long serialVersionUID = 7078019879911908296L;
/**
*
*/
private Long cartId;
/**
* 買家ID
*/
private Long buyerId;
/**
* 商品規格id
*/
private Long formatId;
/**
* 所屬賣家ID
*/
private Long sellerId;
/**
* 店鋪名稱
*/
private String sellerName;
/**
* 商品數量
*/
private BigDecimal goodsNumber;
/**
* 加工方式ID
*/
private Long methodId;
/**
* 是否選擇 (1是 -1否)
*/
private Integer isSelected;
/**
* 創建時間
*/
private Date createTime;
/**
* 查詢創建時間
*/
private String queryTime;
/**
* 買家
*/
private Buyer buyer;
/**
* 賣家
*/
private Seller seller;
/**
*sku
*/
private GoodsFormat goodsFormat;
/**
* 加工方式
*/
private ProcessMethod processMethod;
public String getSellerName() {
return sellerName;
}
public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}
public Long getCartId() {
return cartId;
}
public void setCartId(Long cartId) {
this.cartId = cartId;
}
public Long getBuyerId() {
return buyerId;
}
public void setBuyerId(Long buyerId) {
this.buyerId = buyerId;
}
public Long getFormatId() {
return formatId;
}
public void setFormatId(Long formatId) {
this.formatId = formatId;
}
public Long getSellerId() {
return sellerId;
}
public void setSellerId(Long sellerId) {
this.sellerId = sellerId;
}
public BigDecimal getGoodsNumber() {
return goodsNumber;
}
public void setGoodsNumber(BigDecimal goodsNumber) {
this.goodsNumber = goodsNumber;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Buyer getBuyer() {
return buyer;
}
public void setBuyer(Buyer buyer) {
this.buyer = buyer;
}
public Seller getSeller() {
return seller;
}
public void setSeller(Seller seller) {
this.seller = seller;
}
public GoodsFormat getGoodsFormat() {
return goodsFormat;
}
public void setGoodsFormat(GoodsFormat goodsFormat) {
this.goodsFormat = goodsFormat;
}
public String getQueryTime() {
return queryTime;
}
public void setQueryTime(String queryTime) {
this.queryTime = queryTime;
}
public Long getMethodId() {
return methodId;
}
public void setMethodId(Long methodId) {
this.methodId = methodId;
}
public Integer getIsSelected() {
return isSelected;
}
public void setIsSelected(Integer isSelected) {
this.isSelected = isSelected;
}
public ProcessMethod getProcessMethod() {
return processMethod;
}
public void setProcessMethod(ProcessMethod processMethod) {
this.processMethod = processMethod;
}
講解:
1.這個菜品的規格,以及所屬賣家,買家,包括是否需要加工等等。(比如買家買了魚,這個魚到底是需要怎么樣處理呢?活魚,肚殺,背殺),特別說明:這個跟實際的業
務有關,如果不是做生鮮這塊的話,可能很難體會。
2. 買家肯定會買多個菜品,而不是一個,所以需要有一個List<GoodsCart> list;
相關實際代碼如下:
public class GoodsListVo implements Serializable{
/**
*
*/
private static final long serialVersionUID = -2024011567608945523L;
private List<GoodsCart> list;
private Long userId;
private Long sellerId;
public List<GoodsCart> getList() {
return list;
}
public void setList(List<GoodsCart> list) {
this.list = list;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public Long getSellerId() {
return sellerId;
}
public void setSellerId(Long sellerId) {
this.sellerId = sellerId;
}
}
Java開源生鮮電商平台-購物車模塊的設計與架構(源碼可下載),如果需要下載的話,可以在我的github下面進行下載。
相關的運營截圖如下:





