mango.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package thirdp
  2. import (
  3. "context"
  4. "math"
  5. "go-common/app/interface/main/tv/dao/thirdp"
  6. "go-common/app/interface/main/tv/model"
  7. tpMdl "go-common/app/interface/main/tv/model/thirdp"
  8. arcwar "go-common/app/service/main/archive/api"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. "github.com/pkg/errors"
  12. )
  13. // mangoPage picks the Mango's page
  14. func (s *Service) mangoPage(page int64, typeC string) (pager *model.IdxPager, dataSet []*tpMdl.RespSid, err error) {
  15. var (
  16. cPageID int64
  17. req = &tpMdl.ReqDBeiPages{
  18. Page: page,
  19. TypeC: typeC,
  20. Ps: int64(s.conf.Cfg.Dangbei.MangoPS),
  21. }
  22. )
  23. if pager, err = s.buildPager(req); err != nil {
  24. return
  25. }
  26. if req.LastID, err = s.dao.LoadPageID(ctx, req); err != nil {
  27. log.Error("MangoPage getPageID Page %d Miss, Pass by offset", page-1)
  28. return
  29. }
  30. if dataSet, cPageID, err = s.dao.MangoPages(ctx, req); err != nil {
  31. return
  32. }
  33. if len(dataSet) == 0 {
  34. err = errors.Wrapf(ecode.NothingFound, "Type_C [%s], Page [%d], Offset Result Empty", typeC, page)
  35. return
  36. }
  37. cache.Save(func() {
  38. s.dao.SetPageID(ctx, &tpMdl.ReqPageID{Page: page, ID: cPageID, TypeC: typeC})
  39. })
  40. return
  41. }
  42. // MangoSns picks mango season pages
  43. func (s *Service) MangoSns(ctx context.Context, page int64) (data *tpMdl.MangoSnPage, err error) {
  44. var (
  45. pager *model.IdxPager
  46. dataSet []*tpMdl.RespSid
  47. snMetas = map[int64]*model.SeasonCMS{}
  48. snAuths = map[int64]*model.SnAuth{}
  49. newestEpids []int64
  50. epMetas = map[int64]*model.EpCMS{}
  51. )
  52. if pager, dataSet, err = s.mangoPage(page, thirdp.MangoPGC); err != nil {
  53. return
  54. }
  55. data = &tpMdl.MangoSnPage{
  56. Pager: pager,
  57. }
  58. sids := tpMdl.PickSids(dataSet)
  59. if snMetas, err = s.cmsDao.LoadSnsCMSMap(ctx, sids); err != nil {
  60. log.Error("MangoSns - PGC - LoadSnsCMS - Sids %v, Error %v", sids, err)
  61. return
  62. }
  63. if snAuths, err = s.cmsDao.LoadSnsAuthMap(ctx, sids); err != nil {
  64. log.Error("MangoSns - PGC - LoadSnsAuthMap - Sids %v, Error %v", sids, err)
  65. return
  66. }
  67. for _, v := range snMetas { // pick newestEpids
  68. if v.NewestEPID != 0 {
  69. newestEpids = append(newestEpids, v.NewestEPID)
  70. }
  71. }
  72. if len(newestEpids) > 0 { // pick eps cms meta info
  73. if epMetas, err = s.cmsDao.LoadEpsCMS(ctx, newestEpids); err != nil {
  74. log.Error("MangoSns - PGC - LoadEpsCMS - Epids %v, Error %v", newestEpids, err)
  75. return
  76. }
  77. }
  78. for _, v := range dataSet { // transform the object to DbeiSeason
  79. var (
  80. snMeta *model.SeasonCMS
  81. snAuth *model.SnAuth
  82. okMeta, okAuth bool
  83. )
  84. if snMeta, okMeta = snMetas[v.Sid]; okMeta {
  85. if snAuth, okAuth = snAuths[v.Sid]; okAuth {
  86. mangoSn := tpMdl.ToMangoSn(snMeta, v.Mtime, snAuth.CanPlay())
  87. if newestEp := snMeta.NewestEPID; newestEp != 0 {
  88. if epMeta, ok := epMetas[snMeta.NewestEPID]; ok {
  89. mangoSn.EpCover = epMeta.Cover
  90. }
  91. }
  92. data.List = append(data.List, mangoSn)
  93. continue
  94. }
  95. }
  96. log.Warn("MangoSns Sid %d Missing Info, Meta %v, Auth %v", v.Sid, okMeta, okAuth)
  97. }
  98. return
  99. }
  100. // MangoArcs picks mango archive pages
  101. func (s *Service) MangoArcs(ctx context.Context, page int64) (data *tpMdl.MangoArcPage, err error) {
  102. var (
  103. pager *model.IdxPager
  104. dataSet []*tpMdl.RespSid
  105. arcMetas map[int64]*model.ArcCMS
  106. )
  107. if pager, dataSet, err = s.mangoPage(page, thirdp.MangoUGC); err != nil {
  108. return
  109. }
  110. data = &tpMdl.MangoArcPage{
  111. Pager: pager,
  112. }
  113. sids := tpMdl.PickSids(dataSet)
  114. if arcMetas, err = s.cmsDao.LoadArcsMediaMap(ctx, sids); err != nil {
  115. log.Error("MangoArcs - UGC - LoadArcsMediaMap - Sids %v, Error %v", sids, err)
  116. return
  117. }
  118. for _, v := range dataSet { // transform the object to DbeiSeason
  119. if arcMeta, ok := arcMetas[v.Sid]; ok {
  120. cat1, cat2 := s.arcDao.GetPTypeName(int32(arcMeta.TypeID))
  121. data.List = append(data.List, tpMdl.ToMangoArc(arcMeta, v.Mtime, cat1, cat2))
  122. continue
  123. }
  124. log.Warn("MangoSns Aid %d Missing Info", v.Sid)
  125. }
  126. return
  127. }
  128. // MangoEps returns mango eps data
  129. func (s *Service) MangoEps(ctx context.Context, sid int64, page int) (data *tpMdl.MangoEpPage, err error) {
  130. var (
  131. count int
  132. pagesize = s.conf.Cfg.Dangbei.MangoPS
  133. resp []*tpMdl.RespSid
  134. epMetas map[int64]*model.EpCMS
  135. epAuths map[int64]*model.EpAuth
  136. )
  137. if count, err = s.dao.LoadSnCnt(ctx, true, sid); err != nil {
  138. log.Error("MangoEps LoadSnCnt Sid %d, Err %v", sid, err)
  139. return
  140. }
  141. totalPages := int(math.Ceil(float64(count) / float64(pagesize)))
  142. if page > totalPages {
  143. return nil, ecode.TvDangbeiPageNotExist
  144. }
  145. data = &tpMdl.MangoEpPage{
  146. SeasonID: sid,
  147. Pager: &model.IdxPager{
  148. CurrentPage: page,
  149. TotalItems: count,
  150. TotalPages: int(math.Ceil(float64(count) / float64(pagesize))),
  151. PageSize: int(pagesize),
  152. },
  153. }
  154. if resp, err = s.dao.MangoSnOffset(ctx, true, sid, page, pagesize); err != nil {
  155. log.Error("MangoEps MangoSnOffset Sid %d, Err %v", sid, err)
  156. return
  157. }
  158. epids := tpMdl.PickSids(resp)
  159. if epMetas, err = s.cmsDao.LoadEpsCMS(ctx, epids); err != nil {
  160. log.Error("MangoEps LoadEpsCMS Sid %d, Err %v", sid, err)
  161. return
  162. }
  163. if epAuths, err = s.cmsDao.LoadEpsAuthMap(ctx, epids); err != nil {
  164. log.Error("MangoEps LoadEpsAuthMap Sid %d, Err %v", sid, err)
  165. return
  166. }
  167. for _, v := range resp {
  168. var (
  169. epMeta *model.EpCMS
  170. epAuth *model.EpAuth
  171. okMeta, okAuth bool
  172. )
  173. if epMeta, okMeta = epMetas[v.Sid]; okMeta {
  174. if epAuth, okAuth = epAuths[v.Sid]; okAuth {
  175. data.List = append(data.List, &tpMdl.MangoEP{
  176. EpCMS: *epMeta,
  177. SeasonID: sid,
  178. Mtime: v.Mtime,
  179. Autorised: epAuth.CanPlay(),
  180. })
  181. continue
  182. }
  183. }
  184. log.Warn("MangoEps Sid %d, Epids %d Missing Info, Meta %v, Auth %v", sid, v.Sid, okMeta, okAuth)
  185. }
  186. return
  187. }
  188. // MangoVideos returns mango videos data
  189. func (s *Service) MangoVideos(ctx context.Context, sid int64, page int) (data *tpMdl.MangoVideoPage, err error) {
  190. var (
  191. count int
  192. pagesize = s.conf.Cfg.Dangbei.MangoPS
  193. resp []*tpMdl.RespSid
  194. videoMetas map[int64]*model.VideoCMS
  195. vp *arcwar.ViewReply
  196. vPages = make(map[int64]*arcwar.Page)
  197. )
  198. if count, err = s.dao.LoadSnCnt(ctx, false, sid); err != nil {
  199. log.Error("MangoVideos LoadSnCnt Sid %d, Err %v", sid, err)
  200. return
  201. }
  202. totalPages := int(math.Ceil(float64(count) / float64(pagesize)))
  203. if page > totalPages {
  204. return nil, ecode.TvDangbeiPageNotExist
  205. }
  206. data = &tpMdl.MangoVideoPage{
  207. AVID: sid,
  208. Pager: &model.IdxPager{
  209. CurrentPage: page,
  210. TotalItems: count,
  211. TotalPages: int(math.Ceil(float64(count) / float64(pagesize))),
  212. PageSize: int(pagesize),
  213. },
  214. }
  215. if resp, err = s.dao.MangoSnOffset(ctx, false, sid, page, pagesize); err != nil {
  216. log.Error("MangoVideos MangoSnOffset Sid %d, Err %v", sid, err)
  217. return
  218. }
  219. epids := tpMdl.PickSids(resp)
  220. if videoMetas, err = s.cmsDao.LoadVideosMeta(ctx, epids); err != nil {
  221. log.Error("MangoVideos LoadEpsCMS Sid %d, Err %v", sid, err)
  222. return
  223. }
  224. if vp, err = s.arcDao.GetView(ctx, sid); err != nil {
  225. log.Error("MangoVideos ViewPage getView Aid:%d, Err:%v", sid, err)
  226. return
  227. }
  228. for _, v := range vp.Pages {
  229. vPages[v.Cid] = v
  230. }
  231. for _, v := range resp {
  232. var (
  233. vMeta *model.VideoCMS
  234. view *arcwar.Page
  235. okMeta, okView bool
  236. )
  237. if vMeta, okMeta = videoMetas[v.Sid]; okMeta {
  238. if view, okView = vPages[v.Sid]; okView {
  239. data.List = append(data.List, &tpMdl.MangoVideo{
  240. CID: v.Sid,
  241. Page: vMeta.IndexOrder,
  242. Desc: view.Desc,
  243. Title: vMeta.Title,
  244. Duration: view.Duration,
  245. Autorised: vMeta.CanPlay(),
  246. Mtime: v.Mtime,
  247. })
  248. continue
  249. }
  250. }
  251. log.Warn("MangoViews Sid %d, Epids %d Missing Info, Meta %v, View %v", sid, v.Sid, okMeta, okView)
  252. }
  253. return
  254. }