diff --git a/api/model/SystemSingle.go b/api/model/SystemSingle.go index 3547ac0..9840028 100644 --- a/api/model/SystemSingle.go +++ b/api/model/SystemSingle.go @@ -8,9 +8,9 @@ import ( // SystemSingle 配置-单项配置 type SystemSingle struct { - SystemSingleId int64 `gorm:"column:system_single_id;type:bigint(19);primary_key;comment:主键id" json:"system_single_id"` - FirstTimePrice float64 `gorm:"column:first_time_price;type:decimal(10,2);default:0.00;comment:首次购买价格" json:"first_time_price"` - ValidDays int `gorm:"column:valid_days;type:int(5);default:1;comment:购买后有效天数" json:"valid_days"` + SystemSingleId int64 `gorm:"column:system_single_id;type:bigint(19);primary_key;comment:主键id" json:"system_single_id"` + FirstTimePrice *float64 `gorm:"column:first_time_price;type:decimal(10,2);comment:首次购买价格" json:"first_time_price"` + ValidDays int `gorm:"column:valid_days;type:int(5);default:1;comment:购买后有效天数" json:"valid_days"` Model } diff --git a/api/service/OrderMember.go b/api/service/OrderMember.go index b136d2c..6a07c23 100644 --- a/api/service/OrderMember.go +++ b/api/service/OrderMember.go @@ -76,7 +76,7 @@ func (r *OrderMemberService) AddOrderMember(tx *gorm.DB, UserId, SystemMemberId // 获取会员购买价格 systemMemberService := &SystemMemberService{} - amountTotal = systemMemberService.GetSystemMemberBuyPrice(systemMember) + amountTotal = systemMemberService.GetUserBuyPrice(UserId, systemMember) if err != nil { return nil, err } diff --git a/api/service/OrderSingle.go b/api/service/OrderSingle.go index ccd89b5..1bd26d3 100644 --- a/api/service/OrderSingle.go +++ b/api/service/OrderSingle.go @@ -66,7 +66,7 @@ func (r *OrderSingleService) AddOrderSingle(tx *gorm.DB, UserId, QuestionId int6 return nil, errors.New("用户非会员,不可使用会员支付") } - var amountTotal *float64 // 总金额 + var amountTotal float64 // 总金额 var couponAmountTotal float64 // 优惠卷总金额 var paymentAmountTotal float64 // 实际付款金额 var orderStatus int // 订单状态(1:待支付 2:已完成 3:已取消) @@ -80,10 +80,6 @@ func (r *OrderSingleService) AddOrderSingle(tx *gorm.DB, UserId, QuestionId int6 return nil, err } - if amountTotal == nil { - return nil, errors.New("价格错误") - } - // 检测用户优惠卷 var userCoupon *model.UserCoupon if UserCouponId != nil { @@ -96,7 +92,7 @@ func (r *OrderSingleService) AddOrderSingle(tx *gorm.DB, UserId, QuestionId int6 // 检测用户优惠卷 userCouponService := &UserCouponService{} - isCanUse, err := userCouponService.CheckUserCoupon(userCoupon, QuestionId, 1, *amountTotal) + isCanUse, err := userCouponService.CheckUserCoupon(userCoupon, QuestionId, 1, amountTotal) if err != nil || isCanUse == false { return nil, errors.New("价格异常") } @@ -117,7 +113,7 @@ func (r *OrderSingleService) AddOrderSingle(tx *gorm.DB, UserId, QuestionId int6 escrowTradeNo = "GD" + global.Snowflake.Generate().String() // 第三方支付流水号 } else { // 实际付款金额 - paymentAmountTotal = *amountTotal - couponAmountTotal + paymentAmountTotal = amountTotal - couponAmountTotal if orderPrice != paymentAmountTotal { return nil, errors.New("价格异常") } @@ -142,7 +138,7 @@ func (r *OrderSingleService) AddOrderSingle(tx *gorm.DB, UserId, QuestionId int6 RefundStatus: 0, OrderNo: orderNo, EscrowTradeNo: escrowTradeNo, - AmountTotal: *amountTotal, + AmountTotal: amountTotal, CouponAmountTotal: couponAmountTotal, PaymentAmountTotal: paymentAmountTotal, CancelStatus: 0, diff --git a/api/service/Question.go b/api/service/Question.go index 4eb4148..b206513 100644 --- a/api/service/Question.go +++ b/api/service/Question.go @@ -125,7 +125,7 @@ func (r *QuestionService) GetUserFirstTimeBuyPrice(userId int64) (f *float64, er return nil, err } - return &systemSingle.FirstTimePrice, nil + return systemSingle.FirstTimePrice, nil } return nil, nil @@ -150,15 +150,16 @@ func (r *QuestionService) GetQuestionBuyCount(userId, questionId int64) (c int, } // GetUserBuyPrice 获取问题最终价格 -func (r *QuestionService) GetUserBuyPrice(userId, questionId int64) (p *float64, err error) { +func (r *QuestionService) GetUserBuyPrice(userId, questionId int64) (p float64, err error) { // 获取问题详情 questionDao := dao.QuestionDao{} question, err := questionDao.GetQuestionById(questionId) if err != nil { - return nil, errors.New("题目异常") + return 0, errors.New("题目异常") } // 检测用户是否购买过单项产品 + var firstTimePrice *float64 userService := &UserService{} isBuy := userService.CheckUserBuySingle(userId) if isBuy == false { @@ -168,19 +169,27 @@ func (r *QuestionService) GetUserBuyPrice(userId, questionId int64) (p *float64, maps := make(map[string]interface{}) systemSingle, err := systemSingleDao.GetSystemSingle(maps) if err != nil { - return nil, err + return 0, err } - p = &systemSingle.FirstTimePrice + if systemSingle.FirstTimePrice != nil { + // 首次购买价格 + firstTimePrice = systemSingle.FirstTimePrice + } } - // 处理问题优惠价格 - if p == nil { - p = r.HandleQuestionDiscountPrice(question.DiscountPrice, question.DiscountEndTime) - } - - if p == nil { - p = &question.Price + if firstTimePrice != nil { + // 首次购买价格 + p = *firstTimePrice + } else { + // 优惠价格 + price := r.HandleQuestionDiscountPrice(question.DiscountPrice, question.DiscountEndTime) + if price == nil { + // 正常价格 + p = question.Price + } else { + p = *price + } } return p, nil diff --git a/api/service/SystemMember.go b/api/service/SystemMember.go index 3d9008a..3004048 100644 --- a/api/service/SystemMember.go +++ b/api/service/SystemMember.go @@ -1,6 +1,9 @@ package service -import "hepa-calc-api/api/model" +import ( + "hepa-calc-api/api/model" + "time" +) type SystemMemberService struct { } @@ -15,3 +18,52 @@ func (r *SystemMemberService) GetSystemMemberBuyPrice(m *model.SystemMember) (p return p } + +// GetUserBuyPrice 获取会员最终价格 +func (r *SystemMemberService) GetUserBuyPrice(userId int64, m *model.SystemMember) (p float64) { + // 检测用户是否购买过单项产品 + var firstTimePrice *float64 + userService := &UserService{} + isBuy := userService.CheckUserBuyMember(userId) + if isBuy == false { + // 未购买过 + if m.FirstTimePrice != nil { + // 首次购买价格 + firstTimePrice = m.FirstTimePrice + } + } + + if firstTimePrice != nil { + // 首次购买价格 + p = *firstTimePrice + } else { + // 优惠价格 + price := r.HandleSystemMemberDiscountPrice(m.DiscountPrice, m.DiscountEndTime) + if price == nil { + // 正常价格 + p = m.Price + } else { + p = *price + } + } + + return p +} + +// HandleSystemMemberDiscountPrice 处理会员优惠价格 +func (r *SystemMemberService) HandleSystemMemberDiscountPrice(discountPrice *float64, discountEndTime *model.LocalTime) (p *float64) { + // 优惠价格 + if discountPrice != nil { + discountEndTime := time.Time(*discountEndTime) + + // 检测是否超出优惠时间 + now := time.Now() + if discountEndTime.Before(now) { + p = nil + } else { + p = discountPrice + } + } + + return p +}