like.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package like
  2. import (
  3. "context"
  4. "database/sql"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/job/main/activity/model/like"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/net/metadata"
  11. "go-common/library/xstr"
  12. "github.com/pkg/errors"
  13. )
  14. const (
  15. _selLikeSQL = "SELECT id,wid FROM likes WHERE state=1 AND sid=? ORDER BY type"
  16. _likeListSQL = "SELECT id,wid FROM likes WHERE state= 1 AND sid = ? ORDER BY id LIMIT ?,?"
  17. _likesCntSQL = "SELECT COUNT(1) AS cnt FROM likes WHERE state = 1 AND sid = ?"
  18. _setObjStatURI = "/x/internal/activity/object/stat/set"
  19. _setViewRankURI = "/x/internal/activity/view/rank/set"
  20. _setLikeContentURI = "/x/internal/activity/like/content/set"
  21. )
  22. // Like get like by sid
  23. func (d *Dao) Like(c context.Context, sid int64) (ns []*like.Like, err error) {
  24. rows, err := d.db.Query(c, _selLikeSQL, sid)
  25. if err != nil {
  26. log.Error("notice.Query error(%v)", err)
  27. return
  28. }
  29. defer rows.Close()
  30. for rows.Next() {
  31. n := &like.Like{}
  32. if err = rows.Scan(&n.ID, &n.Wid); err != nil {
  33. log.Error("row.Scan error(%v)", err)
  34. return
  35. }
  36. ns = append(ns, n)
  37. }
  38. return
  39. }
  40. // LikeList get like list by sid.
  41. func (d *Dao) LikeList(c context.Context, sid int64, offset, limit int) (list []*like.Like, err error) {
  42. rows, err := d.db.Query(c, _likeListSQL, sid, offset, limit)
  43. if err != nil {
  44. err = errors.Wrapf(err, "LikeList:d.db.Query(%d,%d,%d)", sid, offset, limit)
  45. return
  46. }
  47. defer rows.Close()
  48. for rows.Next() {
  49. n := new(like.Like)
  50. if err = rows.Scan(&n.ID, &n.Wid); err != nil {
  51. err = errors.Wrapf(err, "LikeList:row.Scan row (%d,%d,%d)", sid, offset, limit)
  52. return
  53. }
  54. list = append(list, n)
  55. }
  56. if err = rows.Err(); err != nil {
  57. err = errors.Wrapf(err, "LikeList:rowsErr(%d,%d,%d)", sid, offset, limit)
  58. }
  59. return
  60. }
  61. // LikeCnt get like list total count by sid.
  62. func (d *Dao) LikeCnt(c context.Context, sid int64) (count int, err error) {
  63. row := d.db.QueryRow(c, _likesCntSQL, sid)
  64. if err = row.Scan(&count); err != nil {
  65. if err == sql.ErrNoRows {
  66. err = nil
  67. } else {
  68. err = errors.Wrapf(err, "LikeCnt:QueryRow(%d)", sid)
  69. }
  70. }
  71. return
  72. }
  73. // SetObjectStat .
  74. func (d *Dao) SetObjectStat(c context.Context, sid int64, stat *like.SubjectTotalStat, count int) (err error) {
  75. params := url.Values{}
  76. params.Set("sid", strconv.FormatInt(sid, 10))
  77. params.Set("like", strconv.FormatInt(stat.SumLike, 10))
  78. params.Set("view", strconv.FormatInt(stat.SumView, 10))
  79. params.Set("fav", strconv.FormatInt(stat.SumFav, 10))
  80. params.Set("coin", strconv.FormatInt(stat.SumCoin, 10))
  81. params.Set("count", strconv.Itoa(count))
  82. var res struct {
  83. Code int `json:"code"`
  84. }
  85. if err = d.httpClient.Get(c, d.setObjStatURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  86. err = errors.Wrapf(err, "SetObjectStat(%d,%v)", sid, stat)
  87. return
  88. }
  89. if res.Code != ecode.OK.Code() {
  90. err = errors.Wrapf(ecode.Int(res.Code), "SetObjectStat Code(%d,%v)", sid, stat)
  91. }
  92. return
  93. }
  94. // SetViewRank set view rank list.
  95. func (d *Dao) SetViewRank(c context.Context, sid int64, aids []int64) (err error) {
  96. params := url.Values{}
  97. params.Set("sid", strconv.FormatInt(sid, 10))
  98. params.Set("aids", xstr.JoinInts(aids))
  99. var res struct {
  100. Code int `json:"code"`
  101. }
  102. if err = d.httpClient.Get(c, d.setViewRankURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  103. err = errors.Wrapf(err, "SetViewRank(%d,%v)", sid, aids)
  104. return
  105. }
  106. if res.Code != ecode.OK.Code() {
  107. err = errors.Wrapf(ecode.Int(res.Code), "SetViewRank Code(%d,%v)", sid, aids)
  108. }
  109. return
  110. }
  111. // SetLikeContent .
  112. func (d *Dao) SetLikeContent(c context.Context, id int64) (err error) {
  113. var res struct {
  114. Code int `json:"code"`
  115. }
  116. params := url.Values{}
  117. params.Set("lid", strconv.FormatInt(id, 10))
  118. if err = d.httpClient.Get(c, d.setLikeContentURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  119. err = errors.Wrapf(err, "SetLikeContent(%d)", id)
  120. return
  121. }
  122. if res.Code != ecode.OK.Code() {
  123. err = errors.Wrapf(ecode.Int(res.Code), "SetLikeContent Code(%d)", id)
  124. }
  125. return
  126. }