event.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/admin/main/workflow/model"
  5. "github.com/pkg/errors"
  6. )
  7. // EventsByCid will select events by cid
  8. func (d *Dao) EventsByCid(c context.Context, cid int64) (events map[int64]*model.Event, err error) {
  9. events = make(map[int64]*model.Event)
  10. elist := make([]*model.Event, 0)
  11. if err = d.ReadORM.Table("workflow_event").Where("cid=?", cid).Find(&elist).Error; err != nil {
  12. err = errors.WithStack(err)
  13. return
  14. }
  15. for _, e := range elist {
  16. e.FixAttachments()
  17. events[e.Eid] = e
  18. }
  19. return
  20. }
  21. // EventsByIDs will select events by eids
  22. func (d *Dao) EventsByIDs(c context.Context, eids []int64) (events map[int64]*model.Event, err error) {
  23. if len(eids) == 0 {
  24. return
  25. }
  26. events = make(map[int64]*model.Event, len(eids))
  27. elist := make([]*model.Event, 0)
  28. if err = d.ReadORM.Table("workflow_event").Where("id IN (?)", eids).Find(&elist).Error; err != nil {
  29. err = errors.WithStack(err)
  30. return
  31. }
  32. for _, e := range elist {
  33. e.FixAttachments()
  34. events[e.Eid] = e
  35. }
  36. return
  37. }
  38. // LastEventByCid will retrive last event by cid
  39. func (d *Dao) LastEventByCid(c context.Context, cid int64) (event *model.Event, err error) {
  40. event = new(model.Event)
  41. err = d.ReadORM.Table("workflow_event").Where("cid=?", cid).Order("id").Last(&event).Error
  42. if err != nil {
  43. err = errors.WithStack(err)
  44. return
  45. }
  46. return
  47. }
  48. // BatchLastEventIDs will retrive the last event ids by serveral conditions
  49. func (d *Dao) BatchLastEventIDs(c context.Context, cids []int64) (eids []int64, err error) {
  50. eids = make([]int64, 0, len(cids))
  51. if len(cids) <= 0 {
  52. return
  53. }
  54. rows, err := d.ReadORM.Table("workflow_event").Select("max(id)").Where("cid IN (?)", cids).Group("cid").Rows()
  55. if err != nil {
  56. err = errors.WithStack(err)
  57. return
  58. }
  59. defer rows.Close()
  60. for rows.Next() {
  61. var eid int64
  62. if err = rows.Scan(&eid); err != nil {
  63. err = errors.WithStack(err)
  64. return
  65. }
  66. eids = append(eids, eid)
  67. }
  68. return
  69. }