02_achievement_ranking.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // 总手续费的70%
  13. func (s *Service) achievementRankingProfitCal(jobDate string) error {
  14. dailyJob, err := s.FirstSysJob(s.DB().Where("job_date", jobDate))
  15. if err != nil {
  16. return err
  17. }
  18. if dailyJob.IsSendAcProfit {
  19. return nil
  20. }
  21. defer func() {
  22. msg := "业绩排行奖励发放完成"
  23. send := true
  24. if err != nil {
  25. msg = err.Error()
  26. send = false
  27. }
  28. errMsg := fmt.Sprintf("%s:%s", msg, utils.NowTimeSecStr())
  29. if dailyJob.Desc != "" {
  30. errMsg = fmt.Sprintf("%s\n%s", dailyJob.Desc, errMsg)
  31. }
  32. if err = s.DB().Model(&entity.SysJob{}).Where("id", dailyJob.Id).Updates(map[string]interface{}{
  33. "desc": errMsg,
  34. "is_send_node_profit": send,
  35. }).Error; err != nil {
  36. core.JobLog.Errorf(err.Error())
  37. }
  38. }()
  39. if dailyJob.TodayFeeProfit.LessThanOrEqual(decimal.Zero) {
  40. return nil
  41. }
  42. dailyFeeProfit := dailyJob.TodayFeeProfit
  43. quotas, err := s.AllUserQuota(s.DB().Where("few_team_achievement > 0").Order("few_team_achievement desc").Limit(33))
  44. if err != nil {
  45. return err
  46. }
  47. // 每日总手续费 规则:第一名20%,其余按小区业绩加权分50%
  48. totalAc := decimal.Zero
  49. for i, quota := range quotas {
  50. if i == 0 {
  51. continue
  52. }
  53. totalAc = totalAc.Add(quota.FewTeamAchievement)
  54. }
  55. price, err := exchange.GetCurrentSymbolUsdPrice(constant.CoinSymbolTD)
  56. if err != nil {
  57. return err
  58. }
  59. for i, quota := range quotas {
  60. ratio := quota.FewTeamAchievement.Div(totalAc).Mul(decimal.NewFromFloat(0.5)) // 业绩占比 * 总比例0.5
  61. if i == 0 {
  62. ratio = decimal.NewFromFloat(0.2) // 第一名独占20%
  63. }
  64. profit := dailyFeeProfit.Mul(ratio) //
  65. usdProfit := profit.Mul(price)
  66. bs := constant.BsById(constant.BsAchievementRankingProfit)
  67. bs.ContextValue = fmt.Sprintf("%d", quota.UserId)
  68. bs.ContextName = quota.TableName()
  69. bs.Desc = fmt.Sprintf("%s - 第%d名 总手续费:%s 占比:%s 奖励:%s", bs.BusinessName, i+1, dailyFeeProfit, ratio, profit)
  70. profitInfo := &entity.ProfitInfo{
  71. RawUserId: quota.UserId,
  72. RawUserUid: quota.Uid,
  73. RewardUserId: quota.UserId,
  74. RewardUserUid: quota.Uid,
  75. RewardSymbol: constant.CoinSymbolTD,
  76. SymbolUsdPrice: price,
  77. CapitalUsd: dailyFeeProfit,
  78. RewardRatio: ratio,
  79. RewardUsdValue: usdProfit,
  80. RewardQuantity: profit,
  81. RewardPeriod: jobDate,
  82. RewardDate: jobDate,
  83. }
  84. profitRecord := s.BuildUserProfitRecord(profitInfo, bs)
  85. profitRecord.State = entity.ProfitSendStateSuccess
  86. txDb := s.DB().Begin()
  87. if err = txDb.Create(&profitRecord).Error; err != nil {
  88. txDb.Rollback()
  89. continue
  90. }
  91. if err = s.GenBillAndActionAsset(txDb, quota.UserId, constant.CoinSymbolTD, decimal.Zero.Add(profit), decimal.Zero, bs); err != nil {
  92. txDb.Rollback()
  93. continue
  94. }
  95. txDb.Commit()
  96. }
  97. return nil
  98. }