subject.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package like
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "net/url"
  7. "strconv"
  8. "time"
  9. "go-common/app/job/main/activity/model/like"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. "go-common/library/net/metadata"
  13. "go-common/library/xstr"
  14. "github.com/pkg/errors"
  15. )
  16. const (
  17. _selSubjectSQL = "SELECT s.id,s.name,s.dic,s.cover,s.stime,f.interval,f.ltime,f.tlimit FROM act_subject s INNER JOIN act_time_config f ON s.id=f.sid WHERE s.id=?"
  18. _inOnlineLogSQL = "INSERT INTO act_online_vote_end_log(sid,aid,stage,yes,no) VALUES(?,?,?,?,?)"
  19. _subjectsSQL = "SELECT id,name,dic,cover,stime,etime FROM act_subject WHERE state = 1 AND type IN (%s) AND stime <= ? AND etime>= ?"
  20. _addLotteryTimesURI = "/matsuri/api/add/times"
  21. )
  22. // Subject subject
  23. func (dao *Dao) Subject(c context.Context, sid int64) (n *like.Subject, err error) {
  24. rows := dao.subjectStmt.QueryRow(c, sid)
  25. n = &like.Subject{}
  26. if err = rows.Scan(&n.ID, &n.Name, &n.Dic, &n.Cover, &n.Stime, &n.Interval, &n.Ltime, &n.Tlimit); err != nil {
  27. if err == sql.ErrNoRows {
  28. n = nil
  29. err = nil
  30. } else {
  31. log.Error("row.Scan error(%v)", err)
  32. }
  33. return
  34. }
  35. return
  36. }
  37. // InOnlinelog InOnlinelog
  38. func (dao *Dao) InOnlinelog(c context.Context, sid, aid, stage, yes, no int64) (rows int64, err error) {
  39. rs, err := dao.inOnlineLog.Exec(c, sid, aid, stage, yes, no)
  40. if err != nil {
  41. log.Error("d.InOnlinelog.Exec(%d, %d, %d, %d, %d) error(%v)", sid, aid, stage, yes, no, err)
  42. return
  43. }
  44. return rs.RowsAffected()
  45. }
  46. // SubjectList get online subject list by type.
  47. func (dao *Dao) SubjectList(c context.Context, types []int64, ts time.Time) (res []*like.Subject, err error) {
  48. rows, err := dao.db.Query(c, fmt.Sprintf(_subjectsSQL, xstr.JoinInts(types)), ts, ts)
  49. if err != nil {
  50. err = errors.Wrapf(err, "SubjectList:d.db.Query(%v,%d)", types, ts.Unix())
  51. return
  52. }
  53. defer rows.Close()
  54. for rows.Next() {
  55. n := new(like.Subject)
  56. if err = rows.Scan(&n.ID, &n.Name, &n.Dic, &n.Cover, &n.Stime, &n.Etime); err != nil {
  57. err = errors.Wrapf(err, "SubjectList:row.Scan row (%v,%d)", types, ts.Unix())
  58. return
  59. }
  60. res = append(res, n)
  61. }
  62. if err = rows.Err(); err != nil {
  63. err = errors.Wrapf(err, "SubjectList:rowsErr(%v,%d)", types, ts.Unix())
  64. }
  65. return
  66. }
  67. // SubjectTotalStat total stat.
  68. func (dao *Dao) SubjectTotalStat(c context.Context, sid int64) (rs *like.SubjectTotalStat, err error) {
  69. req := dao.es.NewRequest(_activity).Index(_activity).WhereEq("state", 1).WhereEq("sid", sid).Sum("click").Sum("likes").Sum("fav").Sum("coin")
  70. res := new(struct {
  71. Result struct {
  72. SumCoin []struct {
  73. Value float64 `json:"value"`
  74. } `json:"sum_coin"`
  75. SumFav []struct {
  76. Value float64 `json:"value"`
  77. } `json:"sum_fav"`
  78. SumLikes []struct {
  79. Value float64 `json:"value"`
  80. } `json:"sum_likes"`
  81. SumClick []struct {
  82. Value float64 `json:"value"`
  83. } `json:"sum_click"`
  84. }
  85. Page struct {
  86. Total int `json:"total"`
  87. }
  88. })
  89. if err = req.Scan(c, &res); err != nil || res == nil {
  90. log.Error("SearchArc req.Scan error(%v)", err)
  91. return
  92. }
  93. rs = &like.SubjectTotalStat{
  94. SumCoin: int64(res.Result.SumCoin[0].Value),
  95. SumFav: int64(res.Result.SumFav[0].Value),
  96. SumLike: int64(res.Result.SumLikes[0].Value),
  97. SumView: int64(res.Result.SumClick[0].Value),
  98. Count: res.Page.Total,
  99. }
  100. return
  101. }
  102. // AddLotteryTimes .
  103. func (dao *Dao) AddLotteryTimes(c context.Context, sid, mid int64) (err error) {
  104. params := url.Values{}
  105. params.Set("act_id", strconv.FormatInt(sid, 10))
  106. params.Set("mid", strconv.FormatInt(mid, 10))
  107. var res struct {
  108. Code int `json:"code"`
  109. }
  110. if err = dao.httpClient.Get(c, dao.addLotteryTimesURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  111. err = errors.Wrapf(err, "dao.client.Get(%s)", dao.addLotteryTimesURL+"?"+params.Encode())
  112. return
  113. }
  114. if res.Code != ecode.OK.Code() {
  115. err = ecode.Int(res.Code)
  116. }
  117. return
  118. }