medal.go 3.22 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3
package res_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"
hujiebin's avatar
hujiebin committed
6
	"github.com/bluele/gcache"
hujiebin's avatar
hujiebin committed
7 8
	"gorm.io/gorm"
	"hilo-group/_const/enum/res_e"
hujiebin's avatar
hujiebin committed
9 10
	"hilo-group/myerr"
	"time"
hujiebin's avatar
hujiebin committed
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
)

type ResMedal struct {
	mysql.Entity
	Name     string
	PicUrl   string
	SvgaUrl  string
	Sort     mysql.Num
	NoPicUrl *string
	//类型 (0:代表自身就是一种类型 > 0, 一种相关联的类型)
	Type  res_e.ResMedalType
	Scope res_e.ResMedalScope
	//限制 (-1, 没有限制)
	Threshold mysql.Num
	I         mysql.Num
}

// 查勋章中的等级关系
func GetUserMedalLevelMap(db *gorm.DB) (map[uint64]uint8, map[uint8][]uint64, error) {
	rows := make([]ResMedal, 0)
	if err := db.Model(&ResMedal{}).Order("threshold ASC").Find(&rows).Error; err != nil {
		return nil, nil, err
	}
	medalTypes := make(map[uint64]uint8, 0)
	result := make(map[uint8][]uint64, 0)
	for _, i := range rows {
		medalTypes[i.ID] = i.Type

		if i.Type != 0 {
			if _, ok := result[i.Type]; !ok {
				result[i.Type] = make([]uint64, 0)
			}
			result[i.Type] = append(result[i.Type], i.ID)
		}
	}
	return medalTypes, result, nil
}
hujiebin's avatar
hujiebin committed
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

type RoomMedalConfig struct {
	Level       uint16
	Threshold   uint64
	Desc        string
	InactiveUrl string
	ActiveUrl   string
}

func GetRoomMedalConfig(db *gorm.DB) ([]RoomMedalConfig, error) {
	rows := make([]RoomMedalConfig, 0)
	if err := db.Model(&RoomMedalConfig{}).Order("level").Find(&rows).Error; err != nil {
		return nil, err
	}
	return rows, nil
}

var medalCache = gcache.New(1000).LRU().Build()
var medalMapCache = gcache.New(1).LRU().Build()

const medalKey = "MEDAL"

// pprof看到内存分配很多
// 加上15min lru
func MedalGetAll(db *gorm.DB) ([]ResMedal, error) {
	if data, err := medalCache.Get(medalKey); err == nil {
		return data.([]ResMedal), nil
	}
	rows := make([]ResMedal, 0)
	err := db.Find(&rows).Error
	if err != nil {
		return nil, err
	}
	_ = medalCache.SetWithExpire(medalKey, rows, time.Minute*15)
	return rows, nil
}

func MedalGetAllMap(db *gorm.DB) (map[uint32]ResMedal, error) {
	if data, err := medalMapCache.Get(medalKey); err == nil {
		return data.(map[uint32]ResMedal), nil
	}
	rows, err := MedalGetAll(db)
	if err != nil {
		return nil, err
	}
	result := make(map[uint32]ResMedal, 0)
	for _, i := range rows {
		result[uint32(i.ID)] = i
	}
	_ = medalMapCache.SetWithExpire(medalKey, result, time.Minute*15)
	return result, nil
}

//每个公开勋章都要有对应的获得来源,
type ResMedalPublicObtain struct {
	mysql.Entity
	Type       res_e.ResMedalObtainType
	ResMedalId mysql.ID
	ResGiftId  mysql.ID
}

func ResMedalObtainGetByMedalId(model *domain.Model, medalId mysql.ID) (*ResMedalPublicObtain, error) {
	resMedalPublicObtain := ResMedalPublicObtain{}
	if err := model.Db.Model(&ResMedalPublicObtain{}).Where(&ResMedalPublicObtain{
		ResMedalId: medalId,
	}).First(&resMedalPublicObtain).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		} else {
			return nil, myerr.WrapErr(err)
		}
	}
	return &resMedalPublicObtain, nil
}

func GetResMedalById(model *domain.Model, id mysql.ID) (ResMedal, error) {
	resMedal := ResMedal{}
	if err := model.Db.Model(&ResMedal{}).First(&resMedal, id).Error; err != nil {
		return resMedal, myerr.WrapErr(err)
	}
	return resMedal, nil
}