func (this *orderManagerImpl) SubmitOrder(c cart.ICart, subject string, couponCode string, useBalanceDiscount bool) (order.IOrder, payment.IPaymentOrder, error) { order, py, err := this.PrepareOrder(c, subject, couponCode) if err != nil { return order, py, err } orderNo, err := order.Submit() tradeNo := orderNo if err == nil { cv := c.GetValue() cv.PaymentOpt = enum.PaymentOnlinePay pyUpdate := false //todo: 设置配送门店 //err = order.SetShop(cv.ShopId) //err = order.SetDeliver(cv.DeliverId) // 设置支付方式 if err = py.SetPaymentSign(cv.PaymentOpt); err != nil { return order, py, err } // 处理支付单 py.BindOrder(order.GetAggregateRootId(), tradeNo) if _, err = py.Save(); err != nil { err = errors.New("下单出错:" + err.Error()) //todo: 取消订单 //order.Cancel(err.Error()) domain.HandleError(err, "domain") return order, py, err } // 使用余额支付 if useBalanceDiscount { err = py.BalanceDiscount() pyUpdate = true } // 如果已支付完成,则将订单设为支付完成 if v := py.GetValue(); v.FinalFee == 0 && v.State == payment.StateFinishPayment { for _, sub := range order.GetSubOrders() { sub.PaymentFinishByOnlineTrade() } } // 更新支付单 if err == nil && pyUpdate { _, err = py.Save() } } return order, py, err }
// 读取购物车数据,用于预生成订单 func (o *orderImpl) RequireCart(c cart.ICart) error { if o.GetAggregateRootId() > 0 || o._cart != nil { return order.ErrRequireCart } items := c.GetValue().Items if len(items) == 0 { return cart.ErrEmptyShoppingCart } // 绑定结算购物车 o._cart = c // 将购物车的商品分类整理 o._vendorItemsMap = o.buildVendorItemMap(items) // 更新订单的金额 o._vendorExpressMap = o.updateOrderFee(o._vendorItemsMap) // 状态设为待支付 o._value.State = 1 return nil }
// 将购物车转换为订单 func (this *orderManagerImpl) ParseToOrder(c cart.ICart) (order.IOrder, member.IMember, error) { var m member.IMember err := this.checkCartForOrder(c) if err != nil { return nil, m, err } val := &order.Order{} // 判断购买会员 buyerId := c.GetValue().BuyerId if buyerId > 0 { val.BuyerId = buyerId m = this._memberRep.GetMember(val.BuyerId) } if m == nil { return nil, m, member.ErrNoSuchMember } val.State = order.StatAwaitingPayment o := this.CreateOrder(val) err = o.RequireCart(c) o.GetByVendor() return o, m, err }
// 应用购物车内商品的促销 func (o *orderImpl) applyCartPromotionOnSubmit(vo *order.Order, cart cart.ICart) ([]promotion.IPromotion, int) { var proms []promotion.IPromotion = make([]promotion.IPromotion, 0) var prom promotion.IPromotion var saveFee int var totalSaveFee int var intOrderFee = int(vo.FinalAmount) var rightBack bool for _, v := range cart.GetCartGoods() { prom = nil saveFee = 0 rightBack = false // 判断商品的最省促销 for _, v1 := range v.GetPromotions() { // 返现 if v1.Type() == promotion.TypeFlagCashBack { vc := v1.GetRelationValue().(*promotion.ValueCashBack) if vc.MinFee < intOrderFee { if vc.BackFee > saveFee { prom = v1 saveFee = vc.BackFee rightBack = vc.BackType == promotion.BackUseForOrder // 是否立即抵扣 } } } //todo: 其他促销 } if prom != nil { proms = append(proms, prom) if rightBack { totalSaveFee += saveFee } } } return proms, totalSaveFee }
// 合并购物车,并返回新的购物车 func (this *cartImpl) Combine(c cart.ICart) cart.ICart { if c.GetAggregateRootId() != this.GetAggregateRootId() { for _, v := range c.GetValue().Items { if item, err := this.AddItem(v.VendorId, v.ShopId, v.SkuId, v.Quantity, v.Checked == 1); err == nil { if v.Checked == 1 { item.Checked = 1 } } } c.Destroy() //合并后,需销毁购物车 } this._snapMap = nil //clean return this }
// 获取购物车 func (this *shoppingService) getShoppingCart(buyerId int, cartKey string) cart.ICart { var c cart.ICart if len(cartKey) > 0 { c = this._cartRep.GetShoppingCartByKey(cartKey) } else if buyerId > 0 { c = this._cartRep.GetMemberCurrentCart(buyerId) } if c == nil { c = this._cartRep.NewCart() _, err := c.Save() domain.HandleError(err, "service") } if c.GetValue().BuyerId <= 0 { err := c.SetBuyer(buyerId) domain.HandleError(err, "service") } return c }
// 在下单前检查购物车 func (this *orderManagerImpl) checkCartForOrder(c cart.ICart) error { if c == nil { return cart.ErrEmptyShoppingCart } return c.Check() }