dao.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package history
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "go-common/app/interface/main/app-interface/conf"
  7. model "go-common/app/interface/main/app-interface/model/history"
  8. hismodle "go-common/app/interface/main/history/model"
  9. hisrpc "go-common/app/interface/main/history/rpc/client"
  10. artmodle "go-common/app/interface/openplatform/article/model"
  11. artrpc "go-common/app/interface/openplatform/article/rpc/client"
  12. arcrpc "go-common/app/service/main/archive/api/gorpc"
  13. arcmodle "go-common/app/service/main/archive/model/archive"
  14. "go-common/library/ecode"
  15. bm "go-common/library/net/http/blademaster"
  16. "go-common/library/net/metadata"
  17. "github.com/pkg/errors"
  18. )
  19. const (
  20. _apiPGC = "/internal_api/get_eps_v2"
  21. )
  22. // Dao is history dao
  23. type Dao struct {
  24. client *bm.Client
  25. historyRPC *hisrpc.Service
  26. arcRPC *arcrpc.Service2
  27. artRPC *artrpc.Service
  28. pgcAPI string
  29. }
  30. // New initial history dao
  31. func New(c *conf.Config) (d *Dao) {
  32. d = &Dao{
  33. client: bm.NewClient(c.HTTPClient),
  34. historyRPC: hisrpc.New(c.HistoryRPC),
  35. arcRPC: arcrpc.New2(c.ArchiveRPC),
  36. artRPC: artrpc.New(c.ArticleRPC),
  37. pgcAPI: c.Host.Bangumi + _apiPGC,
  38. }
  39. return
  40. }
  41. // History get history
  42. func (d *Dao) History(c context.Context, mid int64, pn, ps int) (res []*hismodle.Resource, err error) {
  43. ip := metadata.String(c, metadata.RemoteIP)
  44. arg := &hismodle.ArgHistories{Mid: mid, Pn: pn, Ps: ps, RealIP: ip}
  45. if res, err = d.historyRPC.History(c, arg); err != nil {
  46. err = errors.Wrapf(err, "d.historyRPC.History(%+v)", arg)
  47. }
  48. return
  49. }
  50. // Archive get archive info
  51. func (d *Dao) Archive(c context.Context, aids []int64) (info map[int64]*arcmodle.View3, err error) {
  52. arg := &arcmodle.ArgAids2{Aids: aids}
  53. if info, err = d.arcRPC.Views3(c, arg); err != nil {
  54. err = errors.Wrapf(err, "d.arcRPC.Views3(%+v)", arg)
  55. }
  56. return
  57. }
  58. // PGC get PGC info
  59. func (d *Dao) PGC(c context.Context, epid, platform string, build, mid int64) (info map[int64]*model.PGCRes, err error) {
  60. ip := metadata.String(c, metadata.RemoteIP)
  61. params := url.Values{}
  62. params.Set("ep_ids", epid)
  63. params.Set("mid", strconv.FormatInt(mid, 10))
  64. params.Set("build", strconv.FormatInt(build, 10))
  65. params.Set("platform", platform)
  66. var res struct {
  67. Code int `json:"code"`
  68. Data []*model.PGCRes `json:"result"`
  69. }
  70. if err = d.client.Get(c, d.pgcAPI, ip, params, &res); err != nil {
  71. return
  72. }
  73. if res.Code != ecode.OK.Code() {
  74. err = errors.Wrap(ecode.Int(res.Code), d.pgcAPI+"?"+params.Encode())
  75. return
  76. }
  77. info = make(map[int64]*model.PGCRes, len(res.Data))
  78. for _, v := range res.Data {
  79. v.Title = v.Season.Title
  80. info[v.EpID] = v
  81. }
  82. return
  83. }
  84. // Article get articl info
  85. func (d *Dao) Article(c context.Context, articleIDs []int64) (info map[int64]*artmodle.Meta, err error) {
  86. ip := metadata.String(c, metadata.RemoteIP)
  87. arg := &artmodle.ArgAids{Aids: articleIDs, RealIP: ip}
  88. if info, err = d.artRPC.ArticleMetas(c, arg); err != nil {
  89. err = errors.Wrapf(err, "d.artRPC.ArticleMetas(%+v)", arg)
  90. }
  91. return
  92. }
  93. // HistoryByTP histroy by tp
  94. func (d *Dao) HistoryByTP(c context.Context, mid int64, pn, ps int, tp int8) (res []*hismodle.Resource, err error) {
  95. ip := metadata.String(c, metadata.RemoteIP)
  96. arg := &hismodle.ArgHistories{Mid: mid, Pn: pn, Ps: ps, RealIP: ip, TP: tp}
  97. if res, err = d.historyRPC.History(c, arg); err != nil {
  98. err = errors.Wrapf(err, "d.historyRPC.History(%+v)", arg)
  99. }
  100. return
  101. }
  102. // Cursor 5.28游标由MaxOid+MaxTP唯一确定 改为 由ViewAt唯一确定(防止客户端改动对客户端仍用max字段)
  103. func (d *Dao) Cursor(c context.Context, mid, max int64, ps int, tp int8, businesses []string) (res []*hismodle.Resource, err error) {
  104. ip := metadata.String(c, metadata.RemoteIP)
  105. arg := &hismodle.ArgCursor{Mid: mid, Max: max, Ps: ps, RealIP: ip, TP: tp, ViewAt: max, Businesses: businesses}
  106. if res, err = d.historyRPC.HistoryCursor(c, arg); err != nil {
  107. err = errors.Wrapf(err, "d.historyRPC.HistoryCursor(%+v)", arg)
  108. }
  109. return
  110. }
  111. // Del for history
  112. func (d *Dao) Del(c context.Context, mid int64, hisRes []*hismodle.Resource) (err error) {
  113. ip := metadata.String(c, metadata.RemoteIP)
  114. arg := &hismodle.ArgDelete{Mid: mid, RealIP: ip, Resources: hisRes}
  115. if err = d.historyRPC.Delete(c, arg); err != nil {
  116. err = errors.Wrapf(err, "d.historyRPC.Delete(%+v)", arg)
  117. }
  118. return
  119. }
  120. // Clear for history
  121. func (d *Dao) Clear(c context.Context, mid int64, businesses []string) (err error) {
  122. ip := metadata.String(c, metadata.RemoteIP)
  123. arg := &hismodle.ArgClear{Mid: mid, RealIP: ip, Businesses: businesses}
  124. if err = d.historyRPC.Clear(c, arg); err != nil {
  125. err = errors.Wrapf(err, "d.historyRPC.Clear(%+v)", arg)
  126. }
  127. return
  128. }