dangbei.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package thirdp
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/tv/model/thirdp"
  6. "go-common/library/cache/redis"
  7. "go-common/library/database/sql"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _dangbeiPGCPage = "SELECT id FROM tv_ep_season WHERE `check`= 1 AND valid = 1 AND is_deleted=0 %s ORDER BY id"
  13. _dangbeiUGCPage = "SELECT aid FROM ugc_archive WHERE result = 1 AND valid = 1 AND deleted = 0 %s ORDER BY aid"
  14. _dangbeiPGCCount = "SELECT count(1) FROM tv_ep_season WHERE `check`= 1 AND valid= 1 AND is_deleted=0"
  15. _dangbeiUGCCount = "SELECT count(1) FROM ugc_archive WHERE result = 1 AND valid= 1 AND deleted =0"
  16. _dangbeiPGCKey = "TV_Dangbei_PGC_Page"
  17. _dangbeiUGCKey = "TV_Dangbei_UGC_Page"
  18. _countField = "Dangbei_Count"
  19. // DBeiPGC is dangbei pgc typeC
  20. DBeiPGC = "pgc"
  21. // DBeiUGC is dangbei ugc typeC
  22. DBeiUGC = "ugc"
  23. )
  24. // KeyThirdp returns the key in Redis according to the type input
  25. func KeyThirdp(typeC string) (key string, err error) {
  26. switch typeC {
  27. case DBeiPGC:
  28. key = _dangbeiPGCKey
  29. case DBeiUGC:
  30. key = _dangbeiUGCKey
  31. case MangoPGC:
  32. key = _mangoPGCKey
  33. case MangoUGC:
  34. key = _mangoUGCKey
  35. default:
  36. err = ecode.TvDangbeiWrongType
  37. }
  38. return
  39. }
  40. // ThirdpCnt counts the number of pgc/ugc data to display for dangbei
  41. func (d *Dao) ThirdpCnt(ctx context.Context, typeC string) (count int, err error) {
  42. var query string
  43. switch typeC {
  44. case DBeiPGC:
  45. query = _dangbeiPGCCount
  46. case DBeiUGC:
  47. query = _dangbeiUGCCount
  48. case MangoPGC:
  49. query = _mangoPGCCount
  50. case MangoUGC:
  51. query = _mangoUGCCount
  52. default:
  53. err = ecode.TvDangbeiWrongType
  54. return
  55. }
  56. if err = d.db.QueryRow(ctx, query).Scan(&count); err != nil {
  57. log.Error("PickDBeiPage ThirdpCnt Error %v", err)
  58. }
  59. return
  60. }
  61. // DBeiPages picks a page for dangbei api, lastID is the last page's biggest ID
  62. func (d *Dao) DBeiPages(ctx context.Context, req *thirdp.ReqDBeiPages) (sids []int64, myLast int64, err error) {
  63. var (
  64. rows *sql.Rows
  65. query string
  66. lastID = req.LastID
  67. ps = req.Ps
  68. )
  69. switch req.TypeC {
  70. case DBeiPGC:
  71. query = fmt.Sprintf(_dangbeiPGCPage+" LIMIT %d", "AND id> ?", ps)
  72. case DBeiUGC:
  73. query = fmt.Sprintf(_dangbeiUGCPage+" LIMIT %d", "AND aid> ?", ps)
  74. default:
  75. err = ecode.TvDangbeiWrongType
  76. return
  77. }
  78. if rows, err = d.db.Query(ctx, query, lastID); err != nil {
  79. log.Error("DangbeiPage, lastID %d, Err %v", lastID, err)
  80. return
  81. }
  82. defer rows.Close()
  83. if sids, myLast, err = dbeiRowsTreat(rows); err != nil {
  84. log.Error("dbeiOffset lastID %d, Err %v", lastID, err)
  85. }
  86. return
  87. }
  88. func dbeiRowsTreat(rows *sql.Rows) (sids []int64, myLast int64, err error) {
  89. for rows.Next() {
  90. var r int64
  91. if err = rows.Scan(&r); err != nil {
  92. return
  93. }
  94. sids = append(sids, r)
  95. }
  96. if err = rows.Err(); err != nil {
  97. log.Error("rows.Err() error(%v)", err)
  98. return
  99. }
  100. // record my biggest id
  101. if len(sids) > 0 {
  102. myLast = sids[len(sids)-1]
  103. }
  104. return
  105. }
  106. // thirdpOffset is used in case of missing pageID record in Redis
  107. func (d *Dao) thirdpOffset(ctx context.Context, page int64, ps int64, typeC string) (lastPageMax int64, err error) {
  108. if page <= 0 {
  109. return 0, nil
  110. }
  111. var query string
  112. switch typeC {
  113. case DBeiPGC:
  114. query = _dangbeiPGCPage
  115. case DBeiUGC:
  116. query = _dangbeiUGCPage
  117. case MangoPGC:
  118. query = _mangoPGCOffset
  119. case MangoUGC:
  120. query = _mangoUGCOffset
  121. default:
  122. err = ecode.TvDangbeiWrongType
  123. return
  124. }
  125. querySQL := fmt.Sprintf(query+" LIMIT %d,%d", "", page*ps-1, 1)
  126. if err = d.db.QueryRow(ctx, querySQL).Scan(&lastPageMax); err != nil {
  127. log.Error("DangbeiPage, page %d, Err %v", page, err)
  128. }
  129. return
  130. }
  131. // SetPageID is used to record each dangbei page's biggest ID, it's to ease the next page's pickup
  132. func (d *Dao) SetPageID(c context.Context, req *thirdp.ReqPageID) (err error) {
  133. var key string
  134. if key, err = KeyThirdp(req.TypeC); err != nil {
  135. log.Error("PickDBeiPage Dangbei Key TypeC = %v, Error %v", req.TypeC, err)
  136. return
  137. }
  138. conn := d.redis.Get(c)
  139. defer conn.Close()
  140. if err = conn.Send("HSET", key, req.Page, req.ID); err != nil {
  141. log.Error("PickDBeiPage conn.Send(HSET Key %v, field %d, value %d) error(%v)", key, req.Page, req.ID, err)
  142. return
  143. }
  144. if err = conn.Send("EXPIRE", key, d.dbeiExpire); err != nil {
  145. log.Error("PickDBeiPage conn.Send error(%v)", err)
  146. return
  147. }
  148. if err = conn.Flush(); err != nil {
  149. log.Error("PickDBeiPage conn.Flush error(%v)", err)
  150. return
  151. }
  152. for i := 0; i < 2; i++ {
  153. if _, err = conn.Receive(); err != nil {
  154. log.Error("PickDBeiPage conn.Receive error(%v)", err)
  155. return
  156. }
  157. }
  158. return
  159. }
  160. // SetThirdpCnt is used to record dangbei pgc data count
  161. func (d *Dao) SetThirdpCnt(c context.Context, count int, typeC string) (err error) {
  162. var key string
  163. if key, err = KeyThirdp(typeC); err != nil {
  164. log.Error("PickDBeiPage Dangbei Key TypeC = %v, Error %v", typeC, err)
  165. return
  166. }
  167. conn := d.redis.Get(c)
  168. defer conn.Close()
  169. if err = conn.Send("HSET", key, _countField, count); err != nil {
  170. log.Error("PickDBeiPage conn.Send(HSET Key %v, field %d, value %d) error(%v)", key, _countField, count, err)
  171. return
  172. }
  173. if err = conn.Send("EXPIRE", key, d.dbeiExpire); err != nil {
  174. log.Error("PickDBeiPage conn.Send error(%v)", err)
  175. return
  176. }
  177. if err = conn.Flush(); err != nil {
  178. log.Error("PickDBeiPage conn.Flush error(%v)", err)
  179. return
  180. }
  181. for i := 0; i < 2; i++ {
  182. if _, err = conn.Receive(); err != nil {
  183. log.Error("PickDBeiPage conn.Receive error(%v)", err)
  184. return
  185. }
  186. }
  187. return
  188. }
  189. // GetThirdpCnt get dangbei pgc data count
  190. func (d *Dao) GetThirdpCnt(c context.Context, typeC string) (count int, err error) {
  191. var key string
  192. if key, err = KeyThirdp(typeC); err != nil {
  193. log.Error("PickDBeiPage Dangbei Key TypeC = %v, Error %v", typeC, err)
  194. return
  195. }
  196. conn := d.redis.Get(c)
  197. defer conn.Close()
  198. if count, err = redis.Int(conn.Do("HGET", key, _countField)); err != nil {
  199. if err == redis.ErrNil {
  200. log.Info("PickDBeiPage conn.HGET(field:%s) not found", _countField)
  201. } else {
  202. log.Error("PickDBeiPage conn.HGET(field:%s) error(%v)", _countField, err)
  203. }
  204. }
  205. return
  206. }
  207. // getPageID get pageNumber's biggestID from redis hashmap
  208. func (d *Dao) getPageID(c context.Context, pageNumber int64, typeC string) (biggestID int64, err error) {
  209. var key string
  210. if key, err = KeyThirdp(typeC); err != nil {
  211. log.Error("Dangbei Key TypeC = %v, Error %v", typeC, err)
  212. return
  213. }
  214. conn := d.redis.Get(c)
  215. defer conn.Close()
  216. if biggestID, err = redis.Int64(conn.Do("HGET", key, pageNumber)); err != nil {
  217. if err == redis.ErrNil {
  218. log.Info("conn.HGET(page:%d) not found", pageNumber)
  219. } else {
  220. log.Error("conn.HGET(page:%d) error(%v)", pageNumber, err)
  221. }
  222. }
  223. return
  224. }
  225. // LoadPageID picks the last page's biggest ID
  226. func (d *Dao) LoadPageID(c context.Context, req *thirdp.ReqDBeiPages) (biggestID int64, err error) {
  227. var (
  228. pageNum = req.Page - 1
  229. typeC = req.TypeC
  230. )
  231. if pageNum <= 0 {
  232. return 0, nil
  233. }
  234. if biggestID, err = d.getPageID(c, pageNum, typeC); err == nil { // get directly from cache
  235. cachedCount.Add("thirdp-page", 1)
  236. return
  237. }
  238. missedCount.Add("thirdp-page", 1)
  239. if biggestID, err = d.thirdpOffset(c, pageNum, req.Ps, typeC); err != nil {
  240. log.Error("ThirdpOffSet TypeC %s, PageNum %d, Pagesize %d", typeC, pageNum, req.Ps)
  241. return
  242. }
  243. if err = d.SetPageID(c, &thirdp.ReqPageID{
  244. Page: pageNum,
  245. ID: biggestID,
  246. TypeC: typeC,
  247. }); err != nil {
  248. log.Error("ThirdpOffset TypeC %s, PageNum %d, SetPageID Err %v", typeC, pageNum, err)
  249. }
  250. return
  251. }