up_status.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/growup/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "go-common/library/net/metadata"
  10. xtime "go-common/library/time"
  11. "golang.org/x/sync/errgroup"
  12. )
  13. func (s *Service) getUpFans(c context.Context, mid int64) (fans int64, err error) {
  14. pfl, err := s.dao.ProfileWithStat(c, mid)
  15. if err != nil {
  16. return
  17. }
  18. fans = int64(pfl.Follower)
  19. return
  20. }
  21. // GetUpStatus status of user in growup plan by mid
  22. func (s *Service) GetUpStatus(c context.Context, mid int64, ip string) (status *model.UpStatus, err error) {
  23. id, err := s.dao.Blocked(c, mid)
  24. if err != nil {
  25. log.Error("s.dao.Blocked mid(%d) error(%v)", mid, err)
  26. return
  27. }
  28. status = &model.UpStatus{}
  29. status.Blocked = id != 0
  30. if status.Blocked {
  31. return
  32. }
  33. status.Status = make([]*model.BusinessStatus, 3)
  34. var g errgroup.Group
  35. g.Go(func() (err error) {
  36. stat, err := s.dao.AvUpStatus(c, mid)
  37. if err != nil {
  38. log.Error("s.dao.AvUpStatus mid(%d) error(%v)", mid, err)
  39. return
  40. }
  41. if stat.AccountState == 0 {
  42. if stat.QuitAt.After(stat.CTime) {
  43. stat.AccountState = 8
  44. }
  45. }
  46. // check av threshold
  47. if stat.AccountState == 3 {
  48. stat.ShowPanel = true
  49. } else {
  50. stat.ShowPanel, err = s.checkAvStat(c, mid, ip)
  51. if err != nil {
  52. log.Error("s.checkAvStat mid(%d) error(%v)", mid, err)
  53. return
  54. }
  55. }
  56. stat.IsWhite = true
  57. stat.Type = 0
  58. status.Status[0] = stat
  59. return
  60. })
  61. g.Go(func() (err error) {
  62. stat, err := s.dao.ColumnUpStatus(c, mid)
  63. if err != nil {
  64. log.Error("s.dao.ColumnUpStatus mid(%d) error(%v)", mid, err)
  65. return
  66. }
  67. stat.IsWhite = true
  68. if stat.AccountState == 3 {
  69. stat.ShowPanel = true
  70. } else {
  71. stat.ShowPanel, err = s.checkArticleStat(c, mid, ip)
  72. if err != nil {
  73. log.Error("s.checkArticleStat mid(%d) error(%v)", mid, err)
  74. return
  75. }
  76. }
  77. stat.Type = 1
  78. status.Status[1] = stat
  79. return
  80. })
  81. g.Go(func() (err error) {
  82. stat, err := s.dao.BgmUpStatus(c, mid)
  83. if err != nil {
  84. return
  85. }
  86. if stat.AccountState == 3 {
  87. stat.ShowPanel = true
  88. } else {
  89. stat.ShowPanel, err = s.checkBgmStat(c, mid)
  90. if err != nil {
  91. return
  92. }
  93. }
  94. stat.Type = 2
  95. status.Status[2] = stat
  96. return
  97. })
  98. err = g.Wait()
  99. return
  100. }
  101. func (s *Service) checkAvStat(c context.Context, mid int64, ip string) (ok bool, err error) {
  102. identify, err := s.dao.UpBusinessInfos(c, mid)
  103. if err != nil {
  104. log.Error("s.dao.UpBusinessInfos mid(%d) error(%v)", mid, err)
  105. return
  106. }
  107. if identify.Archive != 1 {
  108. ok = false
  109. return
  110. }
  111. stat, err := s.avStat(c, mid, ip)
  112. if err != nil {
  113. log.Error("s.dao.AvStat mid(%d) error(%v)", mid, err)
  114. return
  115. }
  116. if stat.Fans >= s.conf.Threshold.LimitFanCnt || stat.View >= s.conf.Threshold.LimitTotalClick {
  117. ok = true
  118. } else {
  119. ok = false
  120. }
  121. return
  122. }
  123. func (s *Service) checkArticleStat(c context.Context, mid int64, ip string) (ok bool, err error) {
  124. stat, err := s.dao.ArticleStat(c, mid, ip)
  125. if err != nil {
  126. log.Error("s.dao.ArticleStat mid(%d) error(%v)", mid, err)
  127. return
  128. }
  129. if stat.View >= s.conf.Threshold.LimitArticleView {
  130. ok = true
  131. } else {
  132. ok = false
  133. }
  134. return
  135. }
  136. func (s *Service) checkBgmStat(c context.Context, mid int64) (ok bool, err error) {
  137. count, err := s.dao.BgmUpCount(c, mid)
  138. if err != nil {
  139. return
  140. }
  141. if count > 0 {
  142. ok = true
  143. return
  144. }
  145. count, err = s.dao.BgmWhiteList(c, mid)
  146. if err != nil {
  147. return
  148. }
  149. if count > 0 {
  150. ok = true
  151. }
  152. return
  153. }
  154. // JoinAv add user to growup plan (video)
  155. func (s *Service) JoinAv(c context.Context, accountType int, mid int64, signType int) (err error) {
  156. id, err := s.dao.Blocked(c, mid)
  157. if err != nil {
  158. log.Error("s.dao.GetBlocked mid(%d) error(%v)", mid, err)
  159. return
  160. }
  161. if id != 0 {
  162. log.Info("mid(%d) is blocked", mid)
  163. return ecode.GrowupDisabled
  164. }
  165. ip := metadata.String(c, metadata.RemoteIP)
  166. ok, err := s.checkAvStat(c, mid, ip)
  167. if err != nil {
  168. log.Error("s.checkAvStat mid(%d) error(%v)", mid, err)
  169. return
  170. }
  171. if !ok {
  172. log.Info("mid(%d) video not reach standard", mid)
  173. return ecode.GrowupDisabled
  174. }
  175. nickname, categoryID, err := s.dao.CategoryInfo(c, mid)
  176. if err != nil {
  177. return
  178. }
  179. fans, err := s.dao.Fans(c, mid)
  180. if err != nil {
  181. return
  182. }
  183. state, err := s.dao.GetAccountState(c, "up_info_video", mid)
  184. if err != nil {
  185. return
  186. }
  187. // if account state is 2 3 4 5 6 7 return
  188. if state >= 2 && state < 8 {
  189. return
  190. }
  191. now := xtime.Time(time.Now().Unix())
  192. // sign_type: 1.basic; 2.first publish; 0:default.
  193. v := &model.UpInfo{
  194. MID: mid,
  195. Nickname: nickname,
  196. AccountType: accountType,
  197. MainCategory: categoryID,
  198. Fans: fans,
  199. AccountState: 2,
  200. SignType: signType,
  201. ApplyAt: now,
  202. }
  203. _, err = s.dao.InsertUpInfo(c, "up_info_video", "total_play_count", v)
  204. return
  205. }
  206. // Quit user quit growup plan
  207. func (s *Service) Quit(c context.Context, mid int64, reason string) (err error) {
  208. var (
  209. tx *sql.Tx
  210. now = time.Now().Unix()
  211. quitAt = xtime.Time(now)
  212. expiredIn = xtime.Time(now + 86400*3)
  213. )
  214. if tx, err = s.dao.BeginTran(c); err != nil {
  215. return
  216. }
  217. nickname, err := s.dao.Nickname(c, mid)
  218. if err != nil {
  219. return
  220. }
  221. current, err := s.dao.CreditScore(c, mid)
  222. if err != nil {
  223. return
  224. }
  225. _, err = s.dao.TxQuit(tx, "up_info_video", mid, quitAt, expiredIn, reason)
  226. if err != nil {
  227. tx.Rollback()
  228. return
  229. }
  230. _, err = s.dao.TxQuit(tx, "up_info_column", mid, quitAt, expiredIn, reason)
  231. if err != nil {
  232. tx.Rollback()
  233. return
  234. }
  235. _, err = s.dao.TxQuit(tx, "up_info_bgm", mid, quitAt, expiredIn, reason)
  236. if err != nil {
  237. tx.Rollback()
  238. return
  239. }
  240. cr := &model.CreditRecord{
  241. MID: mid,
  242. OperateAt: xtime.Time(now),
  243. Operator: nickname,
  244. Reason: 5,
  245. Deducted: 1,
  246. Remaining: current - 1,
  247. }
  248. _, err = s.dao.TxInsertCreditRecord(tx, cr)
  249. if err != nil {
  250. tx.Rollback()
  251. return
  252. }
  253. // quit deduct 1
  254. _, err = s.dao.TxDeductCreditScore(tx, 1, mid)
  255. if err != nil {
  256. tx.Rollback()
  257. return
  258. }
  259. if err = tx.Commit(); err != nil {
  260. log.Error("tx.Commit error(%v)", err)
  261. }
  262. return
  263. }