reply_content.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/reply/model"
  6. xsql "go-common/library/database/sql"
  7. "go-common/library/xstr"
  8. "time"
  9. )
  10. const _contSharding int64 = 200
  11. const (
  12. _selReplyContentSQL = "SELECT rpid,message,ats,ip,plat,device,ctime,mtime FROM reply_content_%d WHERE rpid=?"
  13. _selReplyContentsSQL = "SELECT rpid,message,ats,ip,plat,device,ctime,mtime FROM reply_content_%d WHERE rpid IN (%s)"
  14. _selContSQL = "SELECT rpid,message,ats,ip,plat,device FROM reply_content_%d WHERE rpid=?"
  15. _selContsSQL = "SELECT rpid,message,ats,ip,plat,device FROM reply_content_%d WHERE rpid IN (%s)"
  16. _upContMsgSQL = "UPDATE reply_content_%d SET message=?,mtime=? WHERE rpid=?"
  17. )
  18. // UpReplyContent update reply content's message.
  19. func (d *Dao) UpReplyContent(c context.Context, oid int64, rpID int64, msg string, now time.Time) (rows int64, err error) {
  20. res, err := d.db.Exec(c, fmt.Sprintf(_upContMsgSQL, hit(oid)), msg, now, rpID)
  21. if err != nil {
  22. return
  23. }
  24. return res.RowsAffected()
  25. }
  26. // ReplyContent get a ReplyContent from database.
  27. func (d *Dao) ReplyContent(c context.Context, oid, rpID int64) (rc *model.ReplyContent, err error) {
  28. rc = new(model.ReplyContent)
  29. row := d.db.QueryRow(c, fmt.Sprintf(_selReplyContentSQL, hit(oid)), rpID)
  30. if err = row.Scan(&rc.ID, &rc.Message, &rc.Ats, &rc.IP, &rc.Plat, &rc.Device, &rc.CTime, &rc.MTime); err != nil {
  31. if err == xsql.ErrNoRows {
  32. rc = nil
  33. err = nil
  34. }
  35. }
  36. return
  37. }
  38. // ReplyContents get reply contents by ids.
  39. func (d *Dao) ReplyContents(c context.Context, oids []int64, rpIds []int64) (rcMap map[int64]*model.ReplyContent, err error) {
  40. hitMap := make(map[int64][]int64)
  41. for i, oid := range oids {
  42. hitMap[hit(oid)] = append(hitMap[hit(oid)], rpIds[i])
  43. }
  44. rcMap = make(map[int64]*model.ReplyContent, len(rpIds))
  45. for hit, ids := range hitMap {
  46. var rows *xsql.Rows
  47. rows, err = d.db.Query(c, fmt.Sprintf(_selReplyContentsSQL, hit, xstr.JoinInts(ids)))
  48. if err != nil {
  49. return
  50. }
  51. defer rows.Close()
  52. for rows.Next() {
  53. rc := &model.ReplyContent{}
  54. if err = rows.Scan(&rc.ID, &rc.Message, &rc.Ats, &rc.IP, &rc.Plat, &rc.Device, &rc.CTime, &rc.MTime); err != nil {
  55. return
  56. }
  57. rcMap[rc.ID] = rc
  58. }
  59. if err = rows.Err(); err != nil {
  60. return
  61. }
  62. }
  63. return
  64. }