medal.go 6.8 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
package user_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
	"github.com/sirupsen/logrus"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"hilo-group/_const/enum/res_e"
	"hilo-group/domain/model/common"
	"hilo-group/domain/model/count_m"
	"hilo-group/domain/model/fruit_m"
	"hilo-group/domain/model/gift_m"
	"hilo-group/domain/model/luckybox_m"
	"hilo-group/domain/model/res_m"
	"hilo-group/domain/model/rocket_m"
	"hilo-group/myerr"
	"hilo-group/myerr/bizerr"
	"time"
)

//用户道具
type UserMedal struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	UserId        mysql.ID
	MedalId       uint32
	//MedalType     res_m2.ResMedalType
	//Scope         res_m2.ResMedalScope
	EndTime *time.Time
}

func GetUserMedalMerge(logger *logrus.Entry, db *gorm.DB, userId mysql.ID) ([]uint32, error) {
	// 缓存拿
	res, _ := common.GetUserMedalMergeCache(userId)
	if res != nil {
		return res, nil
	}

	// 数据库加载
	result, err := GetUserMedal(db, userId)
	if err != nil {
		return nil, err
	}
	logger.Infof("GetUserMedalMerge, user %d, %+v", userId, result)

	medalTypes, medalList, err := res_m.GetUserMedalLevelMap(db)
	if err != nil {
		return nil, err
	}
hujiebin's avatar
hujiebin committed
51 52
	//logger.Infof("GetUserMedalLevelMap, user %d, medalMap %+v", medalTypes)
	//logger.Infof("GetUserMedalLevelMap, user %d, medalList %+v", medalList)
hujiebin's avatar
hujiebin committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

	maxGrades := make(map[uint8]int, 0)
	maxMedalIds := make(map[uint8]uint32, 0)
	for _, m := range result {
		mt := medalTypes[uint64(m)]
		if mt > 0 {
			if r, ok := medalList[mt]; ok {
				for i, j := range r {
					if j == uint64(m) {
						if i+1 > maxGrades[mt] {
							maxGrades[mt] = i + 1
							maxMedalIds[mt] = m
							logger.Infof("maxGrade of %d set to %d, due to %d", mt, i+1, m)
						}
						break
					}
				}
			}
		}
	}
iamhujiebin's avatar
iamhujiebin committed
73 74
	//logger.Infof("maxGrade %+v", maxGrades)
	//logger.Infof("maxMedalIds %+v", maxMedalIds)
hujiebin's avatar
hujiebin committed
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

	mIds := result
	result = make([]uint32, 0)
	for _, m := range mIds {
		mt := medalTypes[uint64(m)]
		if mt == 0 || maxMedalIds[mt] == m {
			result = append(result, m)
		}
	}

	// 写入缓存
	common.SetUserMedalMergeCache(userId, result)
	return result, nil
}

func GetUserMedal(db *gorm.DB, userId mysql.ID) ([]uint32, error) {
	rows := make([]UserMedal, 0)
	if err := db.Model(&UserMedal{}).Where("user_id = ? AND (end_time >= NOW() or end_time is null)", userId).Find(&rows).Error; err != nil {
		return nil, err
	}
	result := make([]uint32, 0)
	for _, i := range rows {
		result = append(result, i.MedalId)
	}
	return result, nil
}

func BatchGetUserMedalMerge(logger *logrus.Entry, db *gorm.DB, userIds []mysql.ID) (map[uint64][]uint32, error) {
	result := make(map[uint64][]uint32, 0)
	for _, u := range userIds {
		a, err := GetUserMedalMerge(logger, db, u)
		if err != nil {
			return nil, err
		}
		result[u] = a
	}
	return result, nil
}

func BatchGetUserMedal(db *gorm.DB, userIds []mysql.ID) (map[uint64][]uint32, error) {
	rows := make([]UserMedal, 0)
	if err := db.Model(&UserMedal{}).Where("user_id IN ? and (end_time >= NOW() or end_time is null)", userIds).Find(&rows).Error; err != nil {
		return nil, err
	}
	result := make(map[uint64][]uint32, 0)
	for _, i := range rows {
		if _, ok := result[i.UserId]; !ok {
			result[i.UserId] = make([]uint32, 0)
		}
		result[i.UserId] = append(result[i.UserId], i.MedalId)
	}
	return result, nil
}

func (um *UserMedal) Create(db *gorm.DB, day int) error {
	err := db.Clauses(clause.OnConflict{
		DoUpdates: clause.Assignments(map[string]interface{}{
			"end_time": gorm.Expr("DATE_ADD(GREATEST(end_time,NOW()), INTERVAL ? DAY);", day)}),
	}).Create(um).Error
	// 删除勋章缓存, 延迟删除
	common.DelUserMedalMergeCacheDelay(um.UserId)
	return err
}

func (um *UserMedal) Delete(db *gorm.DB) error {
	err := db.Where(um).Delete(&UserMedal{}).Error
	// 删除勋章缓存
	common.DelUserMedalMergeCache(um.UserId)
	return err
}

func GetUserMedalThreshold(model *domain.Model, userId mysql.ID, resMedalId mysql.ID) (uint64, error) {
	resMedalObtain, err := res_m.ResMedalObtainGetByMedalId(model, resMedalId)
	if err != nil {
		return 0, err
	}
	if resMedalObtain == nil {
		return 0, myerr.NewSysErrorF("res_m.ResMedalObtainGetByMedalId nil resMedalId:%v", resMedalId)
	}
	if resMedalObtain.Type == res_e.WealthResMedalObtainType {
		wealthGrade, _, err := GetWealthGrade(model, userId)
		if err != nil {
			return 0, err
		} else {
			return uint64(wealthGrade), nil
		}
	} else if resMedalObtain.Type == res_e.CharmResMedalObtainType {
		charmGrade, _, err := GetCharmGrade(model, userId)
		if err != nil {
			return 0, err
		} else {
			return uint64(charmGrade), nil
		}
	} else if resMedalObtain.Type == res_e.GiftResMedalObtainType {
		sum, err := gift_m.SumSendGift(model, userId, resMedalObtain.ResGiftId)
		if err != nil {
			return 0, err
		}
		return uint64(sum), nil
	} else if resMedalObtain.Type == res_e.BoomRocketResMedalObtainType {
		s, err := rocket_m.GetUserTopCount(model.Db, userId)
		if err != nil {
			return 0, err
		}
		return uint64(s), nil
	} else if resMedalObtain.Type == res_e.ActityResMedalObtainType {
		wealthGrade, _, err := GetActityGrade(model, userId)
		if err != nil {
			return 0, err
		} else {
			return uint64(wealthGrade), nil
		}
	} else if resMedalObtain.Type == res_e.FruitKingResMedalObtainType {
		diamond, err := fruit_m.SumAwardAll(model.Db, userId)
		if err != nil {
			return 0, err
		} else {
			return uint64(diamond), nil
		}
	} else if resMedalObtain.Type == res_e.LuckyBoxKingResMedalObtainType {
		diamond, err := luckybox_m.GetSumLuckyboxDiamondV2(model, userId)
		if err != nil {
			return 0, err
		} else {
			return diamond, nil
		}
	} else if resMedalObtain.Type == res_e.VideoChatResMedalObtainType {
		videoSeconds, err := count_m.GetVideoChatTimeTotal(model, userId)
		if err != nil {
			return 0, err
		} else {
			return uint64(videoSeconds / 60), nil // min
		}
	} else {
		return 0, myerr.NewSysErrorF("GetUserMedalThreshold 类型错误 ResMedalType:%v", resMedalObtain.Type)
	}
}

//增加勋章
func (user *User) AddPublicMedal(resMedalId mysql.ID) (*UserMedal, error) {
	//判断是否已经拥有了该勋章
	var n int64
	if err := user.Db.Model(&UserMedal{}).Where(&UserMedal{
		UserId:  user.ID,
		MedalId: uint32(resMedalId),
	}).Count(&n).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	if n > 0 {
		return nil, myerr.NewWaringErrorF("用户 userId:%v, 已经获取了勋章 resMedalId:%v", user.ID, resMedalId)
	}
	resMedal, err := res_m.GetResMedalById(user.Model, resMedalId)
	if err != nil {
		return nil, err
	}
	//检查是否符合要求
	threshold, err := GetUserMedalThreshold(user.Model, user.ID, resMedalId)
	if err != nil {
		return nil, err
	}
	if threshold < uint64(resMedal.Threshold) {
		return nil, bizerr.UserMedalThresholdLimit
	}
	//
	return &UserMedal{
		Model:   user.Model,
		UserId:  user.ID,
		MedalId: uint32(resMedalId),
		//MedalType: resMedal.Type,
		//Scope:     resMedal.Scope,
		EndTime: nil,
	}, nil
}