history.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. package history
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/interface/main/app-interface/model"
  6. "go-common/app/interface/main/app-interface/model/history"
  7. livemdl "go-common/app/interface/main/app-interface/model/live"
  8. hismodle "go-common/app/interface/main/history/model"
  9. artmodle "go-common/app/interface/openplatform/article/model"
  10. arcmdl "go-common/app/service/main/archive/model/archive"
  11. "go-common/library/log"
  12. "go-common/library/sync/errgroup"
  13. "go-common/library/xstr"
  14. )
  15. const (
  16. _tpOld = -1
  17. _tpOffline = 0
  18. _tpArchive = 3
  19. _tpPGC = 4
  20. _tpArticle = 5
  21. _tpLive = 6
  22. _tpCorpus = 7
  23. _androidBroadcast = 5305000
  24. )
  25. var (
  26. gotoDesc = map[int8]string{
  27. _tpOld: "av",
  28. _tpOffline: "av",
  29. _tpArchive: "av",
  30. _tpPGC: "pgc",
  31. _tpArticle: "article",
  32. _tpLive: "live",
  33. _tpCorpus: "article",
  34. }
  35. badge = map[int8]string{
  36. 1: "番剧",
  37. 2: "电影",
  38. 3: "纪录片",
  39. 4: "国创",
  40. 5: "电视剧",
  41. 7: "综艺",
  42. }
  43. busTab = []*history.BusTab{
  44. {
  45. Business: "all",
  46. Name: "全部",
  47. },
  48. {
  49. Business: "archive",
  50. Name: "视频",
  51. },
  52. {
  53. Business: "live",
  54. Name: "直播",
  55. },
  56. {
  57. Business: "article",
  58. Name: "专栏",
  59. },
  60. }
  61. )
  62. // List history list
  63. func (s *Service) List(c context.Context, mid, build int64, pn, ps int, platform string, plat int8) (data []*history.ListRes, err error) {
  64. res, err := s.historyDao.History(c, mid, pn, ps)
  65. if len(res) > 50 {
  66. log.Warn("history lens(%d) mid(%d) pn(%d) ps(%d)", len(res), mid, pn, ps)
  67. }
  68. if err != nil {
  69. log.Error("%+v ", err)
  70. return
  71. }
  72. if len(res) == 0 {
  73. data = []*history.ListRes{}
  74. return
  75. }
  76. data = s.TogetherHistory(c, res, mid, build, platform, plat)
  77. return
  78. }
  79. // Live get live for history
  80. func (s *Service) Live(c context.Context, roomIDs []int64) (res []*livemdl.RoomInfo, err error) {
  81. live, err := s.liveDao.LiveByRIDs(c, roomIDs)
  82. if err != nil {
  83. log.Error("%+v", err)
  84. return
  85. }
  86. if len(live) == 0 {
  87. res = []*livemdl.RoomInfo{}
  88. return
  89. }
  90. for _, lv := range live {
  91. item := &livemdl.RoomInfo{
  92. RoomID: lv.RoomID,
  93. URI: model.FillURI("live", strconv.FormatInt(lv.RoomID, 10), model.LiveHandler(lv)),
  94. }
  95. if lv.Status == 1 {
  96. item.Status = lv.Status
  97. }
  98. res = append(res, item)
  99. }
  100. return
  101. }
  102. // LiveList get live list for history
  103. func (s *Service) LiveList(c context.Context, mid, build int64, pn, ps int, platform string, plat int8) (data []*history.ListRes, err error) {
  104. res, err := s.historyDao.HistoryByTP(c, mid, pn, ps, _tpLive)
  105. if err != nil {
  106. log.Error("%+v ", err)
  107. return
  108. }
  109. if len(res) == 0 {
  110. data = []*history.ListRes{}
  111. return
  112. }
  113. data = s.TogetherHistory(c, res, mid, build, platform, plat)
  114. return
  115. }
  116. // Cursor for history
  117. func (s *Service) Cursor(c context.Context, mid, build, paramMax int64, ps int, platform string, paramMaxTP, plat int8, businesses []string) (data *history.ListCursor, err error) {
  118. data = &history.ListCursor{
  119. List: []*history.ListRes{},
  120. Tab: busTab,
  121. }
  122. // 国际版不出直播
  123. if plat == model.PlatAndroidI {
  124. data.Tab = []*history.BusTab{
  125. {
  126. Business: "all",
  127. Name: "全部",
  128. },
  129. {
  130. Business: "archive",
  131. Name: "视频",
  132. },
  133. {
  134. Business: "article",
  135. Name: "专栏",
  136. },
  137. }
  138. }
  139. curPs := 50
  140. res, err := s.historyDao.Cursor(c, mid, paramMax, curPs, paramMaxTP, businesses)
  141. if len(res) > curPs {
  142. log.Warn("history lens(%d) mid(%d) paramMax(%d) paramMaxTP(%d) curPs(%d)", len(res), mid, paramMax, paramMaxTP, curPs)
  143. }
  144. if err != nil {
  145. log.Error("%+v ", err)
  146. return
  147. }
  148. if len(res) == 0 {
  149. return
  150. }
  151. data.List = s.TogetherHistory(c, res, mid, build, platform, plat)
  152. if len(data.List) >= ps {
  153. data.List = data.List[:ps]
  154. }
  155. if len(data.List) > 0 {
  156. data.Cursor = &history.Cursor{
  157. Max: data.List[len(data.List)-1].ViewAt,
  158. MaxTP: data.List[len(data.List)-1].History.Tp,
  159. Ps: ps,
  160. }
  161. }
  162. return
  163. }
  164. // TogetherHistory always return 0~50
  165. func (s *Service) TogetherHistory(c context.Context, res []*hismodle.Resource, mid, build int64, platform string, plat int8) (data []*history.ListRes) {
  166. var (
  167. aids []int64
  168. epids []int64
  169. articleIDs []int64
  170. roomIDs []int64
  171. archive map[int64]*arcmdl.View3
  172. pgcInfo map[int64]*history.PGCRes
  173. article map[int64]*artmodle.Meta
  174. live map[int64]*livemdl.RoomInfo
  175. )
  176. i := 0
  177. for _, his := range res {
  178. i++
  179. //由于出现过history吐出数量无限制,限制请求archive的数量逻辑保留
  180. if i > 80 {
  181. break
  182. }
  183. switch his.TP {
  184. case _tpOld, _tpOffline, _tpArchive:
  185. aids = append(aids, his.Oid)
  186. case _tpPGC:
  187. aids = append(aids, his.Oid) //用cid拿时长duration
  188. epids = append(epids, his.Epid)
  189. case _tpArticle:
  190. articleIDs = append(articleIDs, his.Oid)
  191. case _tpLive:
  192. roomIDs = append(roomIDs, his.Oid)
  193. case _tpCorpus:
  194. articleIDs = append(articleIDs, his.Cid)
  195. default:
  196. log.Warn("unknow history type(%d) msg(%+v)", his.TP, his)
  197. }
  198. }
  199. eg, ctx := errgroup.WithContext(c)
  200. if len(aids) > 0 {
  201. eg.Go(func() (err error) {
  202. archive, err = s.historyDao.Archive(ctx, aids)
  203. if err != nil {
  204. log.Error("%+v", err)
  205. err = nil
  206. }
  207. return
  208. })
  209. }
  210. if len(epids) > 0 {
  211. eg.Go(func() (err error) {
  212. pgcInfo, err = s.historyDao.PGC(ctx, xstr.JoinInts(epids), platform, build, mid)
  213. if err != nil {
  214. log.Error("%+v", err)
  215. err = nil
  216. }
  217. return
  218. })
  219. }
  220. if len(articleIDs) > 0 {
  221. eg.Go(func() (err error) {
  222. article, err = s.historyDao.Article(ctx, articleIDs)
  223. if err != nil {
  224. log.Error("%+v", err)
  225. err = nil
  226. }
  227. return
  228. })
  229. }
  230. if len(roomIDs) > 0 {
  231. eg.Go(func() (err error) {
  232. live, err = s.liveDao.LiveByRIDs(ctx, roomIDs)
  233. if err != nil {
  234. log.Error("%+v", err)
  235. err = nil
  236. }
  237. return
  238. })
  239. }
  240. eg.Wait()
  241. for _, his := range res {
  242. // 国际版不出直播
  243. if plat == model.PlatAndroidI && his.TP == _tpLive {
  244. continue
  245. }
  246. tmpInfo := &history.ListRes{
  247. Goto: gotoDesc[his.TP],
  248. ViewAt: his.Unix,
  249. }
  250. tmpInfo.History.Oid = his.Oid
  251. tmpInfo.History.Tp = his.TP
  252. tmpInfo.History.Business = his.Business
  253. switch his.TP {
  254. case _tpOld, _tpOffline, _tpArchive:
  255. arc, ok := archive[his.Oid]
  256. if !ok {
  257. continue
  258. }
  259. tmpInfo.Title = arc.Title
  260. tmpInfo.Cover = arc.Pic
  261. tmpInfo.Mid = arc.Author.Mid
  262. tmpInfo.Name = arc.Author.Name
  263. tmpInfo.Progress = his.Pro
  264. tmpInfo.Videos = arc.Videos
  265. for _, p := range arc.Pages {
  266. if p.Cid == his.Cid {
  267. tmpInfo.Duration = p.Duration
  268. tmpInfo.History.Cid = p.Cid
  269. tmpInfo.History.Page = p.Page
  270. tmpInfo.History.Part = p.Part
  271. break
  272. }
  273. }
  274. tmpInfo.URI = model.FillURI(tmpInfo.Goto, strconv.FormatInt(his.Oid, 10), model.AvHandler(arc.Archive3))
  275. case _tpPGC:
  276. pgc, okPGC := pgcInfo[his.Epid]
  277. arc, okArc := archive[his.Oid]
  278. if !okPGC || !okArc {
  279. continue
  280. }
  281. tmpInfo.Title = pgc.Title
  282. tmpInfo.ShowTitle = pgc.ShowTitle
  283. tmpInfo.Cover = pgc.Cover
  284. tmpInfo.Badge = badge[his.STP]
  285. tmpInfo.Progress = his.Pro
  286. tmpInfo.URI = model.FillURI(tmpInfo.Goto, strconv.FormatInt(his.Sid, 10), nil)
  287. for _, p := range arc.Pages {
  288. if p.Cid == his.Cid {
  289. tmpInfo.Duration = p.Duration
  290. break
  291. }
  292. }
  293. case _tpArticle:
  294. art, ok := article[his.Oid]
  295. if !ok {
  296. continue
  297. }
  298. tmpInfo.Title = art.Title
  299. tmpInfo.Covers = art.ImageURLs
  300. tmpInfo.Mid = art.Author.Mid
  301. tmpInfo.Name = art.Author.Name
  302. tmpInfo.Badge = "专栏"
  303. tmpInfo.URI = model.FillURI(tmpInfo.Goto, strconv.FormatInt(his.Oid, 10), nil)
  304. case _tpLive:
  305. lv, ok := live[his.Oid]
  306. if !ok {
  307. continue
  308. }
  309. tmpInfo.Title = lv.Title
  310. tmpInfo.Cover = lv.UserCover
  311. if lv.UserCover == "" {
  312. tmpInfo.Cover = lv.Cover
  313. }
  314. tmpInfo.Mid = lv.Mid
  315. tmpInfo.Name = lv.Name
  316. tmpInfo.TagName = lv.TagName
  317. if lv.Status == 1 { //1是直播中,0、2是未开播
  318. tmpInfo.LiveStatus = 1
  319. }
  320. if model.IsAndroid(plat) && build < _androidBroadcast {
  321. lv = nil
  322. }
  323. tmpInfo.URI = model.FillURI(tmpInfo.Goto, strconv.FormatInt(his.Oid, 10), model.LiveHandler(lv))
  324. case _tpCorpus:
  325. art, ok := article[his.Cid]
  326. if !ok {
  327. continue
  328. }
  329. tmpInfo.Title = art.Title
  330. tmpInfo.Covers = art.ImageURLs
  331. tmpInfo.Mid = art.Author.Mid
  332. tmpInfo.Name = art.Author.Name
  333. tmpInfo.Badge = "专栏"
  334. tmpInfo.URI = model.FillURI("article", strconv.FormatInt(his.Cid, 10), nil)
  335. default:
  336. continue
  337. }
  338. data = append(data, tmpInfo)
  339. }
  340. return
  341. }
  342. // Del for history
  343. func (s *Service) Del(c context.Context, mid int64, hisRes []*hismodle.Resource) (err error) {
  344. err = s.historyDao.Del(c, mid, hisRes)
  345. if err != nil {
  346. log.Error("%+v ", err)
  347. return
  348. }
  349. return
  350. }
  351. // Clear for history
  352. func (s *Service) Clear(c context.Context, mid int64, businesses []string) (err error) {
  353. err = s.historyDao.Clear(c, mid, businesses)
  354. if err != nil {
  355. log.Error("%+v ", err)
  356. return
  357. }
  358. return
  359. }