| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 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"
- )
- // 小区业绩奖励
- // 总手续费的70%
- func (s *Service) achievementRankingProfitCal(jobDate string) error {
- dailyJob, err := s.FirstSysJob(s.DB().Where("job_date", jobDate))
- if err != nil {
- return err
- }
- if dailyJob.IsSendAcProfit {
- 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_node_profit": send,
- }).Error; err != nil {
- core.JobLog.Errorf(err.Error())
- }
- }()
- if dailyJob.TodayFeeProfit.LessThanOrEqual(decimal.Zero) {
- return nil
- }
- dailyFeeProfit := dailyJob.TodayFeeProfit
- quotas, err := s.AllUserQuota(s.DB().Where("few_team_achievement > 0").Order("few_team_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.FewTeamAchievement)
- }
- price, err := exchange.GetCurrentSymbolUsdPrice(constant.CoinSymbolTD)
- if err != nil {
- return err
- }
- for i, quota := range quotas {
- ratio := quota.FewTeamAchievement.Div(totalAc).Mul(decimal.NewFromFloat(0.5)) // 业绩占比 * 总比例0.5
- if i == 0 {
- ratio = decimal.NewFromFloat(0.2) // 第一名独占20%
- }
- profit := dailyFeeProfit.Mul(ratio) //
- usdProfit := profit.Mul(price)
- bs := constant.BsById(constant.BsAchievementRankingProfit)
- bs.ContextValue = fmt.Sprintf("%d", quota.UserId)
- bs.ContextName = quota.TableName()
- bs.Desc = fmt.Sprintf("%s - 第%d名 总手续费:%s 占比:%s 奖励:%s", bs.BusinessName, i+1, dailyFeeProfit, ratio, profit)
- profitInfo := &entity.ProfitInfo{
- RawUserId: quota.UserId,
- RawUserUid: quota.Uid,
- RewardUserId: quota.UserId,
- RewardUserUid: quota.Uid,
- RewardSymbol: constant.CoinSymbolTD,
- SymbolUsdPrice: price,
- CapitalUsd: dailyFeeProfit,
- 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
- }
|