mysql.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package pendant
  2. import (
  3. "context"
  4. "database/sql"
  5. "go-common/app/job/main/usersuit/model"
  6. xsql "go-common/library/database/sql"
  7. )
  8. const (
  9. _upEquipSQL = "UPDATE user_pendant_equip SET pid = 0 AND expires = 0 WHERE mid = ?"
  10. _upEquipExpiresSQL = "UPDATE user_pendant_equip SET expires = ? WHERE mid = ?"
  11. _selEquipSQL = "SELECT mid FROM user_pendant_equip WHERE expires =< ?"
  12. _selEquipMIDSQL = "SELECT mid,pid,expires FROM user_pendant_equip WHERE mid = ?"
  13. _selGidPidSQL = "SELECT gid FROM pendant_group_ref WHERE pid = ?"
  14. )
  15. // UpEquipMID update equip empty by mid
  16. func (d *Dao) UpEquipMID(c context.Context, mid int64) (affected int64, err error) {
  17. var res sql.Result
  18. if res, err = d.db.Exec(c, _upEquipSQL, mid); err != nil {
  19. return
  20. }
  21. return res.RowsAffected()
  22. }
  23. // UpEquipExpires update equip expires by mid
  24. func (d *Dao) UpEquipExpires(c context.Context, mid, expires int64) (affected int64, err error) {
  25. var res sql.Result
  26. if res, err = d.db.Exec(c, _upEquipExpiresSQL, expires, mid); err != nil {
  27. return
  28. }
  29. return res.RowsAffected()
  30. }
  31. // PendantEquipMID get user equip pendant by mid.
  32. func (d *Dao) PendantEquipMID(c context.Context, mid int64) (pe *model.PendantEquip, err error) {
  33. row := d.db.QueryRow(c, _selEquipMIDSQL, mid)
  34. pe = new(model.PendantEquip)
  35. if err = row.Scan(&pe.Mid, &pe.Pid, &pe.Expires); err != nil {
  36. return
  37. }
  38. return
  39. }
  40. // ExpireEquipPendant get expire equip pendant
  41. func (d *Dao) ExpireEquipPendant(c context.Context, expires int64) (res []int64, err error) {
  42. var (
  43. row *xsql.Rows
  44. mid int64
  45. )
  46. if row, err = d.db.Query(c, _selEquipSQL, expires); err != nil {
  47. return
  48. }
  49. defer row.Close()
  50. for row.Next() {
  51. if err = row.Scan(&mid); err != nil {
  52. return
  53. }
  54. res = append(res, mid)
  55. }
  56. return
  57. }
  58. // PendantEquipGidPid get gid of its equip pendant by pid.
  59. func (d *Dao) PendantEquipGidPid(c context.Context, pid int64) (gid int64, err error) {
  60. row := d.db.QueryRow(c, _selGidPidSQL, pid)
  61. if err = row.Scan(&gid); err != nil {
  62. return
  63. }
  64. return
  65. }