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/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 //绿色积分双链兑换账户余额-减少贡献积分 ) diff --git a/md/fin_user_flow.go b/md/fin_user_flow.go index b8a63e9..dac1ba7 100644 --- a/md/fin_user_flow.go +++ b/md/fin_user_flow.go @@ -33,8 +33,8 @@ const ( ) const DealUserAmountRequestIdPrefix = "%s:deal_user_amount:%d" -const UserAmountRedisKey = "%s:rule_user_amount:%d" const StoreUserAmountRedisKey = "%s:rule_store_user_amount:%d" +const UserAmountRedisKey = "%s:rule_cache_user_amount:%d" type DealIntegralReleaseInterpositionUserAmountReq struct { Kind string `json:"kind"` diff --git a/md/mq.go b/md/mq.go index b6f49a0..ca73399 100644 --- a/md/mq.go +++ b/md/mq.go @@ -3,7 +3,10 @@ package md const OneCirclesExchange = "one.circles" const ( - OneCirclesRoutKeyForSignIn = "sign_in" // 签到 + OneCirclesRoutKeyForSignIn = "sign_in" // 签到 + OneCirclesRoutKeyForStarLevelDividend = "star_level_dividend" // 星级分红 + OneCirclesRoutKeyForAutoExchangeGreenEnergyToPerson = "auto_exchange_green_energy_to_person" // 自动兑换个人绿色能量 + OneCirclesRoutKeyForAutoExchangeGreenEnergyToTeam = "auto_exchange_green_energy_to_team" // 自动兑换团队绿色能量 ) type OneCirclesStructForSignIn struct { @@ -11,3 +14,13 @@ type OneCirclesStructForSignIn struct { Uid int `json:"uid"` Id int64 `json:"id"` } + +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/green_coin_double_chain_settlement.go b/rule/green_coin_double_chain_settlement.go index 0ce88c4..cf09b70 100644 --- a/rule/green_coin_double_chain_settlement.go +++ b/rule/green_coin_double_chain_settlement.go @@ -23,7 +23,20 @@ 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 + } + if greenCoinDoubleChain == nil { + return + } + + //2、判断当前会员等级 user, err := db.DbsUserFindByUid(Db, uid) if err != nil { return @@ -33,25 +46,24 @@ 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 + } + + //4、处理“绿色积分”作用于下级 + err = HandleUserGreenCoinDoubleChainIntegralBySon(Db, uid, ordId, masterId, greenCoinDoubleChain) + if err != nil { + return err } - //2、查找对应直推上级用户 + return +} + +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 { return @@ -61,40 +73,29 @@ func DealUserGreenCoinDoubleChainIntegral(Db *xorm.Engine, uid int, amount, ordI return } - //3、查询用户和对应上级的虚拟币余额 - userAmount, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, uid) - if err != nil { - return - } - parentAmount, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin1, parentUserRelate.ParentUid) + //2、查询对应上级的虚拟币余额 + parentAmount, err := svc.GetUserCoinAmount(Db.NewSession(), masterId, greenCoinDoubleChain.Coin1, parentUserRelate.ParentUid) if err != nil { return } - userAmountValue, _ := decimal.NewFromString(userAmount) parentAmountValue, _ := decimal.NewFromString(parentAmount) - if parentAmountValue.LessThan(amountValue) { - //TODO::上级绿色积分小于当前需要兑换积分金额 - return - } - if userAmountValue.LessThan(amountValue) { - err = errors.New("当前用户金额有误,不足以进行兑换") + if parentAmountValue.LessThanOrEqual(decimal.NewFromFloat(0)) { + //TODO::上级绿色积分小于0不需要进行兑换 return } - //4、查询上级直推的所有下级用户 + //3、查询上级直推的所有下级用户 sonUserRelates, err := db.DbsUserRelateByParentUid(Db, parentUserRelate.ParentUid, 1) if err != nil { 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) @@ -109,126 +110,369 @@ 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 = ? and amount > 0", 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), - }) + 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) + } + }() + for _, v := range sonUserVirtualWallet { + + 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 + } + + //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() + break + } + + //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()), + }) + if err != nil { + session.Rollback() + break + } + //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() + break + } + //5.4上级余额增加 + var coin model.VirtualCoin + _, err = session.Where("id = ?", greenCoinDoubleChain.Coin1).Get(&coin) + if err != nil { + session.Rollback() + break + } + 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() + break + } + + //6、新增 green_coin_double_chain_exchange_records 记录 + afterUserCoinAmount, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin1, parentUserRelate.ParentUid) + if err != nil { + session.Rollback() + break + } + afterCoinAmountContributeUser1, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, v.Uid) + if err != nil { + session.Rollback() + break + } + afterCoinAmountContributeUser2, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, lastUid) + if err != nil { + session.Rollback() + break + } + + 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() + break + } + + if zhios_order_relate_utils.StrToFloat64(afterUserCoinAmount) == 0 { + break + } else { + amount = afterCoinAmountContributeUser1 + lastUid = v.Uid + } + if zhios_order_relate_utils.StrToFloat64(amount) == 0 { + amount = "" + } + } + session.Commit() + return +} + +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 { - 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.LessThanOrEqual(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 { + //未查询到直推下级 return } - afterCoinAmountContributeUser2, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, sonUserVirtualWallet.Uid) + var sonUserIds []int + for _, v := range *sonUserRelates { + 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 = ? and amount > 0 ", greenCoinDoubleChain.Coin2).In("uid", userIds).Asc("amount").Find(&sonUserVirtualWallet) if err != nil { - session.Rollback() return } + /* + 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) + } + }() + for _, v := range sonUserVirtualWallet { + + 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 + } + + //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() + break + } + //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()), + }) + if err != nil { + session.Rollback() + break + } + //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() + break + } + //4.4上级余额增加 + var coin model.VirtualCoin + _, err = session.Where("id = ?", greenCoinDoubleChain.Coin1).Get(&coin) + if err != nil { + session.Rollback() + break + } + 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() + break + } + + //5、新增 green_coin_double_chain_exchange_records 记录 + afterUserCoinAmount, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin1, uid) + if err != nil { + session.Rollback() + break + } + afterCoinAmountContributeUser1, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, v.Uid) + if err != nil { + session.Rollback() + break + } + afterCoinAmountContributeUser2, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, lastUid) + if err != nil { + session.Rollback() + break + } + 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() + break + } + + if zhios_order_relate_utils.StrToFloat64(afterUserCoinAmount) == 0 { + break + } else { + amount = afterCoinAmountContributeUser1 + lastUid = v.Uid + } + if zhios_order_relate_utils.StrToFloat64(amount) == 0 { + amount = "" + } + } session.Commit() - isExchange = true return } 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..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 @@ -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(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(1000, 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 +} 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) //当前价格 diff --git a/rule/one_circles/one_circles_star_level_dividends.go b/rule/one_circles/one_circles_star_level_dividends.go index 781f748..072420d 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" @@ -20,7 +21,7 @@ import ( const SettlementStarLevelDividendsLockKey = "settlement_star_level_dividends_lock_key" // SettlementStarLevelDividends 绿色能量分红 -func SettlementStarLevelDividends(engine *xorm.Engine, masterId string) (err error) { +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) { @@ -59,12 +60,6 @@ func SettlementStarLevelDividends(engine *xorm.Engine, masterId string) (err 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,41 +137,143 @@ func SettlementStarLevelDividends(engine *xorm.Engine, masterId string) (err 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)) + } + } + + 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) + } + + 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, - }) + } + + 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 + } + + 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)) + } + } + + 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) + } + + 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) } //5、更新 one_circles_star_level_dividends_records 记录 @@ -184,6 +281,7 @@ 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 } 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", diff --git a/svc/svc_deal_user_amount.go b/svc/svc_deal_user_amount.go index 43b8453..62a6897 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 } @@ -171,7 +172,7 @@ func DealUserAmountFlow(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, false) if err != nil { return err } @@ -265,25 +266,36 @@ func DealUserStoreAmountFlow(session *xorm.Session, req md.DealUserAmount) (err } // 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 } - return amount, err + 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 + } + //将获取到的余额值缓存至redis + _ = SetCacheUserAmount(session, masterId, amount, uid, false) } return amount, nil } @@ -302,7 +314,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*0.5) if err != nil { return err }