user_achievements.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package like
  2. import (
  3. "context"
  4. "database/sql"
  5. l "go-common/app/interface/main/activity/model/like"
  6. "github.com/pkg/errors"
  7. )
  8. const (
  9. _userAchieveSQL = "select id,aid,sid,mid,award from act_like_user_achievements where id = ?"
  10. _userAchieveUpSQL = "update act_like_user_achievements set award = ? where id = ? and award = 0"
  11. _userAchievementAddSQL = "insert into act_like_user_achievements (`aid`,`mid`,`sid`,`award`) values(?,?,?,?)"
  12. _userAchievementSQL = "select id,aid,sid,mid,award from act_like_user_achievements where sid = ? and mid = ? and del = 0"
  13. // AwardNotChange .
  14. AwardNotChange = 0
  15. // AwardHasChange .
  16. AwardHasChange = 1
  17. // AwardNoGet .
  18. AwardNoGet = 2
  19. )
  20. // AddUserAchievment .
  21. func (d *Dao) AddUserAchievment(c context.Context, userAchi *l.ActLikeUserAchievement) (ID int64, err error) {
  22. var res sql.Result
  23. if res, err = d.db.Exec(c, _userAchievementAddSQL, userAchi.Aid, userAchi.Mid, userAchi.Sid, userAchi.Award); err != nil {
  24. err = errors.Wrapf(err, "d.db.Exec(%s)", _userAchievementAddSQL)
  25. return
  26. }
  27. return res.LastInsertId()
  28. }
  29. // UserAchievement .
  30. func (d *Dao) UserAchievement(c context.Context, sid, mid int64) (res []*l.ActLikeUserAchievement, err error) {
  31. rows, err := d.db.Query(c, _userAchievementSQL, sid, mid)
  32. if err != nil {
  33. err = errors.Wrapf(err, "d.db.Query(%s)", _userAchievementSQL)
  34. return
  35. }
  36. for rows.Next() {
  37. n := &l.ActLikeUserAchievement{}
  38. if err = rows.Scan(&n.ID, &n.Aid, &n.Sid, &n.Mid, &n.Award); err != nil {
  39. err = errors.Wrapf(err, "d.db.Scan(%s)", _userAchievementSQL)
  40. return
  41. }
  42. res = append(res, n)
  43. }
  44. if err = rows.Err(); err != nil {
  45. err = errors.Wrapf(err, "rows.Err(%s)", _userAchievementSQL)
  46. }
  47. return
  48. }
  49. // RawActUserAchieve .
  50. func (d *Dao) RawActUserAchieve(c context.Context, id int64) (res *l.ActLikeUserAchievement, err error) {
  51. res = &l.ActLikeUserAchievement{}
  52. if err = d.db.QueryRow(c, _userAchieveSQL, id).Scan(&res.ID, &res.Aid, &res.Sid, &res.Mid, &res.Award); err != nil {
  53. if err == sql.ErrNoRows {
  54. err = nil
  55. } else {
  56. err = errors.Wrapf(err, "d.db.QueryRow(%s)", _userAchieveSQL)
  57. }
  58. }
  59. return
  60. }
  61. // ActUserAchieveChange .
  62. func (d *Dao) ActUserAchieveChange(c context.Context, id, award int64) (upID int64, err error) {
  63. var res sql.Result
  64. if res, err = d.db.Exec(c, _userAchieveUpSQL, award, id); err != nil {
  65. err = errors.Wrapf(err, "d.db.Exec(%s)", _userAchieveUpSQL)
  66. return
  67. }
  68. return res.RowsAffected()
  69. }