|
- package egg_energy
-
- import (
- "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
- "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/utils/cache"
- zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/logx"
- "errors"
- "fmt"
- "github.com/shopspring/decimal"
- "time"
- "xorm.io/xorm"
- )
-
- const LockKey = "auto_release_exchange_team_egg_energy_lock_key"
-
- // AutoReleaseExchangeTeamEggEnergy 团队蛋蛋能量自动释放成可用绿色能量
- func AutoReleaseExchangeTeamEggEnergy(engine *xorm.Engine, masterId string) (err error) {
- now := time.Now()
- fmt.Println(now.Hour())
- if !(now.Hour() > 1 && now.Hour() < 8) {
- //TODO::只在凌晨一点 ~ 凌晨 8 点运行
- return errors.New("非运行时间")
- }
- //TODO::增加“悲观锁”防止串行
- getString, _ := cache.GetString(LockKey)
- //if err != nil {
- // return err
- //}
- if getString != "" {
- fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完")
- return errors.New("上一次结算未执行完")
- }
- cache.SetEx(LockKey, "running", 3600*8) //8小时
-
- //1、查找`egg_energy_basic_setting` 基础设置
- eggEnergyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(engine)
- eggEnergyBasicSetting, err := eggEnergyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
- "key": "is_open",
- "value": 1,
- })
- if err != nil {
- return
- }
- if eggEnergyBasicSetting.SettlementQuantity == 0 {
- err = errors.New("结算百分比未设置!")
- return
- }
-
- settlementQuantity := eggEnergyBasicSetting.SettlementQuantity
- fmt.Println("settlementQuantity>>>>>>>>>>>>", settlementQuantity)
-
- //2、当前 "可用"绿色能量可以兑换的用户数据
- var list1 []model.UserVirtualAmount
- err = engine.Where("coin_id = ?", eggEnergyBasicSetting.TeamEggEnergyCoinId).And("amount > ?", 0).Find(&list1)
- if err != nil {
- fmt.Println("err:::::1111", err)
- return
- }
-
- //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()
- if settlementQuantityAmount > 0 {
- session := engine.NewSession()
- defer func() {
- session.Close()
- if err := recover(); err != nil {
- _ = zhios_order_relate_logx.Error(err)
- }
- }()
- session.Begin()
-
- //4.2给相应的用户减少团队蛋蛋能量
- err = rule.DealUserVirtualCoin(session, md.DealUserVirtualCoinReq{
- Kind: "sub",
- Title: enum.TeamEggEnergyReleaseToPersonEggEnergy.String(),
- TransferType: int(enum.TeamEggEnergyReleaseToPersonEggEnergy),
- CoinId: eggEnergyBasicSetting.TeamEggEnergyCoinId,
- Uid: v.Uid,
- Amount: settlementQuantityAmount,
- })
- if err != nil {
- _ = session.Rollback()
- fmt.Println("err:::::33333", err)
- continue
- }
-
- //4.3给相应的用户增加个人的蛋蛋能量
- err = rule.DealUserVirtualCoin(session, md.DealUserVirtualCoinReq{
- Kind: "add",
- Title: enum.PersonEggGetByTeamEggEnergyRelease.String(),
- TransferType: int(enum.PersonEggGetByTeamEggEnergyRelease),
- CoinId: eggEnergyBasicSetting.PersonEggEnergyCoinId,
- Uid: v.Uid,
- Amount: settlementQuantityAmount,
- })
- if err != nil {
- _ = session.Rollback()
- fmt.Println("err:::::22222", err)
- continue
- }
- err = session.Commit()
- if err != nil {
- _ = session.Rollback()
- continue
- }
- }
- }
-
- return
- }
|