@@ -23,7 +23,9 @@ func ActivityCoinAutoExchangeEggPersonEnergy(engine *xorm.Engine, req md.EggEner | |||||
now := time.Now() | now := time.Now() | ||||
endTimeValue, _ := egg_system_rules.StringToTime(req.EndExchangeTime) | endTimeValue, _ := egg_system_rules.StringToTime(req.EndExchangeTime) | ||||
if endTimeValue.After(now) { | if endTimeValue.After(now) { | ||||
time.Sleep(time.Microsecond * time.Duration(rand.Intn(5))) //TODO::随机休眠5s不等 | |||||
seconds := time.Duration(rand.Intn(10)) | |||||
fmt.Println("休眠:::::", seconds) | |||||
time.Sleep(time.Second * seconds) //TODO::随机休眠10s不等 | |||||
return errors.New("未到可执行时间~~~") | return errors.New("未到可执行时间~~~") | ||||
} | } | ||||
@@ -142,15 +142,31 @@ func ActivityCoinReadyExchangeEggEnergy(engine *xorm.Engine, uid int64, ch *rabb | |||||
} | } | ||||
//TODO::推入rabbitmq 异步处理 | //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) | |||||
if egg_system_rules.StrToFloat64(autoExchangeNumsByPersonAmountValue) > 0 { | |||||
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) | |||||
} | |||||
if egg_system_rules.StrToFloat64(autoExchangeNumsByTeamAmountValue) > 0 { | |||||
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 | return | ||||
} | } |
@@ -71,20 +71,14 @@ func ActivityCoinStartExchangeEggEnergy(engine *xorm.Engine, req md.EggEnergyStr | |||||
if cb != nil { | if cb != nil { | ||||
defer cb() // 释放锁 | defer cb() // 释放锁 | ||||
} | } | ||||
//3.1、计算涨价公式 | |||||
err1, calcPriceIncreaseFormulaRespForPerson := CalcPriceIncreaseFormula(sign.TotalPersonEggPointsAmountValue, eggEnergyCoreData) | |||||
if err1 != nil { | |||||
_ = session.Rollback() | |||||
return err1 | |||||
} | |||||
err1, calcPriceIncreaseFormulaRespForTeam := CalcPriceIncreaseFormula(sign.TotalTeamEggPointsAmountValue, eggEnergyCoreData) | |||||
//3.1、"个人" 购买 | |||||
err1, calcPriceIncreaseFormulaRespForPerson := CalcPriceIncreaseFormula(sign.TotalPersonEggPointsAmountValue, eggEnergyCoreData) | |||||
if err1 != nil { | if err1 != nil { | ||||
_ = session.Rollback() | _ = session.Rollback() | ||||
return err1 | return err1 | ||||
} | } | ||||
//3.2、更改动态数据 | |||||
err = DealAvailableEggEnergyCoin(session, int(enum2.PersonalActivePointRedemption), eggEnergyCoreData, md.DealAvailableEggEnergyCoinReq{ | err = DealAvailableEggEnergyCoin(session, int(enum2.PersonalActivePointRedemption), eggEnergyCoreData, md.DealAvailableEggEnergyCoinReq{ | ||||
Amount: calcPriceIncreaseFormulaRespForPerson.GetEggEnergyNums, | Amount: calcPriceIncreaseFormulaRespForPerson.GetEggEnergyNums, | ||||
AmountFee: "", | AmountFee: "", | ||||
@@ -100,6 +94,13 @@ func ActivityCoinStartExchangeEggEnergy(engine *xorm.Engine, req md.EggEnergyStr | |||||
_ = session.Rollback() | _ = session.Rollback() | ||||
return err | return err | ||||
} | } | ||||
//3.2、"团队" 购买 | |||||
err1, calcPriceIncreaseFormulaRespForTeam := CalcPriceIncreaseFormula(sign.TotalTeamEggPointsAmountValue, eggEnergyCoreData) | |||||
if err1 != nil { | |||||
_ = session.Rollback() | |||||
return err1 | |||||
} | |||||
err = DealAvailableEggEnergyCoin(session, int(enum2.TeamActivePointRedemption), eggEnergyCoreData, md.DealAvailableEggEnergyCoinReq{ | err = DealAvailableEggEnergyCoin(session, int(enum2.TeamActivePointRedemption), eggEnergyCoreData, md.DealAvailableEggEnergyCoinReq{ | ||||
Amount: calcPriceIncreaseFormulaRespForTeam.GetEggEnergyNums, | Amount: calcPriceIncreaseFormulaRespForTeam.GetEggEnergyNums, | ||||
AmountFee: "", | AmountFee: "", | ||||
@@ -4,27 +4,33 @@ import ( | |||||
"code.fnuoos.com/EggPlanet/egg_models.git/src/model" | "code.fnuoos.com/EggPlanet/egg_models.git/src/model" | ||||
"code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" | "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" | ||||
egg_system_rules "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils" | egg_system_rules "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils" | ||||
"fmt" | |||||
"github.com/shopspring/decimal" | "github.com/shopspring/decimal" | ||||
) | ) | ||||
// CalcPriceIncreaseFormula 计算涨价公式(【用户资金 ÷(用户资金+星球价值)÷ 现行数量】 = 用户获得蛋蛋能量个数) | // CalcPriceIncreaseFormula 计算涨价公式(【用户资金 ÷(用户资金+星球价值)÷ 现行数量】 = 用户获得蛋蛋能量个数) | ||||
func CalcPriceIncreaseFormula(userAmountValue string, eggEnergyCoreData *model.EggEnergyCoreData) (err error, resp md.CalcPriceIncreaseFormulaResp) { | func CalcPriceIncreaseFormula(userAmountValue string, eggEnergyCoreData *model.EggEnergyCoreData) (err error, resp md.CalcPriceIncreaseFormulaResp) { | ||||
userAmount, _ := decimal.NewFromString(userAmountValue) //用户资金 | |||||
nowPrice, _ := decimal.NewFromString(eggEnergyCoreData.NowPrice) //当前价格 | |||||
nowEnergyTotalNums, _ := decimal.NewFromString(eggEnergyCoreData.NowEnergyTotalNums) //现行数量 | |||||
planetTotalValue, _ := decimal.NewFromString(eggEnergyCoreData.PlanetTotalValue) //星球价值 | |||||
afterPrice := (planetTotalValue.Add(userAmount)).Div(nowEnergyTotalNums).Truncate(8) //涨价后的价格 | |||||
getEggEnergyNumsValue := userAmount.Div(afterPrice).Truncate(8) //用户得到绿色能量个数 | |||||
afterEnergyTotalNumsValue := nowEnergyTotalNums.Add(getEggEnergyNumsValue) //变更后-现行数量 | |||||
afterPlanetTotalValue := afterEnergyTotalNumsValue.Mul(afterPrice) //变更后-星球价值 | |||||
userAmount, _ := decimal.NewFromString(userAmountValue) //用户资金 | |||||
nowPrice, _ := decimal.NewFromString(eggEnergyCoreData.NowPrice) //当前价格 | |||||
nowEnergyTotalNums, _ := decimal.NewFromString(eggEnergyCoreData.NowEnergyTotalNums) //现行数量 | |||||
planetTotalValue, _ := decimal.NewFromString(eggEnergyCoreData.PlanetTotalValue) //星球价值 | |||||
afterPrice := (planetTotalValue.Add(userAmount)).Div(nowEnergyTotalNums).Truncate(16) //涨价后的价格 | |||||
fmt.Println("userAmount:::", userAmount.String()) | |||||
fmt.Println("nowEnergyTotalNums:::", nowEnergyTotalNums.String()) | |||||
fmt.Println("planetTotalValue:::", planetTotalValue.String()) | |||||
fmt.Println("afterPrice:::", afterPrice.String()) | |||||
getEggEnergyNumsValue := userAmount.Div(afterPrice).Truncate(8) //用户得到绿色能量个数 | |||||
afterEnergyTotalNumsValue := nowEnergyTotalNums.Add(getEggEnergyNumsValue) //变更后-现行数量 | |||||
afterPlanetTotalValue := afterEnergyTotalNumsValue.Mul(afterPrice) //变更后-星球价值 | |||||
fmt.Println("afterPlanetTotalValue:::", afterPlanetTotalValue.String()) | |||||
resp.GetEggEnergyNums = getEggEnergyNumsValue.String() | resp.GetEggEnergyNums = getEggEnergyNumsValue.String() | ||||
resp.BeforePrice = nowPrice.String() | resp.BeforePrice = nowPrice.String() | ||||
resp.AfterPrice = afterPrice.String() | resp.AfterPrice = afterPrice.String() | ||||
resp.BeforePlanetTotalValue = eggEnergyCoreData.PlanetTotalValue | resp.BeforePlanetTotalValue = eggEnergyCoreData.PlanetTotalValue | ||||
resp.AfterPlanetTotalValue = afterPlanetTotalValue.String() | |||||
resp.AfterPlanetTotalValue = afterPlanetTotalValue.Truncate(8).String() | |||||
resp.BeforeEnergyTotalNums = eggEnergyCoreData.NowEnergyTotalNums | resp.BeforeEnergyTotalNums = eggEnergyCoreData.NowEnergyTotalNums | ||||
resp.AfterEnergyTotalNums = afterEnergyTotalNumsValue.String() | |||||
resp.AfterEnergyTotalNums = afterEnergyTotalNumsValue.Truncate(8).String() | |||||
egg_system_rules.FilePutContents("CalcPriceIncreaseFormula", egg_system_rules.SerializeStr(resp)) | egg_system_rules.FilePutContents("CalcPriceIncreaseFormula", egg_system_rules.SerializeStr(resp)) | ||||
return | return | ||||
} | } | ||||
@@ -39,7 +45,7 @@ func CalcPriceReductionFormula(userExchangeNumsValue string, eggEnergyCoreData * | |||||
afterEnergyTotalNumsValue := nowEnergyTotalNums.Sub(userExchangeNums) //变更后-现行数量 | afterEnergyTotalNumsValue := nowEnergyTotalNums.Sub(userExchangeNums) //变更后-现行数量 | ||||
afterPlanetTotalValue := afterEnergyTotalNumsValue.Mul(afterPrice) //变更后-星球价值 | afterPlanetTotalValue := afterEnergyTotalNumsValue.Mul(afterPrice) //变更后-星球价值 | ||||
resp.GetEggEnergyAmount = greenEnergyValues.Truncate(8).String() | |||||
resp.GetEggEnergyAmount = greenEnergyValues.Truncate(16).String() | |||||
resp.BeforePrice = eggEnergyCoreData.NowPrice | resp.BeforePrice = eggEnergyCoreData.NowPrice | ||||
resp.AfterPrice = afterPrice.String() | resp.AfterPrice = afterPrice.String() | ||||
resp.BeforePlanetTotalValue = eggEnergyCoreData.PlanetTotalValue | resp.BeforePlanetTotalValue = eggEnergyCoreData.PlanetTotalValue | ||||
@@ -36,7 +36,7 @@ func DealAvailableEggEnergyCoin(session *xorm.Session, kind int, eggEnergyCoreDa | |||||
eggEnergyAvailableEnergyFlow.BeforeNowEnergyTotalNums = req.BeforeEnergyTotalNums //现行总量 | eggEnergyAvailableEnergyFlow.BeforeNowEnergyTotalNums = req.BeforeEnergyTotalNums //现行总量 | ||||
eggEnergyAvailableEnergyFlow.AfterNowEnergyTotalNums = req.AfterEnergyTotalNums | eggEnergyAvailableEnergyFlow.AfterNowEnergyTotalNums = req.AfterEnergyTotalNums | ||||
eggEnergyAvailableEnergyFlow.BeforePlanetTotalValue = req.BeforePlanetTotalValue //星球总价值 | eggEnergyAvailableEnergyFlow.BeforePlanetTotalValue = req.BeforePlanetTotalValue //星球总价值 | ||||
eggEnergyAvailableEnergyFlow.AfterNowEnergyTotalNums = req.AfterPlanetTotalValue | |||||
eggEnergyAvailableEnergyFlow.AfterPlanetTotalValue = req.AfterPlanetTotalValue | |||||
eggEnergyAvailableEnergyFlow.CreateTime = now.Format("2006-01-02 15:04:05") | eggEnergyAvailableEnergyFlow.CreateTime = now.Format("2006-01-02 15:04:05") | ||||
switch kind { | switch kind { | ||||
@@ -273,6 +273,16 @@ func DealAvailableEggEnergyCoin(session *xorm.Session, kind int, eggEnergyCoreDa | |||||
break | break | ||||
} | } | ||||
eggEnergyCoreData.NowPrice = req.AfterPrice | |||||
eggEnergyCoreData.PlanetTotalValue = eggEnergyAvailableEnergyFlow.AfterPlanetTotalValue | |||||
eggEnergyCoreData.NowEnergyTotalNums = eggEnergyAvailableEnergyFlow.AfterNowEnergyTotalNums | |||||
eggEnergyCoreData.MarketplaceMerchantNums = eggEnergyAvailableEnergyFlow.AfterMarketplaceMerchantValues | |||||
eggEnergyCoreData.MarketplaceMerchantFunds = eggEnergyAvailableEnergyFlow.AfterMarketplaceMerchantFundValues | |||||
eggEnergyCoreData.DevelopmentCommittee = eggEnergyAvailableEnergyFlow.AfterDevelopmentCommitteeValues | |||||
eggEnergyCoreData.PublicWelfareAndCharity = eggEnergyAvailableEnergyFlow.AfterPublicWelfareAndCharityValues | |||||
eggEnergyCoreData.StarLevelDividends = eggEnergyAvailableEnergyFlow.AfterStarLevelDividendsValues | |||||
eggEnergyCoreData.CommunityDividends = eggEnergyAvailableEnergyFlow.AfterCommunityDividends | |||||
eggEnergyCoreData.DestructionQuantityNums = eggEnergyAvailableEnergyFlow.AfterDestructionQuantityValues | |||||
//更新 `egg_energy_core_data` 表 | //更新 `egg_energy_core_data` 表 | ||||
eggEnergyCoreDataDb := implement.NewEggEnergyCoreDataDb(session.Engine()) | eggEnergyCoreDataDb := implement.NewEggEnergyCoreDataDb(session.Engine()) | ||||
_, err := eggEnergyCoreDataDb.EggEnergyCoreDataUpdateBySession(session, eggEnergyCoreData.Id, eggEnergyCoreData, | _, err := eggEnergyCoreDataDb.EggEnergyCoreDataUpdateBySession(session, eggEnergyCoreData.Id, eggEnergyCoreData, | ||||