// 自动生成模板RedPackage
package airDrop

import (
	"fmt"
	"math/rand"
	"time"

	"courseSign/server/global"
)

const (
	PackageTypeCommon = iota
	PackageTypeRand
)
const MinAvgPackage = 1e7

const (
	PackageVerifyGoogle = iota
	PackageVerifyVoice
	PackageVerifyTele
)

// RedPackage 结构体
type RedPackage struct {
	global.GVA_MODEL
	Balance int64       `json:"balance" form:"balance" gorm:"column:balance;comment:;"`
	Remain   int64 `json:"remain" form:"remain" gorm:"column:remain;comment:;"`
	Type    bool      `json:"type" form:"type" gorm:"column:type;comment:;"`
	Num     int       `json:"num" form:"num" gorm:"column:num;comment:;size:10;"`
	IpLimit int       `json:"ip_limit" form:"ip_limit" gorm:"column:ip_limit;comment:;size:10;"`
	IpWhite string       `json:"ip_white" form:"ip_white" gorm:"column:ip_white;comment:;size:72;"`
	IsShow  bool      `json:"is_show" form:"is_show" gorm:"column:is_show;comment:;"`
	StartAt time.Time `json:"start_at" form:"start_at" gorm:"column:start_at;comment:;"`
	EndAt   time.Time `json:"end_at" form:"end_at" gorm:"column:end_at;comment:;"`
	RemainNum   int `json:"remain_num" form:"remain_num" gorm:"column:remain_num;comment:;"`
	Name    string      `json:"name" form:"name" gorm:"column:name;comment:;size:64;"`
	Desc    string      `json:"desc" form:"desc" gorm:"column:desc;comment:;size:256;"`
	White string `json:"white" form:"white" gorm:"column:white;comment:;type:text"`
	VerifyType int `json:"verify_type" form:"verify_type" gorm:"column:verify_type;default:0;comment:;"` // 验证方式 0 google code 1 科大讯飞语音
	GroupName string `json:"group_name" form:"group_name" gorm:"column:group_name;comment:;size:64;"`
	GroupId string `json:"group_id" form:"group_id" gorm:"column:group_id;default:-1;comment:;size:64;"`
	GroupInfo string `json:"group_info" form:"group_info" gorm:"column:group_info;comment:;size:64;"`
	IsPassword  bool      `json:"is_password" form:"is_password" gorm:"column:is_password;comment:;"`
	Password  string      `json:"password" form:"password" gorm:"column:password;comment:;size:256;"`
	IsBlack  bool      `json:"is_black" form:"is_black" gorm:"column:is_black;comment:;"` // 是否开启黑名单
}

// TableName RedPackage 表名
func (RedPackage) TableName() string {
	return "red_package"
}

//
func (r RedPackage) GetRandOne() int64 {
	if r.ID == 0 {
		return 0
	}
	// 最后一个
	if r.RemainNum - 1 == 0 {
		return r.Remain
	}

	if r.Type { // 简单随机
		// 动态计算红包平均值
		avg := int64(float64(r.Remain / int64(r.RemainNum))/MinAvgPackage)
		if avg == 0 {
			return 0
		}
		// 随机计算红包金额(1到2倍均值)
		rand.Seed(time.Now().UnixNano())
		rr := rand.Intn(2*int(avg))
		amount := int64(rr) * MinAvgPackage
		fmt.Println("平均值：",avg, "实际随机：",rr,"实际值",amount,r.Remain,r.RemainNum)
		if amount < MinAvgPackage {
			return MinAvgPackage
		}
		// 本次+剩余次数*最小红包 需要小于剩余金额
		if (amount + int64(r.RemainNum - 1) * MinAvgPackage) > r.Remain && r.RemainNum >=2 {
			return MinAvgPackage
		}
		return amount
	} else {
		amount := int64(float64(r.Balance / int64(r.Num))/MinAvgPackage* MinAvgPackage)
		fmt.Println("amount:",amount,"balance",r.Remain,r.RemainNum)
		// amount := int64(utils.Decimal(float64(r.Balance / int64(r.Num))/float64(1e7),1)*float64(1e7))
		// fmt.Println("amount:",amount,"balance",r.Balance,r.Num)
		return amount
	}
	return 0
}

type WhiteList struct {

}