func (this *shoppingService) parseDtoCart(c shopping.ICart) *dto.ShoppingCart { var cart = new(dto.ShoppingCart) v := c.GetValue() cart.Id = c.GetDomainId() cart.BuyerId = v.BuyerId cart.CartKey = v.CartKey cart.UpdateTime = v.UpdateTime t, f := c.GetFee() cart.TotalFee = t cart.OrderFee = f cart.Summary = c.GetSummary() cart.IsBought = v.IsBought if v.Items != nil { if l := len(v.Items); l != 0 { cart.Items = make([]*dto.CartItem, l) for i, v := range v.Items { cart.Items[i] = &dto.CartItem{ GoodsId: v.GoodsId, GoodsName: v.Name, GoodsNo: v.GoodsNo, SmallTitle: v.SmallTitle, GoodsImage: v.Image, Num: v.Num, Price: v.Price, SalePrice: v.SalePrice, } } } } return cart }
// 将购物车转换为订单 func (this *Shopping) ParseShoppingCart(memberId int) (shopping.IOrder, member.IMember, shopping.ICart, error) { var order shopping.IOrder var val shopping.ValueOrder var cart shopping.ICart var m member.IMember var err error m = this._memberRep.GetMember(memberId) if m == nil { return nil, m, nil, member.ErrSessionTimeout } cart, err = this.GetCurrentCart(memberId) if err != nil || cart == nil || len(cart.GetValue().Items) == 0 { return nil, m, cart, shopping.ErrEmptyShoppingCart } val.MemberId = memberId val.PartnerId = this._partnerId tf, of := cart.GetFee() val.TotalFee = tf //总金额 val.Fee = of //实际金额 val.PayFee = of val.DiscountFee = tf - of //优惠金额 val.PartnerId = this._partnerId val.Status = 1 order = this.CreateOrder(&val, cart) return order, m, cart, nil }
// 合并购物车,并返回新的购物车 func (this *Cart) Combine(c shopping.ICart) (shopping.ICart, error) { if c.GetDomainId() != this.GetDomainId() { for _, v := range c.GetValue().Items { this.AddItem(v.GoodsId, v.Num) } } return this, nil }
// 获取购物车 func (this *shoppingService) getShoppingCart(partnerId int, memberId int, cartKey string) shopping.ICart { sp := this._rep.GetShopping(partnerId) var c shopping.ICart var mc shopping.ICart var skIsNil = len(cartKey) == 0 var mmNotNil = memberId != 0 if mmNotNil { mc, _ = sp.GetNotBoughtCart(memberId) if mc != nil && (skIsNil || mc.GetValue().CartKey == cartKey) { return mc } } if !skIsNil { // 根据Key获取购物车 c, _ = sp.GetCart(cartKey) if c == nil { // 新的购物车不存在,直接返回会员的购物车 if mc != nil { return mc } } else { cv := c.GetValue() //合并购物车 if cv.BuyerId <= 0 { // 设置购买者 if mmNotNil { c.SetBuyer(memberId) } } else if mc != nil { // 合并购物车 nc, err := mc.Combine(c) if err == nil { nc.Save() return nc } return mc } // 如果没有购买,则返回 if cv.IsBought == 0 { return c } } } // 返回一个新的购物车 return sp.NewCart(memberId) }
// 检查购物车 func (this *Shopping) CheckCart(cart shopping.ICart) error { if cart == nil || len(cart.GetValue().Items) == 0 { return shopping.ErrEmptyShoppingCart } sl := this._saleRep.GetSale(this._partnerId) for _, v := range cart.GetValue().Items { goods := sl.GetGoods(v.GoodsId) if goods == nil { return sale.ErrNoSuchGoods // 没有商品 } stockNum := goods.GetValue().StockNum if stockNum == 0 { return sale.ErrFullOfStock // 已经卖完了 } if stockNum < v.Quantity { return sale.ErrOutOfStock // 超出库存 } } return nil }
func (this *Shopping) GetShoppingCart(buyerId int, cartKey string) shopping.ICart { var hasOutCart = len(cartKey) != 0 var hasBuyer = buyerId != 0 var memCart shopping.ICart = nil // 消费者的购物车 var outCart shopping.ICart = nil // 通过cartKey传入的购物车 if hasBuyer { // 如果没有传递cartKey ,或者传递的cart和会员绑定的购物车相同,直接返回 if memCart, _ = this.GetCurrentCart(buyerId); memCart != nil { if !hasOutCart || memCart.GetValue().CartKey == cartKey { return memCart } } else { memCart = this.NewCart(buyerId) } } if hasOutCart { outCart, _ = this.GetCartByKey(cartKey) } // 合并购物车 if outCart != nil && hasBuyer { if bid := outCart.GetValue().BuyerId; bid <= 0 || bid == buyerId { memCart, _ = memCart.Combine(outCart) outCart.Destroy() memCart.Save() } } if memCart != nil { return memCart } if outCart != nil { return outCart } return this.NewCart(buyerId) // if !hasOutCart { // if c == nil { // // 新的购物车不存在,直接返回会员的购物车 // if mc != nil { // return mc // } // } else { // cv := c.GetValue() // //合并购物车 // if cv.BuyerId <= 0 { // // 设置购买者 // if hasBuyer { // c.SetBuyer(buyerId) // } // } else if mc != nil && cv.BuyerId == buyerId { // // 合并购物车 // nc, err := mc.Combine(c) // if err == nil { // nc.Save() // return nc // } // return mc // } // // // 如果没有购买,则返回 // return c // } // } // 返回一个新的购物车 // return this.NewCart(buyerId) }