package dao

import (
	"fmt"
	log "github.com/sirupsen/logrus"
	"youngee_b_api/app/entity"
)

type InfoPricingStrategylDao struct{}

func (d InfoPricingStrategylDao) GetPricingStrategy(fansLow int64, fansUp int64, feeForm int64, platForm int64) (*entity.InfoPricingStrategy, error) {
	// fansLow粉丝量下限  fansUp粉丝量上限  feeForm稿费形式 1产品置换 2固定稿费 3自报价  platForm平台
	var pricingStrategys []entity.InfoPricingStrategy
	whereStr := fmt.Sprintf("fee_form = %d and platform = %d and fans_low <= %d and fans_up > %d", feeForm, platForm, fansLow, fansLow)
	orStr := fmt.Sprintf("fee_form = %d and platform = %d and fans_low < %d and fans_up >= %d", feeForm, platForm, fansUp, fansUp)
	orStr1 := fmt.Sprintf("fee_form = %d and platform = %d and fans_low >= %d and fans_up <= %d", feeForm, platForm, fansLow, fansUp)
	orStr2 := fmt.Sprintf("fee_form = %d and platform = %d and fans_low <= %d and fans_up >= %d", feeForm, platForm, fansLow, fansUp)
	err := Db.Model(entity.InfoPricingStrategy{}).Where(whereStr).Or(orStr).Or(orStr1).Or(orStr2).Scan(&pricingStrategys).Error
	if err != nil {
		log.Println("DB GetLastAutoDefaultID:", err)
		return nil, err
	}
	fmt.Printf("PricingStrategys%+v \n", pricingStrategys)
	pricingStrategy := entity.InfoPricingStrategy{}
	if feeForm == 1 { // 如果是产品置换，则选取服务费最高时的定价策略
		var maxCharge float64 = 0
		for _, v := range pricingStrategys {
			if v.ServiceCharge >= maxCharge {
				maxCharge = v.ServiceCharge
				pricingStrategy = v
			}
		}
	} else { // 如果是固定稿费或自报价，则选取服务费率最高时的定价策略
		var maxRate int64 = 0
		for _, v := range pricingStrategys {
			if v.ServiceRate >= maxRate {
				maxRate = v.ServiceRate
				pricingStrategy = v
			}
		}
	}
	return &pricingStrategy, nil
}
