|
|
@@ -0,0 +1,154 @@ |
|
|
|
package service_award_dividend |
|
|
|
|
|
|
|
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" |
|
|
|
md2 "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" |
|
|
|
zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils" |
|
|
|
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/cache" |
|
|
|
"code.fnuoos.com/go_rely_warehouse/zyos_model.git/src/implement" |
|
|
|
"code.fnuoos.com/go_rely_warehouse/zyos_model.git/src/models" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"github.com/shopspring/decimal" |
|
|
|
"math/rand" |
|
|
|
"time" |
|
|
|
"xorm.io/xorm" |
|
|
|
) |
|
|
|
|
|
|
|
func InitContribution(redisAddr string) (err error) { |
|
|
|
if redisAddr != "" { |
|
|
|
cache.NewRedis(redisAddr) |
|
|
|
} |
|
|
|
_, err = cache.SelectDb(md2.RedisDataBase) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
func CollectContributionExchangeData(eg *xorm.Engine, masterId string, ch *rabbit.Channel) (err error) { |
|
|
|
honeyDividendBasicDb := implement.NewHoneyDividendBasicDb(eg) |
|
|
|
base, _ := honeyDividendBasicDb.GetHoneyDividendBasic() |
|
|
|
if base.IsOpen != 1 { |
|
|
|
err = errors.New("蜂蜜分红未开启!") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
//1、统计当前`现有贡献值数量` |
|
|
|
var sql1 = fmt.Sprintf("SELECT SUM(amount) AS total FROM `user_virtual_amount` WHERE coin_id = %d", base.CoinIdForContribution) |
|
|
|
results1, _ := db.QueryNativeString(eg, sql1) |
|
|
|
nowHoneys := results1[0]["total"] //当前贡献值总量 |
|
|
|
|
|
|
|
//2、统计当前`可以兑换`的用户数量 |
|
|
|
var sql2 = fmt.Sprintf("SELECT COUNT(*)AS total FROM `user_virtual_amount` WHERE coin_id = %d and amount >= %d", base.CoinIdForContribution, base.ContributionExchangeLowerLimit) |
|
|
|
results2, _ := db.QueryNativeString(eg, sql2) |
|
|
|
nowUsers := results2[0]["total"] //当前用户总量 |
|
|
|
|
|
|
|
//3、计算数据 |
|
|
|
honeyContributionExchangeRecords := new(models.HoneyContributionExchangeRecords) |
|
|
|
has, err := eg.Where("state =?", 0).Desc("id").Get(&honeyContributionExchangeRecords) |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
if has == false { |
|
|
|
fmt.Println(">>>>>>>>>>>当前没有需要兑换的记录") |
|
|
|
return nil |
|
|
|
} |
|
|
|
honeyContributionExchangeRecordsAmountValue, _ := decimal.NewFromString(honeyContributionExchangeRecords.Amount) |
|
|
|
nowHoneyValue, _ := decimal.NewFromString(nowHoneys) |
|
|
|
|
|
|
|
//3、进行分红(推入mq处理) |
|
|
|
var page = 1 |
|
|
|
var pageSize = 100 |
|
|
|
var totalValues decimal.Decimal |
|
|
|
for { |
|
|
|
var list []model.UserVirtualAmount |
|
|
|
err = eg.Where("coin_id = ? and amount >= ?", base.CoinId, base.DestroyHoneyValues).Limit(pageSize, (page-1)*pageSize).Asc("id").Find(&list) |
|
|
|
if err != nil { |
|
|
|
fmt.Println("err:::::1111", err) |
|
|
|
return |
|
|
|
} |
|
|
|
if len(list) <= 0 { |
|
|
|
break |
|
|
|
} |
|
|
|
|
|
|
|
for _, v := range list { |
|
|
|
tmpValue, _ := decimal.NewFromString(v.Amount) |
|
|
|
amount := tmpValue.Div(nowHoneyValue).Mul(honeyContributionExchangeRecordsAmountValue).String() |
|
|
|
//TODO::推入rabbitmq 异步处理 |
|
|
|
ch.Publish(md2.HoneyDividendExchange, md2.HoneyContributionExchangeForUserDividend{ |
|
|
|
RecordsId: honeyContributionExchangeRecords.Id, |
|
|
|
MasterId: masterId, |
|
|
|
Uid: v.Uid, |
|
|
|
Amount: amount, |
|
|
|
DestroyValues: v.Amount, |
|
|
|
CoinId: base.CoinId, |
|
|
|
CoinIdForContribution: base.CoinIdForContribution, |
|
|
|
}, md2.HoneyDividendRoutKeyForUserContributionExchange) |
|
|
|
totalValues = totalValues.Add(tmpValue) |
|
|
|
} |
|
|
|
time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000))) |
|
|
|
} |
|
|
|
|
|
|
|
//6、修改分红记录数据 |
|
|
|
honeyContributionExchangeRecords.TotalContributionValues = totalValues.String() |
|
|
|
honeyContributionExchangeRecords.TotalExchangeHoneyValues = nowHoneys |
|
|
|
honeyContributionExchangeRecords.UserNums = zhios_order_relate_utils.StrToInt(nowUsers) |
|
|
|
honeyContributionExchangeRecords.State = 1 |
|
|
|
|
|
|
|
honeyContributionExchangeRecordsDb := implement.NewHoneyContributionExchangeRecordsDb(eg) |
|
|
|
_, err = honeyContributionExchangeRecordsDb.UpdateHoneyContributionExchangeRecords(honeyContributionExchangeRecords, |
|
|
|
"total_contribution_values", "total_exchange_honey_values", "user_nums", "state") |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
func DealContributionExchange(session *xorm.Session, data md2.HoneyContributionExchangeForUserDividend) error { |
|
|
|
//1、进行贡献值销毁 |
|
|
|
err := rule.DealUserCoin(session, md2.DealUserCoinReq{ |
|
|
|
Kind: "sub", |
|
|
|
Mid: data.MasterId, |
|
|
|
Title: md2.HoneyDividendForContributionExchangeSubContribution, |
|
|
|
TransferType: md2.HoneyDividendForContributionExchangeSubContributionForUserVirtualCoinFlow, |
|
|
|
OrdId: "", |
|
|
|
CoinId: data.CoinIdForContribution, |
|
|
|
Uid: data.Uid, |
|
|
|
Amount: zhios_order_relate_utils.StrToFloat64(data.DestroyValues), |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
//2、给用户加上蜂蜜 |
|
|
|
err = rule.DealUserCoin(session, md2.DealUserCoinReq{ |
|
|
|
Kind: "add", |
|
|
|
Mid: data.MasterId, |
|
|
|
Title: md2.HoneyDividendForContributionExchangeAddHoney, |
|
|
|
TransferType: md2.HoneyDividendForContributionExchangeAddHoneyForUserVirtualCoinFlow, |
|
|
|
OrdId: "", |
|
|
|
CoinId: data.CoinId, |
|
|
|
Uid: data.Uid, |
|
|
|
Amount: zhios_order_relate_utils.StrToFloat64(data.Amount), |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
//3、进行数据记录 |
|
|
|
honeyContributionExchangeRecordsWithFlowDb := implement.NewHoneyContributionExchangeRecordsWithFlowDb(nil) |
|
|
|
_, err = honeyContributionExchangeRecordsWithFlowDb.HoneyContributionExchangeRecordsWithFlowInsert(session, &models.HoneyContributionExchangeRecordsWithFlow{ |
|
|
|
RecordsId: int(data.RecordsId), |
|
|
|
Uid: data.Uid, |
|
|
|
Amount: data.Amount, |
|
|
|
DestroyValues: data.DestroyValues, |
|
|
|
CreateAt: time.Now().Format("2006-01-02 15:04:05"), |
|
|
|
UpdateAt: time.Now().Format("2006-01-02 15:04:05"), |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
return nil |
|
|
|
} |