masterpiece.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/space/model"
  6. xsql "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _masterpieceFmt = "spc_mp_%d"
  11. _masterpieceSQL = `SELECT aid,recommend_reason FROM member_masterpiece%d WHERE mid = ?`
  12. _masterpieceAddSQL = `INSERT INTO member_masterpiece%d (mid,aid,recommend_reason) VALUES (?,?,?)`
  13. _masterpieceEditSQL = `UPDATE member_masterpiece%d SET aid = ?,recommend_reason = ? WHERE mid = ? AND aid = ?`
  14. _masterpieceDelSQL = `DELETE FROM member_masterpiece%d WHERE mid = ? AND aid = ?`
  15. )
  16. func masterpieceHit(mid int64) int64 {
  17. return mid % 10
  18. }
  19. func masterpieceKey(mid int64) string {
  20. return fmt.Sprintf(_masterpieceFmt, mid)
  21. }
  22. // RawMasterpiece get masterpiece from db.
  23. func (d *Dao) RawMasterpiece(c context.Context, mid int64) (res *model.AidReasons, err error) {
  24. var (
  25. rows *xsql.Rows
  26. list []*model.AidReason
  27. )
  28. res = new(model.AidReasons)
  29. if rows, err = d.db.Query(c, fmt.Sprintf(_masterpieceSQL, masterpieceHit(mid)), mid); err != nil {
  30. log.Error("RawMasterpiece d.db.Query(%d) error(%v)", mid, err)
  31. return
  32. }
  33. defer rows.Close()
  34. for rows.Next() {
  35. r := new(model.AidReason)
  36. if err = rows.Scan(&r.Aid, &r.Reason); err != nil {
  37. log.Error("RawMasterpiece row.Scan() error(%v)", err)
  38. return
  39. }
  40. list = append(list, r)
  41. }
  42. if err = rows.Err(); err != nil {
  43. log.Error("RawMasterpiece rows.error(%v)", err)
  44. return
  45. }
  46. res.List = list
  47. return
  48. }
  49. // AddMasterpiece add masterpiece data.
  50. func (d *Dao) AddMasterpiece(c context.Context, mid, aid int64, reason string) (err error) {
  51. if _, err = d.db.Exec(c, fmt.Sprintf(_masterpieceAddSQL, masterpieceHit(mid)), mid, aid, reason); err != nil {
  52. log.Error("AddMasterpiece error d.db.Exec(%d,%d,%s) error(%v)", mid, aid, reason, err)
  53. }
  54. return
  55. }
  56. // EditMasterpiece edit masterpiece data.
  57. func (d *Dao) EditMasterpiece(c context.Context, mid, aid, preAid int64, reason string) (err error) {
  58. if _, err = d.db.Exec(c, fmt.Sprintf(_masterpieceEditSQL, masterpieceHit(mid)), aid, reason, mid, preAid); err != nil {
  59. log.Error("EditMasterpiece error d.db.Exec(%d,%d,%s) error(%v)", mid, aid, reason, err)
  60. }
  61. return
  62. }
  63. // DelMasterpiece delete masterpiece.
  64. func (d *Dao) DelMasterpiece(c context.Context, mid, aid int64) (err error) {
  65. if _, err = d.db.Exec(c, fmt.Sprintf(_masterpieceDelSQL, masterpieceHit(mid)), mid, aid); err != nil {
  66. log.Error("DelMasterpiece error d.db.Exec(%d,%d) error(%v)", mid, aid, err)
  67. }
  68. return
  69. }