| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package daily_profit
- import (
- "app/commons/constant"
- "app/commons/core"
- "app/commons/core/exchange"
- "app/commons/model/entity"
- "app/commons/utils"
- "fmt"
- "github.com/shopspring/decimal"
- )
- // 节点奖励
- func (s *Service) nodeProfitCal(jobDate string) error {
- var err error
- dailyJob, err := s.FirstSysJob(s.DB().Where("job_date", jobDate))
- if err != nil {
- return err
- }
- defer func() {
- msg := "节点奖励发放完成"
- send := true
- if err != nil {
- msg = err.Error()
- send = false
- }
- errMsg := fmt.Sprintf("%s:%s", msg, utils.NowTimeSecStr())
- if dailyJob.Desc != "" {
- errMsg = fmt.Sprintf("%s\n%s", dailyJob.Desc, errMsg)
- }
- if err = s.DB().Model(&entity.SysJob{}).Where("id", dailyJob.Id).Updates(map[string]interface{}{
- "desc": errMsg,
- "is_send_ac_profit": send,
- }).Error; err != nil {
- core.JobLog.Errorf(err.Error())
- }
- }()
- if dailyJob.IsSendNodeProfit {
- return nil
- }
- if dailyJob.TodayStaticProfit.LessThanOrEqual(decimal.Zero) {
- return nil
- }
- dailyProfit := dailyJob.TodayStaticProfit.Mul(decimal.NewFromFloat(0.1))
- // 获取所有节点
- nodes, err := s.BatchNodeOrder(s.DB().Where("pay_state = ?", entity.NodeOrderPayStateSuccess).Where("state = ?", entity.NodeOrderStateEffective))
- if err != nil {
- return err
- }
- count := len(nodes)
- if count == 0 {
- return nil
- }
- ratio := decimal.NewFromInt(1).Div(decimal.NewFromInt(int64(count)))
- perNodeProfit := dailyProfit.Div(decimal.NewFromInt(int64(count)))
- price, err := exchange.GetCurrentSymbolUsdPrice(constant.CoinSymbolTD)
- if err != nil {
- return err
- }
- usdProfit := perNodeProfit.Mul(price)
- for _, node := range nodes {
- bs := constant.BsById(constant.BsPartnerNodeProfit)
- bs.ContextValue = fmt.Sprintf("%d", node.Id)
- bs.ContextName = node.TableName()
- bs.Desc = fmt.Sprintf("%s - 节点总数:%d 占比:%s 奖励:%s", bs.BusinessName, count, ratio, perNodeProfit)
- profitInfo := &entity.ProfitInfo{
- RawUserId: node.UserId,
- RawUserUid: node.Uid,
- RewardUserId: node.UserId,
- RewardUserUid: node.Uid,
- RewardSymbol: constant.CoinSymbolTD,
- SymbolUsdPrice: price,
- CapitalUsd: dailyProfit,
- RewardRatio: ratio,
- RewardUsdValue: usdProfit,
- RewardQuantity: perNodeProfit,
- RewardPeriod: jobDate,
- RewardDate: jobDate,
- }
- profitRecord := s.BuildUserProfitRecord(profitInfo, bs)
- profitRecord.State = entity.ProfitSendStateSuccess
- txDb := s.DB().Begin()
- if err = txDb.Create(&profitRecord).Error; err != nil {
- txDb.Rollback()
- continue
- }
- if err = s.GenBillAndActionAsset(txDb, node.UserId, constant.CoinSymbolTD, decimal.Zero.Add(perNodeProfit), decimal.Zero, bs); err != nil {
- txDb.Rollback()
- continue
- }
- txDb.Commit()
- }
- return nil
- }
|