| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- 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) stakeRankingProfitCal(jobDate string) error {
- dailyJob, err := s.FirstSysJob(s.DB().Where("job_date", jobDate))
- if err != nil {
- return err
- }
- if dailyJob.IsSendStakeProfit {
- return nil
- }
- 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_stake_profit": send,
- }).Error; err != nil {
- core.JobLog.Errorf(err.Error())
- }
- }()
- if dailyJob.TodayStaticProfit.LessThanOrEqual(decimal.Zero) {
- return nil
- }
- dailyProfit := dailyJob.TodayStaticProfit
- quotas, err := s.AllUserQuota(s.DB().Where("person_achievement > 0").Order("person_achievement desc").Limit(33))
- if err != nil {
- return err
- }
- // 每日总手续费 规则:第一名20%,其余按小区业绩加权分50%
- totalAc := decimal.Zero
- for i, quota := range quotas {
- if i == 0 {
- continue
- }
- totalAc = totalAc.Add(quota.PersonAchievement)
- }
- price, err := exchange.GetCurrentSymbolUsdPrice(constant.CoinSymbolTD)
- if err != nil {
- return err
- }
- for i, quota := range quotas {
- ratio := quota.PersonAchievement.Div(totalAc).Mul(decimal.NewFromFloat(0.2)) // 业绩占比 * 总比例0.2
- if i == 0 {
- ratio = decimal.NewFromFloat(0.1) // 第一名独占10%
- }
- profit := dailyProfit.Mul(ratio) //
- usdProfit := profit.Mul(price)
- bs := constant.BsById(constant.BsStakeRankingProfit)
- bs.ContextValue = fmt.Sprintf("%d", quota.UserId)
- bs.ContextName = quota.TableName()
- bs.Desc = fmt.Sprintf("%s - 第%d名 总手续费:%s 占比:%s 奖励:%s", bs.BusinessName, i+1, dailyProfit, ratio, profit)
- profitInfo := &entity.ProfitInfo{
- RawUserId: quota.UserId,
- RawUserUid: quota.Uid,
- RewardUserId: quota.UserId,
- RewardUserUid: quota.Uid,
- RewardSymbol: constant.CoinSymbolTD,
- SymbolUsdPrice: price,
- CapitalUsd: dailyProfit,
- RewardRatio: ratio,
- RewardUsdValue: usdProfit,
- RewardQuantity: profit,
- 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, quota.UserId, constant.CoinSymbolTD, decimal.Zero.Add(profit), decimal.Zero, bs); err != nil {
- txDb.Rollback()
- continue
- }
- txDb.Commit()
- }
- return nil
- }
|