refresh.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package upper
  2. import (
  3. "context"
  4. "fmt"
  5. ugcMdl "go-common/app/job/main/tv/model/ugc"
  6. "go-common/library/database/sql"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _countUpper = "SELECT count(1) FROM ugc_uploader WHERE deleted = 0"
  12. _pickUppers = "SELECT mid FROM ugc_uploader WHERE deleted = 0 AND mid > ? ORDER BY mid LIMIT 0,%d"
  13. _setUpperName = "UPDATE ugc_uploader SET ori_name = ?, cms_name = ? WHERE mid = ? AND deleted = 0"
  14. _setUpperFace = "UPDATE ugc_uploader SET ori_face = ?, cms_face = ? WHERE mid = ? AND deleted = 0"
  15. _sendUpper = "UPDATE ugc_uploader SET submit = 1 WHERE mid = ? AND deleted = 0"
  16. _importUpper = "REPLACE INTO ugc_uploader (ori_name, cms_name, ori_face, cms_face, mid) VALUES (?,?,?,?,?)"
  17. _upName = 1
  18. _upFace = 2
  19. )
  20. // CountUP counts the uppers
  21. func (d *Dao) CountUP(c context.Context) (count int64, err error) {
  22. if err = d.DB.QueryRow(c, _countUpper).Scan(&count); err != nil {
  23. log.Error("d.CountUP.Query error(%v)", err)
  24. }
  25. return
  26. }
  27. // PickUppers picks data by Piece to refresh uppers
  28. func (d *Dao) PickUppers(ctx context.Context, LastID int64, nbData int) (res []int64, myLast int64, err error) {
  29. var (
  30. rows *sql.Rows
  31. query = fmt.Sprintf(_pickUppers, nbData)
  32. )
  33. if rows, err = d.DB.Query(ctx, query, LastID); err != nil {
  34. log.Error("d.refreshArcMC.Query: %s error(%v)", query, err)
  35. return
  36. }
  37. defer rows.Close()
  38. for rows.Next() {
  39. var li int64
  40. if err = rows.Scan(&li); err != nil {
  41. log.Error("refreshArcMC row.Scan() error(%v)", err)
  42. return
  43. }
  44. res = append(res, li)
  45. }
  46. if err = rows.Err(); err != nil {
  47. log.Error("d.PickUppers.Query error(%v)", err)
  48. return
  49. }
  50. // record the max ID in this piece
  51. if len(res) > 0 {
  52. myLast = res[len(res)-1]
  53. }
  54. return
  55. }
  56. // SendUpper updates one upper's submit to 1
  57. func (d *Dao) SendUpper(ctx context.Context, mid int64) (err error) {
  58. if _, err = d.DB.Exec(ctx, _sendUpper, mid); err != nil {
  59. log.Error("SendUpper Error: %v", mid, err)
  60. }
  61. return
  62. }
  63. // setUpperV updates the upper's name or face value
  64. func (d *Dao) setUpperV(ctx context.Context, req *ugcMdl.ReqSetUp, oriUp *ugcMdl.Upper) (err error) {
  65. var sql string
  66. if req.UpType == _upName {
  67. sql = _setUpperName
  68. oriUp.CMSName = req.Value
  69. oriUp.OriName = req.Value
  70. } else if req.UpType == _upFace {
  71. sql = _setUpperFace
  72. oriUp.CMSFace = req.Value
  73. oriUp.OriFace = req.Value
  74. } else {
  75. return ecode.TvDangbeiWrongType
  76. }
  77. if _, err = d.DB.Exec(ctx, sql, req.Value, req.Value, req.MID); err != nil {
  78. log.Error("SetUpperName Error: %v", req.MID, err)
  79. }
  80. return
  81. }
  82. // ImportUp is used to import a new up when we manually add an archive
  83. func (d *Dao) ImportUp(ctx context.Context, up *ugcMdl.EasyUp) (err error) {
  84. if _, err = d.DB.Exec(ctx, _importUpper, up.Name, up.Name, up.Face, up.Face, up.MID); err != nil {
  85. log.Error("ImportUp Error: %v", up.MID, err)
  86. }
  87. d.addUpMetaCache(ctx, up.ToUpper(nil)) // add this upper into the cache
  88. return
  89. }
  90. // RefreshUp refreshes the upper's name and face in both DB and cache
  91. func (d *Dao) RefreshUp(ctx context.Context, req *ugcMdl.ReqSetUp) (err error) {
  92. var oriUp *ugcMdl.Upper
  93. if oriUp, err = d.LoadUpMeta(ctx, req.MID); err != nil || oriUp == nil {
  94. return
  95. }
  96. if err = d.setUpperV(ctx, req, oriUp); err != nil { // update DB
  97. return
  98. }
  99. d.addUpMetaCache(ctx, oriUp) // update cache
  100. return
  101. }