upper.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "go-common/app/admin/main/tv/model"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/log"
  9. "github.com/jinzhu/gorm"
  10. )
  11. const (
  12. _setUploader = "REPLACE INTO ugc_uploader (mid, state) VALUES (?,?)"
  13. _auditMids = "UPDATE ugc_uploader SET valid = ? WHERE mid IN (?) AND deleted = 0"
  14. _intervUp = "UPDATE ugc_uploader SET cms_face = ?, cms_name = ? WHERE mid = ? AND deleted = 0"
  15. _stateNormal = 1
  16. _deleted = 1
  17. // up list oder type
  18. _ctimeNew = 1
  19. _ctimeOld = 2
  20. _mtimeNew = 3
  21. _mtimeOld = 4
  22. size = 20
  23. )
  24. // UpAdd def.
  25. func (d *Dao) UpAdd(mid int64) (err error) {
  26. if err = d.DB.Exec(_setUploader, mid, _stateNormal).Error; err != nil {
  27. log.Error("UpAdd Error %v", err)
  28. }
  29. return
  30. }
  31. // UpList def.
  32. func (d *Dao) UpList(order int, page int, ids []int64) (ups []*model.Upper, pager *model.Page, err error) {
  33. var db = d.DB.Where("deleted != ?", _deleted)
  34. if len(ids) != 0 {
  35. db = db.Where("mid IN (?)", ids)
  36. }
  37. pager = &model.Page{
  38. Num: page,
  39. Size: size,
  40. }
  41. if err = db.Model(&model.Upper{}).Count(&pager.Total).Error; err != nil {
  42. log.Error("Uplist Count Error %v", err)
  43. return
  44. }
  45. db = treatOrder(order, db)
  46. if err = db.Offset((page - 1) * size).Limit(size).Find(&ups).Error; err != nil {
  47. log.Error("UpList Error %v, Order: %d", err, order)
  48. }
  49. return
  50. }
  51. func treatOrder(order int, db *gorm.DB) *gorm.DB {
  52. switch order {
  53. case _ctimeNew:
  54. db = db.Order("ctime DESC")
  55. case _ctimeOld:
  56. db = db.Order("ctime ASC")
  57. case _mtimeNew:
  58. db = db.Order("mtime DESC")
  59. case _mtimeOld:
  60. db = db.Order("mtime ASC")
  61. default:
  62. db = db.Order("ctime DESC")
  63. }
  64. return db
  65. }
  66. // UpCmsList def.
  67. func (d *Dao) UpCmsList(req *model.ReqUpCms) (ups []*model.CmsUpper, pager *model.Page, err error) {
  68. var db = d.DB.Where("deleted = 0")
  69. if req.MID != 0 {
  70. db = db.Where("mid = ?", req.MID)
  71. }
  72. if req.Name != "" {
  73. db = db.Where("ori_name LIKE ?", "%"+req.Name+"%")
  74. }
  75. if req.Valid != "" {
  76. valid, _ := strconv.Atoi(req.Valid)
  77. db = db.Where("valid = ?", valid)
  78. }
  79. pager = &model.Page{
  80. Num: req.Pn,
  81. Size: size,
  82. }
  83. if err = db.Model(&model.Upper{}).Count(&pager.Total).Error; err != nil {
  84. log.Error("Uplist Count Error %v", err)
  85. return
  86. }
  87. db = treatOrder(req.Order, db)
  88. if err = db.Offset((req.Pn - 1) * size).Limit(size).Find(&ups).Error; err != nil {
  89. log.Error("UpList Error %v, Order: %d", err, req.Order)
  90. }
  91. return
  92. }
  93. // VerifyIds verifies whether all the mids exist
  94. func (d *Dao) VerifyIds(mids []int64) (okMids map[int64]*model.UpMC, err error) {
  95. if len(mids) == 0 {
  96. return
  97. }
  98. okMids = make(map[int64]*model.UpMC)
  99. var ups []*model.UpMC
  100. db := d.DB.Where("deleted != ?", _deleted).Where("mid IN (?)", mids)
  101. if err = db.Find(&ups).Error; err != nil {
  102. log.Error("VerifyIds Error %v, Mids %v", err, mids)
  103. }
  104. for _, v := range ups {
  105. okMids[v.MID] = v
  106. }
  107. return
  108. }
  109. // AuditIds carries out the action to the given mids
  110. func (d *Dao) AuditIds(mids []int64, validAct int) (err error) {
  111. if err = d.DB.Exec(_auditMids, validAct, mids).Error; err != nil {
  112. log.Error("AuditIds Error %v, Mids %v", err, mids)
  113. }
  114. return
  115. }
  116. // SetUpper updates the cms info of an upper in DB
  117. func (d *Dao) SetUpper(req *model.ReqUpEdit) (err error) {
  118. if err = d.DB.Exec(_intervUp, req.Face, req.Name, req.MID).Error; err != nil {
  119. log.Error("SetUpper Error %v, Mid %v", err, req)
  120. }
  121. return
  122. }
  123. func upperMetaKey(MID int64) string {
  124. return fmt.Sprintf("up_cms_%d", MID)
  125. }
  126. // SetUpMetaCache updates upinfo in MC
  127. func (d *Dao) SetUpMetaCache(c context.Context, upper *model.UpMC) (err error) {
  128. var (
  129. key = upperMetaKey(upper.MID)
  130. conn = d.mc.Get(c)
  131. )
  132. defer conn.Close()
  133. if err = conn.Set(&memcache.Item{Key: key, Object: upper, Flags: memcache.FlagJSON, Expiration: d.cmsExpire}); err != nil {
  134. log.Error("conn.Set error(%v)", err)
  135. }
  136. return
  137. }
  138. // DelCache deletes the cache from CM
  139. func (d *Dao) DelCache(c context.Context, mid int64) (err error) {
  140. var (
  141. key = upperMetaKey(mid)
  142. conn = d.mc.Get(c)
  143. )
  144. defer conn.Close()
  145. if err = conn.Delete(key); err != nil {
  146. log.Error("conn.Set error(%v)", err)
  147. }
  148. return
  149. }