subject.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package like
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/interface/main/activity/model/like"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. "go-common/library/xstr"
  10. "github.com/pkg/errors"
  11. )
  12. const (
  13. _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 = ?"
  14. _votLogSQL = "INSERT INTO act_online_vote_log(sid,aid,mid,stage,vote) VALUES(?,?,?,?,?)"
  15. _subjectNewestSQL = "SELECT id,ctime FROM act_subject WHERE state = 1 AND type IN (%s) AND stime <= ? AND etime >= ? ORDER BY ctime DESC LIMIT 1"
  16. _actSubjectSQL = "SELECT id,name,dic,cover,stime,etime,flag,type,lstime,letime,act_url,uetime,ustime,level,h5_cover,rank,author,oid,state,ctime,mtime,like_limit,android_url,ios_url,daily_like_limit,daily_single_like_limit FROM act_subject WHERE id = ? and state = 1"
  17. _subjectInitSQL = "SELECT id,name,dic,cover,stime,etime,flag,type,lstime,letime,act_url,uetime,ustime,level,h5_cover,rank,author,oid,state,ctime,mtime,like_limit,android_url,ios_url,daily_like_limit,daily_single_like_limit FROM act_subject WHERE id > ? order by id asc limit 1000"
  18. _subjectMaxIDSQL = "SELECT id FROM act_subject order by id desc limit 1"
  19. //SubjectValidState act_subject valid state
  20. SubjectValidState = 1
  21. )
  22. // Subject Dao sql
  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. // VoteLog Dao sql
  38. func (dao *Dao) VoteLog(c context.Context, sid int64, aid int64, mid int64, stage int64, vote int64) (rows int64, err error) {
  39. rs, err := dao.voteLogStmt.Exec(c, sid, aid, mid, stage, vote)
  40. if err != nil {
  41. log.Error("d.VoteLog.Exec(%d, %d,%d, %d, %d) error(%v)", sid, aid, mid, stage, vote, err)
  42. return
  43. }
  44. rows, err = rs.RowsAffected()
  45. return
  46. }
  47. // NewestSubject get newest subject list.
  48. func (dao *Dao) NewestSubject(c context.Context, typeIDs []int64) (res *like.SubItem, err error) {
  49. res = new(like.SubItem)
  50. now := time.Now()
  51. row := dao.db.QueryRow(c, fmt.Sprintf(_subjectNewestSQL, xstr.JoinInts(typeIDs)), now, now)
  52. if err = row.Scan(&res.ID, &res.Ctime); err != nil {
  53. if err == sql.ErrNoRows {
  54. err = nil
  55. } else {
  56. err = errors.Wrap(err, "NewestPage:QueryRow")
  57. }
  58. }
  59. return
  60. }
  61. // RawActSubject get act_subject by id .
  62. func (dao *Dao) RawActSubject(c context.Context, id int64) (res *like.SubjectItem, err error) {
  63. res = new(like.SubjectItem)
  64. row := dao.db.QueryRow(c, _actSubjectSQL, id)
  65. if err = row.Scan(&res.ID, &res.Name, &res.Dic, &res.Cover, &res.Stime, &res.Etime, &res.Flag, &res.Type, &res.Lstime, &res.Letime, &res.ActURL, &res.Uetime, &res.Ustime, &res.Level, &res.H5Cover, &res.Rank, &res.Author, &res.Oid, &res.State, &res.Ctime, &res.Mtime, &res.LikeLimit, &res.AndroidURL, &res.IosURL, &res.DailyLikeLimit, &res.DailySingleLikeLimit); err != nil {
  66. if err == sql.ErrNoRows {
  67. err = nil
  68. } else {
  69. err = errors.Wrap(err, "ActSubject:QueryRow")
  70. }
  71. }
  72. return
  73. }
  74. // SubjectListMoreSid get subject more sid .
  75. func (dao *Dao) SubjectListMoreSid(c context.Context, minSid int64) (res []*like.SubjectItem, err error) {
  76. var rows *sql.Rows
  77. if rows, err = dao.db.Query(c, _subjectInitSQL, minSid); err != nil {
  78. if err == sql.ErrNoRows {
  79. err = nil
  80. } else {
  81. err = errors.Wrap(err, "SubjectInitialize:dao.db.Query()")
  82. }
  83. return
  84. }
  85. defer rows.Close()
  86. res = make([]*like.SubjectItem, 0, 1000)
  87. for rows.Next() {
  88. a := &like.SubjectItem{}
  89. if err = rows.Scan(&a.ID, &a.Name, &a.Dic, &a.Cover, &a.Stime, &a.Etime, &a.Flag, &a.Type, &a.Lstime, &a.Letime, &a.ActURL, &a.Uetime, &a.Ustime, &a.Level, &a.H5Cover, &a.Rank, &a.Author, &a.Oid, &a.State, &a.Ctime, &a.Mtime, &a.LikeLimit, &a.AndroidURL, &a.IosURL, &a.DailyLikeLimit, &a.DailySingleLikeLimit); err != nil {
  90. err = errors.Wrap(err, "SubjectInitialize:rows.Scan()")
  91. return
  92. }
  93. res = append(res, a)
  94. }
  95. if err = rows.Err(); err != nil {
  96. err = errors.Wrap(err, "SubjectInitialize:rows.Err()")
  97. }
  98. return
  99. }
  100. // SubjectMaxID get act_subject max id .
  101. func (dao *Dao) SubjectMaxID(c context.Context) (res *like.SubjectItem, err error) {
  102. res = new(like.SubjectItem)
  103. row := dao.db.QueryRow(c, _subjectMaxIDSQL)
  104. if err = row.Scan(&res.ID); err != nil {
  105. if err == sql.ErrNoRows {
  106. err = nil
  107. } else {
  108. err = errors.Wrap(err, "SubjectMaxID:QueryRow")
  109. }
  110. }
  111. return
  112. }