opinion.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/credit/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. _addOpinionSQL = "INSERT INTO blocked_opinion(vid,cid,content,attr,mid,vote,state) VALUE(?,?,?,?,?,?,?)"
  12. _addLikesSQL = "UPDATE blocked_opinion SET likes=likes+1 where vid in(%s)"
  13. _addHatesSQL = "UPDATE blocked_opinion SET hates=hates+1 where vid in(%s)"
  14. _delOpinionSQL = "UPDATE blocked_opinion SET state = 1 WHERE vid = ?"
  15. _selOpinionIdxSQL = "SELECT vid,likes,hates,vote FROM blocked_opinion WHERE cid = ? AND state = 0 ORDER BY likes DESC"
  16. _selOpinionsSQL = "SELECT vid,content,attr,hates,likes,mid,vote FROM blocked_opinion WHERE vid IN(%s) AND state = 0"
  17. _opContentMidSQL = "SELECT content FROM blocked_opinion WHERE mid = ? AND state = 0 ORDER BY id DESC LIMIT 1"
  18. )
  19. // AddOpinionTx add user opinion by transaction.
  20. func (d *Dao) AddOpinionTx(tx *sql.Tx, cid, opid, mid int64, content string, attr, vote, state int8) (affect int64, err error) {
  21. row, err := tx.Exec(_addOpinionSQL, opid, cid, content, attr, mid, vote, state)
  22. if err != nil {
  23. log.Error("add opinion err(%v)", err)
  24. return
  25. }
  26. return row.RowsAffected()
  27. }
  28. // AddLikes add user like opinions.
  29. func (d *Dao) AddLikes(c context.Context, ids []int64) (affect int64, err error) {
  30. rows, err := d.db.Exec(c, fmt.Sprintf(_addLikesSQL, xstr.JoinInts(ids)))
  31. if err != nil {
  32. log.Error("d.AddLikes err(%v) ids(%v)", err, ids)
  33. return
  34. }
  35. return rows.RowsAffected()
  36. }
  37. // AddHates add user like opinions.
  38. func (d *Dao) AddHates(c context.Context, ids []int64) (affect int64, err error) {
  39. rows, err := d.db.Exec(c, fmt.Sprintf(_addHatesSQL, xstr.JoinInts(ids)))
  40. if err != nil {
  41. log.Error("d.AddHates err(%v) ids(%v)", err, ids)
  42. return
  43. }
  44. return rows.RowsAffected()
  45. }
  46. // DelOpinion del opinion.
  47. func (d *Dao) DelOpinion(c context.Context, opid int64) (err error) {
  48. if _, err = d.db.Exec(c, _delOpinionSQL, opid); err != nil {
  49. log.Error("d.Delopinion err(%v)", err)
  50. }
  51. return
  52. }
  53. // OpinionIdx get opinion index.
  54. func (d *Dao) OpinionIdx(c context.Context, cid int64) (ops []*model.Opinion, err error) {
  55. rows, err := d.db.Query(c, _selOpinionIdxSQL, cid)
  56. if err != nil {
  57. log.Error("OpinionIdx err(%v)", err)
  58. return
  59. }
  60. defer rows.Close()
  61. for rows.Next() {
  62. op := &model.Opinion{}
  63. if err = rows.Scan(&op.OpID, &op.Like, &op.Hate, &op.Vote); err != nil {
  64. log.Error("row.Scan err(%v)", err)
  65. return
  66. }
  67. ops = append(ops, op)
  68. }
  69. return
  70. }
  71. // OpinionCaseIdx get opinion case index.
  72. func (d *Dao) OpinionCaseIdx(c context.Context, cid int64) (ops []*model.Opinion, err error) {
  73. rows, err := d.db.Query(c, _selOpinionIdxSQL, cid)
  74. if err != nil {
  75. log.Error("OpinionIdx err(%v)", err)
  76. return
  77. }
  78. defer rows.Close()
  79. for rows.Next() {
  80. op := &model.Opinion{}
  81. if err = rows.Scan(&op.OpID, &op.Like, &op.Hate, &op.Vote); err != nil {
  82. log.Error("row.Scan err(%v)", err)
  83. return
  84. }
  85. if op.Like-op.Hate > -5 {
  86. ops = append(ops, op)
  87. }
  88. }
  89. return
  90. }
  91. // Opinions get opinions.
  92. func (d *Dao) Opinions(c context.Context, opids []int64) (ops []*model.Opinion, err error) {
  93. rows, err := d.db.Query(c, fmt.Sprintf(_selOpinionsSQL, xstr.JoinInts(opids)))
  94. if err != nil {
  95. log.Error("d.Opinions vids(%v) err(%v)", err, opids)
  96. return
  97. }
  98. defer rows.Close()
  99. for rows.Next() {
  100. op := &model.Opinion{}
  101. if err = rows.Scan(&op.OpID, &op.Content, &op.Attr, &op.Hate, &op.Like, &op.Mid, &op.Vote); err != nil {
  102. log.Error("row.Scan err(%v)", err)
  103. }
  104. ops = append(ops, op)
  105. }
  106. return
  107. }
  108. // OpContentMid get opinion content.
  109. func (d *Dao) OpContentMid(c context.Context, mid int64) (content string, err error) {
  110. row := d.db.QueryRow(c, _opContentMidSQL, mid)
  111. if err = row.Scan(&content); err != nil {
  112. if err != sql.ErrNoRows {
  113. log.Error("row.Scan() error(%v)", err)
  114. return
  115. }
  116. content = ""
  117. err = nil
  118. }
  119. return
  120. }