From 5b62e6dc008267cea3d6b3fe8143e50647dfffb4 Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Wed, 6 Mar 2024 19:45:17 +0800 Subject: [PATCH 01/16] =?UTF-8?q?update=20=E7=BB=BF=E8=89=B2=E5=8F=8C?= =?UTF-8?q?=E9=93=BE=E7=A7=AF=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rule/green_coin_double_chain_settlement.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rule/green_coin_double_chain_settlement.go b/rule/green_coin_double_chain_settlement.go index 0ce88c4..9884985 100644 --- a/rule/green_coin_double_chain_settlement.go +++ b/rule/green_coin_double_chain_settlement.go @@ -74,6 +74,8 @@ func DealUserGreenCoinDoubleChainIntegral(Db *xorm.Engine, uid int, amount, ordI parentAmountValue, _ := decimal.NewFromString(parentAmount) if parentAmountValue.LessThan(amountValue) { //TODO::上级绿色积分小于当前需要兑换积分金额 + amountValue = parentAmountValue + amount = amountValue.String() return } if userAmountValue.LessThan(amountValue) { From aef7b2846e2945cb01ffddc931d33eb844626f0b Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Thu, 7 Mar 2024 17:31:52 +0800 Subject: [PATCH 02/16] =?UTF-8?q?update=20=E7=BB=BF=E8=89=B2=E5=8F=8C?= =?UTF-8?q?=E9=93=BE=E7=A7=AF=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/db_green_coin_double_chain.go | 2 +- rule/green_coin_double_chain_settlement.go | 503 +++++++++++++++------ 2 files changed, 372 insertions(+), 133 deletions(-) diff --git a/db/db_green_coin_double_chain.go b/db/db_green_coin_double_chain.go index 38ade81..dada9b8 100644 --- a/db/db_green_coin_double_chain.go +++ b/db/db_green_coin_double_chain.go @@ -75,7 +75,7 @@ func GreenCoinDoubleChainUpdate(session *xorm.Session, id interface{}, GreenCoin } // GreenCoinDoubleChainGetOneByParams 通过传入的参数查询数据(单条) -func GreenCoinDoubleChainGetOneByParams(session *xorm.Session, params map[string]interface{}) (*model.GreenCoinDoubleChain, error) { +func GreenCoinDoubleChainGetOneByParams(session *xorm.Engine, params map[string]interface{}) (*model.GreenCoinDoubleChain, error) { var m model.GreenCoinDoubleChain var query = fmt.Sprintf("%s =?", params["key"]) has, err := session.Where(query, params["value"]).Get(&m) diff --git a/rule/green_coin_double_chain_settlement.go b/rule/green_coin_double_chain_settlement.go index 9884985..064f370 100644 --- a/rule/green_coin_double_chain_settlement.go +++ b/rule/green_coin_double_chain_settlement.go @@ -23,7 +23,17 @@ func InitForGreenCoinDoubleChainIntegral(redisAddr string) (err error) { return } -func DealUserGreenCoinDoubleChainIntegral(Db *xorm.Engine, uid int, amount, ordId, masterId string) (isExchange bool, err error) { +func DealUserGreenCoinDoubleChainIntegral(Db *xorm.Engine, uid int, ordId, masterId string) (err error) { + //1、查找 `green_coin_double_chain` 基础设置 + greenCoinDoubleChain, err := db.GreenCoinDoubleChainGetOneByParams(Db, map[string]interface{}{ + "key": "is_use", + "value": 1, + }) + if err != nil { + return + } + + //2、判断当前会员等级 user, err := db.DbsUserFindByUid(Db, uid) if err != nil { return @@ -33,25 +43,23 @@ func DealUserGreenCoinDoubleChainIntegral(Db *xorm.Engine, uid int, amount, ordI return } - amountValue, _ := decimal.NewFromString(amount) - session := Db.NewSession() - defer func() { - session.Close() - if err := recover(); err != nil { - _ = zhios_order_relate_logx.Error(err) - } - }() - session.Begin() - //1、查找 `green_coin_double_chain` 基础设置 - greenCoinDoubleChain, err := db.GreenCoinDoubleChainGetOneByParams(session, map[string]interface{}{ - "key": "is_use", - "value": 1, - }) + //3、处理“贡献积分”作用于上级 + err = HandleUserGreenCoinDoubleChainIntegralByParent(Db, uid, ordId, masterId, greenCoinDoubleChain) if err != nil { - return + return err } - //2、查找对应直推上级用户 + //4、处理“绿色积分”作用于下级 + err = HandleUserGreenCoinDoubleChainIntegralBySon(Db, uid, ordId, masterId, greenCoinDoubleChain) + if err != nil { + return err + } + + return +} + +func HandleUserGreenCoinDoubleChainIntegralByParent(Db *xorm.Engine, uid int, ordId, masterId string, greenCoinDoubleChain *model.GreenCoinDoubleChain) (err error) { + //1、查找对应直推上级用户 parentUserRelate, err := db.GetUserParentUserRelate(Db, uid) if err != nil { return @@ -61,29 +69,18 @@ func DealUserGreenCoinDoubleChainIntegral(Db *xorm.Engine, uid int, amount, ordI return } - //3、查询用户和对应上级的虚拟币余额 - userAmount, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, uid) + //2、查询对应上级的虚拟币余额 + parentAmount, err := svc.GetUserCoinAmount(Db.NewSession(), masterId, greenCoinDoubleChain.Coin1, parentUserRelate.ParentUid) if err != nil { return } - parentAmount, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin1, parentUserRelate.ParentUid) - if err != nil { - return - } - userAmountValue, _ := decimal.NewFromString(userAmount) parentAmountValue, _ := decimal.NewFromString(parentAmount) - if parentAmountValue.LessThan(amountValue) { - //TODO::上级绿色积分小于当前需要兑换积分金额 - amountValue = parentAmountValue - amount = amountValue.String() - return - } - if userAmountValue.LessThan(amountValue) { - err = errors.New("当前用户金额有误,不足以进行兑换") + if parentAmountValue.LessThan(decimal.NewFromFloat(0)) { + //TODO::上级绿色积分小于0不需要进行兑换 return } - //4、查询上级直推的所有下级用户 + //3、查询上级直推的所有下级用户 sonUserRelates, err := db.DbsUserRelateByParentUid(Db, parentUserRelate.ParentUid, 1) if err != nil { return @@ -111,126 +108,368 @@ func DealUserGreenCoinDoubleChainIntegral(Db *xorm.Engine, uid int, amount, ordI } } - //5、查询上级直推的所有下级用户的虚拟币金额是否有大于当前用户的 - var sonUserVirtualWallet model.UserVirtualAmount - isHas, err := session.Where(" coin_id = ? and amount >=?", greenCoinDoubleChain.Coin2, amount).In("uid", userIds).Get(&sonUserVirtualWallet) + //4、查询上级直推的所有下级用户的虚拟币金额(按升序) + var sonUserVirtualWallet []model.UserVirtualAmount + Db.Where(" coin_id = ?", greenCoinDoubleChain.Coin2).In("uid", userIds).Asc("amount").Find(&sonUserVirtualWallet) if err != nil { return } - if isHas == false { - //TODO::没有符合匹配的下级用户 - return - } /* - TODO::6、进行“数据变更” + TODO::5、进行“数据变更” */ - //6.1、上级减少amount 绿色积分 - err = DealUserCoin(session, md.DealUserCoinReq{ - Kind: "sub", - Mid: masterId, - Title: md.GreenCoinDoubleChainExchangeByCoin1, - TransferType: md.GreenCoinDoubleChainExchangeByCoin1ForForUserVirtualCoinFlow, - OrdId: ordId, - CoinId: greenCoinDoubleChain.Coin1, - Uid: parentUserRelate.ParentUid, - Amount: zhios_order_relate_utils.StrToFloat64(amount), - }) - if err != nil { - session.Rollback() - return - } - //6.2、用户减少amount 贡献积分 - err = DealUserCoin(session, md.DealUserCoinReq{ - Kind: "sub", - Mid: masterId, - Title: md.GreenCoinDoubleChainExchangeByCoin2, - TransferType: md.GreenCoinDoubleChainExchangeByCoin2ForForUserVirtualCoinFlow, - OrdId: ordId, - CoinId: greenCoinDoubleChain.Coin2, - Uid: uid, - Amount: zhios_order_relate_utils.StrToFloat64(amount), - }) - //6.3、另一下级减少amount 贡献积分 - err = DealUserCoin(session, md.DealUserCoinReq{ - Kind: "sub", - Mid: masterId, - Title: md.GreenCoinDoubleChainExchangeByCoin2, - TransferType: md.GreenCoinDoubleChainExchangeByCoin2ForForUserVirtualCoinFlow, - OrdId: ordId, - CoinId: greenCoinDoubleChain.Coin2, - Uid: sonUserVirtualWallet.Uid, - Amount: zhios_order_relate_utils.StrToFloat64(amount), - }) + session := Db.NewSession() + defer func() { + session.Close() + if err := recover(); err != nil { + _ = zhios_order_relate_logx.Error(err) + } + }() + var amount string + var lastUid int + for _, v := range sonUserVirtualWallet { + if lastUid == -1 { + break + } + + userAmount, _ := decimal.NewFromString(v.Amount) + if amount == "" { + amount = userAmount.String() + lastUid = v.Uid + continue + } + + amountValue, _ := decimal.NewFromString(amount) + if userAmount.LessThan(amountValue) { + //当前用户虚拟币金额小于上一个用户虚拟币金额 + amountValue = userAmount + } + + if parentAmountValue.LessThan(amountValue) { + //TODO::上级绿色积分小于当前需要兑换积分金额 + amountValue = parentAmountValue + } + + session.Begin() + //5.1、上级减少amount 绿色积分 + err = DealUserCoin(session, md.DealUserCoinReq{ + Kind: "sub", + Mid: masterId, + Title: md.GreenCoinDoubleChainExchangeByCoin1, + TransferType: md.GreenCoinDoubleChainExchangeByCoin1ForForUserVirtualCoinFlow, + OrdId: ordId, + CoinId: greenCoinDoubleChain.Coin1, + Uid: parentUserRelate.ParentUid, + Amount: zhios_order_relate_utils.StrToFloat64(amountValue.String()), + }) + if err != nil { + session.Rollback() + return + } + //5.2、用户减少amount 贡献积分 + err = DealUserCoin(session, md.DealUserCoinReq{ + Kind: "sub", + Mid: masterId, + Title: md.GreenCoinDoubleChainExchangeByCoin2, + TransferType: md.GreenCoinDoubleChainExchangeByCoin2ForForUserVirtualCoinFlow, + OrdId: ordId, + CoinId: greenCoinDoubleChain.Coin2, + Uid: v.Uid, + Amount: zhios_order_relate_utils.StrToFloat64(amountValue.String()), + }) + //5.3、另一下级减少amount 贡献积分 + err = DealUserCoin(session, md.DealUserCoinReq{ + Kind: "sub", + Mid: masterId, + Title: md.GreenCoinDoubleChainExchangeByCoin2, + TransferType: md.GreenCoinDoubleChainExchangeByCoin2ForForUserVirtualCoinFlow, + OrdId: ordId, + CoinId: greenCoinDoubleChain.Coin2, + Uid: lastUid, + Amount: zhios_order_relate_utils.StrToFloat64(amountValue.String()), + }) + if err != nil { + session.Rollback() + return + } + //5.4上级余额增加 + var coin model.VirtualCoin + _, err = session.Where("id = ?", greenCoinDoubleChain.Coin1).Get(&coin) + if err != nil { + session.Rollback() + return + } + coinExchangeRatioValue, _ := decimal.NewFromString(coin.ExchangeRatio) + exchangeAmount, _ := amountValue.Div(coinExchangeRatioValue).Float64() + orderType := enum.FinUserFlowOrderActionString(md.GreenCoinDoubleChainExchangeForBalanceForFinUserFlow) + err = svc.DealUserAmount(session, md.DealUserAmount{ + Kind: "add", + Mid: masterId, + Title: md.GreenCoinDoubleChainExchangeForBalanceTitleForFinUserFlow, + OrderType: orderType, + OrdAction: md.GreenCoinDoubleChainExchangeForBalanceForFinUserFlow, + OrdId: ordId, + Uid: parentUserRelate.ParentUid, + Amount: exchangeAmount, + Num: "", + }) + if err != nil { + session.Rollback() + return + } + + //6、新增 green_coin_double_chain_exchange_records 记录 + afterUserCoinAmount, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin1, parentUserRelate.ParentUid) + if err != nil { + session.Rollback() + return + } + afterCoinAmountContributeUser1, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, v.Uid) + if err != nil { + session.Rollback() + return + } + afterCoinAmountContributeUser2, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, lastUid) + if err != nil { + session.Rollback() + return + } + now := time.Now() + _, err = db.GreenCoinDoubleChainExchangeRecordsInsert(Db, &model.GreenCoinDoubleChainExchangeRecords{ + Uid: parentUserRelate.ParentUid, + ContributeUid1: v.Uid, + ContributeUid2: lastUid, + CoinId1: greenCoinDoubleChain.Coin1, + CoinId2: greenCoinDoubleChain.Coin2, + Amount: amountValue.String(), + BeforeUserCoinAmount: parentAmount, + AfterUserCoinAmount: afterUserCoinAmount, + BeforeCoinAmountContributeUser1: v.Amount, + AfterCoinAmountContributeUser1: afterCoinAmountContributeUser1, + BeforeCoinAmountContributeUser2: amountValue.String(), + AfterCoinAmountContributeUser2: afterCoinAmountContributeUser2, + CreateAt: now.Format("2006-01-02 15:04:05"), + UpdateAt: now.Format("2006-01-02 15:04:05"), + }) + if err != nil { + session.Rollback() + return + } + session.Commit() + + if zhios_order_relate_utils.StrToFloat64(afterUserCoinAmount) == 0 { + lastUid = -1 + } else { + if amountValue.String() == amount { + amount = afterCoinAmountContributeUser2 + } else { + amount = afterCoinAmountContributeUser1 + lastUid = v.Uid + } + } + } + return +} + +func HandleUserGreenCoinDoubleChainIntegralBySon(Db *xorm.Engine, uid int, ordId, masterId string, greenCoinDoubleChain *model.GreenCoinDoubleChain) (err error) { + //1、查询对应上级的虚拟币余额 + parentAmount, err := svc.GetUserCoinAmount(Db.NewSession(), masterId, greenCoinDoubleChain.Coin1, uid) if err != nil { - session.Rollback() return } - //6.4上级余额增加 amount - var coin model.VirtualCoin - _, err = session.Where("id = ?", greenCoinDoubleChain.Coin1).Get(&coin) - if err != nil { - session.Rollback() - return - } - coinExchangeRatioValue, _ := decimal.NewFromString(coin.ExchangeRatio) - exchangeAmount, _ := amountValue.Div(coinExchangeRatioValue).Float64() - orderType := enum.FinUserFlowOrderActionString(md.GreenCoinDoubleChainExchangeForBalanceForFinUserFlow) - err = svc.DealUserAmount(session, md.DealUserAmount{ - Kind: "add", - Mid: masterId, - Title: md.GreenCoinDoubleChainExchangeForBalanceTitleForFinUserFlow, - OrderType: orderType, - OrdAction: md.GreenCoinDoubleChainExchangeForBalanceForFinUserFlow, - OrdId: ordId, - Uid: parentUserRelate.ParentUid, - Amount: exchangeAmount, - Num: "", - }) - if err != nil { - session.Rollback() + parentAmountValue, _ := decimal.NewFromString(parentAmount) + if parentAmountValue.LessThan(decimal.NewFromFloat(0)) { + //TODO::上级绿色积分小于0不需要进行兑换 return } - //7、新增 green_coin_double_chain_exchange_records 记录 - afterUserCoinAmount, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin1, parentUserRelate.ParentUid) + //2、查询上级直推的所有下级用户 + sonUserRelates, err := db.DbsUserRelateByParentUid(Db, uid, 1) if err != nil { - session.Rollback() return } - afterCoinAmountContributeUser1, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, uid) - if err != nil { - session.Rollback() + if sonUserRelates == nil { + err = errors.New("未查询到直推下级") return } - afterCoinAmountContributeUser2, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, sonUserVirtualWallet.Uid) + var sonUserIds []int + for _, v := range *sonUserRelates { + if v.Uid != uid { + sonUserIds = append(sonUserIds, v.Uid) + } + } + + users, err := db.DbsUserFindByIds(Db, sonUserIds) if err != nil { - session.Rollback() - return - } - now := time.Now() - _, err = db.GreenCoinDoubleChainExchangeRecordsInsert(Db, &model.GreenCoinDoubleChainExchangeRecords{ - Uid: parentUserRelate.ParentUid, - ContributeUid1: uid, - ContributeUid2: sonUserVirtualWallet.Uid, - CoinId1: greenCoinDoubleChain.Coin1, - CoinId2: greenCoinDoubleChain.Coin2, - Amount: amount, - BeforeUserCoinAmount: parentAmount, - AfterUserCoinAmount: afterUserCoinAmount, - BeforeCoinAmountContributeUser1: userAmount, - AfterCoinAmountContributeUser1: afterCoinAmountContributeUser1, - BeforeCoinAmountContributeUser2: sonUserVirtualWallet.Amount, - AfterCoinAmountContributeUser2: afterCoinAmountContributeUser2, - CreateAt: now.Format("2006-01-02 15:04:05"), - UpdateAt: now.Format("2006-01-02 15:04:05"), - }) + return + } + var userIds []int + for _, v := range *users { + if v.Level >= 2 { + //TODO::暂时写死(至少是银卡会员等级才能兑换) + userIds = append(userIds, v.Uid) + } + } + + //3、查询上级直推的所有下级用户的虚拟币金额(按升序) + var sonUserVirtualWallet []model.UserVirtualAmount + Db.Where(" coin_id = ?", greenCoinDoubleChain.Coin2).In("uid", userIds).Asc("amount").Find(&sonUserVirtualWallet) if err != nil { - session.Rollback() return } - session.Commit() - isExchange = true + /* + TODO::4、进行“数据变更” + */ + session := Db.NewSession() + defer func() { + session.Close() + if err := recover(); err != nil { + _ = zhios_order_relate_logx.Error(err) + } + }() + var amount string + var lastUid int + for _, v := range sonUserVirtualWallet { + if lastUid == -1 { + break + } + + userAmount, _ := decimal.NewFromString(v.Amount) + if amount == "" { + amount = userAmount.String() + lastUid = v.Uid + continue + } + + amountValue, _ := decimal.NewFromString(amount) + if userAmount.LessThan(amountValue) { + //当前用户虚拟币金额小于上一个用户虚拟币金额 + amountValue = userAmount + } + + if parentAmountValue.LessThan(amountValue) { + //TODO::上级绿色积分小于当前需要兑换积分金额 + amountValue = parentAmountValue + } + + session.Begin() + //4.1、上级减少amount 绿色积分 + err = DealUserCoin(session, md.DealUserCoinReq{ + Kind: "sub", + Mid: masterId, + Title: md.GreenCoinDoubleChainExchangeByCoin1, + TransferType: md.GreenCoinDoubleChainExchangeByCoin1ForForUserVirtualCoinFlow, + OrdId: ordId, + CoinId: greenCoinDoubleChain.Coin1, + Uid: uid, + Amount: zhios_order_relate_utils.StrToFloat64(amountValue.String()), + }) + if err != nil { + session.Rollback() + return + } + //4.2、用户减少amount 贡献积分 + err = DealUserCoin(session, md.DealUserCoinReq{ + Kind: "sub", + Mid: masterId, + Title: md.GreenCoinDoubleChainExchangeByCoin2, + TransferType: md.GreenCoinDoubleChainExchangeByCoin2ForForUserVirtualCoinFlow, + OrdId: ordId, + CoinId: greenCoinDoubleChain.Coin2, + Uid: v.Uid, + Amount: zhios_order_relate_utils.StrToFloat64(amountValue.String()), + }) + //4.3、另一下级减少amount 贡献积分 + err = DealUserCoin(session, md.DealUserCoinReq{ + Kind: "sub", + Mid: masterId, + Title: md.GreenCoinDoubleChainExchangeByCoin2, + TransferType: md.GreenCoinDoubleChainExchangeByCoin2ForForUserVirtualCoinFlow, + OrdId: ordId, + CoinId: greenCoinDoubleChain.Coin2, + Uid: lastUid, + Amount: zhios_order_relate_utils.StrToFloat64(amountValue.String()), + }) + if err != nil { + session.Rollback() + return + } + //4.4上级余额增加 + var coin model.VirtualCoin + _, err = session.Where("id = ?", greenCoinDoubleChain.Coin1).Get(&coin) + if err != nil { + session.Rollback() + return + } + coinExchangeRatioValue, _ := decimal.NewFromString(coin.ExchangeRatio) + exchangeAmount, _ := amountValue.Div(coinExchangeRatioValue).Float64() + orderType := enum.FinUserFlowOrderActionString(md.GreenCoinDoubleChainExchangeForBalanceForFinUserFlow) + err = svc.DealUserAmount(session, md.DealUserAmount{ + Kind: "add", + Mid: masterId, + Title: md.GreenCoinDoubleChainExchangeForBalanceTitleForFinUserFlow, + OrderType: orderType, + OrdAction: md.GreenCoinDoubleChainExchangeForBalanceForFinUserFlow, + OrdId: ordId, + Uid: uid, + Amount: exchangeAmount, + Num: "", + }) + if err != nil { + session.Rollback() + return + } + + //5、新增 green_coin_double_chain_exchange_records 记录 + afterUserCoinAmount, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin1, uid) + if err != nil { + session.Rollback() + return + } + afterCoinAmountContributeUser1, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, v.Uid) + if err != nil { + session.Rollback() + return + } + afterCoinAmountContributeUser2, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, lastUid) + if err != nil { + session.Rollback() + return + } + now := time.Now() + _, err = db.GreenCoinDoubleChainExchangeRecordsInsert(Db, &model.GreenCoinDoubleChainExchangeRecords{ + Uid: uid, + ContributeUid1: v.Uid, + ContributeUid2: lastUid, + CoinId1: greenCoinDoubleChain.Coin1, + CoinId2: greenCoinDoubleChain.Coin2, + Amount: amountValue.String(), + BeforeUserCoinAmount: parentAmount, + AfterUserCoinAmount: afterUserCoinAmount, + BeforeCoinAmountContributeUser1: v.Amount, + AfterCoinAmountContributeUser1: afterCoinAmountContributeUser1, + BeforeCoinAmountContributeUser2: amountValue.String(), + AfterCoinAmountContributeUser2: afterCoinAmountContributeUser2, + CreateAt: now.Format("2006-01-02 15:04:05"), + UpdateAt: now.Format("2006-01-02 15:04:05"), + }) + if err != nil { + session.Rollback() + return + } + session.Commit() + + if zhios_order_relate_utils.StrToFloat64(afterUserCoinAmount) == 0 { + lastUid = -1 + } else { + if amountValue.String() == amount { + amount = afterCoinAmountContributeUser2 + } else { + amount = afterCoinAmountContributeUser1 + lastUid = v.Uid + } + } + } return } From 3557d568389b634ef2eadb741df1a45bcdb448e6 Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Fri, 8 Mar 2024 19:16:32 +0800 Subject: [PATCH 03/16] =?UTF-8?q?update=20=E7=BB=BF=E8=89=B2=E5=8F=8C?= =?UTF-8?q?=E9=93=BE=E7=A7=AF=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rule/green_coin_double_chain_settlement.go | 120 +++++++++++---------- 1 file changed, 64 insertions(+), 56 deletions(-) diff --git a/rule/green_coin_double_chain_settlement.go b/rule/green_coin_double_chain_settlement.go index 064f370..56a9dfc 100644 --- a/rule/green_coin_double_chain_settlement.go +++ b/rule/green_coin_double_chain_settlement.go @@ -32,6 +32,9 @@ func DealUserGreenCoinDoubleChainIntegral(Db *xorm.Engine, uid int, ordId, maste if err != nil { return } + if greenCoinDoubleChain == nil { + return + } //2、判断当前会员等级 user, err := db.DbsUserFindByUid(Db, uid) @@ -75,7 +78,7 @@ func HandleUserGreenCoinDoubleChainIntegralByParent(Db *xorm.Engine, uid int, or return } parentAmountValue, _ := decimal.NewFromString(parentAmount) - if parentAmountValue.LessThan(decimal.NewFromFloat(0)) { + if parentAmountValue.LessThanOrEqual(decimal.NewFromFloat(0)) { //TODO::上级绿色积分小于0不需要进行兑换 return } @@ -86,14 +89,12 @@ func HandleUserGreenCoinDoubleChainIntegralByParent(Db *xorm.Engine, uid int, or return } if sonUserRelates == nil { - err = errors.New("未查询到直推下级") + //未查询到直推下级 return } var sonUserIds []int for _, v := range *sonUserRelates { - if v.Uid != uid { - sonUserIds = append(sonUserIds, v.Uid) - } + sonUserIds = append(sonUserIds, v.Uid) } users, err := db.DbsUserFindByIds(Db, sonUserIds) @@ -110,7 +111,7 @@ func HandleUserGreenCoinDoubleChainIntegralByParent(Db *xorm.Engine, uid int, or //4、查询上级直推的所有下级用户的虚拟币金额(按升序) var sonUserVirtualWallet []model.UserVirtualAmount - Db.Where(" coin_id = ?", greenCoinDoubleChain.Coin2).In("uid", userIds).Asc("amount").Find(&sonUserVirtualWallet) + Db.Where(" coin_id = ? and amount > 0", greenCoinDoubleChain.Coin2).In("uid", userIds).Asc("amount").Find(&sonUserVirtualWallet) if err != nil { return } @@ -118,15 +119,16 @@ func HandleUserGreenCoinDoubleChainIntegralByParent(Db *xorm.Engine, uid int, or /* TODO::5、进行“数据变更” */ + var amount string + var lastUid int session := Db.NewSession() + session.Begin() defer func() { session.Close() if err := recover(); err != nil { _ = zhios_order_relate_logx.Error(err) } }() - var amount string - var lastUid int for _, v := range sonUserVirtualWallet { if lastUid == -1 { break @@ -140,17 +142,16 @@ func HandleUserGreenCoinDoubleChainIntegralByParent(Db *xorm.Engine, uid int, or } amountValue, _ := decimal.NewFromString(amount) - if userAmount.LessThan(amountValue) { - //当前用户虚拟币金额小于上一个用户虚拟币金额 - amountValue = userAmount - } + //if userAmount.LessThan(amountValue) { + // //当前用户虚拟币金额小于上一个用户虚拟币金额 + // amountValue = userAmount + //} if parentAmountValue.LessThan(amountValue) { //TODO::上级绿色积分小于当前需要兑换积分金额 amountValue = parentAmountValue } - session.Begin() //5.1、上级减少amount 绿色积分 err = DealUserCoin(session, md.DealUserCoinReq{ Kind: "sub", @@ -164,8 +165,9 @@ func HandleUserGreenCoinDoubleChainIntegralByParent(Db *xorm.Engine, uid int, or }) if err != nil { session.Rollback() - return + break } + //5.2、用户减少amount 贡献积分 err = DealUserCoin(session, md.DealUserCoinReq{ Kind: "sub", @@ -177,6 +179,10 @@ func HandleUserGreenCoinDoubleChainIntegralByParent(Db *xorm.Engine, uid int, or Uid: v.Uid, Amount: zhios_order_relate_utils.StrToFloat64(amountValue.String()), }) + if err != nil { + session.Rollback() + break + } //5.3、另一下级减少amount 贡献积分 err = DealUserCoin(session, md.DealUserCoinReq{ Kind: "sub", @@ -190,14 +196,14 @@ func HandleUserGreenCoinDoubleChainIntegralByParent(Db *xorm.Engine, uid int, or }) if err != nil { session.Rollback() - return + break } //5.4上级余额增加 var coin model.VirtualCoin _, err = session.Where("id = ?", greenCoinDoubleChain.Coin1).Get(&coin) if err != nil { session.Rollback() - return + break } coinExchangeRatioValue, _ := decimal.NewFromString(coin.ExchangeRatio) exchangeAmount, _ := amountValue.Div(coinExchangeRatioValue).Float64() @@ -215,25 +221,26 @@ func HandleUserGreenCoinDoubleChainIntegralByParent(Db *xorm.Engine, uid int, or }) if err != nil { session.Rollback() - return + break } //6、新增 green_coin_double_chain_exchange_records 记录 afterUserCoinAmount, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin1, parentUserRelate.ParentUid) if err != nil { session.Rollback() - return + break } afterCoinAmountContributeUser1, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, v.Uid) if err != nil { session.Rollback() - return + break } afterCoinAmountContributeUser2, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, lastUid) if err != nil { session.Rollback() - return + break } + now := time.Now() _, err = db.GreenCoinDoubleChainExchangeRecordsInsert(Db, &model.GreenCoinDoubleChainExchangeRecords{ Uid: parentUserRelate.ParentUid, @@ -253,21 +260,20 @@ func HandleUserGreenCoinDoubleChainIntegralByParent(Db *xorm.Engine, uid int, or }) if err != nil { session.Rollback() - return + break } - session.Commit() if zhios_order_relate_utils.StrToFloat64(afterUserCoinAmount) == 0 { lastUid = -1 } else { - if amountValue.String() == amount { - amount = afterCoinAmountContributeUser2 - } else { - amount = afterCoinAmountContributeUser1 - lastUid = v.Uid - } + amount = afterCoinAmountContributeUser1 + lastUid = v.Uid + } + if zhios_order_relate_utils.StrToFloat64(amount) == 0 { + break } } + session.Commit() return } @@ -278,7 +284,7 @@ func HandleUserGreenCoinDoubleChainIntegralBySon(Db *xorm.Engine, uid int, ordId return } parentAmountValue, _ := decimal.NewFromString(parentAmount) - if parentAmountValue.LessThan(decimal.NewFromFloat(0)) { + if parentAmountValue.LessThanOrEqual(decimal.NewFromFloat(0)) { //TODO::上级绿色积分小于0不需要进行兑换 return } @@ -289,14 +295,12 @@ func HandleUserGreenCoinDoubleChainIntegralBySon(Db *xorm.Engine, uid int, ordId return } if sonUserRelates == nil { - err = errors.New("未查询到直推下级") + //未查询到直推下级 return } var sonUserIds []int for _, v := range *sonUserRelates { - if v.Uid != uid { - sonUserIds = append(sonUserIds, v.Uid) - } + sonUserIds = append(sonUserIds, v.Uid) } users, err := db.DbsUserFindByIds(Db, sonUserIds) @@ -313,7 +317,7 @@ func HandleUserGreenCoinDoubleChainIntegralBySon(Db *xorm.Engine, uid int, ordId //3、查询上级直推的所有下级用户的虚拟币金额(按升序) var sonUserVirtualWallet []model.UserVirtualAmount - Db.Where(" coin_id = ?", greenCoinDoubleChain.Coin2).In("uid", userIds).Asc("amount").Find(&sonUserVirtualWallet) + Db.Where(" coin_id = ? and amount > 0 ", greenCoinDoubleChain.Coin2).In("uid", userIds).Asc("amount").Find(&sonUserVirtualWallet) if err != nil { return } @@ -321,16 +325,18 @@ func HandleUserGreenCoinDoubleChainIntegralBySon(Db *xorm.Engine, uid int, ordId /* TODO::4、进行“数据变更” */ + var amount string + var lastUid int session := Db.NewSession() + session.Begin() defer func() { session.Close() if err := recover(); err != nil { _ = zhios_order_relate_logx.Error(err) } }() - var amount string - var lastUid int for _, v := range sonUserVirtualWallet { + if lastUid == -1 { break } @@ -343,17 +349,16 @@ func HandleUserGreenCoinDoubleChainIntegralBySon(Db *xorm.Engine, uid int, ordId } amountValue, _ := decimal.NewFromString(amount) - if userAmount.LessThan(amountValue) { - //当前用户虚拟币金额小于上一个用户虚拟币金额 - amountValue = userAmount - } + //if userAmount.LessThan(amountValue) { + // //当前用户虚拟币金额小于上一个用户虚拟币金额 + // amountValue = userAmount + //} if parentAmountValue.LessThan(amountValue) { //TODO::上级绿色积分小于当前需要兑换积分金额 amountValue = parentAmountValue } - session.Begin() //4.1、上级减少amount 绿色积分 err = DealUserCoin(session, md.DealUserCoinReq{ Kind: "sub", @@ -367,7 +372,7 @@ func HandleUserGreenCoinDoubleChainIntegralBySon(Db *xorm.Engine, uid int, ordId }) if err != nil { session.Rollback() - return + break } //4.2、用户减少amount 贡献积分 err = DealUserCoin(session, md.DealUserCoinReq{ @@ -380,6 +385,10 @@ func HandleUserGreenCoinDoubleChainIntegralBySon(Db *xorm.Engine, uid int, ordId Uid: v.Uid, Amount: zhios_order_relate_utils.StrToFloat64(amountValue.String()), }) + if err != nil { + session.Rollback() + break + } //4.3、另一下级减少amount 贡献积分 err = DealUserCoin(session, md.DealUserCoinReq{ Kind: "sub", @@ -393,14 +402,14 @@ func HandleUserGreenCoinDoubleChainIntegralBySon(Db *xorm.Engine, uid int, ordId }) if err != nil { session.Rollback() - return + break } //4.4上级余额增加 var coin model.VirtualCoin _, err = session.Where("id = ?", greenCoinDoubleChain.Coin1).Get(&coin) if err != nil { session.Rollback() - return + break } coinExchangeRatioValue, _ := decimal.NewFromString(coin.ExchangeRatio) exchangeAmount, _ := amountValue.Div(coinExchangeRatioValue).Float64() @@ -418,24 +427,24 @@ func HandleUserGreenCoinDoubleChainIntegralBySon(Db *xorm.Engine, uid int, ordId }) if err != nil { session.Rollback() - return + break } //5、新增 green_coin_double_chain_exchange_records 记录 afterUserCoinAmount, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin1, uid) if err != nil { session.Rollback() - return + break } afterCoinAmountContributeUser1, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, v.Uid) if err != nil { session.Rollback() - return + break } afterCoinAmountContributeUser2, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, lastUid) if err != nil { session.Rollback() - return + break } now := time.Now() _, err = db.GreenCoinDoubleChainExchangeRecordsInsert(Db, &model.GreenCoinDoubleChainExchangeRecords{ @@ -456,20 +465,19 @@ func HandleUserGreenCoinDoubleChainIntegralBySon(Db *xorm.Engine, uid int, ordId }) if err != nil { session.Rollback() - return + break } - session.Commit() if zhios_order_relate_utils.StrToFloat64(afterUserCoinAmount) == 0 { lastUid = -1 } else { - if amountValue.String() == amount { - amount = afterCoinAmountContributeUser2 - } else { - amount = afterCoinAmountContributeUser1 - lastUid = v.Uid - } + amount = afterCoinAmountContributeUser1 + lastUid = v.Uid + } + if zhios_order_relate_utils.StrToFloat64(amount) == 0 { + break } } + session.Commit() return } From 6a1fd91277e7375c61d321574ac74f475eb64886 Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Sun, 10 Mar 2024 16:17:00 +0800 Subject: [PATCH 04/16] =?UTF-8?q?update=20=E7=BB=BF=E8=89=B2=E5=8F=8C?= =?UTF-8?q?=E9=93=BE=E7=A7=AF=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rule/green_coin_double_chain_settlement.go | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/rule/green_coin_double_chain_settlement.go b/rule/green_coin_double_chain_settlement.go index 56a9dfc..cf09b70 100644 --- a/rule/green_coin_double_chain_settlement.go +++ b/rule/green_coin_double_chain_settlement.go @@ -62,6 +62,7 @@ func DealUserGreenCoinDoubleChainIntegral(Db *xorm.Engine, uid int, ordId, maste } func HandleUserGreenCoinDoubleChainIntegralByParent(Db *xorm.Engine, uid int, ordId, masterId string, greenCoinDoubleChain *model.GreenCoinDoubleChain) (err error) { + time.Sleep(time.Millisecond * 200) //TODO::等待100毫秒 //1、查找对应直推上级用户 parentUserRelate, err := db.GetUserParentUserRelate(Db, uid) if err != nil { @@ -130,9 +131,6 @@ func HandleUserGreenCoinDoubleChainIntegralByParent(Db *xorm.Engine, uid int, or } }() for _, v := range sonUserVirtualWallet { - if lastUid == -1 { - break - } userAmount, _ := decimal.NewFromString(v.Amount) if amount == "" { @@ -264,13 +262,13 @@ func HandleUserGreenCoinDoubleChainIntegralByParent(Db *xorm.Engine, uid int, or } if zhios_order_relate_utils.StrToFloat64(afterUserCoinAmount) == 0 { - lastUid = -1 + break } else { amount = afterCoinAmountContributeUser1 lastUid = v.Uid } if zhios_order_relate_utils.StrToFloat64(amount) == 0 { - break + amount = "" } } session.Commit() @@ -278,6 +276,7 @@ func HandleUserGreenCoinDoubleChainIntegralByParent(Db *xorm.Engine, uid int, or } func HandleUserGreenCoinDoubleChainIntegralBySon(Db *xorm.Engine, uid int, ordId, masterId string, greenCoinDoubleChain *model.GreenCoinDoubleChain) (err error) { + time.Sleep(time.Millisecond * 200) //TODO::等待100毫秒 //1、查询对应上级的虚拟币余额 parentAmount, err := svc.GetUserCoinAmount(Db.NewSession(), masterId, greenCoinDoubleChain.Coin1, uid) if err != nil { @@ -337,10 +336,6 @@ func HandleUserGreenCoinDoubleChainIntegralBySon(Db *xorm.Engine, uid int, ordId }() for _, v := range sonUserVirtualWallet { - if lastUid == -1 { - break - } - userAmount, _ := decimal.NewFromString(v.Amount) if amount == "" { amount = userAmount.String() @@ -469,13 +464,13 @@ func HandleUserGreenCoinDoubleChainIntegralBySon(Db *xorm.Engine, uid int, ordId } if zhios_order_relate_utils.StrToFloat64(afterUserCoinAmount) == 0 { - lastUid = -1 + break } else { amount = afterCoinAmountContributeUser1 lastUid = v.Uid } if zhios_order_relate_utils.StrToFloat64(amount) == 0 { - break + amount = "" } } session.Commit() From 890662df724aeeda1c658e30f4d7798c61c0b4cf Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Mon, 11 Mar 2024 14:48:04 +0800 Subject: [PATCH 05/16] =?UTF-8?q?update=20=E4=BD=99=E9=A2=9D=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- svc/svc_deal_user_amount.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/svc/svc_deal_user_amount.go b/svc/svc_deal_user_amount.go index 826c385..aaafb7e 100644 --- a/svc/svc_deal_user_amount.go +++ b/svc/svc_deal_user_amount.go @@ -127,7 +127,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*2) + _, err := cache.SetEx(redisKey, zhios_order_relate_utils.StrToFloat64(amount), 60*1) if err != nil { return err } From 5dfc32ec6901461a641fe872e31ba954a791d4e8 Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Tue, 12 Mar 2024 19:06:08 +0800 Subject: [PATCH 06/16] =?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/block_star_chain.go | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/md/block_star_chain.go b/md/block_star_chain.go index b0fe91b..a5ea277 100644 --- a/md/block_star_chain.go +++ b/md/block_star_chain.go @@ -60,16 +60,16 @@ const ( NiuBeiCoinByReleaseOptionDestroyConsumeCoinNumTitleForUserVirtualCoinFlow = "释放期权积分数量-消耗消费积分数量" OneCirclesGreenEnergySignInSettlementPersonalReward = "签到奖励" - OneCirclesGreenEnergySignInSettlementTeamReward = "团队加速奖励" + OneCirclesGreenEnergySignInSettlementTeamReward = "粉丝助力奖励" OneCirclesWatchAdRewardPersonalActiveCoin = "浏览视频奖励" - OneCirclesWatchAdRewardTeamActiveCoin = "九维公排奖励" + OneCirclesWatchAdRewardTeamActiveCoin = "九维奖励" OneCirclesBalanceExchangeForGreenEnergy = "账户余额兑换" OneCirclesGreenEnergyExchangeForBalance = "兑换账户余额" OneCirclesPersonalActiveCoinExchangeGreenEnergy = "B活跃积分兑换" OneCirclesTeamActiveCoinExchangeGreenEnergy = "T活跃积分兑换" OneCirclesSettlementGreenEnergyExchangeGreenEnergy = "绿色能量释放" - OneCirclesWelfareOrdersExchangeGreenEnergy = "福利订单兑换绿色能量" - OneCirclesConsumeOrdersExchangeGreenEnergy = "消费订单兑换绿色能量" + OneCirclesWelfareOrdersExchangeGreenEnergy = "福利订单兑换" + OneCirclesConsumeOrdersExchangeGreenEnergy = "消费订单兑换" OneCirclesPersonalActiveCoinExchangeToBeGreenEnergy = "B兑换绿色能量" OneCirclesTeamActiveCoinExchangeToBeGreenEnergy = "T兑换绿色能量" OneCirclesSettlementGreenEnergyExchangeTobeGreenEnergy = "绿色能量释放" @@ -135,23 +135,23 @@ const ( NiuBeiCoinByTeamForUserVirtualCoinFlow = 152 //团队奖励 OneCirclesGreenEnergySignInSettlementPersonalRewardForUserVirtualCoinFlow = 155 //签到奖励 - OneCirclesGreenEnergySignInSettlementTeamRewardForUserVirtualCoinFlow = 156 //团队加速奖励 - OneCirclesWatchAdRewardPersonalActiveCoinForUserVirtualCoinFlow = 156 //浏览视频奖励 - OneCirclesWatchAdRewardTeamActiveCoinForUserVirtualCoinFlow = 157 //九维公排奖励 - OneCirclesBalanceExchangeForGreenEnergyForUserVirtualCoinFlow = 158 //账户余额兑换 - OneCirclesGreenEnergyExchangeForBalanceForUserVirtualCoinFlow = 159 //兑换账户余额 - OneCirclesPersonalActiveCoinExchangeGreenEnergyForUserVirtualCoinFlow = 160 //B活跃积分兑换 - OneCirclesTeamActiveCoinExchangeGreenEnergyForUserVirtualCoinFlow = 161 //T活跃积分兑换 - OneCirclesSettlementGreenEnergyExchangeGreenEnergyForUserVirtualCoinFlow = 162 //绿色能量释放 - OneCirclesWelfareOrdersExchangeGreenEnergyForUserVirtualCoinFlow = 163 //福利订单兑换绿色能量 - OneCirclesConsumeOrdersExchangeGreenEnergyForUserVirtualCoinFlow = 164 //消费订单兑换绿色能量 - OneCirclesPersonalActiveCoinExchangeToBeGreenEnergyForUserVirtualCoinFlow = 165 //B兑换绿色能量 - OneCirclesTeamActiveCoinExchangeToBeGreenEnergyForUserVirtualCoinFlow = 166 //T兑换绿色能量 - OneCirclesSettlementGreenEnergyExchangeTobeGreenEnergyForUserVirtualCoinFlow = 167 //绿色能量释放 - OneCirclesSettlementStarLevelDividendsForUserVirtualCoinFlow = 168 //结算星级分红-得到结算绿色能量 - - GreenCoinDoubleChainExchangeByCoin1ForForUserVirtualCoinFlow = 169 //绿色积分双链兑换账户余额-减少绿色积分 - GreenCoinDoubleChainExchangeByCoin2ForForUserVirtualCoinFlow = 170 //绿色积分双链兑换账户余额-减少贡献积分 + OneCirclesGreenEnergySignInSettlementTeamRewardForUserVirtualCoinFlow = 156 //粉丝助力奖励 + OneCirclesWatchAdRewardPersonalActiveCoinForUserVirtualCoinFlow = 157 //浏览视频奖励 + OneCirclesWatchAdRewardTeamActiveCoinForUserVirtualCoinFlow = 158 //九维奖励 + OneCirclesBalanceExchangeForGreenEnergyForUserVirtualCoinFlow = 159 //账户余额兑换 + OneCirclesGreenEnergyExchangeForBalanceForUserVirtualCoinFlow = 160 //兑换账户余额 + OneCirclesPersonalActiveCoinExchangeGreenEnergyForUserVirtualCoinFlow = 161 //B活跃积分兑换 + OneCirclesTeamActiveCoinExchangeGreenEnergyForUserVirtualCoinFlow = 162 //T活跃积分兑换 + OneCirclesSettlementGreenEnergyExchangeGreenEnergyForUserVirtualCoinFlow = 163 //绿色能量释放 + OneCirclesWelfareOrdersExchangeGreenEnergyForUserVirtualCoinFlow = 164 //福利订单兑换 + OneCirclesConsumeOrdersExchangeGreenEnergyForUserVirtualCoinFlow = 165 //消费订单兑换 + OneCirclesPersonalActiveCoinExchangeToBeGreenEnergyForUserVirtualCoinFlow = 166 //B兑换绿色能量 + OneCirclesTeamActiveCoinExchangeToBeGreenEnergyForUserVirtualCoinFlow = 167 //T兑换绿色能量 + OneCirclesSettlementGreenEnergyExchangeTobeGreenEnergyForUserVirtualCoinFlow = 168 //绿色能量释放 + OneCirclesSettlementStarLevelDividendsForUserVirtualCoinFlow = 169 //绿色能量分红(结算星级分红-得到结算绿色能量) + + GreenCoinDoubleChainExchangeByCoin1ForForUserVirtualCoinFlow = 170 //绿色积分双链兑换账户余额-减少绿色积分 + GreenCoinDoubleChainExchangeByCoin2ForForUserVirtualCoinFlow = 171 //绿色积分双链兑换账户余额-减少贡献积分 ) From 01e978c4bff8b91abfd5d77b1acafa209d054672 Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Wed, 13 Mar 2024 11:58:56 +0800 Subject: [PATCH 07/16] =?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 | 9 +- ...ctivity_coin_auto_exchange_green_energy.go | 221 +++++++++++++++++- 2 files changed, 227 insertions(+), 3 deletions(-) diff --git a/md/mq.go b/md/mq.go index b6f49a0..c923dcc 100644 --- a/md/mq.go +++ b/md/mq.go @@ -3,7 +3,9 @@ package md const OneCirclesExchange = "one.circles" const ( - OneCirclesRoutKeyForSignIn = "sign_in" // 签到 + OneCirclesRoutKeyForSignIn = "sign_in" // 签到 + OneCirclesRoutKeyForAutoExchangeGreenEnergyToPerson = "auto_exchange_green_energy_to_person" // 自动兑换个人绿色能量 + OneCirclesRoutKeyForAutoExchangeGreenEnergyToTeam = "auto_exchange_green_energy_to_team" // 自动兑换团队绿色能量 ) type OneCirclesStructForSignIn struct { @@ -11,3 +13,8 @@ type OneCirclesStructForSignIn struct { Uid int `json:"uid"` Id int64 `json:"id"` } + +type OneCirclesStructForAutoExchangeGreenEnergy struct { + MasterId string `json:"master_id"` + Uid int `json:"uid"` +} 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 f194745..760a339 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 @@ -1,6 +1,7 @@ package one_circles import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "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" @@ -15,8 +16,8 @@ import ( "xorm.io/xorm" ) -// ActivityCoinAutoExchangeGreenEnergy 活跃积分自动兑换成绿色能量 -func ActivityCoinAutoExchangeGreenEnergy(engine *xorm.Engine, masterId string) (err error) { +// ActivityCoinAutoExchangeGreenEnergyNew 活跃积分自动兑换成绿色能量 +func ActivityCoinAutoExchangeGreenEnergyNew(engine *xorm.Engine, masterId string, ch *rabbit.Channel) (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", @@ -82,6 +83,22 @@ func ActivityCoinAutoExchangeGreenEnergy(engine *xorm.Engine, masterId string) ( }() session.Begin() + for _, v := range list1 { + //TODO::推入rabbitmq 异步处理 + ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForAutoExchangeGreenEnergy{ + MasterId: masterId, + Uid: v.Uid, + }, md.OneCirclesRoutKeyForAutoExchangeGreenEnergyToPerson) + } + + for _, v := range list2 { + //TODO::推入rabbitmq 异步处理 + ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForAutoExchangeGreenEnergy{ + MasterId: masterId, + Uid: v.Uid, + }, md.OneCirclesRoutKeyForAutoExchangeGreenEnergyToTeam) + } + //4、处理"个人活跃积分"兑换 for _, v := range list1 { autoExchangeNumsByPersonValue, _ := decimal.NewFromString(v.Amount) @@ -205,3 +222,203 @@ func ActivityCoinAutoExchangeGreenEnergy(engine *xorm.Engine, masterId string) ( } return } + +// ActivityCoinAutoExchangeGreenEnergy 活跃积分自动兑换成绿色能量 +func ActivityCoinAutoExchangeGreenEnergy(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", + "value": 1, + }) + if err != nil { + return + } + + 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个自动兑换 + var autoExchangeNumsByTeam = zhios_order_relate_utils.StrToFloat64(exchangeRulesStruct.AutoExchangeNumsByTeam) //团队活跃积分X个自动兑换 + fmt.Println("autoExchangeNumsByPerson>>>>>>>>>>>>", autoExchangeNumsByPerson) + fmt.Println("autoExchangeNumsByTeam>>>>>>>>>>>>", autoExchangeNumsByTeam) + + //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) + teamActivePointsCoinExchangeRatioValue, _ := decimal.NewFromString(coin2.ExchangeRatio) + + //3、当前 "个人活跃积分"可以自动兑换的用户数据 && "团队活跃积分"可以自动兑换的用户数据 + var list1, list2 []model.UserVirtualAmount + err = engine.Where("coin_id = ?", oneCirclesPublicPlatoonBasicSetting.PersonActivePointsCoinId).And("amount >=?", autoExchangeNumsByPerson).Limit(200, 0).Find(&list1) + if err != nil { + fmt.Println("err:::::1111", err.Error()) + return + } + err = engine.Where("coin_id = ?", oneCirclesPublicPlatoonBasicSetting.TeamActivePointsCoinId).And("amount >=?", autoExchangeNumsByTeam).Limit(200, 0).Find(&list2) + if err != nil { + fmt.Println("err:::::2222", err.Error()) + return + } + + //4、处理"个人活跃积分"兑换 + for _, v := range list1 { + session := engine.NewSession() + session.Begin() + + autoExchangeNumsByPersonValue, _ := decimal.NewFromString(v.Amount) + autoExchangeNumsByPersonAmount := autoExchangeNumsByPersonValue.Div(personActivePointsCoinExchangeRatioValue).String() + //4.1计算涨价公式 + err1, values, _, afterPriceValue := NewCalcPriceIncreaseFormula(autoExchangeNumsByPersonAmount, oneCirclesGreenEnergyBasicSetting) + if err1 != nil { + _ = session.Rollback() + return err1 + } + + //4.2给相应的用户加上个人的绿色积分(可用数量) + err = DealUserCoin(session, md.DealUserCoinReq{ + Kind: "add", + Mid: masterId, + Title: md.OneCirclesPersonalActiveCoinExchangeGreenEnergy, + TransferType: md.OneCirclesPersonalActiveCoinExchangeGreenEnergyForUserVirtualCoinFlow, + OrdId: "", + CoinId: oneCirclesGreenEnergyBasicSetting.PersonGreenEnergyCoinId, + Uid: v.Uid, + Amount: zhios_order_relate_utils.StrToFloat64(values), + }) + if err != nil { + _ = session.Rollback() + fmt.Println("err:::::33333", err.Error()) + return err + } + + //4.3给相应的用户减去个人活跃积分 + err = DealUserCoin(session, md.DealUserCoinReq{ + Kind: "sub", + Mid: masterId, + Title: md.OneCirclesPersonalActiveCoinExchangeToBeGreenEnergy, + TransferType: md.OneCirclesPersonalActiveCoinExchangeToBeGreenEnergyForUserVirtualCoinFlow, + OrdId: "", + CoinId: oneCirclesPublicPlatoonBasicSetting.PersonActivePointsCoinId, + Uid: v.Uid, + Amount: zhios_order_relate_utils.StrToFloat64(v.Amount), + }) + if err != nil { + _ = session.Rollback() + fmt.Println("err:::::33333", err.Error()) + return err + } + + //4.4 减少“原始数量”中的绿色能量 + err = DealAvailableGreenEnergyCoin(session, int(enum.PersonalActivePointRedemption), zhios_order_relate_utils.StrToFloat64(values), zhios_order_relate_utils.StrToFloat64(autoExchangeNumsByPersonAmount), enum.PersonalActivePointRedemption.String(), oneCirclesGreenEnergyBasicSetting, afterPriceValue) + if err != nil { + _ = session.Rollback() + fmt.Println("err:::::44444", err.Error()) + return err + } + session.Commit() + session.Close() + } + + //5、处理"团队活跃积分"兑换 + for _, v := range list2 { + session := engine.NewSession() + session.Begin() + + autoExchangeNumsByTeamValue, _ := decimal.NewFromString(v.Amount) + autoExchangeNumsByTeamAmount := autoExchangeNumsByTeamValue.Div(teamActivePointsCoinExchangeRatioValue).String() + //5.1计算涨价公式 + err1, values, _, afterPriceValue := NewCalcPriceIncreaseFormula(autoExchangeNumsByTeamAmount, oneCirclesGreenEnergyBasicSetting) + if err1 != nil { + _ = session.Rollback() + return err1 + } + + //5.2给相应的用户加上个人的绿色积分(结算数量) + err = DealUserCoin(session, md.DealUserCoinReq{ + Kind: "add", + Mid: masterId, + Title: md.OneCirclesTeamActiveCoinExchangeGreenEnergy, + TransferType: md.OneCirclesTeamActiveCoinExchangeGreenEnergyForUserVirtualCoinFlow, + OrdId: "", + CoinId: oneCirclesGreenEnergyBasicSetting.TeamGreenEnergyCoinId, + Uid: v.Uid, + Amount: zhios_order_relate_utils.StrToFloat64(values), + }) + if err != nil { + _ = session.Rollback() + fmt.Println("err:::::33333", err.Error()) + return err + } + + //5.3给相应的用户减去个人团队积分 + err = DealUserCoin(session, md.DealUserCoinReq{ + Kind: "sub", + Mid: masterId, + Title: md.OneCirclesTeamActiveCoinExchangeToBeGreenEnergy, + TransferType: md.OneCirclesTeamActiveCoinExchangeToBeGreenEnergyForUserVirtualCoinFlow, + OrdId: "", + CoinId: oneCirclesPublicPlatoonBasicSetting.TeamActivePointsCoinId, + Uid: v.Uid, + Amount: zhios_order_relate_utils.StrToFloat64(v.Amount), + }) + if err != nil { + _ = session.Rollback() + fmt.Println("err:::::66666", err.Error()) + return err + } + + //5.4 减少“原始数量”中的绿色能量 + err = DealAvailableGreenEnergyCoin(session, int(enum.TeamActivePointRedemption), zhios_order_relate_utils.StrToFloat64(values), zhios_order_relate_utils.StrToFloat64(autoExchangeNumsByTeamAmount), enum.TeamActivePointRedemption.String(), oneCirclesGreenEnergyBasicSetting, afterPriceValue) + if err != nil { + _ = session.Rollback() + fmt.Println("err:::::44444", err.Error()) + return err + } + err = session.Commit() + session.Close() + } + + session := engine.NewSession() + defer func() { + session.Close() + if err := recover(); err != nil { + _ = zhios_order_relate_logx.Error(err) + } + }() + session.Begin() + + //6、修改 one_circles_green_energy_basic_setting 的 now_price + _, err = db.OneCirclesGreenEnergyBasicSettingUpdate(session, oneCirclesGreenEnergyBasicSetting.Id, oneCirclesGreenEnergyBasicSetting) + if err != nil { + _ = session.Rollback() + fmt.Println("err:::::77777", err.Error()) + return err + } + + if err != nil { + _ = session.Rollback() + return errors.New("事务提交失败") + } + return +} From 5e7ef7507328226eb7b19a2217914c4480df2f85 Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Wed, 13 Mar 2024 13:33:38 +0800 Subject: [PATCH 08/16] =?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 --- .../one_circles_activity_coin_auto_exchange_green_energy.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 760a339..f374ad6 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 @@ -270,12 +270,12 @@ func ActivityCoinAutoExchangeGreenEnergy(engine *xorm.Engine, masterId string) ( //3、当前 "个人活跃积分"可以自动兑换的用户数据 && "团队活跃积分"可以自动兑换的用户数据 var list1, list2 []model.UserVirtualAmount - err = engine.Where("coin_id = ?", oneCirclesPublicPlatoonBasicSetting.PersonActivePointsCoinId).And("amount >=?", autoExchangeNumsByPerson).Limit(200, 0).Find(&list1) + err = engine.Where("coin_id = ?", oneCirclesPublicPlatoonBasicSetting.PersonActivePointsCoinId).And("amount >=?", autoExchangeNumsByPerson).Limit(1000, 0).Find(&list1) if err != nil { fmt.Println("err:::::1111", err.Error()) return } - err = engine.Where("coin_id = ?", oneCirclesPublicPlatoonBasicSetting.TeamActivePointsCoinId).And("amount >=?", autoExchangeNumsByTeam).Limit(200, 0).Find(&list2) + err = engine.Where("coin_id = ?", oneCirclesPublicPlatoonBasicSetting.TeamActivePointsCoinId).And("amount >=?", autoExchangeNumsByTeam).Limit(1000, 0).Find(&list2) if err != nil { fmt.Println("err:::::2222", err.Error()) return From 2021e04aaa19bb18942ee07067d166b271833df7 Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Fri, 15 Mar 2024 19:28:58 +0800 Subject: [PATCH 09/16] =?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 | 1 + .../one_circles_star_level_dividends.go | 187 +++++++++++++++++- 2 files changed, 186 insertions(+), 2 deletions(-) diff --git a/md/mq.go b/md/mq.go index c923dcc..d243062 100644 --- a/md/mq.go +++ b/md/mq.go @@ -4,6 +4,7 @@ const OneCirclesExchange = "one.circles" const ( OneCirclesRoutKeyForSignIn = "sign_in" // 签到 + OneCirclesRoutKeyForStarLevelDividend = "star_level_dividend" // 星级分红 OneCirclesRoutKeyForAutoExchangeGreenEnergyToPerson = "auto_exchange_green_energy_to_person" // 自动兑换个人绿色能量 OneCirclesRoutKeyForAutoExchangeGreenEnergyToTeam = "auto_exchange_green_energy_to_team" // 自动兑换团队绿色能量 ) diff --git a/rule/one_circles/one_circles_star_level_dividends.go b/rule/one_circles/one_circles_star_level_dividends.go index 781f748..604a49e 100644 --- a/rule/one_circles/one_circles_star_level_dividends.go +++ b/rule/one_circles/one_circles_star_level_dividends.go @@ -1,6 +1,7 @@ package one_circles import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "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" @@ -19,8 +20,7 @@ import ( const SettlementStarLevelDividendsLockKey = "settlement_star_level_dividends_lock_key" -// SettlementStarLevelDividends 绿色能量分红 -func SettlementStarLevelDividends(engine *xorm.Engine, masterId string) (err error) { +func SettlementStarLevelDividendsNew(engine *xorm.Engine, masterId string) (err error) { now := time.Now() fmt.Println(now.Hour()) if !(now.Hour() > 2 && now.Hour() < 8) { @@ -184,6 +184,189 @@ func SettlementStarLevelDividends(engine *xorm.Engine, masterId string) (err err oneCirclesStarLevelDividendsRecords.NotDividendsAmount = dividendAmountValue.Sub(totalDividendValue).String() _, err = db.OneCirclesStarLevelDividendsRecordsUpdateBySession(session, oneCirclesStarLevelDividendsRecords.Id, oneCirclesStarLevelDividendsRecords, "already_dividends_amount", "not_dividends_amount") if err != nil { + _ = session.Rollback() + fmt.Println("err:::::8888888", err) + return err + } + + //6、 减少“星级分红”中的绿色能量 + totalDividend, _ := totalDividendValue.Float64() + err = DealAvailableGreenEnergyCoin(session, int(enum.SettlementStarLevelDividends), totalDividend, 0, enum.SettlementStarLevelDividends.String(), oneCirclesGreenEnergyBasicSetting, oneCirclesGreenEnergyBasicSetting.NowPrice) + if err != nil { + _ = session.Rollback() + fmt.Println("err:::::9999999", err) + return err + } + err = session.Commit() + if err != nil { + _ = session.Rollback() + return errors.New("事务提交失败") + } + return +} + +// SettlementStarLevelDividends 绿色能量分红 +func SettlementStarLevelDividends(engine *xorm.Engine, masterId string, ch *rabbit.Channel) (err error) { + now := time.Now() + fmt.Println(now.Hour()) + if !(now.Hour() > 2 && now.Hour() < 8) { + //TODO::只在凌晨一点 ~ 凌晨 8 点运行 + return errors.New("非运行时间") + } + //TODO::增加“悲观锁”防止串行 + getString, _ := cache.GetString(SettlementStarLevelDividendsLockKey) + if getString != "" { + fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完") + return errors.New("上一次结算未执行完") + } + cache.SetEx(SettlementStarLevelDividendsLockKey, "running", 3600*8) //8小时 + + //1、查找 `one_circles_green_energy_basic_setting` 基础设置 + oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{ + "key": "is_open", + "value": 1, + }) + if err != nil { + return + } + + isLimitDividend := oneCirclesGreenEnergyBasicSetting.IsLimitDividend + if isLimitDividend != 1 { + return errors.New("必须开启限制分红!") + } + startDate := now.AddDate(0, 0, -1).Format("2006-01-02") + " 00:00:00" + endDate := now.Format("2006-01-02") + " 00:00:00" + var list []model.OneCirclesGreenEnergySignIn + err = engine.Where("start_time >= ?", startDate).And("start_time <=?", endDate).Find(&list) + if err != nil { + fmt.Println("err:::::1111", err) + return + } + if len(list) <= 0 { + return errors.New("无须参与星级分红用户!") + } + var userSignInMap = map[int]string{} + var userSignInArr []int + for _, v := range list { + userSignInMap[v.Uid] = "true" + userSignInArr = append(userSignInArr, v.Uid) + } + + var vipEquitySetting []*md2.VipEquitySettingStruct + err = json.Unmarshal([]byte(oneCirclesGreenEnergyBasicSetting.VipEquitySetting), &vipEquitySetting) + if err != nil { + fmt.Println("err:::::22222", err) + return + } + + allUserLevel, err := db.UserLevlEgAll(engine) + if err != nil { + return err + } + var allUserLevelMap = map[int]model.UserLevel{} + for _, v := range allUserLevel { + allUserLevelMap[v.Id] = *v + } + + session := engine.NewSession() + defer func() { + session.Close() + if err := recover(); err != nil { + _ = zhios_order_relate_logx.Error(err) + } + }() + session.Begin() + + //2、查找 `one_circles_star_level_dividends_records` 基础设置 + oneCirclesStarLevelDividendsRecords, err := db.OneCirclesStarLevelDividendsRecordsGetOneByParamsBySession(session, map[string]interface{}{ + "key": "date", + "value": now.AddDate(0, 0, -1).Format("2006-01-02"), + }) + if err != nil { + fmt.Println("err:::::33333", err) + return + } + if oneCirclesStarLevelDividendsRecords == nil { + return errors.New("今日无分红积分!") + } + + //3、统计各等级人数 + dividendAmountValue, _ := decimal.NewFromString(oneCirclesStarLevelDividendsRecords.Amount) + decimalRate := decimal.NewFromInt(100) //百分比 + var vipLevelUserTotalMap = map[string]map[string]int64{} + var vipEquitySettingMap = map[string]string{} + for _, v := range vipEquitySetting { + dividendRatioValue, _ := decimal.NewFromString(v.DividendRatio) + dividendRatioValue = dividendRatioValue.Div(decimalRate) //分红比例 + vipEquitySettingMap[v.VipLevelId] = dividendAmountValue.Mul(dividendRatioValue).String() //TODO::计算各会员等级能得到多少分红 + + userLevel, err1 := db.UserLevelByID(engine, v.VipLevelId) + if err1 != nil { + fmt.Println("err:::::444444", err1) + return err1 + } + var ms []*model.UserLevel + err1 = session.Where("is_use=1").Where("level_weight > ?", userLevel.LevelWeight).Find(&ms) + if err1 != nil { + fmt.Println("err:::::55555", err1) + return err1 + } + + var tmpVipLevelId = []string{v.VipLevelId} + for _, m := range ms { + tmpVipLevelId = append(tmpVipLevelId, zhios_order_relate_utils.IntToStr(m.Id)) + } + var users model.User + count, err1 := session.In("level", tmpVipLevelId).Count(&users) + if err1 != nil { + fmt.Println("err:::::66666", err1) + return err1 + } + vipLevelUserTotalMap[v.VipLevelId] = map[string]int64{} + vipLevelUserTotalMap[v.VipLevelId]["count"] = count + vipLevelUserTotalMap[v.VipLevelId]["weight"] = int64(userLevel.LevelWeight) + } + + //4、处理分红 + var users []*model.User + err = session.In("uid", userSignInArr).Find(&users) + if err != nil { + fmt.Println("err:::::7777777", err) + return err + } + + var totalDividendValue = decimal.Decimal{} + for _, item := range users { + var siginDividendValue = decimal.Decimal{} + for k, v := range vipLevelUserTotalMap { + if int(v["weight"]) > allUserLevelMap[item.Level].LevelWeight { + continue + } + if vipEquitySettingMap[k] != "" && vipLevelUserTotalMap[k]["count"] > 0 { + dividendValue, _ := decimal.NewFromString(vipEquitySettingMap[k]) + userTotal := decimal.NewFromInt(vipLevelUserTotalMap[k]["count"]) + siginDividendValue = siginDividendValue.Add(dividendValue.Div(userTotal)) + } + } + + siginDividend, _ := siginDividendValue.Float64() + if siginDividend > 0 { + //TODO::推入rabbitmq 异步处理 + ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForAutoExchangeGreenEnergy{ + MasterId: masterId, + Uid: item.Uid, + }, md.OneCirclesRoutKeyForStarLevelDividend) + + totalDividendValue = totalDividendValue.Add(siginDividendValue) + } + } + + //5、更新 one_circles_star_level_dividends_records 记录 + oneCirclesStarLevelDividendsRecords.AlreadyDividendsAmount = totalDividendValue.String() + oneCirclesStarLevelDividendsRecords.NotDividendsAmount = dividendAmountValue.Sub(totalDividendValue).String() + _, err = db.OneCirclesStarLevelDividendsRecordsUpdateBySession(session, oneCirclesStarLevelDividendsRecords.Id, oneCirclesStarLevelDividendsRecords, "already_dividends_amount", "not_dividends_amount") + if err != nil { + _ = session.Rollback() fmt.Println("err:::::8888888", err) return err } From a22c0fb96812a03fe4e923352e37a0786baceadd Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Fri, 15 Mar 2024 19:37:31 +0800 Subject: [PATCH 10/16] =?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 +++++ rule/one_circles/one_circles_star_level_dividends.go | 7 ++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/md/mq.go b/md/mq.go index d243062..ca73399 100644 --- a/md/mq.go +++ b/md/mq.go @@ -19,3 +19,8 @@ type OneCirclesStructForAutoExchangeGreenEnergy struct { MasterId string `json:"master_id"` Uid int `json:"uid"` } +type OneCirclesStructForStarLevelDividends struct { + MasterId string `json:"master_id"` + Uid int `json:"uid"` + SignDividend float64 `json:"sign_dividend"` +} diff --git a/rule/one_circles/one_circles_star_level_dividends.go b/rule/one_circles/one_circles_star_level_dividends.go index 604a49e..c53a54d 100644 --- a/rule/one_circles/one_circles_star_level_dividends.go +++ b/rule/one_circles/one_circles_star_level_dividends.go @@ -352,9 +352,10 @@ func SettlementStarLevelDividends(engine *xorm.Engine, masterId string, ch *rabb siginDividend, _ := siginDividendValue.Float64() if siginDividend > 0 { //TODO::推入rabbitmq 异步处理 - ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForAutoExchangeGreenEnergy{ - MasterId: masterId, - Uid: item.Uid, + ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForStarLevelDividends{ + MasterId: masterId, + Uid: item.Uid, + SignDividend: siginDividend, }, md.OneCirclesRoutKeyForStarLevelDividend) totalDividendValue = totalDividendValue.Add(siginDividendValue) From 66f1f34072ccdbc20515ada7f0fa93b515080323 Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Fri, 15 Mar 2024 21:54:15 +0800 Subject: [PATCH 11/16] =?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 --- .../one_circles_star_level_dividends.go | 284 +++++------------- 1 file changed, 67 insertions(+), 217 deletions(-) diff --git a/rule/one_circles/one_circles_star_level_dividends.go b/rule/one_circles/one_circles_star_level_dividends.go index c53a54d..10d1668 100644 --- a/rule/one_circles/one_circles_star_level_dividends.go +++ b/rule/one_circles/one_circles_star_level_dividends.go @@ -20,13 +20,14 @@ import ( const SettlementStarLevelDividendsLockKey = "settlement_star_level_dividends_lock_key" -func SettlementStarLevelDividendsNew(engine *xorm.Engine, masterId string) (err error) { +// SettlementStarLevelDividends 绿色能量分红 +func SettlementStarLevelDividends(engine *xorm.Engine, masterId string, ch *rabbit.Channel) (err error) { now := time.Now() fmt.Println(now.Hour()) - if !(now.Hour() > 2 && now.Hour() < 8) { - //TODO::只在凌晨一点 ~ 凌晨 8 点运行 - return errors.New("非运行时间") - } + //if !(now.Hour() > 2 && now.Hour() < 8) { + // //TODO::只在凌晨一点 ~ 凌晨 8 点运行 + // return errors.New("非运行时间") + //} //TODO::增加“悲观锁”防止串行 getString, _ := cache.GetString(SettlementStarLevelDividendsLockKey) if getString != "" { @@ -59,12 +60,6 @@ func SettlementStarLevelDividendsNew(engine *xorm.Engine, masterId string) (err if len(list) <= 0 { return errors.New("无须参与星级分红用户!") } - var userSignInMap = map[int]string{} - var userSignInArr []int - for _, v := range list { - userSignInMap[v.Uid] = "true" - userSignInArr = append(userSignInArr, v.Uid) - } var vipEquitySetting []*md2.VipEquitySettingStruct err = json.Unmarshal([]byte(oneCirclesGreenEnergyBasicSetting.VipEquitySetting), &vipEquitySetting) @@ -142,223 +137,78 @@ func SettlementStarLevelDividendsNew(engine *xorm.Engine, masterId string) (err } //4、处理分红 - var users []*model.User - err = session.In("uid", userSignInArr).Find(&users) - if err != nil { - fmt.Println("err:::::7777777", err) - return err - } - + var userSignInArr []int var totalDividendValue = decimal.Decimal{} - for _, item := range users { - var siginDividendValue = decimal.Decimal{} - for k, v := range vipLevelUserTotalMap { - if int(v["weight"]) > allUserLevelMap[item.Level].LevelWeight { - continue + for _, l := range list { + userSignInArr = append(userSignInArr, l.Uid) + if len(userSignInArr) >= 10000 { + var users []*model.User + err = session.In("uid", userSignInArr).Find(&users) + if err != nil { + fmt.Println("err:::::7777777", err) + return err } - if vipEquitySettingMap[k] != "" && vipLevelUserTotalMap[k]["count"] > 0 { - dividendValue, _ := decimal.NewFromString(vipEquitySettingMap[k]) - userTotal := decimal.NewFromInt(vipLevelUserTotalMap[k]["count"]) - siginDividendValue = siginDividendValue.Add(dividendValue.Div(userTotal)) + for _, item := range users { + var siginDividendValue = decimal.Decimal{} + for k, v := range vipLevelUserTotalMap { + if int(v["weight"]) > allUserLevelMap[item.Level].LevelWeight { + continue + } + if vipEquitySettingMap[k] != "" && vipLevelUserTotalMap[k]["count"] > 0 { + dividendValue, _ := decimal.NewFromString(vipEquitySettingMap[k]) + userTotal := decimal.NewFromInt(vipLevelUserTotalMap[k]["count"]) + siginDividendValue = siginDividendValue.Add(dividendValue.Div(userTotal)) + } + } + + siginDividend, _ := siginDividendValue.Float64() + if siginDividend > 0 { + //TODO::推入rabbitmq 异步处理 + ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForStarLevelDividends{ + MasterId: masterId, + Uid: item.Uid, + SignDividend: siginDividend, + }, md.OneCirclesRoutKeyForStarLevelDividend) + + totalDividendValue = totalDividendValue.Add(siginDividendValue) + } } + userSignInArr = []int{} } - siginDividend, _ := siginDividendValue.Float64() - if siginDividend > 0 { - //给相应的用户加上个人的绿色积分(结算数量) - err = DealUserCoin(session, md.DealUserCoinReq{ - Kind: "add", - Mid: masterId, - Title: md.OneCirclesSettlementStarLevelDividends, - TransferType: md.OneCirclesSettlementStarLevelDividendsForUserVirtualCoinFlow, - OrdId: "", - CoinId: oneCirclesGreenEnergyBasicSetting.TeamGreenEnergyCoinId, - Uid: item.Uid, - Amount: siginDividend, - }) - } - totalDividendValue = totalDividendValue.Add(siginDividendValue) - } - - //5、更新 one_circles_star_level_dividends_records 记录 - oneCirclesStarLevelDividendsRecords.AlreadyDividendsAmount = totalDividendValue.String() - oneCirclesStarLevelDividendsRecords.NotDividendsAmount = dividendAmountValue.Sub(totalDividendValue).String() - _, err = db.OneCirclesStarLevelDividendsRecordsUpdateBySession(session, oneCirclesStarLevelDividendsRecords.Id, oneCirclesStarLevelDividendsRecords, "already_dividends_amount", "not_dividends_amount") - if err != nil { - _ = session.Rollback() - fmt.Println("err:::::8888888", err) - return err - } - - //6、 减少“星级分红”中的绿色能量 - totalDividend, _ := totalDividendValue.Float64() - err = DealAvailableGreenEnergyCoin(session, int(enum.SettlementStarLevelDividends), totalDividend, 0, enum.SettlementStarLevelDividends.String(), oneCirclesGreenEnergyBasicSetting, oneCirclesGreenEnergyBasicSetting.NowPrice) - if err != nil { - _ = session.Rollback() - fmt.Println("err:::::9999999", err) - return err - } - err = session.Commit() - if err != nil { - _ = session.Rollback() - return errors.New("事务提交失败") - } - return -} - -// SettlementStarLevelDividends 绿色能量分红 -func SettlementStarLevelDividends(engine *xorm.Engine, masterId string, ch *rabbit.Channel) (err error) { - now := time.Now() - fmt.Println(now.Hour()) - if !(now.Hour() > 2 && now.Hour() < 8) { - //TODO::只在凌晨一点 ~ 凌晨 8 点运行 - return errors.New("非运行时间") - } - //TODO::增加“悲观锁”防止串行 - getString, _ := cache.GetString(SettlementStarLevelDividendsLockKey) - if getString != "" { - fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完") - return errors.New("上一次结算未执行完") - } - cache.SetEx(SettlementStarLevelDividendsLockKey, "running", 3600*8) //8小时 - - //1、查找 `one_circles_green_energy_basic_setting` 基础设置 - oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{ - "key": "is_open", - "value": 1, - }) - if err != nil { - return - } - - isLimitDividend := oneCirclesGreenEnergyBasicSetting.IsLimitDividend - if isLimitDividend != 1 { - return errors.New("必须开启限制分红!") - } - startDate := now.AddDate(0, 0, -1).Format("2006-01-02") + " 00:00:00" - endDate := now.Format("2006-01-02") + " 00:00:00" - var list []model.OneCirclesGreenEnergySignIn - err = engine.Where("start_time >= ?", startDate).And("start_time <=?", endDate).Find(&list) - if err != nil { - fmt.Println("err:::::1111", err) - return - } - if len(list) <= 0 { - return errors.New("无须参与星级分红用户!") - } - var userSignInMap = map[int]string{} - var userSignInArr []int - for _, v := range list { - userSignInMap[v.Uid] = "true" - userSignInArr = append(userSignInArr, v.Uid) } - var vipEquitySetting []*md2.VipEquitySettingStruct - err = json.Unmarshal([]byte(oneCirclesGreenEnergyBasicSetting.VipEquitySetting), &vipEquitySetting) - if err != nil { - fmt.Println("err:::::22222", err) - return - } - - allUserLevel, err := db.UserLevlEgAll(engine) - if err != nil { - return err - } - var allUserLevelMap = map[int]model.UserLevel{} - for _, v := range allUserLevel { - allUserLevelMap[v.Id] = *v - } - - session := engine.NewSession() - defer func() { - session.Close() - if err := recover(); err != nil { - _ = zhios_order_relate_logx.Error(err) + if len(userSignInArr) > 0 { + var users []*model.User + err = session.In("uid", userSignInArr).Find(&users) + if err != nil { + fmt.Println("err:::::7777777", err) + return err } - }() - session.Begin() - //2、查找 `one_circles_star_level_dividends_records` 基础设置 - oneCirclesStarLevelDividendsRecords, err := db.OneCirclesStarLevelDividendsRecordsGetOneByParamsBySession(session, map[string]interface{}{ - "key": "date", - "value": now.AddDate(0, 0, -1).Format("2006-01-02"), - }) - if err != nil { - fmt.Println("err:::::33333", err) - return - } - if oneCirclesStarLevelDividendsRecords == nil { - return errors.New("今日无分红积分!") - } - - //3、统计各等级人数 - dividendAmountValue, _ := decimal.NewFromString(oneCirclesStarLevelDividendsRecords.Amount) - decimalRate := decimal.NewFromInt(100) //百分比 - var vipLevelUserTotalMap = map[string]map[string]int64{} - var vipEquitySettingMap = map[string]string{} - for _, v := range vipEquitySetting { - dividendRatioValue, _ := decimal.NewFromString(v.DividendRatio) - dividendRatioValue = dividendRatioValue.Div(decimalRate) //分红比例 - vipEquitySettingMap[v.VipLevelId] = dividendAmountValue.Mul(dividendRatioValue).String() //TODO::计算各会员等级能得到多少分红 - - userLevel, err1 := db.UserLevelByID(engine, v.VipLevelId) - if err1 != nil { - fmt.Println("err:::::444444", err1) - return err1 - } - var ms []*model.UserLevel - err1 = session.Where("is_use=1").Where("level_weight > ?", userLevel.LevelWeight).Find(&ms) - if err1 != nil { - fmt.Println("err:::::55555", err1) - return err1 - } - - var tmpVipLevelId = []string{v.VipLevelId} - for _, m := range ms { - tmpVipLevelId = append(tmpVipLevelId, zhios_order_relate_utils.IntToStr(m.Id)) - } - var users model.User - count, err1 := session.In("level", tmpVipLevelId).Count(&users) - if err1 != nil { - fmt.Println("err:::::66666", err1) - return err1 - } - vipLevelUserTotalMap[v.VipLevelId] = map[string]int64{} - vipLevelUserTotalMap[v.VipLevelId]["count"] = count - vipLevelUserTotalMap[v.VipLevelId]["weight"] = int64(userLevel.LevelWeight) - } - - //4、处理分红 - var users []*model.User - err = session.In("uid", userSignInArr).Find(&users) - if err != nil { - fmt.Println("err:::::7777777", err) - return err - } - - var totalDividendValue = decimal.Decimal{} - for _, item := range users { - var siginDividendValue = decimal.Decimal{} - for k, v := range vipLevelUserTotalMap { - if int(v["weight"]) > allUserLevelMap[item.Level].LevelWeight { - continue - } - if vipEquitySettingMap[k] != "" && vipLevelUserTotalMap[k]["count"] > 0 { - dividendValue, _ := decimal.NewFromString(vipEquitySettingMap[k]) - userTotal := decimal.NewFromInt(vipLevelUserTotalMap[k]["count"]) - siginDividendValue = siginDividendValue.Add(dividendValue.Div(userTotal)) + for _, item := range users { + var siginDividendValue = decimal.Decimal{} + for k, v := range vipLevelUserTotalMap { + if int(v["weight"]) > allUserLevelMap[item.Level].LevelWeight { + continue + } + if vipEquitySettingMap[k] != "" && vipLevelUserTotalMap[k]["count"] > 0 { + dividendValue, _ := decimal.NewFromString(vipEquitySettingMap[k]) + userTotal := decimal.NewFromInt(vipLevelUserTotalMap[k]["count"]) + siginDividendValue = siginDividendValue.Add(dividendValue.Div(userTotal)) + } } - } - siginDividend, _ := siginDividendValue.Float64() - if siginDividend > 0 { - //TODO::推入rabbitmq 异步处理 - ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForStarLevelDividends{ - MasterId: masterId, - Uid: item.Uid, - SignDividend: siginDividend, - }, md.OneCirclesRoutKeyForStarLevelDividend) + siginDividend, _ := siginDividendValue.Float64() + if siginDividend > 0 { + //TODO::推入rabbitmq 异步处理 + ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForStarLevelDividends{ + MasterId: masterId, + Uid: item.Uid, + SignDividend: siginDividend, + }, md.OneCirclesRoutKeyForStarLevelDividend) - totalDividendValue = totalDividendValue.Add(siginDividendValue) + totalDividendValue = totalDividendValue.Add(siginDividendValue) + } } } From 12db6f56f12a34f339eaa4bbcd6bfd9634384983 Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Fri, 15 Mar 2024 21:55:01 +0800 Subject: [PATCH 12/16] =?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 --- rule/one_circles/one_circles_star_level_dividends.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rule/one_circles/one_circles_star_level_dividends.go b/rule/one_circles/one_circles_star_level_dividends.go index 10d1668..e0c930c 100644 --- a/rule/one_circles/one_circles_star_level_dividends.go +++ b/rule/one_circles/one_circles_star_level_dividends.go @@ -24,10 +24,10 @@ const SettlementStarLevelDividendsLockKey = "settlement_star_level_dividends_loc func SettlementStarLevelDividends(engine *xorm.Engine, masterId string, ch *rabbit.Channel) (err error) { now := time.Now() fmt.Println(now.Hour()) - //if !(now.Hour() > 2 && now.Hour() < 8) { - // //TODO::只在凌晨一点 ~ 凌晨 8 点运行 - // return errors.New("非运行时间") - //} + if !(now.Hour() > 2 && now.Hour() < 8) { + //TODO::只在凌晨一点 ~ 凌晨 8 点运行 + return errors.New("非运行时间") + } //TODO::增加“悲观锁”防止串行 getString, _ := cache.GetString(SettlementStarLevelDividendsLockKey) if getString != "" { 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 13/16] =?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/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 } From f95f573a4c3c94903d94d8185e3e5fb48c50d920 Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Tue, 19 Mar 2024 18:52:48 +0800 Subject: [PATCH 14/16] =?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 --- .../one_circles_star_level_dividends.go | 100 ++++++++++++++---- 1 file changed, 82 insertions(+), 18 deletions(-) diff --git a/rule/one_circles/one_circles_star_level_dividends.go b/rule/one_circles/one_circles_star_level_dividends.go index e0c930c..072420d 100644 --- a/rule/one_circles/one_circles_star_level_dividends.go +++ b/rule/one_circles/one_circles_star_level_dividends.go @@ -161,16 +161,48 @@ func SettlementStarLevelDividends(engine *xorm.Engine, masterId string, ch *rabb } } - siginDividend, _ := siginDividendValue.Float64() - if siginDividend > 0 { - //TODO::推入rabbitmq 异步处理 - ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForStarLevelDividends{ - MasterId: masterId, - Uid: item.Uid, - SignDividend: siginDividend, - }, md.OneCirclesRoutKeyForStarLevelDividend) + if siginDividendValue.GreaterThan(decimal.NewFromInt(0)) { + //TODO::统计当前有多少直推用户昨天签到了 + userRelates, err1 := db.DbsUserRelateByParentUid(engine, item.Uid, 1) + if err1 != nil { + fmt.Println("err:::::88888888", err1) + return err + } + var userRelatesUids []int + if userRelates != nil { + for _, userRelate := range *userRelates { + userRelatesUids = append(userRelatesUids, userRelate.Uid) + } + } + if len(userRelatesUids) > 0 { + var oneCirclesGreenEnergySignIns []*model.OneCirclesGreenEnergySignIn + err = engine.Where("start_time >= ?", startDate).And("start_time <=?", endDate).In("uid", userRelatesUids).And("is_completed =?", 0).Find(&oneCirclesGreenEnergySignIns) + if err != nil { + fmt.Println("err:::::999999999", err) + return + } + if len(oneCirclesGreenEnergySignIns) > 0 { + userRelatesUidsValue := decimal.NewFromInt(int64(len(userRelatesUids))) + oneCirclesGreenEnergySignInsValue := decimal.NewFromInt(int64(len(oneCirclesGreenEnergySignIns))) + siginDividendValue = siginDividendValue.Mul(oneCirclesGreenEnergySignInsValue.Div(userRelatesUidsValue)) + } else { + siginDividendValue = decimal.NewFromFloat(0) + } + } else { + siginDividendValue = decimal.NewFromFloat(0) + } - totalDividendValue = totalDividendValue.Add(siginDividendValue) + siginDividend, _ := siginDividendValue.Float64() + if siginDividend > 0 { + //TODO::推入rabbitmq 异步处理 + ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForStarLevelDividends{ + MasterId: masterId, + Uid: item.Uid, + SignDividend: siginDividend, + }, md.OneCirclesRoutKeyForStarLevelDividend) + + totalDividendValue = totalDividendValue.Add(siginDividendValue) + } } } userSignInArr = []int{} @@ -198,16 +230,48 @@ func SettlementStarLevelDividends(engine *xorm.Engine, masterId string, ch *rabb } } - siginDividend, _ := siginDividendValue.Float64() - if siginDividend > 0 { - //TODO::推入rabbitmq 异步处理 - ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForStarLevelDividends{ - MasterId: masterId, - Uid: item.Uid, - SignDividend: siginDividend, - }, md.OneCirclesRoutKeyForStarLevelDividend) + if siginDividendValue.GreaterThan(decimal.NewFromInt(0)) { + //TODO::统计当前有多少直推用户昨天签到了 + userRelates, err1 := db.DbsUserRelateByParentUid(engine, item.Uid, 1) + if err1 != nil { + fmt.Println("err:::::88888888", err1) + return err + } + var userRelatesUids []int + if userRelates != nil { + for _, userRelate := range *userRelates { + userRelatesUids = append(userRelatesUids, userRelate.Uid) + } + } + if len(userRelatesUids) > 0 { + var oneCirclesGreenEnergySignIns []*model.OneCirclesGreenEnergySignIn + err = engine.Where("start_time >= ?", startDate).And("start_time <=?", endDate).In("uid", userRelatesUids).And("is_completed =?", 0).Find(&oneCirclesGreenEnergySignIns) + if err != nil { + fmt.Println("err:::::999999999", err) + return + } + if len(oneCirclesGreenEnergySignIns) > 0 { + userRelatesUidsValue := decimal.NewFromInt(int64(len(userRelatesUids))) + oneCirclesGreenEnergySignInsValue := decimal.NewFromInt(int64(len(oneCirclesGreenEnergySignIns))) + siginDividendValue = siginDividendValue.Mul(oneCirclesGreenEnergySignInsValue.Div(userRelatesUidsValue)) + } else { + siginDividendValue = decimal.NewFromFloat(0) + } + } else { + siginDividendValue = decimal.NewFromFloat(0) + } - totalDividendValue = totalDividendValue.Add(siginDividendValue) + siginDividend, _ := siginDividendValue.Float64() + if siginDividend > 0 { + //TODO::推入rabbitmq 异步处理 + ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForStarLevelDividends{ + MasterId: masterId, + Uid: item.Uid, + SignDividend: siginDividend, + }, md.OneCirclesRoutKeyForStarLevelDividend) + + totalDividendValue = totalDividendValue.Add(siginDividendValue) + } } } } From 885134dbe21583acbf77cf149168c019b8f3bd5a Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Thu, 21 Mar 2024 15:03:56 +0800 Subject: [PATCH 15/16] =?UTF-8?q?update=20=E5=A4=A7=E5=85=AC=E6=8E=92?= =?UTF-8?q?=E5=8A=A0=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rule/public_platoon_relate_commission.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/rule/public_platoon_relate_commission.go b/rule/public_platoon_relate_commission.go index c5824e2..2d12a02 100644 --- a/rule/public_platoon_relate_commission.go +++ b/rule/public_platoon_relate_commission.go @@ -36,6 +36,15 @@ func AddPublicPlatoonRelateCommission(engine *xorm.Engine, AddPublicPlatoonRelat return nil, err } for _, param := range AddPublicPlatoonRelateCommissionReqList { + //TODO::判断当前uid是否已加入公排网 + isHasUserPublicPlatoonRelation, err := db.UserPublicPlatoonRelationGetOneByParams(engine, map[string]interface{}{ + "key": "uid", + "value": param.Uid, + }) + if isHasUserPublicPlatoonRelation != nil { + continue + } + //TODO::判断是否有uid为-1 (代表等待新用户填充) 的记录 userPublicPlatoonRelation, err := db.UserPublicPlatoonRelationGetOneByParams(engine, map[string]interface{}{ "key": "uid", From 1a82fe15d38a653a50ff4b6e5d3b7f1406d6ecb5 Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Fri, 22 Mar 2024 17:41:46 +0800 Subject: [PATCH 16/16] =?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 --- ...rcles_available_green_energy_settlement.go | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/rule/one_circles/one_circles_available_green_energy_settlement.go b/rule/one_circles/one_circles_available_green_energy_settlement.go index 62abe5a..e71f510 100644 --- a/rule/one_circles/one_circles_available_green_energy_settlement.go +++ b/rule/one_circles/one_circles_available_green_energy_settlement.go @@ -49,7 +49,7 @@ func NewCalcPriceIncreaseFormula(userAmountValue string, oneCirclesGreenEnergyBa } // CalcPriceReductionFormula 计算降价公式(【用户需兑换绿能色量数量*{原始资金÷(用户需兑换绿色数量+原始数量}*(1 - 扣比例50% ~ 23%) = 用户获得钱) -func CalcPriceReductionFormula(engine *xorm.Engine, userExchangeNumsValue string, levelId string) (err error, values, greenEnergy, greenEnergyFee, nowPriceValue, afterPriceValue string) { +func CalcPriceReductionFormula(engine *xorm.Engine, userExchangeNumsValue string, levelId string, uid int) (err error, values, greenEnergy, greenEnergyFee, nowPriceValue, afterPriceValue string) { //1、查找 `one_circles_green_energy_basic_setting` 基础设置 oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{ "key": "is_open", @@ -73,8 +73,40 @@ func CalcPriceReductionFormula(engine *xorm.Engine, userExchangeNumsValue string err = errors.New("未查询到当前会员等级兑换余额手续费") return } - decimalRate := decimal.NewFromInt(100) //百分比 - exchangeAccountBalanceFee, _ := decimal.NewFromString(exchangeAccountBalanceFeeValue) //兑换手续费 + decimalRate := decimal.NewFromInt(100) //百分比 + exchangeAccountBalanceFee, _ := decimal.NewFromString(exchangeAccountBalanceFeeValue) //兑换手续费 + //TODO::统计当前有多少直推用户昨天签到了 + userRelates, err := db.DbsUserRelateByParentUid(engine, uid, 1) + if err != nil { + return + } + var userRelatesUids []int + if userRelates != nil { + for _, userRelate := range *userRelates { + userRelatesUids = append(userRelatesUids, userRelate.Uid) + } + } + if len(userRelatesUids) > 0 { + now := time.Now() + startDate := now.AddDate(0, 0, -1).Format("2006-01-02") + " 00:00:00" + endDate := now.Format("2006-01-02") + " 00:00:00" + var oneCirclesGreenEnergySignIns []*model.OneCirclesGreenEnergySignIn + err = engine.Where("start_time >= ?", startDate).And("start_time <=?", endDate).In("uid", userRelatesUids).And("is_completed =?", 0).Find(&oneCirclesGreenEnergySignIns) + if err != nil { + return + } + + decimalRateV2 := decimal.NewFromInt(10) //百分比 + if len(oneCirclesGreenEnergySignIns) > 0 { + decimalBase := decimal.NewFromInt(1) //基数 + userRelatesUidsValue := decimal.NewFromInt(int64(len(userRelatesUids))) + oneCirclesGreenEnergySignInsValue := decimal.NewFromInt(int64(len(oneCirclesGreenEnergySignIns))) + exchangeAccountBalanceFee = exchangeAccountBalanceFee.Add(decimalBase.Sub(oneCirclesGreenEnergySignInsValue.Div(userRelatesUidsValue)).Mul(decimalRateV2)) + } else { + exchangeAccountBalanceFee = exchangeAccountBalanceFee.Add(decimalRateV2) + } + } + userExchangeNums, _ := decimal.NewFromString(userExchangeNumsValue) //用户兑换绿色能量 originalQuantityNums, _ := decimal.NewFromString(oneCirclesGreenEnergyBasicSetting.OriginalQuantityNums) //原始数量 nowPrice, _ := decimal.NewFromString(oneCirclesGreenEnergyBasicSetting.NowPrice) //当前价格