coin.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/web/model"
  6. arcmdl "go-common/app/service/main/archive/api"
  7. "go-common/app/service/main/archive/model/archive"
  8. coinmdl "go-common/app/service/main/coin/api"
  9. thumbup "go-common/app/service/main/thumbup/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. "go-common/library/net/metadata"
  13. )
  14. var _emptyCoinArcList = make([]*model.CoinArc, 0)
  15. // Coins get archive User added coins.
  16. func (s *Service) Coins(c context.Context, mid, aid int64) (res *model.ArchiveUserCoins, err error) {
  17. var rs *coinmdl.ItemUserCoinsReply
  18. if rs, err = s.coinClient.ItemUserCoins(c, &coinmdl.ItemUserCoinsReq{Mid: mid, Aid: aid, Business: model.CoinArcBusiness}); err != nil {
  19. log.Error("s.coinClient.ItemUserCoins(%d,%d) error(%v)", mid, aid, err)
  20. return
  21. }
  22. res = new(model.ArchiveUserCoins)
  23. if rs != nil {
  24. res.Multiply = rs.Number
  25. }
  26. return
  27. }
  28. // AddCoin add coin to archive.
  29. func (s *Service) AddCoin(c context.Context, aid, mid, upID, multiply, avtype int64, business, ck, ua, refer string, now time.Time, selectLike int) (like bool, err error) {
  30. var (
  31. pubTime int64
  32. typeID int32
  33. maxCoin int64 = 2
  34. ip = metadata.String(c, metadata.RemoteIP)
  35. )
  36. switch avtype {
  37. case model.CoinAddArcType:
  38. var a *arcmdl.ArcReply
  39. if a, err = s.arcClient.Arc(c, &arcmdl.ArcRequest{Aid: aid}); err != nil {
  40. log.Error("s.arcClient.Arc(%v) error(%v)", aid, err)
  41. return
  42. }
  43. if !a.Arc.IsNormal() {
  44. err = ecode.ArchiveNotExist
  45. return
  46. }
  47. if a.Arc.Copyright == int32(archive.CopyrightCopy) {
  48. maxCoin = 1
  49. }
  50. upID = a.Arc.Author.Mid
  51. typeID = a.Arc.TypeID
  52. pubTime = int64(a.Arc.PubDate)
  53. case model.CoinAddArtType:
  54. maxCoin = 1
  55. }
  56. arg := &coinmdl.AddCoinReq{
  57. IP: ip,
  58. Mid: mid,
  59. Upmid: upID,
  60. MaxCoin: maxCoin,
  61. Aid: aid,
  62. Business: business,
  63. Number: multiply,
  64. Typeid: typeID,
  65. PubTime: pubTime,
  66. }
  67. if _, err = s.coinClient.AddCoin(c, arg); err == nil && avtype == model.CoinAddArcType && selectLike == 1 {
  68. if err = s.thumbup.Like(c, &thumbup.ArgLike{Mid: mid, UpMid: upID, Business: _businessLike, MessageID: aid, Type: thumbup.TypeLike, RealIP: ip}); err != nil {
  69. log.Error("AddCoin s.thumbup.Like mid(%d) upID(%d) aid(%d) error(%+v)", mid, upID, aid, err)
  70. err = nil
  71. } else {
  72. like = true
  73. }
  74. }
  75. return
  76. }
  77. // CoinExp get coin exp today
  78. func (s *Service) CoinExp(c context.Context, mid int64) (exp int64, err error) {
  79. var todayExp *coinmdl.TodayExpReply
  80. if todayExp, err = s.coinClient.TodayExp(c, &coinmdl.TodayExpReq{Mid: mid}); err != nil {
  81. log.Error("CoinExp s.coinClient.TodayExp mid(%d) error(%v)", mid, err)
  82. err = nil
  83. return
  84. }
  85. exp = todayExp.Exp
  86. return
  87. }
  88. // CoinList get coin list.
  89. func (s *Service) CoinList(c context.Context, mid int64, pn, ps int) (list []*model.CoinArc, count int, err error) {
  90. var (
  91. coinReply *coinmdl.ListReply
  92. aids []int64
  93. arcsReply *arcmdl.ArcsReply
  94. )
  95. if coinReply, err = s.coinClient.List(c, &coinmdl.ListReq{Mid: mid, Business: model.CoinArcBusiness, Ts: time.Now().Unix()}); err != nil {
  96. log.Error("CoinList s.coinClient.List(%d) error(%v)", mid, err)
  97. err = nil
  98. list = _emptyCoinArcList
  99. return
  100. }
  101. existAids := make(map[int64]int64, len(coinReply.List))
  102. afVideos := make(map[int64]*coinmdl.ModelList, len(coinReply.List))
  103. for _, v := range coinReply.List {
  104. if _, ok := existAids[v.Aid]; ok {
  105. afVideos[v.Aid].Number += v.Number
  106. continue
  107. }
  108. afVideos[v.Aid] = v
  109. aids = append(aids, v.Aid)
  110. existAids[v.Aid] = v.Aid
  111. }
  112. count = len(aids)
  113. start := (pn - 1) * ps
  114. end := pn * ps
  115. switch {
  116. case start > count:
  117. aids = aids[:0]
  118. case end >= count:
  119. aids = aids[start:]
  120. default:
  121. aids = aids[start:end]
  122. }
  123. if len(aids) == 0 {
  124. list = _emptyCoinArcList
  125. return
  126. }
  127. if arcsReply, err = s.arcClient.Arcs(c, &arcmdl.ArcsRequest{Aids: aids}); err != nil {
  128. log.Error("CoinList s.arcClient.Arcs(%v) error(%v)", aids, err)
  129. err = nil
  130. list = _emptyCoinArcList
  131. return
  132. }
  133. for _, aid := range aids {
  134. if arc, ok := arcsReply.Arcs[aid]; ok && arc.IsNormal() {
  135. if arc.Access >= 10000 {
  136. arc.Stat.View = 0
  137. }
  138. if item, ok := afVideos[aid]; ok {
  139. list = append(list, &model.CoinArc{Arc: arc, Coins: item.Number, Time: item.Ts})
  140. }
  141. }
  142. }
  143. if len(list) == 0 {
  144. list = _emptyCoinArcList
  145. }
  146. return
  147. }