From 777f072274b925f77944fa444a227fdc3290e462 Mon Sep 17 00:00:00 2001 From: dengbiao Date: Fri, 26 Jul 2024 18:36:06 +0800 Subject: [PATCH 1/8] update --- rule/one_circles/svc/svc_now_price_redis_mutex_lock.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rule/one_circles/svc/svc_now_price_redis_mutex_lock.go b/rule/one_circles/svc/svc_now_price_redis_mutex_lock.go index 70fe5eb..a324f2a 100644 --- a/rule/one_circles/svc/svc_now_price_redis_mutex_lock.go +++ b/rule/one_circles/svc/svc_now_price_redis_mutex_lock.go @@ -28,7 +28,7 @@ func TryGetDistributedLock(lockKey, requestId string, isNegative bool) bool { if retry > 99 { return false } - time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000))) + time.Sleep(time.Millisecond * time.Duration(rand.Intn(100))) retry += 1 } } else { // 只尝试一次 @@ -73,7 +73,7 @@ func HandleDistributedLock(masterId, requestIdPrefix string) (cb func(), err err requestId := GetDistributedLockRequestId(requestIdPrefix) balanceLockOk := TryGetDistributedLock(balanceLockKey, requestId, true) if !balanceLockOk { - return nil, errors.New("系统繁忙,请稍后再试") + return nil, errors.New("获取价格系统繁忙,请稍后再试") } cb = func() { From 07ae15a5b8fce28972bc3f2495d0e9b96452ba4c Mon Sep 17 00:00:00 2001 From: dengbiao Date: Fri, 26 Jul 2024 19:17:57 +0800 Subject: [PATCH 2/8] =?UTF-8?q?update=20=E4=B8=80=E4=B8=AA=E5=9C=88?= =?UTF-8?q?=E5=9C=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- md/mq.go | 5 ++++ ...circles_sign_in_green_energy_settlement.go | 24 +++++++------------ 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/md/mq.go b/md/mq.go index 04476c0..153407d 100644 --- a/md/mq.go +++ b/md/mq.go @@ -4,6 +4,7 @@ const OneCirclesExchange = "one.circles" const ( OneCirclesRoutKeyForSignIn = "sign_in" // 签到 + OneCirclesRoutKeyForSignInUpdateRecords = "sign_in_update_records" // 签到更新记录 OneCirclesRoutKeyForStarLevelDividend = "star_level_dividend" // 星级分红 OneCirclesRoutKeyForAutoExchangeGreenEnergyToPerson = "auto_exchange_green_energy_to_person_1" // 自动兑换个人绿色能量 OneCirclesRoutKeyForAutoExchangeGreenEnergyToTeam = "auto_exchange_green_energy_to_team" // 自动兑换团队绿色能量 @@ -17,6 +18,10 @@ type OneCirclesStructForSignIn struct { Id int64 `json:"id"` EndTime string `json:"end_time"` } +type UpdateOneCirclesStructForSignInRecords struct { + Id int64 `json:"id"` + Uid int `json:"uid"` +} type OneCirclesStructForAutoExchangeGreenEnergy struct { MasterId string `json:"master_id"` diff --git a/rule/one_circles/one_circles_sign_in_green_energy_settlement.go b/rule/one_circles/one_circles_sign_in_green_energy_settlement.go index 36d4a80..095183b 100644 --- a/rule/one_circles/one_circles_sign_in_green_energy_settlement.go +++ b/rule/one_circles/one_circles_sign_in_green_energy_settlement.go @@ -72,18 +72,7 @@ func SettlementSignInGreenEnergy(engine *xorm.Engine, masterId string, ch *rabbi return } -func HandleSettlementSignInGreenEnergy(engine *xorm.Engine, masterId string, id int64, uid int, endTime string) (err error) { - //var signRecord model.OneCirclesGreenEnergySignIn - //has, err00000 := engine.Where("id =?", id).Get(&signRecord) - //if err00000 != nil { - // fmt.Println("err:::::0000000", err00000) - // return err00000 - //} - //if !has { - // //不活跃不需要奖励 - // return errors.New("未查询到对应记录") - //} - +func HandleSettlementSignInGreenEnergy(ch *rabbit.Channel, engine *xorm.Engine, masterId string, id int64, uid int, endTime string) (err error) { //1、查找 `one_circles_green_energy_basic_setting` 基础设置 oneCirclesGreenEnergyBasicSetting, cb, err := svc.GetPrice(engine, masterId) if err != nil { @@ -214,9 +203,14 @@ func HandleSettlementSignInGreenEnergy(engine *xorm.Engine, masterId string, id } //6、更新 `one_circles_green_energy_sign_in` 中的 is_completed 状态 - _, err = session.Where("id = ?", id).Update(&model.OneCirclesGreenEnergySignIn{ - IsCompleted: 1, - }) + //_, err = session.Where("id = ?", id).Update(&model.OneCirclesGreenEnergySignIn{ + // IsCompleted: 1, + //}) + //TODO::推入rabbitmq 异步处理 + ch.Publish(md.OneCirclesExchange, md.UpdateOneCirclesStructForSignInRecords{ + Uid: uid, + Id: id, + }, md.OneCirclesRoutKeyForSignInUpdateRecords) err = session.Commit() if err != nil { From 0d3528790588849d5e5e449fb6ca4ba84619a3ec Mon Sep 17 00:00:00 2001 From: dengbiao Date: Sat, 27 Jul 2024 12:18:18 +0800 Subject: [PATCH 3/8] update --- .../one_circles_auto_adjust_prices.go | 4 ++-- .../one_circles_community_dividends.go | 2 +- ...cles_deal_available_green_energy_points.go | 5 +++-- ...circles_sign_in_green_energy_settlement.go | 22 +++++++++++-------- .../one_circles_star_level_dividends.go | 2 +- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/rule/one_circles/one_circles_auto_adjust_prices.go b/rule/one_circles/one_circles_auto_adjust_prices.go index 3f100d1..56b436f 100644 --- a/rule/one_circles/one_circles_auto_adjust_prices.go +++ b/rule/one_circles/one_circles_auto_adjust_prices.go @@ -62,7 +62,7 @@ func AutoAdjustPrice(engine *xorm.Engine, masterId string) (err error) { } // 市商数量 减少、原始数量 增加、原始资金 减少、市商资金 增加 - err = DealAvailableGreenEnergyCoin(session, int(enum.MarketplaceMerchantNumsAutoExchangeMarketplaceMerchantFunds), zhios_order_relate_utils.StrToFloat64(marketplaceMerchantsNumsExchangeMarketplaceMerchantsFundValue.String()), zhios_order_relate_utils.StrToFloat64(amount), enum.MarketplaceMerchantNumsAutoExchangeMarketplaceMerchantFunds.String(), oneCirclesGreenEnergyBasicSetting, afterPriceValue) + err = DealAvailableGreenEnergyCoin(session, int(enum.MarketplaceMerchantNumsAutoExchangeMarketplaceMerchantFunds), zhios_order_relate_utils.StrToFloat64(marketplaceMerchantsNumsExchangeMarketplaceMerchantsFundValue.String()), zhios_order_relate_utils.StrToFloat64(amount), enum.MarketplaceMerchantNumsAutoExchangeMarketplaceMerchantFunds.String(), oneCirclesGreenEnergyBasicSetting, afterPriceValue, masterId) if err != nil { fmt.Println("err:::::22222", err) _ = session.Rollback() @@ -78,7 +78,7 @@ func AutoAdjustPrice(engine *xorm.Engine, masterId string) (err error) { return err1 } // 市商数量 增加、原始数量 减少、原始资金 增加、市商资金 减少 - err = DealAvailableGreenEnergyCoin(session, int(enum.MarketplaceMerchantFundsAutoExchangeMarketplaceMerchantNums), zhios_order_relate_utils.StrToFloat64(greenEnergy), zhios_order_relate_utils.StrToFloat64(marketplaceMerchantsFundExchangeMarketplaceMerchantsNumsValue.String()), enum.MarketplaceMerchantFundsAutoExchangeMarketplaceMerchantNums.String(), oneCirclesGreenEnergyBasicSetting, afterPriceValue) + err = DealAvailableGreenEnergyCoin(session, int(enum.MarketplaceMerchantFundsAutoExchangeMarketplaceMerchantNums), zhios_order_relate_utils.StrToFloat64(greenEnergy), zhios_order_relate_utils.StrToFloat64(marketplaceMerchantsFundExchangeMarketplaceMerchantsNumsValue.String()), enum.MarketplaceMerchantFundsAutoExchangeMarketplaceMerchantNums.String(), oneCirclesGreenEnergyBasicSetting, afterPriceValue, masterId) if err != nil { fmt.Println("err:::::44444", err) _ = session.Rollback() diff --git a/rule/one_circles/one_circles_community_dividends.go b/rule/one_circles/one_circles_community_dividends.go index 36a25cc..9941930 100644 --- a/rule/one_circles/one_circles_community_dividends.go +++ b/rule/one_circles/one_circles_community_dividends.go @@ -84,7 +84,7 @@ func CommunityDividends(engine *xorm.Engine, masterId string) (err error) { } //6、 减少“社区分红”钱包中的值 totalDividend, _ := totalValue.Float64() - err = DealAvailableGreenEnergyCoin(session, int(enum.CommunityDividends), totalDividend, 0, enum.CommunityDividends.String(), nil, masterId) + err = DealAvailableGreenEnergyCoin(session, int(enum.CommunityDividends), totalDividend, 0, enum.CommunityDividends.String(), nil, "", masterId) if err != nil { _ = session.Rollback() fmt.Println("err:::::444444", err) diff --git a/rule/one_circles/one_circles_deal_available_green_energy_points.go b/rule/one_circles/one_circles_deal_available_green_energy_points.go index 499a044..b2f3d0b 100644 --- a/rule/one_circles/one_circles_deal_available_green_energy_points.go +++ b/rule/one_circles/one_circles_deal_available_green_energy_points.go @@ -12,10 +12,11 @@ import ( "xorm.io/xorm" ) -func DealAvailableGreenEnergyCoin(session *xorm.Session, kind int, amount, amountFee float64, title string, chain *model.OneCirclesGreenEnergyBasicSetting, nowPriceValue string) error { +func DealAvailableGreenEnergyCoin(session *xorm.Session, kind int, amount, amountFee float64, title string, chain *model.OneCirclesGreenEnergyBasicSetting, nowPriceValue, masterId string) error { if chain == nil { // 查找 `one_circles_green_energy_basic_setting` 基础设置 - oneCirclesGreenEnergyBasicSetting, cb, err := svc.GetPrice(session.Engine(), nowPriceValue) + oneCirclesGreenEnergyBasicSetting, cb, err := svc.GetPrice(session.Engine(), masterId) + nowPriceValue = oneCirclesGreenEnergyBasicSetting.NowPrice if err != nil { return err } diff --git a/rule/one_circles/one_circles_sign_in_green_energy_settlement.go b/rule/one_circles/one_circles_sign_in_green_energy_settlement.go index 095183b..fc1d30f 100644 --- a/rule/one_circles/one_circles_sign_in_green_energy_settlement.go +++ b/rule/one_circles/one_circles_sign_in_green_energy_settlement.go @@ -8,7 +8,6 @@ import ( "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule" "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/enum" md2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/md" - "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/svc" zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils" zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" "encoding/json" @@ -74,13 +73,17 @@ func SettlementSignInGreenEnergy(engine *xorm.Engine, masterId string, ch *rabbi func HandleSettlementSignInGreenEnergy(ch *rabbit.Channel, engine *xorm.Engine, masterId string, id int64, uid int, endTime string) (err error) { //1、查找 `one_circles_green_energy_basic_setting` 基础设置 - oneCirclesGreenEnergyBasicSetting, cb, err := svc.GetPrice(engine, masterId) - if err != nil { - return - } - if cb != nil { - defer cb() // 释放锁 - } + oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{ + "key": "is_open", + "value": 1, + }) + //oneCirclesGreenEnergyBasicSetting, cb, err := svc.GetPrice(engine, masterId) + //if err != nil { + // return + //} + //if cb != nil { + // defer cb() // 释放锁 + //} var teamRewardSetting *md2.TeamRewardSettingStruct err = json.Unmarshal([]byte(oneCirclesGreenEnergyBasicSetting.TeamReward), &teamRewardSetting) @@ -194,7 +197,8 @@ func HandleSettlementSignInGreenEnergy(ch *rabbit.Channel, engine *xorm.Engine, //5、减少“活跃赠送” 中的绿色能量 if reduceTotalGreenEnergy > 0 { - err = DealAvailableGreenEnergyCoin(session, int(enum.SignInReward), reduceTotalGreenEnergy, 0, enum.SignInReward.String(), oneCirclesGreenEnergyBasicSetting, oneCirclesGreenEnergyBasicSetting.NowPrice) + //err = DealAvailableGreenEnergyCoin(session, int(enum.SignInReward), reduceTotalGreenEnergy, 0, enum.SignInReward.String(), oneCirclesGreenEnergyBasicSetting, oneCirclesGreenEnergyBasicSetting.NowPrice) + err = DealAvailableGreenEnergyCoin(session, int(enum.SignInReward), reduceTotalGreenEnergy, 0, enum.SignInReward.String(), nil, "", masterId) if err != nil { _ = session.Rollback() fmt.Println("err:::::55555", err) diff --git a/rule/one_circles/one_circles_star_level_dividends.go b/rule/one_circles/one_circles_star_level_dividends.go index 30c5318..0c059c0 100644 --- a/rule/one_circles/one_circles_star_level_dividends.go +++ b/rule/one_circles/one_circles_star_level_dividends.go @@ -288,7 +288,7 @@ func SettlementStarLevelDividends(engine *xorm.Engine, masterId string, ch *rabb //6、 减少“星级分红”中的绿色能量 totalDividend, _ := totalDividendValue.Float64() - err = DealAvailableGreenEnergyCoin(session, int(enum.SettlementStarLevelDividends), totalDividend, 0, enum.SettlementStarLevelDividends.String(), nil, masterId) + err = DealAvailableGreenEnergyCoin(session, int(enum.SettlementStarLevelDividends), totalDividend, 0, enum.SettlementStarLevelDividends.String(), nil, "", masterId) if err != nil { _ = session.Rollback() fmt.Println("err:::::9999999", err) From e8fcfb50508401554a687f157e65c8a31d8571a5 Mon Sep 17 00:00:00 2001 From: dengbiao Date: Sat, 27 Jul 2024 16:34:03 +0800 Subject: [PATCH 4/8] 1 --- ...circles_sign_in_green_energy_settlement.go | 27 ++++++++++--------- svc/svc_redis_mutex_lock.go | 2 +- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/rule/one_circles/one_circles_sign_in_green_energy_settlement.go b/rule/one_circles/one_circles_sign_in_green_energy_settlement.go index fc1d30f..35c36b5 100644 --- a/rule/one_circles/one_circles_sign_in_green_energy_settlement.go +++ b/rule/one_circles/one_circles_sign_in_green_energy_settlement.go @@ -8,6 +8,7 @@ import ( "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule" "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/enum" md2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/md" + "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/svc" zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils" zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" "encoding/json" @@ -73,17 +74,17 @@ func SettlementSignInGreenEnergy(engine *xorm.Engine, masterId string, ch *rabbi func HandleSettlementSignInGreenEnergy(ch *rabbit.Channel, engine *xorm.Engine, masterId string, id int64, uid int, endTime string) (err error) { //1、查找 `one_circles_green_energy_basic_setting` 基础设置 - oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{ - "key": "is_open", - "value": 1, - }) - //oneCirclesGreenEnergyBasicSetting, cb, err := svc.GetPrice(engine, masterId) - //if err != nil { - // return - //} - //if cb != nil { - // defer cb() // 释放锁 - //} + //oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{ + // "key": "is_open", + // "value": 1, + //}) + oneCirclesGreenEnergyBasicSetting, cb, err := svc.GetPrice(engine, masterId) + if err != nil { + return + } + if cb != nil { + defer cb() // 释放锁 + } var teamRewardSetting *md2.TeamRewardSettingStruct err = json.Unmarshal([]byte(oneCirclesGreenEnergyBasicSetting.TeamReward), &teamRewardSetting) @@ -197,8 +198,8 @@ func HandleSettlementSignInGreenEnergy(ch *rabbit.Channel, engine *xorm.Engine, //5、减少“活跃赠送” 中的绿色能量 if reduceTotalGreenEnergy > 0 { - //err = DealAvailableGreenEnergyCoin(session, int(enum.SignInReward), reduceTotalGreenEnergy, 0, enum.SignInReward.String(), oneCirclesGreenEnergyBasicSetting, oneCirclesGreenEnergyBasicSetting.NowPrice) - err = DealAvailableGreenEnergyCoin(session, int(enum.SignInReward), reduceTotalGreenEnergy, 0, enum.SignInReward.String(), nil, "", masterId) + err = DealAvailableGreenEnergyCoin(session, int(enum.SignInReward), reduceTotalGreenEnergy, 0, enum.SignInReward.String(), oneCirclesGreenEnergyBasicSetting, oneCirclesGreenEnergyBasicSetting.NowPrice, masterId) + //err = DealAvailableGreenEnergyCoin(session, int(enum.SignInReward), reduceTotalGreenEnergy, 0, enum.SignInReward.String(), nil, "", masterId) if err != nil { _ = session.Rollback() fmt.Println("err:::::55555", err) diff --git a/svc/svc_redis_mutex_lock.go b/svc/svc_redis_mutex_lock.go index 98bbfce..96f623d 100644 --- a/svc/svc_redis_mutex_lock.go +++ b/svc/svc_redis_mutex_lock.go @@ -28,7 +28,7 @@ func TryGetDistributedLock(lockKey, requestId string, isNegative bool) bool { if retry > 50 { return false } - time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000))) + time.Sleep(time.Millisecond * time.Duration(rand.Intn(100))) retry += 1 } } else { // 只尝试一次 From 3bb4f991c4fc91490ffbf3480a39e93a86711454 Mon Sep 17 00:00:00 2001 From: dengbiao Date: Sat, 27 Jul 2024 17:35:32 +0800 Subject: [PATCH 5/8] update --- md/app_redis_key.go | 4 ++-- rule/block_green_chain_settlement.go | 2 +- rule/integral_release_settlement.go | 2 +- rule/niu_bei_release_settlement.go | 2 +- rule/user_amount.go | 2 +- svc/svc_block_star_chain_settlement.go | 4 ++-- svc/svc_redis_mutex_lock.go | 4 ++-- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/md/app_redis_key.go b/md/app_redis_key.go index af03a8f..4bd6397 100644 --- a/md/app_redis_key.go +++ b/md/app_redis_key.go @@ -7,8 +7,8 @@ const ( PlanRewardCfgCacheKey = "%s:plan_reward_cfg" UnionSetCacheCfg = "%s:union_set_cfg:%s" // 联盟设置缓存key - UserFinValidUpdateLock = "%s:user_fin_valid_update_lock:%s" // 用户余额更新锁(能拿到锁才能更新余额) - UserVirtualAmountUpdateLock = "%s:user_virtual_amount_update_lock:%s" // 用户虚拟币更新锁(能拿到锁才能更新余额) + UserFinValidUpdateLock = "%s:user_fin_valid_update_lock:%s" // 用户余额更新锁(能拿到锁才能更新余额) + UserVirtualAmountUpdateLock = "%s:user_virtual_amount_update_lock:%s:%s" // 用户虚拟币更新锁(能拿到锁才能更新余额) WithdrawApplyQueueListKey = "withdraw_apply_queue" // 提现队列 diff --git a/rule/block_green_chain_settlement.go b/rule/block_green_chain_settlement.go index 5c74ee4..7792cda 100644 --- a/rule/block_green_chain_settlement.go +++ b/rule/block_green_chain_settlement.go @@ -563,7 +563,7 @@ func DealUserCoinForGreen(session *xorm.Session, req md.DealUserCoinReq) (err er } //1、分布式锁阻拦 requestIdPrefix := fmt.Sprintf(md.DealUserCoinForGreenRequestIdPrefix, req.Mid, req.CoinId, req.Uid) - cb, err := svc.HandleDistributedLock(req.Mid, strconv.Itoa(req.Uid), requestIdPrefix) + cb, err := svc.HandleDistributedLock(req.Mid, strconv.Itoa(req.Uid), requestIdPrefix, strconv.Itoa(req.CoinId)) if err != nil { return err } diff --git a/rule/integral_release_settlement.go b/rule/integral_release_settlement.go index c355aa9..9b59c29 100644 --- a/rule/integral_release_settlement.go +++ b/rule/integral_release_settlement.go @@ -352,7 +352,7 @@ func DealUserCoinForIntegralRelease(session *xorm.Session, req md.DealUserCoinRe } //1、分布式锁阻拦 requestIdPrefix := fmt.Sprintf(DealUserCoinForIntegralReleaseRequestIdPrefix, req.Mid, req.CoinId, req.Uid) - cb, err := svc.HandleDistributedLock(req.Mid, strconv.Itoa(req.Uid), requestIdPrefix) + cb, err := svc.HandleDistributedLock(req.Mid, strconv.Itoa(req.Uid), requestIdPrefix, strconv.Itoa(req.CoinId)) if err != nil { return err } diff --git a/rule/niu_bei_release_settlement.go b/rule/niu_bei_release_settlement.go index 6bf0361..5b6ca1d 100644 --- a/rule/niu_bei_release_settlement.go +++ b/rule/niu_bei_release_settlement.go @@ -365,7 +365,7 @@ func DealUserCoinForNiuBeiRelease(session *xorm.Session, req md.DealUserCoinReq) } //1、分布式锁阻拦 requestIdPrefix := fmt.Sprintf(DealUserCoinForIntegralReleaseRequestIdPrefix, req.Mid, req.CoinId, req.Uid) - cb, err := svc.HandleDistributedLock(req.Mid, strconv.Itoa(req.Uid), requestIdPrefix) + cb, err := svc.HandleDistributedLock(req.Mid, strconv.Itoa(req.Uid), requestIdPrefix, strconv.Itoa(req.CoinId)) if err != nil { return err } diff --git a/rule/user_amount.go b/rule/user_amount.go index 98c04b7..ce77343 100644 --- a/rule/user_amount.go +++ b/rule/user_amount.go @@ -21,7 +21,7 @@ func DealUserCoin(session *xorm.Session, req md.DealUserCoinReq) (err error) { } //1、分布式锁阻拦 requestIdPrefix := fmt.Sprintf(md.DealUserCoinRequestIdPrefix, req.Mid, req.CoinId, req.Uid) - cb, err := svc.HandleDistributedLock(req.Mid, strconv.Itoa(req.Uid), requestIdPrefix) + cb, err := svc.HandleDistributedLock(req.Mid, strconv.Itoa(req.Uid), requestIdPrefix, strconv.Itoa(req.CoinId)) if err != nil { return err } diff --git a/svc/svc_block_star_chain_settlement.go b/svc/svc_block_star_chain_settlement.go index 235e7f6..2ff7a51 100644 --- a/svc/svc_block_star_chain_settlement.go +++ b/svc/svc_block_star_chain_settlement.go @@ -48,8 +48,8 @@ func SetCacheUserVirtualAmount(session *xorm.Session, masterId, amount string, c } } //_, err := cache.Set(redisKey, int64(utils.StrToFloat64(amount))) - //TODO::默认缓存1小时 (先调整为 2 min) - _, err := cache.SetEx(redisKey, zhios_order_relate_utils.StrToFloat64(amount), 60*2) + //TODO::默认缓存1小时 (先调整为 20 min) + _, err := cache.SetEx(redisKey, zhios_order_relate_utils.StrToFloat64(amount), 60*20) if err != nil { return err } diff --git a/svc/svc_redis_mutex_lock.go b/svc/svc_redis_mutex_lock.go index 96f623d..8cc4d32 100644 --- a/svc/svc_redis_mutex_lock.go +++ b/svc/svc_redis_mutex_lock.go @@ -67,9 +67,9 @@ func GetDistributedLockRequestId(prefix string) string { } // HandleDistributedLock 处理余额更新时获取锁和释放锁 如果加锁成功,使用语句 ` defer cb() ` 释放锁 -func HandleDistributedLock(masterId, uid, requestIdPrefix string) (cb func(), err error) { +func HandleDistributedLock(masterId, uid, coinId, requestIdPrefix string) (cb func(), err error) { // 获取余额更新锁 - balanceLockKey := fmt.Sprintf(md.UserVirtualAmountUpdateLock, masterId, uid) + balanceLockKey := fmt.Sprintf(md.UserVirtualAmountUpdateLock, masterId, uid, coinId) requestId := GetDistributedLockRequestId(requestIdPrefix) balanceLockOk := TryGetDistributedLock(balanceLockKey, requestId, true) if !balanceLockOk { From b17347dfda58662e133df1ffd377133d26d6b1e0 Mon Sep 17 00:00:00 2001 From: dengbiao Date: Sat, 27 Jul 2024 18:28:26 +0800 Subject: [PATCH 6/8] 1 --- ...circles_sign_in_green_energy_settlement.go | 56 ++++++++++--------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/rule/one_circles/one_circles_sign_in_green_energy_settlement.go b/rule/one_circles/one_circles_sign_in_green_energy_settlement.go index 35c36b5..1f7cba4 100644 --- a/rule/one_circles/one_circles_sign_in_green_energy_settlement.go +++ b/rule/one_circles/one_circles_sign_in_green_energy_settlement.go @@ -10,6 +10,7 @@ import ( md2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/md" "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/svc" zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils" + "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/cache" zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" "encoding/json" "errors" @@ -228,38 +229,43 @@ func HandleSettlementSignInGreenEnergy(ch *rabbit.Channel, engine *xorm.Engine, // CalcNowSignInGreenEnergy 计算当前签到拿多少绿色能量/秒 func CalcNowSignInGreenEnergy(engine *xorm.Engine, oneCirclesGreenEnergyBasicSetting *model.OneCirclesGreenEnergyBasicSetting) (err error, rewardValue string) { - if oneCirclesGreenEnergyBasicSetting == nil { - //1、查找 `one_circles_green_energy_basic_setting` 基础设置 - oneCirclesGreenEnergyBasicSetting, err = db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{ - "key": "is_open", - "value": 1, - }) + rewardValue, _ = cache.GetString("one_circle_reward_value") + if rewardValue == "" { + if oneCirclesGreenEnergyBasicSetting == nil { + //1、查找 `one_circles_green_energy_basic_setting` 基础设置 + oneCirclesGreenEnergyBasicSetting, err = db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{ + "key": "is_open", + "value": 1, + }) + if err != nil { + return + } + } + + var signInRewards []*md2.SignInRewardStruct + err = json.Unmarshal([]byte(oneCirclesGreenEnergyBasicSetting.SignInReward), &signInRewards) if err != nil { return } - } - - var signInRewards []*md2.SignInRewardStruct - err = json.Unmarshal([]byte(oneCirclesGreenEnergyBasicSetting.SignInReward), &signInRewards) - if err != nil { - return - } - if len(signInRewards) == 0 { - err = errors.New("未设置签到奖励!") - return - } + if len(signInRewards) == 0 { + err = errors.New("未设置签到奖励!") + return + } - //2、统计全网用户数 - sqlStr := "SELECT COUNT(*) AS total FROM user " - nativeString, _ := db.QueryNativeString(engine, sqlStr) - userCount := zhios_order_relate_utils.StrToInt64(nativeString[0]["total"]) + //2、统计全网用户数 + sqlStr := "SELECT COUNT(*) AS total FROM user " + nativeString, _ := db.QueryNativeString(engine, sqlStr) + userCount := zhios_order_relate_utils.StrToInt64(nativeString[0]["total"]) - for _, v := range signInRewards { - if zhios_order_relate_utils.StrToInt64(v.VipMemberEndNums) >= userCount && userCount >= zhios_order_relate_utils.StrToInt64(v.VipMemberStartNums) { - //rewardValue = zhios_order_relate_utils.Float64ToStrPrec8(zhios_order_relate_utils.StrToFloat64(v.RewardValue) * float64(oneRoundDuration) * 60 * 60) - rewardValue = v.RewardValue + for _, v := range signInRewards { + if zhios_order_relate_utils.StrToInt64(v.VipMemberEndNums) >= userCount && userCount >= zhios_order_relate_utils.StrToInt64(v.VipMemberStartNums) { + //rewardValue = zhios_order_relate_utils.Float64ToStrPrec8(zhios_order_relate_utils.StrToFloat64(v.RewardValue) * float64(oneRoundDuration) * 60 * 60) + rewardValue = v.RewardValue + } } + cache.SetEx("one_circle_reward_value", rewardValue, 3600*8) //8小时 } + return } From da7c2fc02dd7f8e84257eb09b715008c5b103135 Mon Sep 17 00:00:00 2001 From: dengbiao Date: Sat, 27 Jul 2024 19:56:14 +0800 Subject: [PATCH 7/8] update --- md/mq.go | 13 +++ ...circles_sign_in_green_energy_settlement.go | 81 +++++++------------ 2 files changed, 41 insertions(+), 53 deletions(-) diff --git a/md/mq.go b/md/mq.go index 153407d..ba10389 100644 --- a/md/mq.go +++ b/md/mq.go @@ -9,6 +9,7 @@ const ( OneCirclesRoutKeyForAutoExchangeGreenEnergyToPerson = "auto_exchange_green_energy_to_person_1" // 自动兑换个人绿色能量 OneCirclesRoutKeyForAutoExchangeGreenEnergyToTeam = "auto_exchange_green_energy_to_team" // 自动兑换团队绿色能量 OneCirclesRoutKeyForSettlementPublicGiveActivityCoin = "settlement_public_give_activity_coin" // 计算观看激励视屏得到活跃积分 + OneCirclesRoutKeyForRewardUserCoin = "reward_user_coin" // 奖励用户虚拟币 ) @@ -35,3 +36,15 @@ type OneCirclesStructForStarLevelDividends struct { Uid int `json:"uid"` SignDividend float64 `json:"sign_dividend"` } + +type OneCirclesDealUserCoinStruct struct { + Kind string `json:"kind"` + Mid string `json:"mid"` + Title string `json:"title"` + TransferType int `json:"transfer_type"` + OrdId string `json:"ord_id"` + CoinId int `json:"coin_id"` + Uid int `json:"uid"` + ToUid int `json:"to_uid"` + Amount float64 `json:"amount"` +} diff --git a/rule/one_circles/one_circles_sign_in_green_energy_settlement.go b/rule/one_circles/one_circles_sign_in_green_energy_settlement.go index 1f7cba4..4d1d34c 100644 --- a/rule/one_circles/one_circles_sign_in_green_energy_settlement.go +++ b/rule/one_circles/one_circles_sign_in_green_energy_settlement.go @@ -5,13 +5,10 @@ import ( "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db" "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model" "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/md" - "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule" "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/enum" md2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/md" - "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/svc" zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils" "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/cache" - zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" "encoding/json" "errors" "fmt" @@ -75,17 +72,18 @@ func SettlementSignInGreenEnergy(engine *xorm.Engine, masterId string, ch *rabbi func HandleSettlementSignInGreenEnergy(ch *rabbit.Channel, engine *xorm.Engine, masterId string, id int64, uid int, endTime string) (err error) { //1、查找 `one_circles_green_energy_basic_setting` 基础设置 - //oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{ - // "key": "is_open", - // "value": 1, - //}) - oneCirclesGreenEnergyBasicSetting, cb, err := svc.GetPrice(engine, masterId) - if err != nil { - return - } - if cb != nil { - defer cb() // 释放锁 - } + //oneCirclesGreenEnergyBasicSetting, cb, err := svc.GetPrice(engine, masterId) + //if err != nil { + // return + //} + //if cb != nil { + // defer cb() // 释放锁 + //} + + oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{ + "key": "is_open", + "value": 1, + }) var teamRewardSetting *md2.TeamRewardSettingStruct err = json.Unmarshal([]byte(oneCirclesGreenEnergyBasicSetting.TeamReward), &teamRewardSetting) @@ -110,18 +108,10 @@ func HandleSettlementSignInGreenEnergy(ch *rabbit.Channel, engine *xorm.Engine, rewardValue := zhios_order_relate_utils.Float64ToStrPrec8(zhios_order_relate_utils.StrToFloat64(reward) * float64(oneRoundDuration) * 60 * 60) //3、统计签到结束的用户数据 - session := engine.NewSession() - defer func() { - session.Close() - if err := recover(); err != nil { - _ = zhios_order_relate_logx.Error(err) - } - }() - session.Begin() var reduceTotalGreenEnergy float64 //4.2给相应的用户加上个人的绿色积分(可用数量) - err = rule.DealUserCoin(session, md.DealUserCoinReq{ + var oneCirclesDealUserCoinData = md.OneCirclesDealUserCoinStruct{ Kind: "add", Mid: masterId, Title: md.OneCirclesGreenEnergySignInSettlementPersonalReward, @@ -130,21 +120,16 @@ func HandleSettlementSignInGreenEnergy(ch *rabbit.Channel, engine *xorm.Engine, CoinId: oneCirclesGreenEnergyBasicSetting.PersonGreenEnergyCoinId, Uid: uid, Amount: zhios_order_relate_utils.StrToFloat64(rewardValue), - }) - if err != nil { - _ = session.Rollback() - fmt.Println("err:::::2222", err) - return err } - reduceTotalGreenEnergy += zhios_order_relate_utils.StrToFloat64(rewardValue) + reduceTotalGreenEnergy += zhios_order_relate_utils.StrToFloat64(rewardValue) //4.2给相应的上级用户加上团队奖励的绿色积分(结算数量) relates, err1 := db.DbsUserRelate(engine, uid, 0) if err1 != nil { - _ = session.Rollback() fmt.Println("err:::::3333", err1) return err1 } + var oneCirclesDealUserCoinDataForParents []md.OneCirclesDealUserCoinStruct var parentRewardValue = zhios_order_relate_utils.StrToFloat64(rewardValue) if relates != nil { for _, relate := range *relates { @@ -157,7 +142,6 @@ func HandleSettlementSignInGreenEnergy(ch *rabbit.Channel, engine *xorm.Engine, if endTime != "" { has, err3333333 := engine.Where("uid =?", relate.ParentUid).And("end_time >=?", endTime).Get(&m) if err3333333 != nil { - _ = session.Rollback() fmt.Println("err:::::3333333", err3333333) return err3333333 } @@ -168,7 +152,6 @@ func HandleSettlementSignInGreenEnergy(ch *rabbit.Channel, engine *xorm.Engine, } else { has, err3333333 := engine.Where("uid =?", relate.ParentUid).And("end_time >=?", time.Now().Format("2006-01-02 15:04:05")).Get(&m) if err3333333 != nil { - _ = session.Rollback() fmt.Println("err:::::3333333", err3333333) return err3333333 } @@ -178,7 +161,7 @@ func HandleSettlementSignInGreenEnergy(ch *rabbit.Channel, engine *xorm.Engine, } } - err = rule.DealUserCoin(session, md.DealUserCoinReq{ + oneCirclesDealUserCoinDataForParents = append(oneCirclesDealUserCoinDataForParents, md.OneCirclesDealUserCoinStruct{ Kind: "add", Mid: masterId, Title: md.OneCirclesGreenEnergySignInSettlementTeamReward, @@ -188,40 +171,32 @@ func HandleSettlementSignInGreenEnergy(ch *rabbit.Channel, engine *xorm.Engine, Uid: relate.ParentUid, Amount: parentRewardValue, }) - if err != nil { - _ = session.Rollback() - fmt.Println("err:::::44444", err) - return err - } reduceTotalGreenEnergy += parentRewardValue } } //5、减少“活跃赠送” 中的绿色能量 if reduceTotalGreenEnergy > 0 { - err = DealAvailableGreenEnergyCoin(session, int(enum.SignInReward), reduceTotalGreenEnergy, 0, enum.SignInReward.String(), oneCirclesGreenEnergyBasicSetting, oneCirclesGreenEnergyBasicSetting.NowPrice, masterId) - //err = DealAvailableGreenEnergyCoin(session, int(enum.SignInReward), reduceTotalGreenEnergy, 0, enum.SignInReward.String(), nil, "", masterId) + err = DealAvailableGreenEnergyCoin(engine.NewSession(), int(enum.SignInReward), reduceTotalGreenEnergy, 0, enum.SignInReward.String(), oneCirclesGreenEnergyBasicSetting, oneCirclesGreenEnergyBasicSetting.NowPrice, masterId) if err != nil { - _ = session.Rollback() fmt.Println("err:::::55555", err) return err } } //6、更新 `one_circles_green_energy_sign_in` 中的 is_completed 状态 - //_, err = session.Where("id = ?", id).Update(&model.OneCirclesGreenEnergySignIn{ - // IsCompleted: 1, - //}) - //TODO::推入rabbitmq 异步处理 - ch.Publish(md.OneCirclesExchange, md.UpdateOneCirclesStructForSignInRecords{ - Uid: uid, - Id: id, - }, md.OneCirclesRoutKeyForSignInUpdateRecords) - - err = session.Commit() + _, err = engine.Where("id = ?", id).Update(&model.OneCirclesGreenEnergySignIn{ + IsCompleted: 1, + }) if err != nil { - _ = session.Rollback() - return errors.New("事务提交失败") + fmt.Println("err:::::66666", err) + return err + } + + ////TODO::推入rabbitmq 异步处理 + ch.Publish(md.OneCirclesExchange, oneCirclesDealUserCoinData, md.OneCirclesRoutKeyForRewardUserCoin) //个人的奖励 + for _, oneCirclesDealUserCoinDataForParent := range oneCirclesDealUserCoinDataForParents { + ch.Publish(md.OneCirclesExchange, oneCirclesDealUserCoinDataForParent, md.OneCirclesRoutKeyForRewardUserCoin) //上级的奖励 } return From efeac1c03858347f8934be832f0872c1736eccef Mon Sep 17 00:00:00 2001 From: dengbiao Date: Sat, 27 Jul 2024 20:06:29 +0800 Subject: [PATCH 8/8] update --- md/mq.go | 12 ------------ .../one_circles_sign_in_green_energy_settlement.go | 6 +++--- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/md/mq.go b/md/mq.go index ba10389..00711f5 100644 --- a/md/mq.go +++ b/md/mq.go @@ -36,15 +36,3 @@ type OneCirclesStructForStarLevelDividends struct { Uid int `json:"uid"` SignDividend float64 `json:"sign_dividend"` } - -type OneCirclesDealUserCoinStruct struct { - Kind string `json:"kind"` - Mid string `json:"mid"` - Title string `json:"title"` - TransferType int `json:"transfer_type"` - OrdId string `json:"ord_id"` - CoinId int `json:"coin_id"` - Uid int `json:"uid"` - ToUid int `json:"to_uid"` - Amount float64 `json:"amount"` -} diff --git a/rule/one_circles/one_circles_sign_in_green_energy_settlement.go b/rule/one_circles/one_circles_sign_in_green_energy_settlement.go index 4d1d34c..73a02f4 100644 --- a/rule/one_circles/one_circles_sign_in_green_energy_settlement.go +++ b/rule/one_circles/one_circles_sign_in_green_energy_settlement.go @@ -111,7 +111,7 @@ func HandleSettlementSignInGreenEnergy(ch *rabbit.Channel, engine *xorm.Engine, var reduceTotalGreenEnergy float64 //4.2给相应的用户加上个人的绿色积分(可用数量) - var oneCirclesDealUserCoinData = md.OneCirclesDealUserCoinStruct{ + var oneCirclesDealUserCoinData = md.DealUserCoinReq{ Kind: "add", Mid: masterId, Title: md.OneCirclesGreenEnergySignInSettlementPersonalReward, @@ -129,7 +129,7 @@ func HandleSettlementSignInGreenEnergy(ch *rabbit.Channel, engine *xorm.Engine, fmt.Println("err:::::3333", err1) return err1 } - var oneCirclesDealUserCoinDataForParents []md.OneCirclesDealUserCoinStruct + var oneCirclesDealUserCoinDataForParents []md.DealUserCoinReq var parentRewardValue = zhios_order_relate_utils.StrToFloat64(rewardValue) if relates != nil { for _, relate := range *relates { @@ -161,7 +161,7 @@ func HandleSettlementSignInGreenEnergy(ch *rabbit.Channel, engine *xorm.Engine, } } - oneCirclesDealUserCoinDataForParents = append(oneCirclesDealUserCoinDataForParents, md.OneCirclesDealUserCoinStruct{ + oneCirclesDealUserCoinDataForParents = append(oneCirclesDealUserCoinDataForParents, md.DealUserCoinReq{ Kind: "add", Mid: masterId, Title: md.OneCirclesGreenEnergySignInSettlementTeamReward,