From 683321c8836638bc037e44e6092254726520c403 Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Mon, 18 Mar 2024 15:59:34 +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/fin_user_flow.go | 2 +- svc/svc_deal_user_amount.go | 63 ++++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/md/fin_user_flow.go b/md/fin_user_flow.go index 34d7e2f..f022c50 100644 --- a/md/fin_user_flow.go +++ b/md/fin_user_flow.go @@ -31,7 +31,7 @@ const ( ) const DealUserAmountRequestIdPrefix = "%s:deal_user_amount:%d" -const UserAmountRedisKey = "%s:rule_user_amount:%d" +const UserAmountRedisKey = "%s:rule_cache_user_amount:%d" type DealIntegralReleaseInterpositionUserAmountReq struct { Kind string `json:"kind"` diff --git a/svc/svc_deal_user_amount.go b/svc/svc_deal_user_amount.go index 6f6d007..ba4985c 100644 --- a/svc/svc_deal_user_amount.go +++ b/svc/svc_deal_user_amount.go @@ -32,12 +32,12 @@ func DealUserAmount(session *xorm.Session, req md.DealUserAmount) (err error) { //2、计算&&组装数据 now := time.Now() - userAmount, err := GetUserAmount(session, req.Mid, req.Uid) + userAmount, err := GetUserAmount(session, req.Mid, req.Uid, true) if err != nil { return err } userAmountValue := decimal.NewFromFloat(zhios_order_relate_utils.StrToFloat64(userAmount)) - amountValue := decimal.NewFromFloat(req.Amount).RoundFloor(8) + amountValue := decimal.NewFromFloat(req.Amount).RoundFloor(4) var finUserFlow = model.FinUserFlow{ Uid: req.Uid, @@ -55,10 +55,10 @@ func DealUserAmount(session *xorm.Session, req md.DealUserAmount) (err error) { } if req.Kind == "add" { finUserFlow.Type = 0 - finUserFlow.AfterAmount = userAmountValue.Add(amountValue).RoundFloor(8).String() + finUserFlow.AfterAmount = userAmountValue.Add(amountValue).RoundFloor(4).String() } else if req.Kind == "sub" { finUserFlow.Type = 1 - finUserFlow.AfterAmount = userAmountValue.Sub(amountValue).RoundFloor(8).String() + finUserFlow.AfterAmount = userAmountValue.Sub(amountValue).RoundFloor(4).String() } else { err = errors.New("错误的kind类型") return err @@ -89,6 +89,7 @@ func DealUserAmount(session *xorm.Session, req md.DealUserAmount) (err error) { return nil } + func DealUserAmountNew(session *xorm.Session, req md.DealUserAmount) (err error) { if req.Amount < 0 { req.Amount = 0 @@ -104,7 +105,7 @@ func DealUserAmountNew(session *xorm.Session, req md.DealUserAmount) (err error) } //2、计算&&组装数据 - userAmount, err := GetUserAmount(session, req.Mid, req.Uid) + userAmount, err := GetUserAmount(session, req.Mid, req.Uid, false) if err != nil { return err } @@ -127,13 +128,14 @@ func DealUserAmountNew(session *xorm.Session, req md.DealUserAmount) (err error) return nil } + func DealUserAmountFlow(session *xorm.Session, req md.DealUserAmount) (err error) { if req.Amount < 0 { req.Amount = 0 } //2、计算&&组装数据 now := time.Now() - userAmount, err := GetUserAmount(session, req.Mid, req.Uid) + userAmount, err := GetUserAmount(session, req.Mid, req.Uid, false) if err != nil { return err } @@ -178,25 +180,36 @@ func DealUserAmountFlow(session *xorm.Session, req md.DealUserAmount) (err error } // GetUserAmount 获取用户余额 -func GetUserAmount(session *xorm.Session, masterId string, uid int) (amount string, err error) { - redisKey := fmt.Sprintf(md.UserAmountRedisKey, masterId, uid) - amount, err = cache.GetString(redisKey) - if err != nil { - if err.Error() == "redigo: nil returned" { - userAmount, err := db.UserProfileFindByIDWithSession(session, uid) - if err != nil { - return amount, err - } - if userAmount == nil { - amount = "0" - } else { - amount = userAmount.FinValid - } - //将获取到的余额值缓存至redis - _ = SetCacheUserAmount(session, masterId, amount, uid, false) - return amount, nil +func GetUserAmount(session *xorm.Session, masterId string, uid int, isForceUpdate bool) (amount string, err error) { + if isForceUpdate { + userAmount, err1 := db.UserProfileFindByIDWithSession(session, uid) + if err1 != nil { + return amount, err1 + } + if userAmount == nil { + amount = "0" + } else { + amount = userAmount.FinValid + } + //将获取到的余额值缓存至redis + _ = SetCacheUserAmount(session, masterId, amount, uid, false) + } else { + redisKey := fmt.Sprintf(md.UserAmountRedisKey, masterId, uid) + amount, err = cache.GetString(redisKey) + if err != nil && err.Error() != "redigo: nil returned" { + return + } + userAmount, err1 := db.UserProfileFindByIDWithSession(session, uid) + if err1 != nil { + return amount, err1 + } + if userAmount == nil { + amount = "0" + } else { + amount = userAmount.FinValid } - return amount, err + //将获取到的余额值缓存至redis + _ = SetCacheUserAmount(session, masterId, amount, uid, false) } return amount, nil } @@ -215,7 +228,7 @@ func SetCacheUserAmount(session *xorm.Session, masterId, amount string, uid int, } //_, err := cache.Set(redisKey, int64(utils.StrToFloat64(amount))) //TODO::默认缓存1小时 (先调整为 2 min) - _, err := cache.SetEx(redisKey, zhios_order_relate_utils.StrToFloat64(amount), 60*1) + _, err := cache.SetEx(redisKey, zhios_order_relate_utils.StrToFloat64(amount), 60*0.5) if err != nil { return err }