Kaynağa Gözat

update 一个圈圈

master
DengBiao 9 ay önce
ebeveyn
işleme
2021e04aaa
2 değiştirilmiş dosya ile 186 ekleme ve 2 silme
  1. +1
    -0
      md/mq.go
  2. +185
    -2
      rule/one_circles/one_circles_star_level_dividends.go

+ 1
- 0
md/mq.go Dosyayı Görüntüle

@@ -4,6 +4,7 @@ const OneCirclesExchange = "one.circles"

const (
OneCirclesRoutKeyForSignIn = "sign_in" // 签到
OneCirclesRoutKeyForStarLevelDividend = "star_level_dividend" // 星级分红
OneCirclesRoutKeyForAutoExchangeGreenEnergyToPerson = "auto_exchange_green_energy_to_person" // 自动兑换个人绿色能量
OneCirclesRoutKeyForAutoExchangeGreenEnergyToTeam = "auto_exchange_green_energy_to_team" // 自动兑换团队绿色能量
)


+ 185
- 2
rule/one_circles/one_circles_star_level_dividends.go Dosyayı Görüntüle

@@ -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"
@@ -19,8 +20,7 @@ import (

const SettlementStarLevelDividendsLockKey = "settlement_star_level_dividends_lock_key"

// SettlementStarLevelDividends 绿色能量分红
func SettlementStarLevelDividends(engine *xorm.Engine, masterId string) (err error) {
func SettlementStarLevelDividendsNew(engine *xorm.Engine, masterId string) (err error) {
now := time.Now()
fmt.Println(now.Hour())
if !(now.Hour() > 2 && now.Hour() < 8) {
@@ -184,6 +184,189 @@ 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
}

//6、 减少“星级分红”中的绿色能量
totalDividend, _ := totalDividendValue.Float64()
err = DealAvailableGreenEnergyCoin(session, int(enum.SettlementStarLevelDividends), totalDividend, 0, enum.SettlementStarLevelDividends.String(), oneCirclesGreenEnergyBasicSetting, oneCirclesGreenEnergyBasicSetting.NowPrice)
if err != nil {
_ = session.Rollback()
fmt.Println("err:::::9999999", err)
return err
}
err = session.Commit()
if err != nil {
_ = session.Rollback()
return errors.New("事务提交失败")
}
return
}

// SettlementStarLevelDividends 绿色能量分红
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) {
//TODO::只在凌晨一点 ~ 凌晨 8 点运行
return errors.New("非运行时间")
}
//TODO::增加“悲观锁”防止串行
getString, _ := cache.GetString(SettlementStarLevelDividendsLockKey)
if getString != "" {
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完")
return errors.New("上一次结算未执行完")
}
cache.SetEx(SettlementStarLevelDividendsLockKey, "running", 3600*8) //8小时

//1、查找 `one_circles_green_energy_basic_setting` 基础设置
oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{
"key": "is_open",
"value": 1,
})
if err != nil {
return
}

isLimitDividend := oneCirclesGreenEnergyBasicSetting.IsLimitDividend
if isLimitDividend != 1 {
return errors.New("必须开启限制分红!")
}
startDate := now.AddDate(0, 0, -1).Format("2006-01-02") + " 00:00:00"
endDate := now.Format("2006-01-02") + " 00:00:00"
var list []model.OneCirclesGreenEnergySignIn
err = engine.Where("start_time >= ?", startDate).And("start_time <=?", endDate).Find(&list)
if err != nil {
fmt.Println("err:::::1111", err)
return
}
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)
if err != nil {
fmt.Println("err:::::22222", err)
return
}

allUserLevel, err := db.UserLevlEgAll(engine)
if err != nil {
return err
}
var allUserLevelMap = map[int]model.UserLevel{}
for _, v := range allUserLevel {
allUserLevelMap[v.Id] = *v
}

session := engine.NewSession()
defer func() {
session.Close()
if err := recover(); err != nil {
_ = zhios_order_relate_logx.Error(err)
}
}()
session.Begin()

//2、查找 `one_circles_star_level_dividends_records` 基础设置
oneCirclesStarLevelDividendsRecords, err := db.OneCirclesStarLevelDividendsRecordsGetOneByParamsBySession(session, map[string]interface{}{
"key": "date",
"value": now.AddDate(0, 0, -1).Format("2006-01-02"),
})
if err != nil {
fmt.Println("err:::::33333", err)
return
}
if oneCirclesStarLevelDividendsRecords == nil {
return errors.New("今日无分红积分!")
}

//3、统计各等级人数
dividendAmountValue, _ := decimal.NewFromString(oneCirclesStarLevelDividendsRecords.Amount)
decimalRate := decimal.NewFromInt(100) //百分比
var vipLevelUserTotalMap = map[string]map[string]int64{}
var vipEquitySettingMap = map[string]string{}
for _, v := range vipEquitySetting {
dividendRatioValue, _ := decimal.NewFromString(v.DividendRatio)
dividendRatioValue = dividendRatioValue.Div(decimalRate) //分红比例
vipEquitySettingMap[v.VipLevelId] = dividendAmountValue.Mul(dividendRatioValue).String() //TODO::计算各会员等级能得到多少分红

userLevel, err1 := db.UserLevelByID(engine, v.VipLevelId)
if err1 != nil {
fmt.Println("err:::::444444", err1)
return err1
}
var ms []*model.UserLevel
err1 = session.Where("is_use=1").Where("level_weight > ?", userLevel.LevelWeight).Find(&ms)
if err1 != nil {
fmt.Println("err:::::55555", err1)
return err1
}

var tmpVipLevelId = []string{v.VipLevelId}
for _, m := range ms {
tmpVipLevelId = append(tmpVipLevelId, zhios_order_relate_utils.IntToStr(m.Id))
}
var users model.User
count, err1 := session.In("level", tmpVipLevelId).Count(&users)
if err1 != nil {
fmt.Println("err:::::66666", err1)
return err1
}
vipLevelUserTotalMap[v.VipLevelId] = map[string]int64{}
vipLevelUserTotalMap[v.VipLevelId]["count"] = count
vipLevelUserTotalMap[v.VipLevelId]["weight"] = int64(userLevel.LevelWeight)
}

//4、处理分红
var users []*model.User
err = session.In("uid", userSignInArr).Find(&users)
if err != nil {
fmt.Println("err:::::7777777", err)
return err
}

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
}
if vipEquitySettingMap[k] != "" && vipLevelUserTotalMap[k]["count"] > 0 {
dividendValue, _ := decimal.NewFromString(vipEquitySettingMap[k])
userTotal := decimal.NewFromInt(vipLevelUserTotalMap[k]["count"])
siginDividendValue = siginDividendValue.Add(dividendValue.Div(userTotal))
}
}

siginDividend, _ := siginDividendValue.Float64()
if siginDividend > 0 {
//TODO::推入rabbitmq 异步处理
ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForAutoExchangeGreenEnergy{
MasterId: masterId,
Uid: item.Uid,
}, md.OneCirclesRoutKeyForStarLevelDividend)

totalDividendValue = totalDividendValue.Add(siginDividendValue)
}
}

//5、更新 one_circles_star_level_dividends_records 记录
oneCirclesStarLevelDividendsRecords.AlreadyDividendsAmount = totalDividendValue.String()
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
}


Yükleniyor…
İptal
Kaydet