From d27aa2c7efe1375b523e9011df6c51f1ccfecb16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=B0=B9=E5=B2=9A?= <770236076@qq.com> Date: Wed, 20 Jan 2021 10:32:53 +0800 Subject: [PATCH] aa --- business/model/order.go | 4 ++ business/partner/purchase/jx/localjx/order.go | 42 ++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/business/model/order.go b/business/model/order.go index b07dd5915..335c3b72d 100644 --- a/business/model/order.go +++ b/business/model/order.go @@ -42,6 +42,9 @@ const ( CouponStatusOverdue = -1 //已过期 CouponStatusNormal = 0 //正常 CouponStatusDeleted = -4 //被删了 + + CouponTypeDiscount = 1 //优惠券类型,总额满减 + CouponTypeDelivery = 2 //运费优惠 ) const ( @@ -143,6 +146,7 @@ type GoodsOrder struct { EarningType int `json:"earningType"` //订单结算方式,2为扣点,1为报价 OrderType int `json:"orderType"` //订单类型,0为普通订单,1为物料订单,2为进货订单 OrderPayPercentage int `json:"orderPayPercentage"` + CouponIDs string `orm:"column(coupon_ids)" json:"couponIDs"` //优惠券IDs(京西商城) // 以下只是用于传递数据 OriginalData string `orm:"-" json:"-"` diff --git a/business/partner/purchase/jx/localjx/order.go b/business/partner/purchase/jx/localjx/order.go index a524e99d7..f6cf2e525 100644 --- a/business/partner/purchase/jx/localjx/order.go +++ b/business/partner/purchase/jx/localjx/order.go @@ -611,7 +611,6 @@ func generateOrder(ctx *jxcontext.Context, jxOrder *JxOrderInfo, addressID int64 if distance := jxutils.Point2StoreDistance(deliveryAddress.Lng, deliveryAddress.Lat, storeDetail.Lng, storeDetail.Lat, storeDetail.DeliveryRangeType, storeDetail.DeliveryRange); distance == 0 { return nil, nil, fmt.Errorf("当前送货地址不在门店%s的配送范围", storeDetail.Name) } - //结算类型 if storeDetail.PayPercentage < 50 { jxOrder.EarningType = model.EarningTypePoints @@ -922,7 +921,6 @@ func generateOrder(ctx *jxcontext.Context, jxOrder *JxOrderInfo, addressID int64 if err == nil { if jxOrder.OrderType == model.OrderTypeNormal { outJxOrder.TotalPrice = outJxOrder.OrderPrice + outJxOrder.FreightPrice - // 判断用户是否是会员 var ( tuserID string dicountCards []*model.DiscountCard @@ -932,6 +930,46 @@ func generateOrder(ctx *jxcontext.Context, jxOrder *JxOrderInfo, addressID int64 } else { tuserID = userID } + // 1.先扣除优惠券 + if len(couponIDs) > 0 { + if userCoupons, err := dao.GetUserCoupons(db, couponIDs, []string{tuserID}, nil, model.CouponStatusNormal); err == nil { + if len(userCoupons) != len(couponIDs) { + return nil, nil, fmt.Errorf("选择的优惠券状态不正常,请刷新后重试!") + } + for _, v := range userCoupons { + coupon := &model.Coupons{} + coupon.ID = int64(v.CouponID) + if err = dao.GetEntity(db, coupon); err == nil && coupon.Name != "" { + checkPrice := func(price int64) (rPrice int64, err error) { + priceInt := int(price) + if priceInt < coupon.UpperLimit { + return 0, fmt.Errorf("抱歉,该订单价格未达到此优惠券[%v]满减额度!", coupon.Name) + } + if priceInt <= coupon.Cut { + rPrice = 0 + } else { + rPrice = int64(priceInt - coupon.Cut) + } + return rPrice, err + } + switch coupon.CouponType { + case model.CouponTypeDiscount: + outJxOrder.OrderPrice, err = checkPrice(outJxOrder.OrderPrice) + case model.CouponTypeDelivery: + outJxOrder.FreightPrice, err = checkPrice(outJxOrder.FreightPrice) + default: + return nil, nil, fmt.Errorf("暂不支持的优惠券类型![%v]", coupon.CouponType) + } + if err != nil { + return nil, nil, err + } + } + } + } else { + return nil, nil, err + } + } + // 2.判断用户是否是会员 userMembers, _ := dao.GetUserMember(db, tuserID, "", model.MemberTypeDiscountCard, model.YES) if len(userMembers) > 0 { if configList, err := dao.QueryConfigs(db, "会员折扣卡", model.ConfigTypeDiscountCard, ""); err == nil {