01_daily_node_profit.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package daily_profit
  2. import (
  3. "app/commons/constant"
  4. "app/commons/core"
  5. "app/commons/core/exchange"
  6. "app/commons/model/entity"
  7. "app/commons/utils"
  8. "fmt"
  9. "github.com/shopspring/decimal"
  10. )
  11. // 节点奖励
  12. func (s *Service) nodeProfitCal(jobDate string) error {
  13. var err error
  14. dailyJob, err := s.FirstSysJob(s.DB().Where("job_date", jobDate))
  15. if err != nil {
  16. return err
  17. }
  18. defer func() {
  19. msg := "节点奖励发放完成"
  20. send := true
  21. if err != nil {
  22. msg = err.Error()
  23. send = false
  24. }
  25. errMsg := fmt.Sprintf("%s:%s", msg, utils.NowTimeSecStr())
  26. if dailyJob.Desc != "" {
  27. errMsg = fmt.Sprintf("%s\n%s", dailyJob.Desc, errMsg)
  28. }
  29. if err = s.DB().Model(&entity.SysJob{}).Where("id", dailyJob.Id).Updates(map[string]interface{}{
  30. "desc": errMsg,
  31. "is_send_ac_profit": send,
  32. }).Error; err != nil {
  33. core.JobLog.Errorf(err.Error())
  34. }
  35. }()
  36. if dailyJob.IsSendNodeProfit {
  37. return nil
  38. }
  39. if dailyJob.TodayStaticProfit.LessThanOrEqual(decimal.Zero) {
  40. return nil
  41. }
  42. dailyProfit := dailyJob.TodayStaticProfit.Mul(decimal.NewFromFloat(0.1))
  43. // 获取所有节点
  44. nodes, err := s.BatchNodeOrder(s.DB().Where("pay_state = ?", entity.NodeOrderPayStateSuccess).Where("state = ?", entity.NodeOrderStateEffective))
  45. if err != nil {
  46. return err
  47. }
  48. count := len(nodes)
  49. if count == 0 {
  50. return nil
  51. }
  52. ratio := decimal.NewFromInt(1).Div(decimal.NewFromInt(int64(count)))
  53. perNodeProfit := dailyProfit.Div(decimal.NewFromInt(int64(count)))
  54. price, err := exchange.GetCurrentSymbolUsdPrice(constant.CoinSymbolTD)
  55. if err != nil {
  56. return err
  57. }
  58. usdProfit := perNodeProfit.Mul(price)
  59. for _, node := range nodes {
  60. bs := constant.BsById(constant.BsPartnerNodeProfit)
  61. bs.ContextValue = fmt.Sprintf("%d", node.Id)
  62. bs.ContextName = node.TableName()
  63. bs.Desc = fmt.Sprintf("%s - 节点总数:%d 占比:%s 奖励:%s", bs.BusinessName, count, ratio, perNodeProfit)
  64. profitInfo := &entity.ProfitInfo{
  65. RawUserId: node.UserId,
  66. RawUserUid: node.Uid,
  67. RewardUserId: node.UserId,
  68. RewardUserUid: node.Uid,
  69. RewardSymbol: constant.CoinSymbolTD,
  70. SymbolUsdPrice: price,
  71. CapitalUsd: dailyProfit,
  72. RewardRatio: ratio,
  73. RewardUsdValue: usdProfit,
  74. RewardQuantity: perNodeProfit,
  75. RewardPeriod: jobDate,
  76. RewardDate: jobDate,
  77. }
  78. profitRecord := s.BuildUserProfitRecord(profitInfo, bs)
  79. profitRecord.State = entity.ProfitSendStateSuccess
  80. txDb := s.DB().Begin()
  81. if err = txDb.Create(&profitRecord).Error; err != nil {
  82. txDb.Rollback()
  83. continue
  84. }
  85. if err = s.GenBillAndActionAsset(txDb, node.UserId, constant.CoinSymbolTD, decimal.Zero.Add(perNodeProfit), decimal.Zero, bs); err != nil {
  86. txDb.Rollback()
  87. continue
  88. }
  89. txDb.Commit()
  90. }
  91. return nil
  92. }