From 5cf091d0397380c2506ac0894cb97844f1778db2 Mon Sep 17 00:00:00 2001 From: dengbiao Date: Wed, 24 Apr 2024 21:02:28 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=B8=80=E4=B8=AA=E5=9C=88=E5=9C=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- md/mq.go | 3 +- ...ctivity_coin_auto_exchange_green_energy.go | 144 +++++++++++++++++- svc/svc_redis_mutex_lock.go | 2 +- 3 files changed, 138 insertions(+), 11 deletions(-) diff --git a/md/mq.go b/md/mq.go index 322f663..d9ef773 100644 --- a/md/mq.go +++ b/md/mq.go @@ -20,8 +20,7 @@ type OneCirclesStructForAutoExchangeGreenEnergy struct { MasterId string `json:"master_id"` Uid int `json:"uid"` Amount string `json:"amount"` - CoinId1 int `json:"coin_id_1"` - CoinId2 int `json:"coin_id_2"` + CoinId int `json:"coin_id"` AutoExchangeNumsAmount string `json:"auto_exchange_nums_amount"` } type OneCirclesStructForStarLevelDividends struct { diff --git a/rule/one_circles/one_circles_activity_coin_auto_exchange_green_energy.go b/rule/one_circles/one_circles_activity_coin_auto_exchange_green_energy.go index 9ed9560..942b3e0 100644 --- a/rule/one_circles/one_circles_activity_coin_auto_exchange_green_energy.go +++ b/rule/one_circles/one_circles_activity_coin_auto_exchange_green_energy.go @@ -91,8 +91,7 @@ func ActivityCoinAutoExchangeGreenEnergyNew(engine *xorm.Engine, masterId string MasterId: masterId, Uid: v.Uid, Amount: v.Amount, - CoinId1: coin1.Id, - CoinId2: coin2.Id, + CoinId: coin1.Id, AutoExchangeNumsAmount: autoExchangeNumsByPersonAmount, }, md.OneCirclesRoutKeyForAutoExchangeGreenEnergyToPerson) } @@ -105,8 +104,7 @@ func ActivityCoinAutoExchangeGreenEnergyNew(engine *xorm.Engine, masterId string MasterId: masterId, Uid: v.Uid, Amount: v.Amount, - CoinId1: coin1.Id, - CoinId2: coin2.Id, + CoinId: coin2.Id, AutoExchangeNumsAmount: autoExchangeNumsByTeamAmount, }, md.OneCirclesRoutKeyForAutoExchangeGreenEnergyToTeam) } @@ -114,8 +112,8 @@ func ActivityCoinAutoExchangeGreenEnergyNew(engine *xorm.Engine, masterId string return } -// ActivityCoinAutoExchangeGreenEnergy 活跃积分自动兑换成绿色能量 -func ActivityCoinAutoExchangeGreenEnergy(engine *xorm.Engine, masterId string) (err error) { +// OldActivityCoinAutoExchangeGreenEnergy 活跃积分自动兑换成绿色能量 +func OldActivityCoinAutoExchangeGreenEnergy(engine *xorm.Engine, masterId string) (err error) { //1、查找 `one_circles_green_energy_basic_setting` 基础设置 && `one_circles_public_platoon_basic_setting` 基础设置 oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{ "key": "is_open", @@ -243,8 +241,8 @@ func ActivityCoinAutoExchangeGreenEnergy(engine *xorm.Engine, masterId string) ( return } -// ActivityCoinAutoExchangeGreenEnergyForTeam 活跃积分自动兑换成绿色能量 -func ActivityCoinAutoExchangeGreenEnergyForTeam(engine *xorm.Engine, masterId string) (err error) { +// OldActivityCoinAutoExchangeGreenEnergyForTeam 活跃积分自动兑换成绿色能量 +func OldActivityCoinAutoExchangeGreenEnergyForTeam(engine *xorm.Engine, masterId string) (err error) { //1、查找 `one_circles_green_energy_basic_setting` 基础设置 && `one_circles_public_platoon_basic_setting` 基础设置 oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{ "key": "is_open", @@ -371,3 +369,133 @@ func ActivityCoinAutoExchangeGreenEnergyForTeam(engine *xorm.Engine, masterId st } return } + +// ActivityCoinAutoExchangeGreenEnergy 活跃积分自动兑换成绿色能量(个人) +func ActivityCoinAutoExchangeGreenEnergy(engine *xorm.Engine, masterId string, ch *rabbit.Channel) (err error) { + //1、查找`one_circles_public_platoon_basic_setting` 基础设置 + oneCirclesPublicPlatoonBasicSetting, err := db.OneCirclesPublicPlatoonBasicSettingGetOneByParams(engine, map[string]interface{}{ + "key": "is_open", + "value": 1, + }) + if err != nil { + return + } + if oneCirclesPublicPlatoonBasicSetting.ExchangeRules == "" { + err = errors.New("自动兑换未设置!") + return + } + var exchangeRulesStruct *md2.ExchangeRulesStruct + err = json.Unmarshal([]byte(oneCirclesPublicPlatoonBasicSetting.ExchangeRules), &exchangeRulesStruct) + if err != nil { + return + } + var autoExchangeNumsByPerson = zhios_order_relate_utils.StrToFloat64(exchangeRulesStruct.AutoExchangeNumsByPerson) //个人活跃积分X个自动兑换 + fmt.Println("autoExchangeNumsByPerson>>>>>>>>>>>>", autoExchangeNumsByPerson) + + //2、获取"个人活跃积分" && 获取"团队活跃积分" + var coin1, coin2 model.VirtualCoin + _, err = engine.Where("id = ?", oneCirclesPublicPlatoonBasicSetting.PersonActivePointsCoinId).Get(&coin1) + if err != nil { + return err + } + _, err = engine.Where("id = ?", oneCirclesPublicPlatoonBasicSetting.TeamActivePointsCoinId).Get(&coin2) + if err != nil { + return err + } + personActivePointsCoinExchangeRatioValue, _ := decimal.NewFromString(coin1.ExchangeRatio) + + //3、当前 "个人活跃积分"可以自动兑换的用户数据 && "团队活跃积分"可以自动兑换的用户数据 + var list1 []model.UserVirtualAmount + err = engine.Where("coin_id = ?", oneCirclesPublicPlatoonBasicSetting.PersonActivePointsCoinId).And("amount >=?", autoExchangeNumsByPerson).Find(&list1) + if err != nil { + fmt.Println("err:::::1111", err) + return + } + + session := engine.NewSession() + defer func() { + session.Close() + if err := recover(); err != nil { + _ = zhios_order_relate_logx.Error(err) + } + }() + session.Begin() + + for _, v := range list1 { + autoExchangeNumsByPersonValue, _ := decimal.NewFromString(v.Amount) + autoExchangeNumsByPersonAmount := autoExchangeNumsByPersonValue.Div(personActivePointsCoinExchangeRatioValue).String() + //TODO::推入rabbitmq 异步处理 + ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForAutoExchangeGreenEnergy{ + MasterId: masterId, + Uid: v.Uid, + Amount: v.Amount, + CoinId: coin1.Id, + AutoExchangeNumsAmount: autoExchangeNumsByPersonAmount, + }, md.OneCirclesRoutKeyForAutoExchangeGreenEnergyToPerson) + } + + return +} + +// ActivityCoinAutoExchangeGreenEnergyForTeam 活跃积分自动兑换成绿色能量(团队) +func ActivityCoinAutoExchangeGreenEnergyForTeam(engine *xorm.Engine, masterId string, ch *rabbit.Channel) (err error) { + //1、查找`one_circles_public_platoon_basic_setting` 基础设置 + oneCirclesPublicPlatoonBasicSetting, err := db.OneCirclesPublicPlatoonBasicSettingGetOneByParams(engine, map[string]interface{}{ + "key": "is_open", + "value": 1, + }) + if err != nil { + return + } + if oneCirclesPublicPlatoonBasicSetting.ExchangeRules == "" { + err = errors.New("自动兑换未设置!") + return + } + var exchangeRulesStruct *md2.ExchangeRulesStruct + err = json.Unmarshal([]byte(oneCirclesPublicPlatoonBasicSetting.ExchangeRules), &exchangeRulesStruct) + if err != nil { + return + } + var autoExchangeNumsByTeam = zhios_order_relate_utils.StrToFloat64(exchangeRulesStruct.AutoExchangeNumsByTeam) //团队活跃积分X个自动兑换 + fmt.Println("autoExchangeNumsByTeam>>>>>>>>>>>>", autoExchangeNumsByTeam) + + //2、获取"个人活跃积分" && 获取"团队活跃积分" + var coin2 model.VirtualCoin + _, err = engine.Where("id = ?", oneCirclesPublicPlatoonBasicSetting.TeamActivePointsCoinId).Get(&coin2) + if err != nil { + return err + } + teamActivePointsCoinExchangeRatioValue, _ := decimal.NewFromString(coin2.ExchangeRatio) + + //3、当前 "个人活跃积分"可以自动兑换的用户数据 && "团队活跃积分"可以自动兑换的用户数据 + var list2 []model.UserVirtualAmount + err = engine.Where("coin_id = ?", oneCirclesPublicPlatoonBasicSetting.TeamActivePointsCoinId).And("amount >=?", autoExchangeNumsByTeam).Find(&list2) + if err != nil { + fmt.Println("err:::::2222", err) + return + } + + session := engine.NewSession() + defer func() { + session.Close() + if err := recover(); err != nil { + _ = zhios_order_relate_logx.Error(err) + } + }() + session.Begin() + + for _, v := range list2 { + autoExchangeNumsByTeamValue, _ := decimal.NewFromString(v.Amount) + autoExchangeNumsByTeamAmount := autoExchangeNumsByTeamValue.Div(teamActivePointsCoinExchangeRatioValue).String() + //TODO::推入rabbitmq 异步处理 + ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForAutoExchangeGreenEnergy{ + MasterId: masterId, + Uid: v.Uid, + Amount: v.Amount, + CoinId: coin2.Id, + AutoExchangeNumsAmount: autoExchangeNumsByTeamAmount, + }, md.OneCirclesRoutKeyForAutoExchangeGreenEnergyToTeam) + } + + return +} diff --git a/svc/svc_redis_mutex_lock.go b/svc/svc_redis_mutex_lock.go index 92cf77e..98bbfce 100644 --- a/svc/svc_redis_mutex_lock.go +++ b/svc/svc_redis_mutex_lock.go @@ -25,7 +25,7 @@ func TryGetDistributedLock(lockKey, requestId string, isNegative bool) bool { return true } // 尝试多次没获取成功 - if retry > 10 { + if retry > 50 { return false } time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000)))