diff --git a/go.mod b/go.mod index 309a58b..05a327f 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module code.fnuoos.com/EggPlanet/egg_system_rules.git go 1.19 // go.mod文件中 -// replace code.fnuoos.com/EggPlanet/egg_models.git => E:/company/Egg/egg_models +//replace code.fnuoos.com/EggPlanet/egg_models.git => E:/company/Egg/egg_models require ( code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.5 @@ -21,6 +21,7 @@ require ( ) require ( + code.fnuoos.com/go_rely_warehouse/zyos_go_es.git v1.0.1-0.20241118083738-0f22da9ba0be // indirect github.com/alibabacloud-go/alibabacloud-gateway-spi v0.0.5 // indirect github.com/alibabacloud-go/debug v1.0.1 // indirect github.com/alibabacloud-go/endpoint-util v1.1.0 // indirect @@ -40,7 +41,7 @@ require ( ) require ( - code.fnuoos.com/EggPlanet/egg_models.git v0.2.1-0.20241128102555-fc839292d728 + code.fnuoos.com/EggPlanet/egg_models.git v0.2.1-0.20241204145112-7b4c29691a6d github.com/goccy/go-json v0.10.2 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/json-iterator/go v1.1.12 // indirect diff --git a/go.sum b/go.sum index f38d755..93dde90 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= code.fnuoos.com/EggPlanet/egg_models.git v0.2.1-0.20241128102555-fc839292d728 h1:BHW0yPeka1RRS4pIcZKwiatVg9U4mE8iiTl1+YCk1vQ= code.fnuoos.com/EggPlanet/egg_models.git v0.2.1-0.20241128102555-fc839292d728/go.mod h1:+bDK4gfBq4LEkhh6K/A46+0urH/sgxDxnjIjlxb9HI8= +code.fnuoos.com/go_rely_warehouse/zyos_go_es.git v1.0.1-0.20241118083738-0f22da9ba0be h1:zuleX5JnIGQpt6wDAmCFs++lLgOmE6kWo3xkJnjrSck= +code.fnuoos.com/go_rely_warehouse/zyos_go_es.git v1.0.1-0.20241118083738-0f22da9ba0be/go.mod h1:Yuc7XnUQ15eaC0ZobcJG7XwkVTttVqp52OTanXhI1Qk= code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.5 h1:dqvWJqlJi0WXCwTxbWPLvSOsKPjP+iEDBVgLcAl9nOE= code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.5/go.mod h1:TTcCnFn/LhBGapnutpezlW+GXkLRNPMWkziOoCsXQqY= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= diff --git a/rule/egg_energy/activity_coin_auto_exchange_egg_energy.go b/rule/egg_energy/activity_coin_auto_exchange_egg_energy.go new file mode 100644 index 0000000..61bba98 --- /dev/null +++ b/rule/egg_energy/activity_coin_auto_exchange_egg_energy.go @@ -0,0 +1,92 @@ +package egg_energy + +import ( + "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" + "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" + md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md" + "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule" + "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" + egg_system_rules "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils" + zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/logx" + "fmt" + "github.com/shopspring/decimal" + "math/rand" + "time" + "xorm.io/xorm" +) + +// ActivityCoinAutoExchangeEggPersonEnergy 个人蛋蛋积分【开始】兑换成个人蛋蛋能量 +func ActivityCoinAutoExchangeEggPersonEnergy(engine *xorm.Engine, req md.EggEnergyStructForAutoExchangeGreenEnergy) (err error) { + //1、判断当前是否可以执行 + now := time.Now() + endTimeValue, _ := egg_system_rules.StringToTime(req.EndExchangeTime) + if endTimeValue.After(now) { + time.Sleep(time.Microsecond * time.Duration(rand.Intn(5))) //TODO::随机休眠5s不等 + } + + //2、查询sign 记录 + eggSignInDb := implement.NewEggSignInDb(engine) + sign, err := eggSignInDb.EggSignInGet(req.SignId) + if err != nil { + return err + } + if sign.IsCompleted == 1 { + //TODO::已完成的直接pass + return + } + + //3、增加用户对应蛋蛋能量数量 + startTime, _ := egg_system_rules.StringToTime(sign.StartTime) + endTime, _ := egg_system_rules.StringToTime(sign.EndTime) + seconds := endTime.Sub(startTime).Seconds() + secondsValue := decimal.NewFromFloat(seconds) + eggEnergyNumsForPersonValue, _ := decimal.NewFromString(sign.EstimatePerSecondPersonEggEnergyValue) + eggEnergyNumsForTeamValue, _ := decimal.NewFromString(sign.EstimatePerSecondTeamEggEnergyValue) + amountForPerson, _ := eggEnergyNumsForPersonValue.Mul(secondsValue).Float64() + amountForTeam, _ := eggEnergyNumsForTeamValue.Mul(secondsValue).Float64() + session := engine.NewSession() + defer func() { + session.Close() + if err := recover(); err != nil { + _ = zhios_order_relate_logx.Error(err) + } + }() + session.Begin() + err = rule.DealUserVirtualCoin(session, md2.DealUserVirtualCoinReq{ + Kind: "add", + Title: enum.PersonEggGetByEnergyEggPointCoin.String(), + TransferType: int(enum.PersonEggGetByEnergyEggPointCoin), + CoinId: req.PersonEggEnergyCoinId, + Uid: req.Uid, + Amount: amountForPerson, + }) + if err != nil { + fmt.Println("ActivityCoinAutoExchangeEggEnergy:::::err111:::", err) + _ = session.Rollback() + return + } + err = rule.DealUserVirtualCoin(session, md2.DealUserVirtualCoinReq{ + Kind: "add", + Title: enum.PersonEggGetByEnergyEggPointCoin.String(), + TransferType: int(enum.PersonEggGetByEnergyEggPointCoin), + CoinId: req.PersonEggEnergyCoinId, + Uid: req.Uid, + Amount: amountForTeam, + }) + if err != nil { + fmt.Println("ActivityCoinAutoExchangeEggEnergy:::::err222:::", err) + _ = session.Rollback() + return + } + + //3、修改sign记录 + sign.IsCompleted = 1 + _, err = eggSignInDb.EggSignInUpdateBySession(session, sign.Id, sign, "is_completed") + if err != nil { + fmt.Println("ActivityCoinAutoExchangeEggEnergy:::::err333:::", err) + _ = session.Rollback() + return err + } + + return session.Commit() +} diff --git a/rule/egg_energy/activity_coin_auto_exchange_egg_person_energy.go b/rule/egg_energy/activity_coin_auto_exchange_egg_person_energy.go deleted file mode 100644 index 26d3d2d..0000000 --- a/rule/egg_energy/activity_coin_auto_exchange_egg_person_energy.go +++ /dev/null @@ -1,76 +0,0 @@ -package egg_energy - -import ( - "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" - md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md" - "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule" - enum2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/enum" - "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" - "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/svc" - egg_system_rules "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils" - zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/logx" - "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" - "fmt" - "xorm.io/xorm" -) - -// ActivityCoinAutoExchangeEggPersonEnergy 个人蛋蛋积分【开始】兑换成个人蛋蛋能量 -func ActivityCoinAutoExchangeEggPersonEnergy(engine *xorm.Engine, req md.EggEnergyStructForAutoExchangeGreenEnergy, ch *rabbit.Channel) (err error) { - session := engine.NewSession() - defer func() { - session.Close() - if err := recover(); err != nil { - _ = zhios_order_relate_logx.Error(err) - } - }() - session.Begin() - - eggEnergyCoreData, cb, err := svc.GetEggEnergyCoreData(engine) - if err != nil { - return err - } - if cb != nil { - defer cb() // 释放锁 - } - - //1、计算涨价公式 - err1, calcPriceIncreaseFormulaResp := CalcPriceIncreaseFormula(req.AutoExchangeNumsAmount, eggEnergyCoreData) - if err1 != nil { - _ = session.Rollback() - return err1 - } - - //2、更改动态数据 - err = DealAvailableEggEnergyCoin(session, int(enum2.PersonalActivePointRedemption), eggEnergyCoreData, md.DealAvailableEggEnergyCoinReq{ - Amount: calcPriceIncreaseFormulaResp.GetEggEnergyNums, - AmountFee: "", - BeforePrice: calcPriceIncreaseFormulaResp.BeforePrice, - AfterPrice: calcPriceIncreaseFormulaResp.AfterPrice, - BeforePlanetTotalValue: calcPriceIncreaseFormulaResp.BeforePlanetTotalValue, - AfterPlanetTotalValue: calcPriceIncreaseFormulaResp.AfterPlanetTotalValue, - BeforeEnergyTotalNums: calcPriceIncreaseFormulaResp.BeforeEnergyTotalNums, - AfterEnergyTotalNums: calcPriceIncreaseFormulaResp.AfterEnergyTotalNums, - }) - if err != nil { - fmt.Println("ActivityCoinAutoExchangeEggPersonEnergy:::::err111:::", err) - _ = session.Rollback() - return err - } - - //3、增加用户对应蛋蛋能量数量 - err = rule.DealUserVirtualCoin(session, md2.DealUserVirtualCoinReq{ - Kind: "add", - Title: enum.PersonEggGetByEnergyEggPointCoin.String(), - TransferType: int(enum.PersonEggGetByEnergyEggPointCoin), - CoinId: req.EnergyCoinId, - Uid: req.Uid, - Amount: egg_system_rules.StrToFloat64(calcPriceIncreaseFormulaResp.GetEggEnergyNums), - }) - if err != nil { - fmt.Println("ActivityCoinAutoExchangeEggPersonEnergy:::::err222:::", err) - _ = session.Rollback() - return - } - - return session.Commit() -} diff --git a/rule/egg_energy/activity_coin_auto_exchange_egg_team_energy.go b/rule/egg_energy/activity_coin_auto_exchange_egg_team_energy.go deleted file mode 100644 index 2a6b82d..0000000 --- a/rule/egg_energy/activity_coin_auto_exchange_egg_team_energy.go +++ /dev/null @@ -1,76 +0,0 @@ -package egg_energy - -import ( - "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" - md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md" - "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule" - enum2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/enum" - "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" - "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/svc" - egg_system_rules "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils" - zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/logx" - "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" - "fmt" - "xorm.io/xorm" -) - -// ActivityCoinAutoExchangeEggTeamEnergy 团队蛋蛋积分【开始】兑换成团队蛋蛋能量 -func ActivityCoinAutoExchangeEggTeamEnergy(engine *xorm.Engine, req md.EggEnergyStructForAutoExchangeGreenEnergy, ch *rabbit.Channel) (err error) { - session := engine.NewSession() - defer func() { - session.Close() - if err := recover(); err != nil { - _ = zhios_order_relate_logx.Error(err) - } - }() - session.Begin() - - eggEnergyCoreData, cb, err := svc.GetEggEnergyCoreData(engine) - if err != nil { - return err - } - if cb != nil { - defer cb() // 释放锁 - } - - //1、计算涨价公式 - err1, calcPriceIncreaseFormulaResp := CalcPriceIncreaseFormula(req.AutoExchangeNumsAmount, eggEnergyCoreData) - if err1 != nil { - _ = session.Rollback() - return err1 - } - - //2、更改动态数据 - err = DealAvailableEggEnergyCoin(session, int(enum2.TeamActivePointRedemption), eggEnergyCoreData, md.DealAvailableEggEnergyCoinReq{ - Amount: calcPriceIncreaseFormulaResp.GetEggEnergyNums, - AmountFee: "", - BeforePrice: calcPriceIncreaseFormulaResp.BeforePrice, - AfterPrice: calcPriceIncreaseFormulaResp.AfterPrice, - BeforePlanetTotalValue: calcPriceIncreaseFormulaResp.BeforePlanetTotalValue, - AfterPlanetTotalValue: calcPriceIncreaseFormulaResp.AfterPlanetTotalValue, - BeforeEnergyTotalNums: calcPriceIncreaseFormulaResp.BeforeEnergyTotalNums, - AfterEnergyTotalNums: calcPriceIncreaseFormulaResp.AfterEnergyTotalNums, - }) - if err != nil { - fmt.Println("ActivityCoinAutoExchangeEggTeamEnergy:::::err111:::", err) - _ = session.Rollback() - return err - } - - //3、增加用户对应蛋蛋能量数量 - err = rule.DealUserVirtualCoin(session, md2.DealUserVirtualCoinReq{ - Kind: "add", - Title: enum.TeamEggGetByEnergyEggPointCoin.String(), - TransferType: int(enum.TeamEggGetByEnergyEggPointCoin), - CoinId: req.EnergyCoinId, - Uid: req.Uid, - Amount: egg_system_rules.StrToFloat64(calcPriceIncreaseFormulaResp.GetEggEnergyNums), - }) - if err != nil { - fmt.Println("ActivityCoinAutoExchangeEggTeamEnergy:::::err222:::", err) - _ = session.Rollback() - return - } - - return session.Commit() -} diff --git a/rule/egg_energy/activity_coin_ready_exchange_egg_energy.go b/rule/egg_energy/activity_coin_ready_exchange_egg_energy.go index 18ec3a7..5ed3fb8 100644 --- a/rule/egg_energy/activity_coin_ready_exchange_egg_energy.go +++ b/rule/egg_energy/activity_coin_ready_exchange_egg_energy.go @@ -1,6 +1,7 @@ package egg_energy import ( + db "code.fnuoos.com/EggPlanet/egg_models.git/src" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" @@ -81,49 +82,74 @@ func ActivityCoinReadyExchangeEggEnergy(engine *xorm.Engine, uid int64, ch *rabb return errors.New("当前活跃值无法发起挖掘~") } - //session := engine.NewSession() - //defer func() { - // session.Close() - // if err := recover(); err != nil { - // _ = zhios_order_relate_logx.Error(err) - // } - //}() - //session.Begin() - now := time.Now() - //4、计算能兑换的个人活跃积分数量 + var autoExchangeNumsByPersonAmountValue, autoExchangeNumsByTeamAmountValue string + var autoExchangeNumsByPersonAmount, autoExchangeNumsByTeamAmount decimal.Decimal if egg_system_rules.StrToFloat64(personUserVirtualAmount.Amount) >= autoExchangeNumsByPerson { autoExchangeNumsByPersonValue, _ := decimal.NewFromString(personUserVirtualAmount.Amount) autoExchangeRateByPersonValue, _ := decimal.NewFromString(exchangeRulesStruct.AutoExchangeRateByPerson) - autoExchangeNumsByPersonAmount := autoExchangeNumsByPersonValue.Mul(autoExchangeRateByPersonValue).Div(personActivePointsCoinExchangeRatioValue).String() //个人活跃积分数量*兑换百分比/兑换比例(与金额) - //TODO::推入rabbitmq 异步处理 - ch.Publish(md2.EggEnergyExchange, md2.EggEnergyStructForStartExchangeGreenEnergy{ - Uid: uid, - Amount: autoExchangeNumsByPersonValue.Mul(autoExchangeRateByPersonValue).String(), - PointCoinId: coin1.Id, - EnergyCoinId: eggEnergyBasicSetting.PersonEggEnergyCoinId, - AutoExchangeNumsAmount: autoExchangeNumsByPersonAmount, - StartExchangeTime: now.Format("2006-01-02 15:04:05"), - EndExchangeTime: now.Add(time.Hour * time.Duration(egg_system_rules.StrToInt64(videoRewardSystem.EachRoundHour))).Format("2006-01-02 15:04:05"), - }, md2.EggEnergyRoutKeyForStartExchangeGreenEnergyToPerson) + autoExchangeNumsByPersonAmountValue = autoExchangeNumsByPersonValue.Mul(autoExchangeRateByPersonValue).Div(decimal.NewFromInt(100)).String() + autoExchangeNumsByPersonAmount = autoExchangeNumsByPersonValue.Mul(autoExchangeRateByPersonValue).Div(decimal.NewFromInt(100)).Div(personActivePointsCoinExchangeRatioValue) //个人活跃积分数量*兑换百分比/兑换比例(与金额) } //5、计算能兑换的团队活跃积分数量 if egg_system_rules.StrToFloat64(teamUserVirtualAmount.Amount) >= autoExchangeNumsByTeam { autoExchangeNumsByTeamValue, _ := decimal.NewFromString(teamUserVirtualAmount.Amount) autoExchangeRateByTeamValue, _ := decimal.NewFromString(exchangeRulesStruct.AutoExchangeRateByTeam) - autoExchangeNumsByTeamAmount := autoExchangeNumsByTeamValue.Mul(autoExchangeRateByTeamValue).Div(teamActivePointsCoinExchangeRatioValue).String() //团队活跃积分数量*兑换百分比/兑换比例(与金额) - //TODO::推入rabbitmq 异步处理 - ch.Publish(md2.EggEnergyExchange, md2.EggEnergyStructForStartExchangeGreenEnergy{ - Uid: uid, - Amount: autoExchangeNumsByTeamValue.Mul(autoExchangeRateByTeamValue).String(), - PointCoinId: coin2.Id, - EnergyCoinId: eggEnergyBasicSetting.TeamEggEnergyCoinId, - AutoExchangeNumsAmount: autoExchangeNumsByTeamAmount, - StartExchangeTime: now.Format("2006-01-02 15:04:05"), - EndExchangeTime: now.Add(time.Hour * time.Duration(egg_system_rules.StrToInt64(videoRewardSystem.EachRoundHour))).Format("2006-01-02 15:04:05"), - }, md2.EggEnergyRoutKeyForStartExchangeGreenEnergyToTeam) + autoExchangeNumsByTeamAmountValue = autoExchangeNumsByTeamValue.Mul(autoExchangeRateByTeamValue).Div(decimal.NewFromInt(100)).String() + autoExchangeNumsByTeamAmount = autoExchangeNumsByTeamValue.Mul(autoExchangeRateByTeamValue).Div(decimal.NewFromInt(100)).Div(teamActivePointsCoinExchangeRatioValue) //团队活跃积分数量*兑换百分比/兑换比例(与金额) + } + + //6、插入签到记录 + now := time.Now() + eggSignInDb := implement.NewEggSignInDb(db.Db) + var sign = &model.EggSignIn{ + Uid: uid, + StartTime: now.Format("2006-01-02 15:04:05"), + EndTime: now.Add(time.Hour * time.Duration(egg_system_rules.StrToInt64(videoRewardSystem.EachRoundHour))).Format("2006-01-02 15:04:05"), + TotalPersonEggPoints: autoExchangeNumsByPersonAmountValue, + TotalTeamEggPoints: autoExchangeNumsByTeamAmountValue, + EstimatePerSecondPersonEggEnergyValue: "", + EstimatePerSecondTeamEggEnergyValue: "", + TotalPersonEggPointsAmountValue: autoExchangeNumsByPersonAmount.String(), + TotalTeamEggPointsAmountValue: autoExchangeNumsByTeamAmount.String(), + IsCompleted: 0, + } + _, err = eggSignInDb.EggSignInInsert(sign) + if err != nil { + return err + } + + //7、新增 `egg_energy_user_activity` 记录 + var m model.EggEnergyUserActivity + has, err := engine.Where("uid =? and date =?", uid, now.Format("2006-01-02")).Get(&m) + if err != nil { + return err + } + if !has { + eggEnergyUserActivityDb := implement.NewEggEnergyUserActivityDb(db.Db) + insertAffected, err1 := eggEnergyUserActivityDb.EggEnergyUserActivityInsert(&model.EggEnergyUserActivity{ + Uid: uid, + Date: now.Format("2006-01-02"), + }) + if err1 != nil { + return err1 + } + if insertAffected <= 0 { + return errors.New("插入活跃记录失败") + } } + //TODO::推入rabbitmq 异步处理 + ch.Publish(md2.EggEnergyExchange, md2.EggEnergyStructForStartExchangeGreenEnergy{ + Uid: uid, + SignId: sign.Id, + PersonEggEnergyCoinId: eggEnergyBasicSetting.PersonEggEnergyCoinId, + TeamEggEnergyCoinId: eggEnergyBasicSetting.TeamEggEnergyCoinId, + PersonPointCoinId: coin1.Id, + PersonEnergyCoinId: eggEnergyBasicSetting.PersonEggEnergyCoinId, + TeamPointCoinId: coin2.Id, + TeamEnergyCoinId: eggEnergyBasicSetting.TeamEggEnergyCoinId, + }, md2.EggEnergyRoutKeyForStartExchangeGreenEnergy) return } diff --git a/rule/egg_energy/activity_coin_start_exchange_egg_person_energy.go b/rule/egg_energy/activity_coin_start_exchange_egg_person_energy.go index 2c03d69..097a131 100644 --- a/rule/egg_energy/activity_coin_start_exchange_egg_person_energy.go +++ b/rule/egg_energy/activity_coin_start_exchange_egg_person_energy.go @@ -1,19 +1,23 @@ package egg_energy import ( + "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md" "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule" + enum2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/enum" "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" + "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/svc" egg_system_rules "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils" zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/logx" "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "fmt" + "github.com/shopspring/decimal" "xorm.io/xorm" ) -// ActivityCoinStartExchangeEggPersonEnergy 个人蛋蛋积分【开始】兑换成个人蛋蛋能量 -func ActivityCoinStartExchangeEggPersonEnergy(engine *xorm.Engine, req md.EggEnergyStructForStartExchangeGreenEnergy, ch *rabbit.Channel) (err error) { +// ActivityCoinStartExchangeEggEnergy 蛋蛋积分【开始】兑换成蛋蛋能量 +func ActivityCoinStartExchangeEggEnergy(engine *xorm.Engine, req md.EggEnergyStructForStartExchangeGreenEnergy, ch *rabbit.Channel) (err error) { session := engine.NewSession() defer func() { session.Close() @@ -23,29 +27,127 @@ func ActivityCoinStartExchangeEggPersonEnergy(engine *xorm.Engine, req md.EggEne }() session.Begin() - //1、扣除用户对应蛋蛋活跃积分数量 + //1、查询sign 记录 + eggSignInDb := implement.NewEggSignInDb(engine) + sign, err := eggSignInDb.EggSignInGet(req.SignId) + if err != nil { + return err + } + + //2、扣除用户对应蛋蛋活跃积分数量 err = rule.DealUserVirtualCoin(session, md2.DealUserVirtualCoinReq{ Kind: "sub", Title: enum.EggPointCoinToExchangeToPersonEggEnergy.String(), TransferType: int(enum.EggPointCoinToExchangeToPersonEggEnergy), - CoinId: req.PointCoinId, + CoinId: req.PersonPointCoinId, + Uid: req.Uid, + Amount: egg_system_rules.StrToFloat64(sign.TotalPersonEggPoints), + }) + if err != nil { + fmt.Println("ActivityCoinStartExchangeEggEnergy:::::err111:::", err) + _ = session.Rollback() + return + } + + err = rule.DealUserVirtualCoin(session, md2.DealUserVirtualCoinReq{ + Kind: "sub", + Title: enum.EggPointCoinToExchangeToTeamEggEnergy.String(), + TransferType: int(enum.EggPointCoinToExchangeToTeamEggEnergy), + CoinId: req.TeamEnergyCoinId, Uid: req.Uid, - Amount: egg_system_rules.StrToFloat64(req.Amount), + Amount: egg_system_rules.StrToFloat64(sign.TotalTeamEggPoints), }) if err != nil { - fmt.Println("ActivityCoinStartExchangeEggPersonEnergy:::::err111:::", err) + fmt.Println("ActivityCoinStartExchangeEggEnergy:::::er222:::", err) _ = session.Rollback() return } + //3、进行兑换购买 + eggEnergyCoreData, cb, err := svc.GetEggEnergyCoreData(engine) + if err != nil { + return err + } + if cb != nil { + defer cb() // 释放锁 + } + //3.1、计算涨价公式 + err1, calcPriceIncreaseFormulaRespForPerson := CalcPriceIncreaseFormula(sign.TotalPersonEggPointsAmountValue, eggEnergyCoreData) + if err1 != nil { + _ = session.Rollback() + return err1 + } + + err1, calcPriceIncreaseFormulaRespForTeam := CalcPriceIncreaseFormula(sign.TotalPersonEggPointsAmountValue, eggEnergyCoreData) + if err1 != nil { + _ = session.Rollback() + return err1 + } + + //3.2、更改动态数据 + err = DealAvailableEggEnergyCoin(session, int(enum2.PersonalActivePointRedemption), eggEnergyCoreData, md.DealAvailableEggEnergyCoinReq{ + Amount: calcPriceIncreaseFormulaRespForPerson.GetEggEnergyNums, + AmountFee: "", + BeforePrice: calcPriceIncreaseFormulaRespForPerson.BeforePrice, + AfterPrice: calcPriceIncreaseFormulaRespForPerson.AfterPrice, + BeforePlanetTotalValue: calcPriceIncreaseFormulaRespForPerson.BeforePlanetTotalValue, + AfterPlanetTotalValue: calcPriceIncreaseFormulaRespForPerson.AfterPlanetTotalValue, + BeforeEnergyTotalNums: calcPriceIncreaseFormulaRespForPerson.BeforeEnergyTotalNums, + AfterEnergyTotalNums: calcPriceIncreaseFormulaRespForPerson.AfterEnergyTotalNums, + }) + if err != nil { + fmt.Println("ActivityCoinStartExchangeEggEnergy:::::er333:::", err) + _ = session.Rollback() + return err + } + err = DealAvailableEggEnergyCoin(session, int(enum2.TeamActivePointRedemption), eggEnergyCoreData, md.DealAvailableEggEnergyCoinReq{ + Amount: calcPriceIncreaseFormulaRespForTeam.GetEggEnergyNums, + AmountFee: "", + BeforePrice: calcPriceIncreaseFormulaRespForTeam.BeforePrice, + AfterPrice: calcPriceIncreaseFormulaRespForTeam.AfterPrice, + BeforePlanetTotalValue: calcPriceIncreaseFormulaRespForTeam.BeforePlanetTotalValue, + AfterPlanetTotalValue: calcPriceIncreaseFormulaRespForTeam.AfterPlanetTotalValue, + BeforeEnergyTotalNums: calcPriceIncreaseFormulaRespForTeam.BeforeEnergyTotalNums, + AfterEnergyTotalNums: calcPriceIncreaseFormulaRespForTeam.AfterEnergyTotalNums, + }) + if err != nil { + fmt.Println("ActivityCoinStartExchangeEggEnergy:::::err444:::", err) + _ = session.Rollback() + return err + } + + //4、修改sign记录 + startTime, _ := egg_system_rules.StringToTime(sign.StartTime) + endTime, _ := egg_system_rules.StringToTime(sign.EndTime) + seconds := endTime.Sub(startTime).Seconds() + secondsValue := decimal.NewFromFloat(seconds) + eggEnergyNumsForPersonValue, _ := decimal.NewFromString(calcPriceIncreaseFormulaRespForPerson.GetEggEnergyNums) + eggEnergyNumsForTeamValue, _ := decimal.NewFromString(calcPriceIncreaseFormulaRespForTeam.GetEggEnergyNums) + sign.EstimatePerSecondPersonEggEnergyValue = eggEnergyNumsForPersonValue.Div(secondsValue).String() + sign.EstimatePerSecondTeamEggEnergyValue = eggEnergyNumsForTeamValue.Div(secondsValue).String() + _, err = eggSignInDb.EggSignInUpdateBySession(session, sign.Id, sign, "estimate_per_second_person_egg_energy_value", "estimate_per_second_team_egg_energy_value") + if err != nil { + fmt.Println("ActivityCoinStartExchangeEggEnergy:::::err555:::", err) + _ = session.Rollback() + return err + } + + err = session.Commit() + if err != nil { + fmt.Println("ActivityCoinStartExchangeEggEnergy:::::err666:::", err) + _ = session.Rollback() + return err + } + //TODO::推入rabbitmq 异步处理 ch.Publish(md.EggEnergyExchange, md.EggEnergyStructForAutoExchangeGreenEnergy{ - Uid: req.Uid, - EnergyCoinId: req.EnergyCoinId, - AutoExchangeNumsAmount: req.AutoExchangeNumsAmount, - StartExchangeTime: req.StartExchangeTime, - EndExchangeTime: req.EndExchangeTime, - }, md.EggEnergyRoutKeyForAutoExchangeGreenEnergyToPerson) - - return session.Commit() + Uid: req.Uid, + SignId: sign.Id, + PersonEggEnergyCoinId: req.PersonEnergyCoinId, + TeamEggEnergyCoinId: req.TeamEggEnergyCoinId, + StartExchangeTime: sign.StartTime, + EndExchangeTime: sign.EndTime, + }, md.EggEnergyRoutKeyForAutoExchangeGreenEnergy) + + return } diff --git a/rule/egg_energy/activity_coin_start_exchange_egg_team_energy.go b/rule/egg_energy/activity_coin_start_exchange_egg_team_energy.go deleted file mode 100644 index b76c33a..0000000 --- a/rule/egg_energy/activity_coin_start_exchange_egg_team_energy.go +++ /dev/null @@ -1,51 +0,0 @@ -package egg_energy - -import ( - "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" - md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md" - "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule" - "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" - egg_system_rules "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils" - zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/logx" - "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" - "fmt" - "xorm.io/xorm" -) - -// ActivityCoinStartExchangeEggTeamEnergy 团队蛋蛋积分【开始】兑换成团队蛋蛋能量 -func ActivityCoinStartExchangeEggTeamEnergy(engine *xorm.Engine, req md.EggEnergyStructForStartExchangeGreenEnergy, ch *rabbit.Channel) (err error) { - session := engine.NewSession() - defer func() { - session.Close() - if err := recover(); err != nil { - _ = zhios_order_relate_logx.Error(err) - } - }() - session.Begin() - - //1、扣除用户对应蛋蛋活跃积分数量 - err = rule.DealUserVirtualCoin(session, md2.DealUserVirtualCoinReq{ - Kind: "sub", - Title: enum.EggPointCoinToExchangeToTeamEggEnergy.String(), - TransferType: int(enum.EggPointCoinToExchangeToTeamEggEnergy), - CoinId: req.PointCoinId, - Uid: req.Uid, - Amount: egg_system_rules.StrToFloat64(req.Amount), - }) - if err != nil { - fmt.Println("ActivityCoinStartExchangeEggTeamEnergy:::::err111:::", err) - _ = session.Rollback() - return - } - - //TODO::推入rabbitmq 异步处理 - ch.Publish(md.EggEnergyExchange, md.EggEnergyStructForAutoExchangeGreenEnergy{ - Uid: req.Uid, - EnergyCoinId: req.EnergyCoinId, - AutoExchangeNumsAmount: req.AutoExchangeNumsAmount, - StartExchangeTime: req.StartExchangeTime, - EndExchangeTime: req.EndExchangeTime, - }, md.EggEnergyRoutKeyForAutoExchangeGreenEnergyToTeam) - - return session.Commit() -} diff --git a/rule/egg_energy/give_activty_coin.go b/rule/egg_energy/give_activty_coin.go index 9b31e0b..c92905a 100644 --- a/rule/egg_energy/give_activty_coin.go +++ b/rule/egg_energy/give_activty_coin.go @@ -9,11 +9,14 @@ import ( md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" egg_system_rules "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils" "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/cache" + "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/es" zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/logx" + es2 "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es" "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "encoding/json" "errors" "fmt" + "github.com/olivere/elastic/v7" "github.com/shopspring/decimal" "strings" "time" @@ -21,7 +24,10 @@ import ( ) // SettlementGiveActivityCoin 结算观看激励广告得到蛋蛋积分 -func SettlementGiveActivityCoin(engine *xorm.Engine, uid int64, ch *rabbit.Channel) (err error) { +func SettlementGiveActivityCoin(engine *xorm.Engine, user model.User, ch *rabbit.Channel) (err error) { + uid := user.Id + now := time.Now() + //1、查找 `egg_energy_basic_setting` 基础设置 eggEnergyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(engine) eggEnergyBasicSetting, err := eggEnergyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{ @@ -45,6 +51,7 @@ func SettlementGiveActivityCoin(engine *xorm.Engine, uid int64, ch *rabbit.Chann err = errors.New("视屏奖励机制设置未完全!") return } + var rewardSystem []*md2.RewardSystemStruct err = json.Unmarshal([]byte(eggEnergyBasicSetting.RewardSystem), &rewardSystem) if err != nil { @@ -54,7 +61,38 @@ func SettlementGiveActivityCoin(engine *xorm.Engine, uid int64, ch *rabbit.Chann err = errors.New("未设置奖励机制!") return } - rewardValue := egg_system_rules.StrToFloat64(videoRewardSystem.RewardValue) //奖励多少个活跃积分 + + rewardValue := egg_system_rules.StrToFloat64(videoRewardSystem.RewardValue) //TODO::奖励多少个活跃积分(根据ecpm, 新用户机制, 蛋蛋分机制) + var newUserRewardRules *md2.NewUserRewardRules + err = json.Unmarshal([]byte(eggEnergyBasicSetting.NewUserIncentiveRules), &newUserRewardRules) + if err != nil { + return + } + userCreateAt := egg_system_rules.String2Time(user.CreateAt) + if int(now.Sub(userCreateAt).Hours()/24) <= newUserRewardRules.ContinueDays { + rewardValue *= newUserRewardRules.RewardCoefficient + } + score := 60.00 + esIndex := es.GetLatestEffectiveIndexFromAlias(now) + esIndexName := md.EggEnergyUserEggScoreEsAlias + "_" + esIndex + results, err1 := es2.FirstDoc(esIndexName, esIndex+"_"+egg_system_rules.Int64ToStr(user.Id)) + if err1 != nil { + if !elastic.IsNotFound(err1) { + return err1 + } + } + if !elastic.IsNotFound(err1) { + var doc md.EggEnergyUserEggScoreEs + err = json.Unmarshal(results.Source, &doc) + if err != nil { + return + } + score = doc.ScoreValue + } + if score > 60 { + //TODO::按照蛋蛋分比重× + } + var rewardSystemMap = map[int]*md2.RewardSystemStruct{} for _, v := range rewardSystem { rewardSystemMap[v.Level] = v diff --git a/rule/egg_energy/home_page_start_sign_in.go b/rule/egg_energy/home_page_start_sign_in.go index 07f13f9..452bd46 100644 --- a/rule/egg_energy/home_page_start_sign_in.go +++ b/rule/egg_energy/home_page_start_sign_in.go @@ -2,7 +2,6 @@ package egg_energy import ( "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" - "code.fnuoos.com/EggPlanet/egg_models.git/src/model" zhios_order_relate_utils "code.fnuoos.com/EggPlanet/egg_models.git/utils" "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" @@ -92,35 +91,7 @@ func HomePageStartSignIn(engine *xorm.Engine, req md.HomePageStartSignInReq) err })) return errors.New("非签到时间," + "请求账号:" + req.Phone + ",请求时间:" + now.Format("2006-01-02 15:04:05") + ",签到时间:" + eggSignIn.EndTime) } - //2、新增 `egg_sign_in` 记录 - endTime := now.Add(time.Hour * time.Duration(oneRoundDuration)).Format("2006-01-02 15:04:05") - _, err = engine.Insert(&model.EggSignIn{ - Uid: req.UID, - StartTime: now.Format("2006-01-02 15:04:05"), - EndTime: endTime, - IsCompleted: 0, - }) - if err != nil { - return err - } - //3、新增 `egg_energy_user_activity` 记录 - var m model.EggEnergyUserActivity - has, err := engine.Where("uid =? and date =?", req.UID, now.Format("2006-01-02")).Get(&m) - if err != nil { - return err - } - if !has { - affected, err1 := engine.Insert(&model.EggEnergyUserActivity{ - Uid: int(req.UID), - Date: now.Format("2006-01-02"), - }) - if err1 != nil { - return err1 - } - if affected <= 0 { - return errors.New("插入活跃记录失败") - } - } + ch, err := rabbit.Cfg.Pool.GetChannel() if err != nil { return err diff --git a/rule/egg_energy/home_page_watch_over_ad.go b/rule/egg_energy/home_page_watch_over_ad.go index b57c075..457468f 100644 --- a/rule/egg_energy/home_page_watch_over_ad.go +++ b/rule/egg_energy/home_page_watch_over_ad.go @@ -223,24 +223,9 @@ func settlementPublicGiveActivityCoin(engine *xorm.Engine, uid int64, ch *rabbit Uid: int64(vv.Uid), Amount: vv.RewardValue, }, md.EggEnergyRoutKeyForDealUserVirtualCoinData) - //err = DealUserCoin(session, md.DealUserCoinReq{ - // Kind: "add", - // Mid: masterId, - // Title: md.OneCirclesWatchAdRewardTeamActiveCoin, - // TransferType: md.OneCirclesWatchAdRewardTeamActiveCoinForUserVirtualCoinFlow, - // OrdId: "", - // CoinId: oneCirclesPublicPlatoonBasicSetting.TeamActivePointsCoinId, - // Uid: vv.Uid, - // Amount: vv.RewardValue, - //}) - //if err != nil { - // _ = session.Rollback() - // fmt.Println("err:::::2222", err) - // return err - //} } - err = session.Commit() - if err != nil { + err11 := session.Commit() + if err11 != nil { _ = session.Rollback() return errors.New("事务提交失败") } diff --git a/rule/egg_energy/md/egg_energy_green_energy.go b/rule/egg_energy/md/egg_energy_green_energy.go index 71c72cb..289d45f 100644 --- a/rule/egg_energy/md/egg_energy_green_energy.go +++ b/rule/egg_energy/md/egg_energy_green_energy.go @@ -92,7 +92,7 @@ type CalcPriceIncreaseFormulaResp struct { } type CalcPriceReductionFormulaResp struct { - GetEggEnergyAmount string `json:"get_egg_energy_amount"` //得到蛋蛋能量的钱 + GetEggEnergyAmount string `json:"get_egg_energy_amount"` //得到蛋蛋能量的钱` BeforePrice string `json:"before_price"` //变更前-价格 AfterPrice string `json:"after_price"` //变更后-价格 BeforePlanetTotalValue string `json:"before_planet_total_value"` //变更前-星球总价值 diff --git a/rule/egg_energy/md/mq_egg_energy.go b/rule/egg_energy/md/mq_egg_energy.go index b4d6068..d48fa3b 100644 --- a/rule/egg_energy/md/mq_egg_energy.go +++ b/rule/egg_energy/md/mq_egg_energy.go @@ -5,10 +5,8 @@ const EggEnergyExchange = "egg.energy" const ( EggEnergyRoutKeyForStarLevelDividend = "star_level_dividend" // 星级分红 EggEnergyRoutKeyForSettlementPublicGiveActivityCoin = "settlement_public_give_activity_coin" // 计算观看激励视屏得到活跃积分 - EggEnergyRoutKeyForStartExchangeGreenEnergyToPerson = "start_exchange_egg_energy_to_person" // 开始兑换个人蛋蛋能量 - EggEnergyRoutKeyForAutoExchangeGreenEnergyToPerson = "auto_exchange_egg_energy_to_person" // 自动兑换个人蛋蛋能量 - EggEnergyRoutKeyForStartExchangeGreenEnergyToTeam = "start_exchange_egg_energy_to_team" // 开始兑换团队蛋蛋能量 - EggEnergyRoutKeyForAutoExchangeGreenEnergyToTeam = "auto_exchange_egg_energy_to_team" // 自动兑换团队蛋蛋能量 + EggEnergyRoutKeyForStartExchangeGreenEnergy = "start_exchange_egg_energy" // 开始兑换蛋蛋能量 + EggEnergyRoutKeyForAutoExchangeGreenEnergy = "auto_exchange_egg_energy" // 自动兑换蛋蛋能量 EggEnergyRoutKeyForEggEnergyFundData = "fund_data" // 资金汇入 EggEnergyRoutKeyForPlatformRevenueData = "platform_revenue_data" // 平台收入 EggEnergyRoutKeyForDealUserVirtualCoinData = "deal_user_virtual_coin" // 处理给用户虚拟币积分 @@ -30,24 +28,25 @@ type EggEnergyStructForStarLevelDividends struct { SignDividend float64 `json:"sign_dividend"` } -type EggEnergyStructForStartExchangeGreenEnergy struct { - Uid int64 `json:"uid"` - Amount string `json:"amount"` - PointCoinId int `json:"point_coin_id"` - EnergyCoinId int `json:"energy_coin_id"` - AutoExchangeNumsAmount string `json:"auto_exchange_nums_amount"` - StartExchangeTime string `json:"start_exchange_time"` //发起兑换时间 - EndExchangeTime string `json:"end_exchange_time"` //截止兑换时间 -} - type EggEnergyStructForAutoExchangeGreenEnergy struct { - Uid int64 `json:"uid"` - EnergyCoinId int `json:"energy_coin_id"` - AutoExchangeNumsAmount string `json:"auto_exchange_nums_amount"` - StartExchangeTime string `json:"start_exchange_time"` //发起兑换时间 - EndExchangeTime string `json:"end_exchange_time"` //截止兑换时间 + Uid int64 `json:"uid"` + SignId int64 `json:"sign_id"` + PersonEggEnergyCoinId int `json:"person_egg_energy_coin_id"` + TeamEggEnergyCoinId int `json:"team_egg_energy_coin_id"` + StartExchangeTime string `json:"start_exchange_time"` //发起兑换时间 + EndExchangeTime string `json:"end_exchange_time"` //截止兑换时间 } +type EggEnergyStructForStartExchangeGreenEnergy struct { + Uid int64 `json:"uid"` + SignId int64 `json:"sign_id"` + PersonEggEnergyCoinId int `json:"person_egg_energy_coin_id"` + TeamEggEnergyCoinId int `json:"team_egg_energy_coin_id"` + PersonPointCoinId int `json:"person_point_coin_id"` + PersonEnergyCoinId int `json:"person_energy_coin_id"` + TeamPointCoinId int `json:"team_point_coin_id"` + TeamEnergyCoinId int `json:"team_energy_coin_id"` +} type EggEnergyStructForPlatformRevenueData struct { ID int `json:"id"` Amount string `json:"amount"` // 资金汇入处理金额