reply.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/interface/main/feedback/model"
  5. "go-common/library/database/sql"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _inReply = "INSERT INTO reply (session_id,reply_id,content,img_url,log_url,ctime,mtime) VALUES (?,?,?,?,?,?,?)"
  10. _selReply = "SELECT reply_id,type,content,img_url,log_url,ctime FROM reply WHERE session_id=? ORDER BY id DESC LIMIT ?,?"
  11. _selReplyByMid = "SELECT r.reply_id,r.type,r.content,r.img_url,r.log_url,r.ctime FROM reply r INNER JOIN session s ON r.session_id=s.id WHERE s.mid=? ORDER BY r.id DESC LIMIT ?,?"
  12. _selReplyBySid = "SELECT r.reply_id,r.type,r.content,r.img_url,r.log_url,r.ctime FROM reply r INNER JOIN session s ON r.session_id=s.id WHERE r.session_id=? AND s.mid=? ORDER BY r.id"
  13. )
  14. // TxAddReply implements add a new reply record
  15. func (d *Dao) TxAddReply(tx *sql.Tx, r *model.Reply) (id int64, err error) {
  16. res, err := tx.Exec(_inReply, r.SessionID, r.ReplyID, r.Content, r.ImgURL, r.LogURL, r.CTime, r.MTime)
  17. if err != nil {
  18. log.Error("AddReply tx.Exec() error(%v)", err)
  19. return
  20. }
  21. return res.LastInsertId()
  22. }
  23. // AddReply insert reply.
  24. func (d *Dao) AddReply(c context.Context, r *model.Reply) (id int64, err error) {
  25. res, err := d.inReply.Exec(c, r.SessionID, r.ReplyID, r.Content, r.ImgURL, r.LogURL, r.CTime, r.MTime)
  26. if err != nil {
  27. log.Error("AddReply error(%v)", err)
  28. return
  29. }
  30. return res.LastInsertId()
  31. }
  32. // Replys returns corresponding user feedback reply records
  33. func (d *Dao) Replys(c context.Context, ssnID int64, offset, limit int) (rs []model.Reply, err error) {
  34. rows, err := d.selReply.Query(c, ssnID, offset, limit)
  35. if err != nil {
  36. log.Error("d.selReply.Query() error(%v)", err)
  37. return
  38. }
  39. defer rows.Close()
  40. for rows.Next() {
  41. var r = model.Reply{}
  42. if err = rows.Scan(&r.ReplyID, &r.Type, &r.Content, &r.ImgURL, &r.LogURL, &r.CTime); err != nil {
  43. log.Error("row.Scan() error(%s)", err)
  44. rs = nil
  45. return
  46. }
  47. rs = append(rs, r)
  48. }
  49. return
  50. }
  51. // WebReplys get by ssnID.
  52. func (d *Dao) WebReplys(c context.Context, ssnID, mid int64) (rs []*model.Reply, err error) {
  53. rows, err := d.selReplyBySid.Query(c, ssnID, mid)
  54. if err != nil {
  55. log.Error("d.selReply.Query(%d, %d) error(%v)", ssnID, mid, err)
  56. return
  57. }
  58. defer rows.Close()
  59. for rows.Next() {
  60. var r = &model.Reply{}
  61. if err = rows.Scan(&r.ReplyID, &r.Type, &r.Content, &r.ImgURL, &r.LogURL, &r.CTime); err != nil {
  62. log.Error("row.Scan() error(%s)", err)
  63. rs = nil
  64. return
  65. }
  66. rs = append(rs, r)
  67. }
  68. return
  69. }
  70. // ReplysByMid returns corresponding user feedback reply records by mid
  71. func (d *Dao) ReplysByMid(c context.Context, mid int64, offset, limit int) (rs []model.Reply, err error) {
  72. rows, err := d.selReplyByMid.Query(c, mid, offset, limit)
  73. if err != nil {
  74. log.Error("d.selReplyByMid.Query() error(%v)", err)
  75. return
  76. }
  77. defer rows.Close()
  78. for rows.Next() {
  79. var r = model.Reply{}
  80. if err = rows.Scan(&r.ReplyID, &r.Type, &r.Content, &r.ImgURL, &r.LogURL, &r.CTime); err != nil {
  81. log.Error("row.Scan() error(%s)", err)
  82. rs = nil
  83. return
  84. }
  85. rs = append(rs, r)
  86. }
  87. return
  88. }