package provider import ( "context" "joybuywork/internal/app" "joybuywork/internal/consts" "joybuywork/internal/consts/ckey" "joybuywork/internal/utils" "joybuywork/internal/utils/lockdo" "joybuywork/internal/utils/merr" "time" ) type FissionClue struct { ctx context.Context be *merr.BusinessError chanceKey string rewardKey string helpKey string preCondKey string clueMapData string } func NewFissionClue(ctx context.Context, userId int64) *FissionClue { return &FissionClue{ ctx: ctx, be: merr.NewBE(ctx), chanceKey: ckey.FissionChanceNotify(userId), rewardKey: ckey.FissionNewcomerRewardNotify(userId), helpKey: ckey.FissionHelpPercentAdd(userId), preCondKey: ckey.FissionUserTaskPreCond(userId), clueMapData: ckey.FissionClueMapData(userId), } } func (s *FissionClue) AddChance(chance int64) { app.Redis.Cache.IncrBy(s.ctx, s.chanceKey, chance) app.Redis.Cache.Expire(s.ctx, s.chanceKey, 2*time.Hour) } func (s *FissionClue) GetChance() (chance int64) { chance, _ = app.Redis.Cache.Get(s.ctx, s.chanceKey).Int64() return } func (s *FissionClue) Notified() { app.Redis.Cache.Del(s.ctx, s.chanceKey, s.rewardKey, s.helpKey, s.preCondKey, s.clueMapData) } func (s *FissionClue) AddNewcomerReward(rewardId int64) { app.Redis.Cache.IncrBy(s.ctx, s.rewardKey, rewardId) app.Redis.Cache.Expire(s.ctx, s.rewardKey, 2*time.Hour) } func (s *FissionClue) GetNewcomerReward() (rewardId int64) { rewardId, _ = app.Redis.Cache.Get(s.ctx, s.rewardKey).Int64() return } func (s *FissionClue) SetPreCond(preCond, expMin int32) { app.Redis.Cache.Set(s.ctx, s.preCondKey, preCond, time.Duration(expMin)*time.Minute) } func (s *FissionClue) GetPreCond() int32 { preCond, _ := app.Redis.Cache.Get(s.ctx, s.preCondKey).Int64() return int32(preCond) } const ( chopRewardIdKey = "chop_reward_id" percentIsFix = "is_fix" oldPercentKey = "old_percent" addPercentKey = "add_percent" ) func (s *FissionClue) ChopHelp(chopRewardId int64, oldPercent, addPercent float64, isFix bool) { _ = lockdo.ExistDo(s.ctx, s.helpKey, func(exist bool) (err error) { if !exist { app.Redis.Cache.HSet(s.ctx, s.helpKey, chopRewardIdKey, chopRewardId, oldPercentKey, oldPercent, percentIsFix, utils.Ternary[string](isFix, percentIsFix, "")) } id, _ := app.Redis.Cache.HGet(s.ctx, s.helpKey, chopRewardIdKey).Int64() if id != chopRewardId { return } app.Redis.Cache.HIncrByFloat(s.ctx, s.helpKey, addPercentKey, addPercent) app.Redis.Cache.Expire(s.ctx, s.helpKey, 2*consts.TimeDay) return }) } type ChopHelp struct { ChopRewardId int64 OldPercent, AddPercent float64 IsFix bool } func (s *FissionClue) GetChopHelp() (chopHelp ChopHelp) { data := app.Redis.Cache.HGetAll(s.ctx, s.helpKey).Val() if len(data) == 0 { return } chopHelp.ChopRewardId = utils.StrToInt64(data[chopRewardIdKey]) chopHelp.OldPercent = utils.StrToFloat64(data[oldPercentKey]).InexactFloat64() chopHelp.AddPercent = utils.StrToFloat64(data[addPercentKey]).InexactFloat64() chopHelp.IsFix = data[percentIsFix] == percentIsFix return } const ( goldIdKey = "gold_id" oldGoldKey = "old_gold" addGoldKey = "add_gold" ) func (s *FissionClue) AddGold(goldId int64, oldGold, add int64) { _ = lockdo.ExistDo(s.ctx, s.clueMapData, func(exist bool) (err error) { if !exist { app.Redis.Cache.HSet(s.ctx, s.clueMapData, goldIdKey, goldId, oldGoldKey, oldGold) } id, _ := app.Redis.Cache.HGet(s.ctx, s.clueMapData, goldIdKey).Int64() if id != goldId { return } app.Redis.Cache.HIncrBy(s.ctx, s.clueMapData, addGoldKey, add) app.Redis.Cache.Expire(s.ctx, s.clueMapData, 2*consts.TimeDay) return }) } type GoldNotify struct { GoldId int64 OldGold, Add int64 } func (s *FissionClue) GetGold() (notify GoldNotify) { data := app.Redis.Cache.HGetAll(s.ctx, s.clueMapData).Val() if len(data) == 0 { return } notify.GoldId = utils.StrToInt64(data[goldIdKey]) notify.OldGold = utils.StrToInt64(data[oldGoldKey]) notify.Add = utils.StrToInt64(data[addGoldKey]) return }