info_pricing_strategy.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "youngee_m_api/model/gorm_model"
  6. log "github.com/sirupsen/logrus"
  7. )
  8. func GetPricingStrategy(ctx context.Context, fansLow int64, fansUp int64, feeForm int64, platForm int64) (*gorm_model.InfoPricingStrategy, error) {
  9. db := GetReadDB(ctx)
  10. var PricingStrategys []gorm_model.InfoPricingStrategy
  11. whereStr := fmt.Sprintf("fee_form = %d and platform = %d and fans_low <= %d and fans_up > %d", feeForm, platForm, fansLow, fansLow)
  12. orStr := fmt.Sprintf("fee_form = %d and platform = %d and fans_low < %d and fans_up >= %d", feeForm, platForm, fansUp, fansUp)
  13. orStr1 := fmt.Sprintf("fee_form = %d and platform = %d and fans_low >= %d and fans_up <= %d", feeForm, platForm, fansLow, fansUp)
  14. orStr2 := fmt.Sprintf("fee_form = %d and platform = %d and fans_low <= %d and fans_up >= %d", feeForm, platForm, fansLow, fansUp)
  15. err := db.Model(gorm_model.InfoPricingStrategy{}).Where(whereStr).Or(orStr).Or(orStr1).Or(orStr2).Scan(&PricingStrategys).Error
  16. if err != nil {
  17. log.Println("DB GetLastAutoDefaultID:", err)
  18. return nil, err
  19. }
  20. PricingStrategy := gorm_model.InfoPricingStrategy{}
  21. if feeForm == 1 {
  22. var maxCharge float64 = 0
  23. for _, v := range PricingStrategys {
  24. fmt.Println(v.ServiceCharge)
  25. if v.ServiceCharge >= maxCharge {
  26. maxCharge = v.ServiceCharge
  27. PricingStrategy = v
  28. }
  29. }
  30. } else {
  31. var maxRate int64 = 0
  32. for _, v := range PricingStrategys {
  33. fmt.Println(v.ServiceRate)
  34. if v.ServiceRate >= maxRate {
  35. maxRate = v.ServiceRate
  36. PricingStrategy = v
  37. }
  38. }
  39. }
  40. return &PricingStrategy, nil
  41. }