mission_group.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package like
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. l "go-common/app/interface/main/activity/model/like"
  7. "go-common/library/xstr"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. _likeMissionBuffSQL = "select id from like_mission_group where sid = ? and mid = ?"
  12. _likeMissionAddSQL = "insert into like_mission_group (`sid`,`mid`,`state`) values (?,?,?)"
  13. _likeMissionGroupSQL = "select id,sid,mid,state,ctime,mtime from like_mission_group where id in (%s)"
  14. // MissionStateInit the init state
  15. MissionStateInit = 0
  16. )
  17. // RawLikeMissionBuff get mid has .
  18. func (d *Dao) RawLikeMissionBuff(c context.Context, sid, mid int64) (ID int64, err error) {
  19. res := &l.MissionGroup{}
  20. row := d.db.QueryRow(c, _likeMissionBuffSQL, sid, mid)
  21. if err = row.Scan(&res.ID); err != nil {
  22. if err == sql.ErrNoRows {
  23. err = nil
  24. } else {
  25. err = errors.Wrap(err, "RawLikeMissionBuff:QueryRow")
  26. return
  27. }
  28. }
  29. ID = res.ID
  30. return
  31. }
  32. // MissionGroupAdd add like_mission_group data .
  33. func (d *Dao) MissionGroupAdd(c context.Context, group *l.MissionGroup) (misID int64, err error) {
  34. var res sql.Result
  35. if res, err = d.db.Exec(c, _likeMissionAddSQL, group.Sid, group.Mid, group.State); err != nil {
  36. err = errors.Wrapf(err, "d.db.Exec(%s)", _likeMissionAddSQL)
  37. return
  38. }
  39. return res.LastInsertId()
  40. }
  41. // RawMissionGroupItems get mission_group item by ids.
  42. func (d *Dao) RawMissionGroupItems(c context.Context, lids []int64) (res map[int64]*l.MissionGroup, err error) {
  43. res = make(map[int64]*l.MissionGroup, len(lids))
  44. rows, err := d.db.Query(c, fmt.Sprintf(_likeMissionGroupSQL, xstr.JoinInts(lids)))
  45. if err != nil {
  46. err = errors.Wrapf(err, "d.db.Query(%s)", _likeMissionGroupSQL)
  47. return
  48. }
  49. defer rows.Close()
  50. for rows.Next() {
  51. n := &l.MissionGroup{}
  52. if err = rows.Scan(&n.ID, &n.Sid, &n.Mid, &n.State, &n.Ctime, &n.Mtime); err != nil {
  53. err = errors.Wrapf(err, "d.db.Scan(%s)", _likeMissionGroupSQL)
  54. return
  55. }
  56. res[n.ID] = n
  57. }
  58. if err = rows.Err(); err != nil {
  59. err = errors.Wrap(err, "RawMissionGroupItem:rows.Err()")
  60. }
  61. return
  62. }