userNoble.go 4.17 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
package noble_m

import (
	"git.hilo.cn/hilo-common/mylogrus"
	"git.hilo.cn/hilo-common/resource/mysql"
	"gorm.io/gorm"
	"hilo-group/myerr"
	"time"
)

type UserNoble struct {
	mysql.Entity
	UserId  uint64
	Level   uint16
	EndTime time.Time
}

func (ub *UserNoble) Create(db *gorm.DB) error {
	return db.Create(ub).Error
}

func (ub *UserNoble) UpdateEndTime(db *gorm.DB, endTime time.Time) (int64, error) {
	r := db.Model(&UserNoble{}).Where(ub).Update("end_time", endTime)
	return r.RowsAffected, r.Error
}

// 查询用户未过期的贵族
func (ub *UserNoble) Find(db *gorm.DB) ([]UserNoble, error) {
	rows := make([]UserNoble, 0)
	if err := db.Where(ub).Where("end_time>=NOW()").Order("level DESC").Find(&rows).Error; err != nil {
		return nil, err
	}
	return rows, nil
}

// 查询用户所有的贵族(包括已过期)
func (ub *UserNoble) FindAll(db *gorm.DB) ([]UserNoble, error) {
	rows := make([]UserNoble, 0)
	if err := db.Where(ub).Order("level DESC").Find(&rows).Error; err != nil {
		return nil, err
	}
	return rows, nil
}

func RemoveNoble(db *gorm.DB, userId uint64, level uint16) error {
	ub := UserNoble{
		UserId: userId,
		Level:  level,
	}
	return ub.Delete(db)
}

func (ub *UserNoble) Delete(db *gorm.DB) error {
	return db.Where(ub).Delete(&UserNoble{}).Error
}

// 查询用户未过期的贵族
func (ub *UserNoble) batchGet(db *gorm.DB, userIds []uint64) (map[uint64][]UserNoble, error) {
	rows := make([]UserNoble, 0)
	if err := db.Model(ub).Where("end_time>=NOW() AND user_id IN ?", userIds).Order("level DESC").Find(&rows).Error; err != nil {
		return nil, err
	}
	result := make(map[uint64][]UserNoble, 0)
	for _, i := range rows {
		if _, ok := result[i.UserId]; !ok {
			result[i.UserId] = make([]UserNoble, 0)
		}
		result[i.UserId] = append(result[i.UserId], i)
	}
	return result, nil
}

func FindActiveNoble(db *gorm.DB, userId uint64) (*UserNoble, error) {
	ub := UserNoble{
		UserId: userId,
	}
	records, err := ub.Find(db)
	if err != nil {
		return nil, err
	}
	if len(records) <= 0 {
		return nil, nil
	}
	return &records[0], nil
}

func GetNobleLevel(db *gorm.DB, userId uint64) (uint16, error) {
	noble, err := FindActiveNoble(db, userId)
	if err != nil {
		return 0, err
	}
	if noble == nil {
		return 0, nil
	} else {
		return noble.Level, nil
	}
}

func IsNoble(db *gorm.DB, userId uint64) (bool, error) {
	level, err := GetNobleLevel(db, userId)
	if err != nil {
		return false, err
	}
	return level > 0, nil
}

func CheckNobleLevel(db *gorm.DB, userId uint64, targetLevel uint16) (bool, error) {
	level, err := GetNobleLevel(db, userId)
	if err != nil {
		return false, err
	}
	return level >= targetLevel, nil
}

// 分几次获取
// 每次500只
func BatchGetActiveNoble(db *gorm.DB, userIds []uint64) (map[uint64]UserNoble, error) {
	ub := UserNoble{}
	result := make(map[uint64]UserNoble, 0)
	total := len(userIds)
	if total <= 0 {
		return result, nil
	}
	var notEndUids []uint64
	const NUM = 500
	start, end := 0, NUM
	for start < total {
		if end > total {
			end = total
		}
		tmpUserId := userIds[start:end]
		records, err := ub.batchGet(db, tmpUserId)
		if err != nil {
			return nil, err
		}
		for _, i := range tmpUserId {
			if len(records[i]) > 0 {
				result[i] = records[i][0]
				notEndUids = append(notEndUids, result[i].UserId)
			} else {
				result[i] = UserNoble{}
			}
		}
		start += NUM
		end += NUM
	}
	mylogrus.MyLog.Infof("BatchGetActiveNoble expected:%v,actual:%v,notEndUids:%v", len(userIds), len(result), notEndUids)
	if len(userIds) != len(result) {
		mylogrus.MyLog.Warnf("BatchGetActiveNoble expected:%v", userIds)
	}
	return result, nil
}

func GetDailyGold(db *gorm.DB, userId uint64) (uint, error) {
	level, err := GetNobleLevel(db, userId)
	if err != nil {
		return 0, err
	}
	r := ResNoble{}
	if err := db.Model(&ResNoble{}).First(&r, level).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return 0, nil
		} else {
			return 0, myerr.WrapErr(err)
		}
	}
	return r.DailyGold, nil
}

func BatchGetNobleLevel(db *gorm.DB, userIds []uint64) (map[uint64]uint16, error) {
	m, err := BatchGetActiveNoble(db, userIds)
	if err != nil {
		return nil, err
	}
	result := make(map[uint64]uint16, 0)
	for _, i := range userIds {
		result[i] = m[i].Level
	}
	return result, nil
}