likes.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package dao
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/base64"
  6. "fmt"
  7. "net/url"
  8. "strings"
  9. "time"
  10. "go-common/app/admin/main/activity/model"
  11. "go-common/library/log"
  12. "go-common/library/xstr"
  13. "github.com/jinzhu/gorm"
  14. "github.com/pkg/errors"
  15. )
  16. const (
  17. _likeBatchSQL = "INSERT INTO likes(`sid`,`wid`,`mid`,`type`,`state`,`stick_top`,`ctime`,`mtime`) VALUES %s"
  18. _likeContentBatchSQL = "INSERT INTO like_content(`id`,`ipv6`,`ctime`,`mtime`) VALUES %s"
  19. )
  20. func imgAddKey(uri string) (url string) {
  21. if strings.Contains(uri, "http://drawyoo.hdslb.com") {
  22. path := strings.Replace(uri, "http://drawyoo.hdslb.com", "", -1)
  23. expire := time.Now().Unix() + 3600
  24. md5Byte := md5.Sum([]byte(fmt.Sprintf("rjZOPr8w%s%d", path, expire)))
  25. md5Key := md5Byte[:]
  26. afterKey := strings.Replace(base64.StdEncoding.EncodeToString(md5Key), "+/", "-_", -1)
  27. return uri + "?key=" + strings.Replace(afterKey, "=", "", -1) + "&expires=" + fmt.Sprintf("%d", expire)
  28. }
  29. return uri
  30. }
  31. // GetLikeContent .
  32. func (d *Dao) GetLikeContent(c context.Context, ids []int64) (outRes map[int64]*model.LikeContent, err error) {
  33. var likeContent []*model.LikeContent
  34. if err = d.DB.Where("id in (?)", ids).Find(&likeContent).Error; err != nil && err != gorm.ErrRecordNotFound {
  35. log.Error("s.DB.Where(id in %v).Find(),error(%v)", ids, err)
  36. return
  37. }
  38. outRes = make(map[int64]*model.LikeContent, len(likeContent))
  39. for _, item := range likeContent {
  40. outRes[item.ID] = item
  41. outRes[item.ID].Image = imgAddKey(item.Image)
  42. }
  43. return
  44. }
  45. // ActSubject get likesubject from db.
  46. func (d *Dao) ActSubject(c context.Context, sid int64) (rp *model.ActSubject, err error) {
  47. rp = new(model.ActSubject)
  48. if err = d.DB.Where("id = ?", sid).First(rp).Error; err != nil {
  49. log.Error(" s.DB.Where(id ,%d).First() error(%v)", sid, err)
  50. }
  51. return
  52. }
  53. // Musics get music info .
  54. func (d *Dao) Musics(c context.Context, aids []int64, ip string) (music *model.MusicRes, err error) {
  55. params := url.Values{}
  56. params.Set("songIds", xstr.JoinInts(aids))
  57. if err = d.client.Post(c, d.songsURL, ip, params, &music); err != nil {
  58. err = errors.Wrapf(err, "d.client.Post(%s)", d.songsURL)
  59. }
  60. if music.Code != 0 {
  61. err = errors.New("get music error")
  62. }
  63. return
  64. }
  65. // BatchLike .
  66. func (d *Dao) BatchLike(c context.Context, item *model.Like, wids []int64, ipv6 []byte) (err error) {
  67. var (
  68. likesVal []*model.Like
  69. )
  70. if len(wids) == 0 {
  71. return
  72. }
  73. lidString := make([]string, 0, len(wids))
  74. rowStrings := make([]string, 0, len(wids))
  75. ctime := time.Now()
  76. for _, v := range wids {
  77. rowStrings = append(rowStrings, fmt.Sprintf("(%d,%d,%d,%d,%d,%d,'%s','%s')", item.Sid, v, item.Mid, item.Type, item.State, item.StickTop, ctime, ctime))
  78. }
  79. tx := d.DB.Begin()
  80. if err = tx.Model(&model.Like{}).Exec(fmt.Sprintf(_likeBatchSQL, strings.Join(rowStrings, ","))).Error; err != nil {
  81. err = errors.Wrapf(err, " d.DB.Model(&model.Like{}).Exec(%s)", _likeBatchSQL)
  82. tx.Rollback()
  83. return
  84. }
  85. if err = tx.Model(&model.Like{}).Where(fmt.Sprintf("sid = ? and wid in (%s)", xstr.JoinInts(wids)), item.Sid).Find(&likesVal).Error; err != nil {
  86. err = errors.Wrapf(err, " d.DB.Model(&model.Like{}).find()")
  87. tx.Rollback()
  88. return
  89. }
  90. for _, itm := range likesVal {
  91. lidString = append(lidString, fmt.Sprintf("(%d,'%s','%s','%s')", itm.ID, ipv6, ctime, ctime))
  92. }
  93. if err = tx.Model(&model.LikeContent{}).Exec(fmt.Sprintf(_likeContentBatchSQL, strings.Join(lidString, ","))).Error; err != nil {
  94. err = errors.Wrapf(err, " d.DB.Model(&model.LikeContent{}).Exec(%s)", _likeContentBatchSQL)
  95. tx.Rollback()
  96. return
  97. }
  98. err = tx.Commit().Error
  99. return
  100. }