|
|
@@ -20,13 +20,14 @@ import ( |
|
|
|
|
|
|
|
const SettlementStarLevelDividendsLockKey = "settlement_star_level_dividends_lock_key" |
|
|
|
|
|
|
|
func SettlementStarLevelDividendsNew(engine *xorm.Engine, masterId string) (err error) { |
|
|
|
// 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("非运行时间") |
|
|
|
} |
|
|
|
//if !(now.Hour() > 2 && now.Hour() < 8) { |
|
|
|
// //TODO::只在凌晨一点 ~ 凌晨 8 点运行 |
|
|
|
// return errors.New("非运行时间") |
|
|
|
//} |
|
|
|
//TODO::增加“悲观锁”防止串行 |
|
|
|
getString, _ := cache.GetString(SettlementStarLevelDividendsLockKey) |
|
|
|
if getString != "" { |
|
|
@@ -59,12 +60,6 @@ func SettlementStarLevelDividendsNew(engine *xorm.Engine, masterId string) (err |
|
|
|
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) |
|
|
@@ -142,223 +137,78 @@ func SettlementStarLevelDividendsNew(engine *xorm.Engine, masterId string) (err |
|
|
|
} |
|
|
|
|
|
|
|
//4、处理分红 |
|
|
|
var users []*model.User |
|
|
|
err = session.In("uid", userSignInArr).Find(&users) |
|
|
|
if err != nil { |
|
|
|
fmt.Println("err:::::7777777", err) |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
var userSignInArr []int |
|
|
|
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 |
|
|
|
for _, l := range list { |
|
|
|
userSignInArr = append(userSignInArr, l.Uid) |
|
|
|
if len(userSignInArr) >= 10000 { |
|
|
|
var users []*model.User |
|
|
|
err = session.In("uid", userSignInArr).Find(&users) |
|
|
|
if err != nil { |
|
|
|
fmt.Println("err:::::7777777", err) |
|
|
|
return err |
|
|
|
} |
|
|
|
if vipEquitySettingMap[k] != "" && vipLevelUserTotalMap[k]["count"] > 0 { |
|
|
|
dividendValue, _ := decimal.NewFromString(vipEquitySettingMap[k]) |
|
|
|
userTotal := decimal.NewFromInt(vipLevelUserTotalMap[k]["count"]) |
|
|
|
siginDividendValue = siginDividendValue.Add(dividendValue.Div(userTotal)) |
|
|
|
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.OneCirclesStructForStarLevelDividends{ |
|
|
|
MasterId: masterId, |
|
|
|
Uid: item.Uid, |
|
|
|
SignDividend: siginDividend, |
|
|
|
}, md.OneCirclesRoutKeyForStarLevelDividend) |
|
|
|
|
|
|
|
totalDividendValue = totalDividendValue.Add(siginDividendValue) |
|
|
|
} |
|
|
|
} |
|
|
|
userSignInArr = []int{} |
|
|
|
} |
|
|
|
siginDividend, _ := siginDividendValue.Float64() |
|
|
|
if siginDividend > 0 { |
|
|
|
//给相应的用户加上个人的绿色积分(结算数量) |
|
|
|
err = DealUserCoin(session, md.DealUserCoinReq{ |
|
|
|
Kind: "add", |
|
|
|
Mid: masterId, |
|
|
|
Title: md.OneCirclesSettlementStarLevelDividends, |
|
|
|
TransferType: md.OneCirclesSettlementStarLevelDividendsForUserVirtualCoinFlow, |
|
|
|
OrdId: "", |
|
|
|
CoinId: oneCirclesGreenEnergyBasicSetting.TeamGreenEnergyCoinId, |
|
|
|
Uid: item.Uid, |
|
|
|
Amount: siginDividend, |
|
|
|
}) |
|
|
|
} |
|
|
|
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 |
|
|
|
} |
|
|
|
|
|
|
|
//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) |
|
|
|
if len(userSignInArr) > 0 { |
|
|
|
var users []*model.User |
|
|
|
err = session.In("uid", userSignInArr).Find(&users) |
|
|
|
if err != nil { |
|
|
|
fmt.Println("err:::::7777777", err) |
|
|
|
return 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)) |
|
|
|
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.OneCirclesStructForStarLevelDividends{ |
|
|
|
MasterId: masterId, |
|
|
|
Uid: item.Uid, |
|
|
|
SignDividend: siginDividend, |
|
|
|
}, md.OneCirclesRoutKeyForStarLevelDividend) |
|
|
|
siginDividend, _ := siginDividendValue.Float64() |
|
|
|
if siginDividend > 0 { |
|
|
|
//TODO::推入rabbitmq 异步处理 |
|
|
|
ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForStarLevelDividends{ |
|
|
|
MasterId: masterId, |
|
|
|
Uid: item.Uid, |
|
|
|
SignDividend: siginDividend, |
|
|
|
}, md.OneCirclesRoutKeyForStarLevelDividend) |
|
|
|
|
|
|
|
totalDividendValue = totalDividendValue.Add(siginDividendValue) |
|
|
|
totalDividendValue = totalDividendValue.Add(siginDividendValue) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|