userNoble.go 2.91 KB
Newer Older
chenweijian's avatar
chenweijian committed
1 2 3
package noble_m

import (
hujiebin's avatar
hujiebin committed
4
	"git.hilo.cn/hilo-common/domain"
hujiebin's avatar
hujiebin committed
5
	"git.hilo.cn/hilo-common/resource/mysql"
chenweijian's avatar
chenweijian committed
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
	"gorm.io/gorm"
	"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 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
	}
}
hujiebin's avatar
hujiebin committed
81

hujiebin's avatar
hujiebin committed
82 83
func BatchGetNobleLevel(model *domain.Model, userIds []uint64) (map[uint64]uint16, error) {
	m, err := BatchGetActiveNoble(model, userIds)
hujiebin's avatar
hujiebin committed
84 85 86 87 88 89 90 91 92 93
	if err != nil {
		return nil, err
	}
	result := make(map[uint64]uint16, 0)
	for _, i := range userIds {
		result[i] = m[i].Level
	}
	return result, nil
}

hujiebin's avatar
hujiebin committed
94
func BatchGetActiveNoble(model *domain.Model, userIds []uint64) (map[uint64]UserNoble, error) {
hujiebin's avatar
hujiebin committed
95 96
	ub := UserNoble{}

hujiebin's avatar
hujiebin committed
97
	records, err := ub.batchGet(model, userIds)
hujiebin's avatar
hujiebin committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	if err != nil {
		return nil, err
	}
	result := make(map[uint64]UserNoble, 0)
	for _, i := range userIds {
		if len(records[i]) > 0 {
			result[i] = records[i][0]
		} else {
			result[i] = UserNoble{}
		}
	}

	return result, nil
}

// 查询用户未过期的贵族
hujiebin's avatar
hujiebin committed
114
func (ub *UserNoble) batchGet(model *domain.Model, userIds []uint64) (map[uint64][]UserNoble, error) {
hujiebin's avatar
hujiebin committed
115
	rows := make([]UserNoble, 0)
hujiebin's avatar
hujiebin committed
116
	if err := model.DB().Model(ub).Where("end_time>=NOW() AND user_id IN ?", userIds).Order("level DESC").Find(&rows).Error; err != nil {
hujiebin's avatar
hujiebin committed
117 118 119 120 121 122 123 124 125 126 127
		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
}