settle.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "sync"
  6. "time"
  7. "go-common/app/job/main/coin/dao"
  8. "go-common/app/job/main/coin/model"
  9. "go-common/app/service/main/archive/api"
  10. comarcmdl "go-common/app/service/main/archive/model/archive"
  11. "go-common/library/log"
  12. )
  13. var lockRedo sync.Mutex
  14. var lockSettle sync.Mutex
  15. const (
  16. _expPerCoin = 1
  17. )
  18. func (s *Service) settleproc() {
  19. for {
  20. now := time.Now()
  21. tmp := now.AddDate(0, 1, 0)
  22. // sleep one month
  23. time.Sleep(time.Date(tmp.Year(), tmp.Month(), 1, 0, 0, 0, 0, tmp.Location()).Sub(now))
  24. // settle coin
  25. s.Settle(0)
  26. }
  27. }
  28. // Redo redo settle.
  29. func (s *Service) Redo(tableID int64) (err error) {
  30. lockRedo.Lock()
  31. defer lockRedo.Unlock()
  32. ctx := context.TODO()
  33. // if tableID has been seted than run test
  34. if tableID != 0 {
  35. return s.redo(ctx, tableID)
  36. }
  37. now := time.Now()
  38. day := now.Day()
  39. if day <= 25 {
  40. err = fmt.Errorf("redo must after every 25th of each month, today is %d ", day)
  41. return
  42. }
  43. period, err := s.coinDao.HitSettlePeriod(ctx, now)
  44. if err != nil {
  45. log.Error("job s.coinDao.HitCoinPeriod error(%v)", err)
  46. return
  47. }
  48. return s.redo(ctx, period.ID-1)
  49. }
  50. func (s *Service) redo(ctx context.Context, tableID int64) (err error) {
  51. period, err := s.coinDao.SettlePeriod(ctx, tableID)
  52. if err != nil {
  53. log.Error("s.coinDao.SettlePeriod(%d) error(%v)", tableID, err)
  54. return
  55. }
  56. if err = s.coinDao.ClearCoinCount(ctx, tableID, time.Now()); err != nil {
  57. log.Error("s.coinDao.ClearCoinCount(%d) error(%v)", tableID, err)
  58. return
  59. }
  60. startTime := time.Date(period.FromYear, time.Month(period.FromMonth), period.FromDay, 0, 0, 0, 0, time.Local)
  61. endTime := time.Date(period.ToYear, time.Month(period.ToMonth), period.ToDay, 0, 0, 0, 0, time.Local)
  62. var (
  63. coins map[int64]int64
  64. aids []int64
  65. aidMids map[int64]int64
  66. argAids *comarcmdl.ArgAids2
  67. arcs map[int64]*api.Arc
  68. peer = 100
  69. )
  70. for i := 0; i < dao.SHARDING; i++ {
  71. if coins, err = s.coinDao.TotalCoins(ctx, i, startTime, endTime); err != nil {
  72. log.Error("s.coinDao.TotalCoins(%d, %v, %v) error(%v)", i, startTime, endTime, err)
  73. return
  74. }
  75. aids = make([]int64, 0, len(coins))
  76. for aid := range coins {
  77. if aid%1000 == 1 {
  78. aids = append(aids, aid/1000)
  79. }
  80. }
  81. var (
  82. length = len(aids)
  83. cop = length / peer
  84. mod = length % peer
  85. )
  86. aidMids = make(map[int64]int64, length)
  87. for i := 0; i < cop; i++ {
  88. argAids = &comarcmdl.ArgAids2{
  89. Aids: aids[i*peer : peer*(i+1)],
  90. }
  91. if arcs, err = s.arcRPC.Archives3(ctx, argAids); err != nil {
  92. log.Error("s.arcRPC.Archives2 error(%v)", err)
  93. return
  94. }
  95. for aid, arc := range arcs {
  96. aidMids[aid] = arc.Author.Mid
  97. }
  98. }
  99. if mod != 0 {
  100. argAids = &comarcmdl.ArgAids2{
  101. Aids: aids[cop*peer:],
  102. }
  103. if arcs, err = s.arcRPC.Archives3(ctx, argAids); err != nil {
  104. log.Error("s.arcRPC.Archives2 error(%v)", err)
  105. return
  106. }
  107. for aid, arc := range arcs {
  108. aidMids[aid] = arc.Author.Mid
  109. }
  110. }
  111. for aid, count := range coins {
  112. if mid, ok := aidMids[aid/1000]; ok {
  113. if err = s.coinDao.UpsertSettle(ctx, tableID, mid, aid/1000, aid%1000, count, time.Now()); err != nil {
  114. log.Error("s.coinDao.UpdateCoinCount(%d, %d, %d) error(%v)", tableID, aid, count, err)
  115. return
  116. }
  117. }
  118. }
  119. }
  120. return
  121. }
  122. // Settle do settle by table.
  123. func (s *Service) Settle(tableID int64) (err error) {
  124. lockSettle.Lock()
  125. defer lockSettle.Unlock()
  126. ctx := context.TODO()
  127. // if tableID has been seted than run test
  128. if tableID != 0 {
  129. return s.settle(ctx, tableID)
  130. }
  131. period, err := s.coinDao.HitSettlePeriod(ctx, time.Now())
  132. if err != nil {
  133. log.Error("job s.coinDao.HitCoinPeriod error(%v)", err)
  134. return
  135. }
  136. return s.settle(ctx, period.ID-1)
  137. }
  138. // job exec at every 1th of each month
  139. func (s *Service) settle(ctx context.Context, tableID int64) (err error) {
  140. var (
  141. settles []*model.CoinSettle
  142. )
  143. period, err := s.coinDao.SettlePeriod(ctx, tableID)
  144. if err != nil {
  145. log.Error("s.coinDao.SettlePeriod(%d) error(%v)", tableID, err)
  146. return
  147. }
  148. var i, maxid int64
  149. for {
  150. if settles, maxid, err = s.coinDao.Every10000(ctx, tableID, i); err != nil {
  151. log.Error("settle: job s.coinDao.Every10000(%d) error(%v)", tableID, err)
  152. time.Sleep(time.Second)
  153. continue
  154. }
  155. if maxid == i {
  156. log.Info("settle: maxid %d", maxid)
  157. fmt.Println("maxid", maxid)
  158. return
  159. }
  160. for _, settle := range settles {
  161. if settle.State == 1 || settle.Mid == 0 {
  162. continue
  163. }
  164. settle.ExpTotal = settle.CoinCount*_expPerCoin - settle.ExpSub
  165. if settle.ExpTotal <= 0 {
  166. log.Errorv(ctx, log.KV("log", "settle: ExpTotal err"), log.KV("count", settle.ExpTotal))
  167. continue
  168. }
  169. // add exp
  170. var reason string
  171. switch settle.AvType {
  172. case 1:
  173. reason = fmt.Sprintf("%d.%d-%d.%d视频av%d投币获得奖励", period.FromMonth, period.FromDay, period.ToMonth, period.ToDay, settle.Aid)
  174. case 2:
  175. reason = fmt.Sprintf("%d.%d-%d.%d文章cv%d投币获得奖励", period.FromMonth, period.FromDay, period.ToMonth, period.ToDay, settle.Aid)
  176. case 3:
  177. reason = fmt.Sprintf("%d.%d-%d.%d音乐mv%d投币获得奖励", period.FromMonth, period.FromDay, period.ToMonth, period.ToDay, settle.Aid)
  178. }
  179. for i := 0; i < 3; i++ {
  180. if err = s.addExp(ctx, settle.Mid, float64(settle.ExpTotal), reason, ""); err != nil {
  181. time.Sleep(time.Second)
  182. } else {
  183. break
  184. }
  185. }
  186. if err != nil {
  187. log.Errorv(ctx, log.KV("log", "s.accRPC.AddExp2"), log.KV("mid", settle.Mid), log.KV("err", err))
  188. continue
  189. }
  190. if err = s.coinDao.UpdateSettle(ctx, tableID, settle.ID, settle.ExpTotal, time.Now()); err != nil {
  191. log.Error("settle: s.coinDao.UpdateState(%d, %d, %d) error(%v)", tableID, settle.ID, settle.ExpTotal, err)
  192. continue
  193. }
  194. log.Info("settle: aid(%d) add exp(%d) success", settle.Aid, settle.ExpTotal)
  195. }
  196. fmt.Printf("settle:%d ,len(settle) %d\n", i, len(settles))
  197. i = maxid
  198. }
  199. }