Beispiel #1
0
func (o *subOrderImpl) handleCashBack() error {
	v := o._value
	mch, err := o._mchRep.GetMerchant(v.VendorId)
	if err != nil {
		return err
	}

	buyer := o.GetBuyer()
	now := time.Now().Unix()

	//******* 返现到账户  ************
	var back_fee float32
	saleConf := mch.ConfManager().GetSaleConf()
	//globSaleConf := o._valRep.GetGlobNumberConf()
	if saleConf.CashBackPercent > 0 {
		back_fee = v.FinalAmount * saleConf.CashBackPercent
		//将此次消费记入会员账户
		err = o.updateShoppingMemberBackFee(mch.GetValue().Name, buyer,
			back_fee*saleConf.CashBackMemberPercent, now)
		domain.HandleError(err, "domain")

	}

	// 处理返现促销
	//todo: ????
	//o.handleCashBackPromotions(mch, m)
	// 三级返现
	if back_fee > 0 {
		err = o.backFor3R(mch, buyer, back_fee, now)
	}
	return err
}
Beispiel #2
0
// 三级返现
func (o *subOrderImpl) backFor3R(mch merchant.IMerchant, m member.IMember,
	back_fee float32, unixTime int64) (err error) {
	if back_fee > 0 {
		i := 0
		mName := m.Profile().GetProfile().Name
		saleConf := mch.ConfManager().GetSaleConf()
		percent := saleConf.CashBackTg2Percent
		for i < 2 {
			rl := m.GetRelation()
			if rl == nil || rl.RefereesId == 0 {
				break
			}

			m = o._memberRep.GetMember(rl.RefereesId)
			if m == nil {
				break
			}

			if i == 1 {
				percent = saleConf.CashBackTg1Percent
			}

			err = o.updateMemberAccount(m, mch.GetValue().Name, mName,
				back_fee*percent, unixTime)
			if err != nil {
				domain.HandleError(err, "domain")
				break
			}
			i++
		}
	}
	return err
}
Beispiel #3
0
// 处理错误
func handleError(err error) error {
	return domain.HandleError(err, "service")
	//if err != nil && gof.CurrentApp.Debug() {
	//	gof.CurrentApp.Log().Println("[ Go2o][ Rep][ Error] -", err.Error())
	//}
	//return err
}
Beispiel #4
0
// 完成订单
func (o *subOrderImpl) onOrderComplete() error {
	// now := time.Now().Unix()
	// v := o._value

	// 更新发货单
	soList := o._shipRep.GetOrders(o.GetDomainId())
	for _, v := range soList {
		domain.HandleError(v.Completed(), "domain")
	}

	// 获取消费者消息
	m := o.GetBuyer()
	if m == nil {
		return member.ErrNoSuchMember
	}

	// 更新会员账户
	err := o.updateAccountForOrder(m)
	if err != nil {
		return err
	}

	// 处理返现
	err = o.handleCashBack()

	return err
}
Beispiel #5
0
// 应用优惠券
func (this *orderManagerImpl) applyCoupon(m member.IMember, order order.IOrder,
	py payment.IPaymentOrder, couponCode string) error {
	po := py.GetValue()
	cp := this._promRep.GetCouponByCode(
		m.GetAggregateRootId(), couponCode)
	// 如果优惠券不存在
	if cp == nil {
		return errors.New("优惠券无效")
	}
	// 获取优惠券
	coupon := cp.(promotion.ICouponPromotion)
	result, err := coupon.CanUse(m, po.TotalFee)
	if result {
		if coupon.CanTake() {
			_, err = coupon.GetTake(m.GetAggregateRootId())
			//如果未占用,则占用
			if err != nil {
				err = coupon.Take(m.GetAggregateRootId())
			}
		} else {
			_, err = coupon.GetBind(m.GetAggregateRootId())
		}
		if err != nil {
			domain.HandleError(err, "domain")
			err = errors.New("优惠券无效")
		} else {
			//应用优惠券
			if err = order.ApplyCoupon(coupon); err == nil {
				_, err = py.CouponDiscount(coupon)
			}
		}
	}
	return err
}
Beispiel #6
0
// 将默认的快递服务商保存
func (this *ExpressRepBase) SaveDefaultExpressProviders(rep express.IExpressRep) []*express.ExpressProvider {
	var err error
	for _, v := range express.SupportedExpressProvider {
		if v.Id, err = rep.SaveExpressProvider(v); err != nil {
			domain.HandleError(err, "domain")
		}
	}
	return express.SupportedExpressProvider
}
Beispiel #7
0
//  获取购物车
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
}
Beispiel #8
0
// 保存
func (this *tmpGoodsImpl) Save() (int, error) {
	id, err := this._goodsRep.SaveValueGoods(this._value)
	if err == nil {
		_, err := this.SnapshotManager().GenerateSnapshot()
		dm.HandleError(err, "domain")
	}
	this._value.Id = id
	return id, err
	//todo: save promotion
	// return id,err
}
Beispiel #9
0
// 根据运营商生成子订单
func (o *orderImpl) createSubOrderByVendor(parentOrderId int, buyerId int,
	vendorId int, newOrderNo bool, items []*order.OrderItem) order.ISubOrder {
	orderNo := o.GetOrderNo()
	if newOrderNo {
		orderNo = o._manager.GetFreeOrderNo(vendorId)
	}

	if len(items) == 0 {
		domain.HandleError(errors.New("拆分订单,运营商下未获取到商品,订单:"+
			o.GetOrderNo()), "domain")
		return nil
	}

	v := &order.SubOrder{
		OrderNo:   orderNo,
		BuyerId:   buyerId,
		VendorId:  vendorId,
		ParentId:  parentOrderId,
		Subject:   "子订单",
		ShopId:    items[0].ShopId,
		ItemsInfo: "",
		// 总金额
		GoodsAmount: 0,
		// 减免金额(包含优惠券金额)
		DiscountAmount: 0,
		ExpressFee:     0,
		FinalAmount:    0,
		// 是否挂起,如遇到无法自动进行的时挂起,来提示人工确认。
		IsSuspend:  0,
		Note:       "",
		Remark:     "",
		State:      order.StatAwaitingPayment,
		UpdateTime: o._value.UpdateTime,
		Items:      items,
	}
	// 计算订单金额
	for _, item := range items {
		//计算商品金额
		v.GoodsAmount += item.Amount
		//计算商品优惠金额
		v.DiscountAmount += item.Amount - item.FinalAmount
	}
	// 设置运费
	v.ExpressFee = o._vendorExpressMap[vendorId]
	// 设置包装费
	v.PackageFee = 0
	// 最终金额 = 商品金额 - 商品抵扣金额(促销折扣) + 包装费 + 快递费
	v.FinalAmount = v.GoodsAmount - v.DiscountAmount + v.PackageFee + v.ExpressFee
	// 判断是否已支付
	if o._value.IsPaid == 1 {
		v.State = enum.ORDER_WAIT_CONFIRM
	}
	return o._manager.CreateSubOrder(v)
}
Beispiel #10
0
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
}
Beispiel #11
0
// 更新订单状态, 需要注意,防止多次订单更新
func (this *paymentOrderImpl) notifyPaymentFinish() {

	err := this._rep.NotifyPaymentFinish(this.GetAggregateRootId())
	if err != nil {
		err = errors.New("Notify payment finish error :" + err.Error())
		domain.HandleError(err, "domain")
	}
	if this._value.OrderId > 0 {
		err = this._orderManager.PaymentForOnlineTrade(this._value.OrderId)
		if err != nil {
			domain.HandleError(err, "domain")
		}
	}

	//todo:  更新订单状态

	//this._value.PaymentSign = buyerType
	//if this._value.Status == enum.ORDER_WAIT_PAYMENT {
	//    this._value.Status = enum.ORDER_WAIT_CONFIRM
	//}
}
Beispiel #12
0
// 生成运营商与订单商品的映射
func (o *orderImpl) buildVendorItemMap(items []*cart.CartItem) map[int][]*order.OrderItem {
	mp := make(map[int][]*order.OrderItem)
	for _, v := range items {
		//必须勾选为结算
		if v.Checked == 1 {
			item := o.parseCartToOrderItem(v)
			if item == nil {
				domain.HandleError(errors.New("转换购物车商品到订单商品时出错: 商品SKU"+
					strconv.Itoa(v.SkuId)), "domain")
				continue
			}
			list, ok := mp[v.VendorId]
			if !ok {
				list = []*order.OrderItem{}
			}
			mp[v.VendorId] = append(list, item)
			//log.Println("--- vendor map len:", len(mp[v.VendorId]))
		}
	}
	return mp
}
Beispiel #13
0
//根据运营商拆单,返回拆单结果,及拆分的订单数组
func (o *orderImpl) breakUpByVendor() []order.ISubOrder {
	parentOrderId := o.GetAggregateRootId()
	if parentOrderId <= 0 ||
		o._vendorItemsMap == nil ||
		len(o._vendorItemsMap) == 0 {
		//todo: 订单要取消掉
		panic(fmt.Sprintf("订单异常: 订单未生成或VendorItemMap为空,"+
			"订单编号:%d,订单号:%s,vendor len:%d",
			parentOrderId, o._value.OrderNo, len(o._vendorItemsMap)))
	}
	l := len(o._vendorItemsMap)
	list := make([]order.ISubOrder, l)
	i := 0
	buyerId := o._buyer.GetAggregateRootId()
	for k, v := range o._vendorItemsMap {
		//log.Println("----- vendor ", k, len(v),l)
		list[i] = o.createSubOrderByVendor(parentOrderId, buyerId, k, l > 1, v)
		if _, err := list[i].Save(); err != nil {
			domain.HandleError(err, "domain")
		}
		i++
	}
	return list
}
Beispiel #14
0
// 转换购物车的商品项为订单项目
func (o *orderImpl) parseCartToOrderItem(c *cart.CartItem) *order.OrderItem {
	gs := o._saleRep.GetSale(c.VendorId).GoodsManager().CreateGoods(
		&goods.ValueGoods{Id: c.SkuId, SkuId: c.SkuId})
	snap := gs.SnapshotManager().GetLatestSaleSnapshot()
	if snap == nil {
		domain.HandleError(errors.New("商品快照生成失败:"+
			strconv.Itoa(c.SkuId)), "domain")
		return nil
	}
	fee := c.SalePrice * float32(c.Quantity)
	return &order.OrderItem{
		Id:          0,
		VendorId:    c.VendorId,
		ShopId:      c.ShopId,
		SkuId:       c.SkuId,
		SnapshotId:  snap.Id,
		Quantity:    c.Quantity,
		Amount:      fee,
		FinalAmount: fee,
		//是否配送
		IsShipped: 0,
		Weight:    c.Snapshot.Weight * c.Quantity, //计算重量
	}
}
Beispiel #15
0
// 已收货
func (o *subOrderImpl) BuyerReceived() error {
	var err error
	if o._value.State < order.StatShipped {
		return order.ErrOrderNotShipped
	}
	if o._value.State >= order.StatCompleted {
		return order.ErrIsCompleted
	}
	dt := time.Now()
	o._value.State = order.StatCompleted
	o._value.UpdateTime = dt.Unix()
	o._value.IsSuspend = 0
	if _, err = o.Save(); err != nil {
		return err
	}
	err = o.AppendLog(order.LogSetup, true, "{completed}")
	if err == nil {
		// 执行其他的操作
		if err2 := o.onOrderComplete(); err != nil {
			domain.HandleError(err2, "domain")
		}
	}
	return err
}