@@ -9,6 +9,7 @@ const ( | |||||
SignInReward | SignInReward | ||||
AccountBalanceExchange | AccountBalanceExchange | ||||
GreenEnergyExchangeBalance | GreenEnergyExchangeBalance | ||||
TeamActivePointRedemption | |||||
) | ) | ||||
func (kind OneCirclesAvailableGreenEnergyPointsFlowKind) String() string { | func (kind OneCirclesAvailableGreenEnergyPointsFlowKind) String() string { | ||||
@@ -23,6 +24,8 @@ func (kind OneCirclesAvailableGreenEnergyPointsFlowKind) String() string { | |||||
return "账户余额兑换" | return "账户余额兑换" | ||||
case GreenEnergyExchangeBalance: | case GreenEnergyExchangeBalance: | ||||
return "绿色能量兑换余额" | return "绿色能量兑换余额" | ||||
case TeamActivePointRedemption: | |||||
return "团队活跃积分兑换" | |||||
default: | default: | ||||
return "未知状态" | return "未知状态" | ||||
} | } | ||||
@@ -12,3 +12,8 @@ type RewardSystemStruct struct { | |||||
RewardCondition string `json:"reward_condition"` //奖励条件 | RewardCondition string `json:"reward_condition"` //奖励条件 | ||||
RewardValue string `json:"reward_value"` //奖励值 | RewardValue string `json:"reward_value"` //奖励值 | ||||
} | } | ||||
type ExchangeRulesStruct struct { | |||||
AutoExchangeNumsByPerson string `json:"auto_exchange_nums_by_person"` //个人数量X个,自动兑换可用绿色能量 | |||||
AutoExchangeNumsByTeam string `json:"auto_exchange_nums_by_team"` //团队量X个,自动兑换结算绿色能量 | |||||
} |
@@ -0,0 +1,189 @@ | |||||
package one_circles | |||||
import ( | |||||
"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" | |||||
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/enum" | |||||
md2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/md" | |||||
zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils" | |||||
"encoding/json" | |||||
"errors" | |||||
"fmt" | |||||
"github.com/shopspring/decimal" | |||||
"xorm.io/xorm" | |||||
) | |||||
// 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).Find(&list1) | |||||
if err != nil { | |||||
fmt.Println("err:::::1111", err) | |||||
return | |||||
} | |||||
err = engine.Where("coin_id = ?", oneCirclesPublicPlatoonBasicSetting.TeamActivePointsCoinId).And("amount >=?", autoExchangeNumsByTeam).Find(&list2) | |||||
if err != nil { | |||||
fmt.Println("err:::::2222", err) | |||||
return | |||||
} | |||||
session := engine.NewSession() | |||||
//4、处理"个人活跃积分"兑换 | |||||
autoExchangeNumsByPersonValue := decimal.NewFromFloat(autoExchangeNumsByPerson) | |||||
autoExchangeNumsByPersonAmount := autoExchangeNumsByPersonValue.Div(personActivePointsCoinExchangeRatioValue).String() | |||||
for _, v := range list1 { | |||||
//4.1计算涨价公式 | |||||
err1, values, _, afterPriceValue := NewCalcPriceIncreaseFormula(autoExchangeNumsByPersonAmount, oneCirclesGreenEnergyBasicSetting) | |||||
if err1 != nil { | |||||
return err1 | |||||
} | |||||
oneCirclesGreenEnergyBasicSetting.NowPrice = afterPriceValue //TODO::改变当前价格 | |||||
//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), | |||||
}) | |||||
//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(autoExchangeNumsByPersonAmount), | |||||
}) | |||||
if err != nil { | |||||
_ = session.Rollback() | |||||
fmt.Println("err:::::33333", err) | |||||
return err | |||||
} | |||||
//4.4 减少“原始数量”中的绿色能量 | |||||
err = DealAvailableGreenEnergyCoin(session, int(enum.PersonalActivePointRedemption), zhios_order_relate_utils.StrToFloat64(values), enum.PersonalActivePointRedemption.String(), oneCirclesGreenEnergyBasicSetting, oneCirclesGreenEnergyBasicSetting.NowPrice) | |||||
if err != nil { | |||||
_ = session.Rollback() | |||||
fmt.Println("err:::::44444", err) | |||||
return err | |||||
} | |||||
} | |||||
//5、处理"团队活跃积分"兑换 | |||||
autoExchangeNumsByTeamValue := decimal.NewFromFloat(autoExchangeNumsByTeam) | |||||
autoExchangeNumsByTeamAmount := autoExchangeNumsByTeamValue.Div(teamActivePointsCoinExchangeRatioValue).String() | |||||
for _, v := range list2 { | |||||
//5.1计算涨价公式 | |||||
err1, values, _, afterPriceValue := NewCalcPriceIncreaseFormula(autoExchangeNumsByTeamAmount, oneCirclesGreenEnergyBasicSetting) | |||||
if err1 != nil { | |||||
return err1 | |||||
} | |||||
oneCirclesGreenEnergyBasicSetting.NowPrice = afterPriceValue //TODO::改变当前价格 | |||||
//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), | |||||
}) | |||||
//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(autoExchangeNumsByPersonAmount), | |||||
}) | |||||
if err != nil { | |||||
_ = session.Rollback() | |||||
fmt.Println("err:::::66666", err) | |||||
return err | |||||
} | |||||
//5.4 减少“原始数量”中的绿色能量 | |||||
err = DealAvailableGreenEnergyCoin(session, int(enum.TeamActivePointRedemption), zhios_order_relate_utils.StrToFloat64(values), enum.TeamActivePointRedemption.String(), oneCirclesGreenEnergyBasicSetting, oneCirclesGreenEnergyBasicSetting.NowPrice) | |||||
if err != nil { | |||||
_ = session.Rollback() | |||||
fmt.Println("err:::::44444", err) | |||||
return err | |||||
} | |||||
} | |||||
//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) | |||||
return err | |||||
} | |||||
err = session.Commit() | |||||
if err != nil { | |||||
_ = session.Rollback() | |||||
return errors.New("事务提交失败") | |||||
} | |||||
return | |||||
} |
@@ -0,0 +1,80 @@ | |||||
package one_circles | |||||
import ( | |||||
"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" | |||||
"errors" | |||||
"fmt" | |||||
"github.com/shopspring/decimal" | |||||
"xorm.io/xorm" | |||||
) | |||||
// AutoReleaseExchangeGreenEnergy 结算绿色能量自动释放成可用绿色能量 | |||||
func AutoReleaseExchangeGreenEnergy(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 | |||||
} | |||||
settlementQuantity := oneCirclesGreenEnergyBasicSetting.SettlementQuantity | |||||
fmt.Println("settlementQuantity>>>>>>>>>>>>", settlementQuantity) | |||||
//2、当前 "可用"绿色能量可以兑换的用户数据 | |||||
var list1 []model.UserVirtualAmount | |||||
err = engine.Where("coin_id = ?", oneCirclesGreenEnergyBasicSetting.TeamGreenEnergyCoinId).And("amount >=?", 0).Find(&list1) | |||||
if err != nil { | |||||
fmt.Println("err:::::1111", err) | |||||
return | |||||
} | |||||
session := engine.NewSession() | |||||
//4、处理释放 | |||||
settlementQuantityRadio := decimal.NewFromFloat(100) | |||||
settlementQuantityValue := decimal.NewFromInt(int64(settlementQuantity)).Div(settlementQuantityRadio) | |||||
for _, v := range list1 { | |||||
userAmount, _ := decimal.NewFromString(v.Amount) | |||||
settlementQuantityAmount, _ := userAmount.Mul(settlementQuantityValue).Float64() | |||||
//4.2给相应的用户加上个人的绿色积分(可用数量) | |||||
err = DealUserCoin(session, md.DealUserCoinReq{ | |||||
Kind: "add", | |||||
Mid: masterId, | |||||
Title: md.OneCirclesSettlementGreenEnergyExchangeGreenEnergy, | |||||
TransferType: md.OneCirclesSettlementGreenEnergyExchangeGreenEnergyForUserVirtualCoinFlow, | |||||
OrdId: "", | |||||
CoinId: oneCirclesGreenEnergyBasicSetting.PersonGreenEnergyCoinId, | |||||
Uid: v.Uid, | |||||
Amount: settlementQuantityAmount, | |||||
}) | |||||
//4.2给相应的用户减少个人的绿色积分(结算数量) | |||||
err = DealUserCoin(session, md.DealUserCoinReq{ | |||||
Kind: "sub", | |||||
Mid: masterId, | |||||
Title: md.OneCirclesSettlementGreenEnergyExchangeTobeGreenEnergy, | |||||
TransferType: md.OneCirclesSettlementGreenEnergyExchangeTobeGreenEnergyForUserVirtualCoinFlow, | |||||
OrdId: "", | |||||
CoinId: oneCirclesGreenEnergyBasicSetting.TeamGreenEnergyCoinId, | |||||
Uid: v.Uid, | |||||
Amount: settlementQuantityAmount, | |||||
}) | |||||
if err != nil { | |||||
_ = session.Rollback() | |||||
fmt.Println("err:::::33333", err) | |||||
return err | |||||
} | |||||
} | |||||
err = session.Commit() | |||||
if err != nil { | |||||
_ = session.Rollback() | |||||
return errors.New("事务提交失败") | |||||
} | |||||
return | |||||
} |
@@ -2,6 +2,7 @@ package one_circles | |||||
import ( | import ( | ||||
"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" | ||||
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model" | |||||
md2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/md" | md2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/md" | ||||
"encoding/json" | "encoding/json" | ||||
"errors" | "errors" | ||||
@@ -31,6 +32,20 @@ func CalcPriceIncreaseFormula(engine *xorm.Engine, userAmountValue string) (err | |||||
return | return | ||||
} | } | ||||
// NewCalcPriceIncreaseFormula 计算涨价公式(【用户资金 ÷(用户资金+原始资金)÷ 原始数量】 = 用户获得绿色能量个数) | |||||
func NewCalcPriceIncreaseFormula(userAmountValue string, oneCirclesGreenEnergyBasicSetting *model.OneCirclesGreenEnergyBasicSetting) (err error, values, nowPriceValue, afterPriceValue string) { | |||||
userAmount, _ := decimal.NewFromString(userAmountValue) //用户资金 | |||||
nowPrice, _ := decimal.NewFromString(oneCirclesGreenEnergyBasicSetting.NowPrice) //当前价格 | |||||
originalQuantityNums, _ := decimal.NewFromString(oneCirclesGreenEnergyBasicSetting.OriginalQuantityNums) //原始数量 | |||||
originalQuantityFunds := originalQuantityNums.Mul(nowPrice) //原始资金 | |||||
afterPrice := (userAmount.Add(originalQuantityFunds)).Div(originalQuantityNums).Truncate(8) //涨价后的价格 | |||||
values = userAmount.Div(afterPrice).Truncate(8).String() //用户得到绿色能量个数 | |||||
nowPriceValue = nowPrice.String() | |||||
afterPriceValue = afterPrice.String() | |||||
return | |||||
} | |||||
// CalcPriceReductionFormula 计算降价公式(【用户需兑换绿能色量数量*{原始资金÷(用户需兑换绿色数量+原始数量}*(1 - 扣比例50% ~ 23%) = 用户获得绿色能量个数) | // 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) (err error, values, greenEnergy, greenEnergyFee, nowPriceValue, afterPriceValue string) { | ||||
//1、查找 `one_circles_green_energy_basic_setting` 基础设置 | //1、查找 `one_circles_green_energy_basic_setting` 基础设置 | ||||
@@ -47,6 +47,30 @@ func DealAvailableGreenEnergyCoin(session *xorm.Session, kind int, amount float6 | |||||
oneCirclesAvailableGreenEnergyPointsFlow.BeforeDestructionQuantityValues = chain.DestructionQuantityNums //销毁数量 | oneCirclesAvailableGreenEnergyPointsFlow.BeforeDestructionQuantityValues = chain.DestructionQuantityNums //销毁数量 | ||||
oneCirclesAvailableGreenEnergyPointsFlow.AfterDestructionQuantityValues = chain.DestructionQuantityNums | oneCirclesAvailableGreenEnergyPointsFlow.AfterDestructionQuantityValues = chain.DestructionQuantityNums | ||||
break | break | ||||
case int(enum.TeamActivePointRedemption): //团队活跃积分兑换 | |||||
beforeOriginalQuantity, _ := decimal.NewFromString(chain.OriginalQuantityNums) | |||||
oneCirclesAvailableGreenEnergyPointsFlow.Direction = 1 | |||||
oneCirclesAvailableGreenEnergyPointsFlow.BeforeEcologicalApplicationValues = chain.EcologicalApplication //生态应用 | |||||
oneCirclesAvailableGreenEnergyPointsFlow.AfterEcologicalApplicationValues = chain.EcologicalApplication | |||||
oneCirclesAvailableGreenEnergyPointsFlow.BeforeTechnicalTeamValues = chain.TotalTechnologyTeam //技术团队 | |||||
oneCirclesAvailableGreenEnergyPointsFlow.AfterTechnicalTeamValues = chain.TotalTechnologyTeam | |||||
oneCirclesAvailableGreenEnergyPointsFlow.BeforeOperateTeamValues = chain.TotalOperateTeam //运营团队 | |||||
oneCirclesAvailableGreenEnergyPointsFlow.AfterOperateTeamValues = chain.TotalOperateTeam | |||||
oneCirclesAvailableGreenEnergyPointsFlow.BeforeActiveGiveawaysValues = chain.TotalActiveGiveaways //活跃赠送 | |||||
oneCirclesAvailableGreenEnergyPointsFlow.AfterActiveGiveawaysValues = chain.TotalActiveGiveaways | |||||
oneCirclesAvailableGreenEnergyPointsFlow.BeforeOriginalQuantityValues = chain.OriginalQuantityNums //原始数量 | |||||
oneCirclesAvailableGreenEnergyPointsFlow.AfterOriginalQuantityValues = beforeOriginalQuantity.Sub(amountValue).RoundFloor(8).String() | |||||
oneCirclesAvailableGreenEnergyPointsFlow.BeforeMarketplaceMerchantValues = chain.MarketplaceMerchantNums //市商数量 | |||||
oneCirclesAvailableGreenEnergyPointsFlow.AfterMarketplaceMerchantValues = chain.MarketplaceMerchantNums | |||||
oneCirclesAvailableGreenEnergyPointsFlow.BeforeDevelopmentCommitteeValues = chain.DevelopmentCommittee //发展委员会 | |||||
oneCirclesAvailableGreenEnergyPointsFlow.AfterDevelopmentCommitteeValues = chain.DevelopmentCommittee | |||||
oneCirclesAvailableGreenEnergyPointsFlow.BeforePublicWelfareAndCharityValues = chain.PublicWelfareAndCharity //公益慈善 | |||||
oneCirclesAvailableGreenEnergyPointsFlow.AfterPublicWelfareAndCharityValues = chain.PublicWelfareAndCharity | |||||
oneCirclesAvailableGreenEnergyPointsFlow.BeforeStarLevelDividendsValues = chain.StarLevelDividends //星级分红 | |||||
oneCirclesAvailableGreenEnergyPointsFlow.AfterStarLevelDividendsValues = chain.StarLevelDividends | |||||
oneCirclesAvailableGreenEnergyPointsFlow.BeforeDestructionQuantityValues = chain.DestructionQuantityNums //销毁数量 | |||||
oneCirclesAvailableGreenEnergyPointsFlow.AfterDestructionQuantityValues = chain.DestructionQuantityNums | |||||
break | |||||
case int(enum.SettlementOfGreenEnergyRelease): //结算绿色能量释放 | case int(enum.SettlementOfGreenEnergyRelease): //结算绿色能量释放 | ||||
oneCirclesAvailableGreenEnergyPointsFlow.Direction = 1 | oneCirclesAvailableGreenEnergyPointsFlow.Direction = 1 | ||||
oneCirclesAvailableGreenEnergyPointsFlow.BeforeEcologicalApplicationValues = chain.EcologicalApplication //生态应用 | oneCirclesAvailableGreenEnergyPointsFlow.BeforeEcologicalApplicationValues = chain.EcologicalApplication //生态应用 | ||||