exp.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/job/main/member/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/time"
  8. )
  9. const (
  10. _initExp = "INSERT IGNORE INTO user_exp_%02d (mid) VALUES (?)"
  11. _updateExpAped = `UPDATE user_exp_%02d SET exp=exp+?,flag=flag|? WHERE mid=? AND flag&?=0`
  12. _updateExpFlag = `UPDATE user_exp_%02d SET addtime=?,flag=? WHERE mid=? AND addtime<?`
  13. _SelExp = "SELECT mid,exp,flag,addtime,mtime FROM user_exp_%02d where mid=?"
  14. )
  15. // InitExp init user exp
  16. func (d *Dao) InitExp(c context.Context, mid int64) (err error) {
  17. _, err = d.db.Exec(c, fmt.Sprintf(_initExp, hit(mid)), mid)
  18. return
  19. }
  20. // SelExp get new user exp by mid
  21. func (d *Dao) SelExp(c context.Context, mid int64) (exp *model.NewExp, err error) {
  22. exp = &model.NewExp{}
  23. row := d.db.QueryRow(c, fmt.Sprintf(_SelExp, hit(mid)), mid)
  24. if err = row.Scan(&exp.Mid, &exp.Exp, &exp.Flag, &exp.Addtime, &exp.Mtime); err != nil {
  25. if err == sql.ErrNoRows {
  26. err = nil
  27. }
  28. return
  29. }
  30. return
  31. }
  32. // UpdateExpAped update user exp append mode.
  33. func (d *Dao) UpdateExpAped(c context.Context, mid, exp int64, flag int32) (rows int64, err error) {
  34. res, err := d.db.Exec(c, fmt.Sprintf(_updateExpAped, hit(mid)), exp, flag, mid, flag)
  35. if err != nil {
  36. return
  37. }
  38. rows, _ = res.RowsAffected()
  39. return
  40. }
  41. // UpdateExpFlag update user exp.
  42. func (d *Dao) UpdateExpFlag(c context.Context, mid int64, flag int32, addtime time.Time) (err error) {
  43. _, err = d.db.Exec(c, fmt.Sprintf(_updateExpFlag, hit(mid)), addtime, flag, mid, addtime)
  44. return
  45. }