bnj.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package view
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/app-view/model/elec"
  6. "go-common/app/interface/main/app-view/model/view"
  7. "go-common/app/service/main/archive/model/archive"
  8. thumbup "go-common/app/service/main/thumbup/model"
  9. "go-common/library/conf/env"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. "go-common/library/sync/errgroup"
  13. )
  14. // CheckAccess check
  15. func (s *Service) CheckAccess(mid int64) bool {
  16. if !s.BnjIsGrey {
  17. return true
  18. }
  19. _, ok := s.BnjWhiteMid[mid]
  20. if !ok && env.DeployEnv == env.DeployEnvProd {
  21. log.Error("mid(%d) env(%s) not allow", mid, env.DeployEnv)
  22. return false
  23. }
  24. return true
  25. }
  26. func (s *Service) initBnjPages(c context.Context, ps []*archive.Page3) (pages []*view.Page) {
  27. for _, v := range ps {
  28. page := &view.Page{}
  29. metas := make([]*view.Meta, 0, 4)
  30. for q, r := range _rate {
  31. meta := &view.Meta{
  32. Quality: q,
  33. Size: int64(float64(r*v.Duration) * 1.1 / 8.0),
  34. }
  35. metas = append(metas, meta)
  36. }
  37. page.Page3 = v
  38. page.Metas = metas
  39. page.DMLink = fmt.Sprintf(_dmformat, v.Cid)
  40. pages = append(pages, page)
  41. }
  42. return
  43. }
  44. // initBnjReqUser is
  45. func (s *Service) initBnjReqUser(c context.Context, authorMid, aid, mid int64) (reqUser *view.ReqUser, err error) {
  46. reqUser = &view.ReqUser{Favorite: 0, Attention: -999, Like: 0, Dislike: 0}
  47. if mid == 0 {
  48. return
  49. }
  50. g := errgroup.Group{}
  51. g.Go(func() error {
  52. var is bool
  53. if is, _ = s.favDao.IsFav(context.Background(), mid, aid); is {
  54. reqUser.Favorite = 1
  55. }
  56. return nil
  57. })
  58. g.Go(func() error {
  59. res, err := s.thumbupDao.HasLike(context.Background(), mid, _businessLike, []int64{aid})
  60. if err != nil {
  61. log.Error("s.thumbupDao.HasLike err(%+v)", err)
  62. return nil
  63. }
  64. if res.States == nil {
  65. return nil
  66. }
  67. if typ, ok := res.States[aid]; ok {
  68. if typ.State == thumbup.StateLike {
  69. reqUser.Like = 1
  70. } else if typ.State == thumbup.StateDislike {
  71. reqUser.Dislike = 1
  72. }
  73. }
  74. return nil
  75. })
  76. g.Go(func() (err error) {
  77. res, err := s.coinDao.ArchiveUserCoins(context.Background(), aid, mid, _avTypeAv)
  78. if err != nil {
  79. log.Error("%+v", err)
  80. err = nil
  81. }
  82. if res != nil && res.Multiply > 0 {
  83. reqUser.Coin = 1
  84. }
  85. return
  86. })
  87. if authorMid > 0 {
  88. g.Go(func() error {
  89. fl, err := s.accDao.Following3(context.Background(), mid, authorMid)
  90. if err != nil {
  91. log.Error("%+v", err)
  92. return nil
  93. }
  94. if fl {
  95. reqUser.Attention = 1
  96. }
  97. return nil
  98. })
  99. }
  100. g.Wait()
  101. return
  102. }
  103. // Bnj2019 is
  104. func (s *Service) Bnj2019(c context.Context, mid int64, relateID int64) (bnj *view.BnjMain, err error) {
  105. if s.BnjMainView == nil || !s.BnjMainView.IsNormal() {
  106. err = ecode.NothingFound
  107. return
  108. }
  109. bnj = new(view.BnjMain)
  110. bnj.ElecSmallText = s.c.Bnj2019.ElecSmallText
  111. bnj.ElecBigText = s.c.Bnj2019.ElecBigText
  112. bnj.Archive3 = s.BnjMainView.Archive3
  113. bnj.ReqUser = &view.ReqUser{}
  114. bnj.Elec = s.BnjElecInfo
  115. bnj.Pages = s.initBnjPages(c, s.BnjMainView.Pages)
  116. bnj.ReqUser, _ = s.initBnjReqUser(c, bnj.Author.Mid, bnj.Aid, mid)
  117. bnj.PlayerIcon = s.playerIcon
  118. bnj.Elec = s.BnjElecInfo
  119. for _, a := range s.BnjLists {
  120. relate := &view.BnjItem{
  121. Aid: a.Aid,
  122. Cid: a.FirstCid,
  123. Tid: a.TypeID,
  124. Pic: a.Pic,
  125. Copyright: a.Copyright,
  126. PubDate: a.PubDate,
  127. Title: a.Title,
  128. Desc: a.Desc,
  129. Stat: a.Stat,
  130. Duration: a.Duration,
  131. Author: a.Author,
  132. Dimension: a.Dimension,
  133. Rights: a.Rights,
  134. }
  135. if relate.Aid == s.c.Bnj2019.AdAv {
  136. relate.IsAd = 1
  137. }
  138. if relate.Aid == relateID {
  139. relate.Pages = s.initBnjPages(c, a.Pages)
  140. relate.ReqUser, _ = s.initBnjReqUser(c, a.Author.Mid, a.Aid, mid)
  141. }
  142. bnj.Relates = append(bnj.Relates, relate)
  143. }
  144. return
  145. }
  146. // BnjList is
  147. func (s *Service) BnjList(c context.Context, mid int64) (list *view.BnjList, err error) {
  148. list = new(view.BnjList)
  149. for _, item := range s.BnjLists {
  150. list.Item = append(list.Item, &view.BnjItem{
  151. Aid: item.Aid,
  152. Cid: item.FirstCid,
  153. Pic: item.Pic,
  154. Duration: item.Duration,
  155. IsAd: 0,
  156. Author: item.Author,
  157. })
  158. }
  159. return
  160. }
  161. // BnjItem is
  162. func (s *Service) BnjItem(c context.Context, aid, mid int64) (item *view.BnjItem, err error) {
  163. var v *archive.View3
  164. if aid == s.BnjMainView.Aid {
  165. v = s.BnjMainView
  166. } else {
  167. for _, l := range s.BnjLists {
  168. if aid == l.Aid {
  169. v = l
  170. break
  171. }
  172. }
  173. }
  174. if v == nil || !v.IsNormal() {
  175. err = ecode.NothingFound
  176. return
  177. }
  178. item = &view.BnjItem{
  179. Aid: v.Aid,
  180. Cid: v.FirstCid,
  181. Tid: v.TypeID,
  182. Pic: v.Pic,
  183. Copyright: v.Copyright,
  184. PubDate: v.PubDate,
  185. Title: v.Title,
  186. Desc: v.Desc,
  187. Stat: v.Stat,
  188. Duration: v.Duration,
  189. Author: v.Author,
  190. Dimension: v.Dimension,
  191. Rights: v.Rights,
  192. }
  193. if item.Aid == s.c.Bnj2019.AdAv {
  194. item.IsAd = 1
  195. }
  196. item.ReqUser, _ = s.initBnjReqUser(c, v.Author.Mid, v.Aid, mid)
  197. item.Pages = s.initBnjPages(c, v.Pages)
  198. return
  199. }
  200. func (s *Service) loadBnj2019Infos() (err error) {
  201. var (
  202. aids []int64
  203. avm map[int64]*archive.View3
  204. list []*archive.View3
  205. mainView *archive.View3
  206. elec *elec.Info
  207. whiteMid = make(map[int64]struct{})
  208. liveMids []int64
  209. bnjStatus int
  210. )
  211. if bnjStatus, liveMids, err = s.liveDao.Bnj2019Conf(context.Background()); err != nil {
  212. log.Error("%+v", err)
  213. } else {
  214. log.Info("got live bnj2019 mids(%v)", liveMids)
  215. for _, mid := range liveMids {
  216. whiteMid[mid] = struct{}{}
  217. }
  218. }
  219. if bnjStatus == 1 {
  220. s.BnjIsGrey = true
  221. } else {
  222. s.BnjIsGrey = false
  223. }
  224. // TODO live mids
  225. for _, mid := range s.c.Bnj2019.WhiteMids {
  226. whiteMid[mid] = struct{}{}
  227. }
  228. s.BnjWhiteMid = whiteMid
  229. aids = append(aids, s.c.Bnj2019.MainAid)
  230. aids = append(aids, s.c.Bnj2019.AidList...)
  231. if avm, err = s.arcDao.ViewsRPC(context.Background(), aids); err != nil {
  232. log.Error("bnj s.arcDao.Archives(%v) error(%v)", aids, err)
  233. return
  234. }
  235. mainView, ok := avm[s.c.Bnj2019.MainAid]
  236. if !ok {
  237. log.Error("bnj main archive(%d) not exist", s.c.Bnj2019.MainAid)
  238. return
  239. }
  240. mainView.Rights.Elec = 1
  241. mainView.Rights.Download = 1
  242. s.BnjMainView = mainView
  243. for _, aid := range s.c.Bnj2019.AidList {
  244. a, ok := avm[aid]
  245. if !ok {
  246. log.Error("bnj list has no aid(%d)", aid)
  247. continue
  248. }
  249. if !a.IsNormal() {
  250. log.Error("bnj list aid(%d) not open(%d)", aid, a.State)
  251. continue
  252. }
  253. a.Rights.Elec = 1
  254. a.Rights.Download = 1
  255. list = append(list, a)
  256. }
  257. if len(list) == 0 {
  258. log.Error("list is zero")
  259. return
  260. }
  261. s.BnjLists = list
  262. if elec, err = s.elcDao.TotalInfo(context.Background(), mainView.Author.Mid, mainView.Aid); err == nil {
  263. s.BnjElecInfo = elec
  264. s.BnjElecInfo.Total += s.c.Bnj2019.FakeElec
  265. } else {
  266. log.Error("s.elecDao.TotalInfo(%d,%d) error(%v)", mainView.Author.Mid, mainView.Aid, err)
  267. }
  268. return
  269. }