luckyWheel.go 3.16 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
package luckyWheel_m

import (
	"git.hilo.cn/hilo-common/resource/mysql"
	"gorm.io/gorm"
	"hilo-group/_const/enum/luckyWheel_e"
	"time"
)

type LuckyWheel struct {
	mysql.Entity
	GroupId     string
	Creator     uint64
	CreatorJoin bool
	EntranceFee uint32
	AutoRestart bool
	PlayerNum   uint
	PlayTime    time.Time
	NextTime    time.Time
	Status      uint8
}

func (lw *LuckyWheel) Create(db *gorm.DB) error {
	return db.Create(lw).Error
}

func (lw *LuckyWheel) Get(db *gorm.DB) error {
	return db.Where(lw).First(lw).Error
}

func (lw *LuckyWheel) Remove(db *gorm.DB) (int64, error) {
	result := db.Where(lw).Delete(&LuckyWheel{})
	return result.RowsAffected, result.Error
}

func (lw *LuckyWheel) SetStatus(db *gorm.DB, status luckyWheel_e.LuckyWheelStatusType) error {
	if status == luckyWheel_e.ROLLING {
		return db.Model(&LuckyWheel{}).Where(lw).Updates(LuckyWheel{PlayTime: time.Now(), Status: status}).Error
	} else {
		return db.Model(&LuckyWheel{}).Where(lw).Update("status", status).Error
	}

}

func (lw *LuckyWheel) AddPlayNum(db *gorm.DB) (int64, error) {
	result := db.Model(&LuckyWheel{}).Where(lw).Update("player_num", gorm.Expr("player_num + 1"))
	return result.RowsAffected, result.Error
}

func (lw *LuckyWheel) SetNextTime(db *gorm.DB, nexTime time.Time) error {
	return db.Model(&LuckyWheel{}).Where(lw).Update("next_time", nexTime).Error
}

func GetTimeoutRecords(db *gorm.DB, status luckyWheel_e.LuckyWheelStatusType) ([]string, error) {
	rows := make([]LuckyWheel, 0)
	err := db.Where(&LuckyWheel{Status: status}).Where("next_time < now()").Find(&rows).Error
	if err != nil {
		return nil, err
	}
	result := make([]string, 0)
	for _, i := range rows {
		result = append(result, i.GroupId)
	}
	return result, nil
}

func GetCronTimeoutRecords(db *gorm.DB) ([]string, []string, []string, []string, error) {
	rows := make([]LuckyWheel, 0)
	err := db.Model(&LuckyWheel{}).Where("next_time < now()").Find(&rows).Error
	if err != nil {
		return nil, nil, nil, nil, err
	}
	//result := make([]string, 0)
	//for _, i := range rows {
	//	result = append(result, i.GroupId)
	//}

	//NONE    LuckyWheelStatusType = 0 // 没有活动/已结束
	//CREATED LuckyWheelStatusType = 1 // 创建,等待用户加入
	//ROLLING LuckyWheelStatusType = 2 // 转动中
	//SHOWING LuckyWheelStatusType = 3 // 结果展示中

	//三种情况
	timeOut := []string{} //过期退钱的
	finish := []string{}  //发钱的
	showEnd := []string{} //下一个的
	restart := []string{} // 需要创建新一轮的
	for i, _ := range rows {
		switch rows[i].Status {
		case luckyWheel_e.CREATED:
			timeOut = append(timeOut, rows[i].GroupId)
		case luckyWheel_e.ROLLING:
			finish = append(finish, rows[i].GroupId)
		case luckyWheel_e.SHOWING:
			showEnd = append(showEnd, rows[i].GroupId)
		case luckyWheel_e.RESTARTING:
			restart = append(restart, rows[i].GroupId)
		}
	}
	return timeOut, finish, showEnd, restart, nil
}

// 计算转盘钻石结果
func CalcDiamond(entranceFee uint32, num int) (uint32, uint32) {
	total := entranceFee * uint32(num)
	winnerDiamnod := total * luckyWheel_e.LUCKY_WHEEL_WINNER_PERCENTILE / 100
	ownerDiamnod := total * luckyWheel_e.LUCKY_WHEEL_OWNER_PERCENTILE / 100
	return winnerDiamnod, ownerDiamnod
}