event.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package service
  2. import (
  3. "context"
  4. "sort"
  5. "go-common/app/admin/main/workflow/model"
  6. "go-common/app/admin/main/workflow/model/param"
  7. "go-common/library/log"
  8. )
  9. // AddEvent will add a event
  10. func (s *Service) AddEvent(c context.Context, ep *param.EventParam) (eid int64, err error) {
  11. e := &model.Event{
  12. Cid: ep.Cid,
  13. AdminID: ep.AdminID,
  14. Content: ep.Content,
  15. Attachments: ep.Attachments,
  16. Event: ep.Event,
  17. }
  18. if err = s.dao.ORM.Create(e).Error; err != nil {
  19. log.Error("Failed to create event(%v): %v", e, err)
  20. return
  21. }
  22. eid = e.Eid
  23. s.task(func() {
  24. var c *model.Chall
  25. if c, err = s.dao.Chall(context.Background(), e.Cid); err != nil {
  26. log.Error("s.dao.Chall(%d) error(%v)", e.Cid, err)
  27. err = nil
  28. return
  29. }
  30. s.afterAddReply(ep, c)
  31. })
  32. return
  33. }
  34. // BatchAddEvent will add events to batch chall
  35. func (s *Service) BatchAddEvent(c context.Context, bep *param.BatchEventParam) (eids []int64, err error) {
  36. if len(bep.Cids) <= 0 {
  37. return
  38. }
  39. eids = make([]int64, 0, len(bep.Cids))
  40. for _, cid := range bep.Cids {
  41. e := &model.Event{
  42. Cid: cid,
  43. AdminID: bep.AdminID,
  44. Content: bep.Content,
  45. Attachments: bep.Attachments,
  46. Event: bep.Event,
  47. }
  48. if err = s.dao.ORM.Create(e).Error; err != nil {
  49. log.Error("Failed to create event(%v): %v", e, err)
  50. return
  51. }
  52. eids = append(eids, int64(e.Eid))
  53. }
  54. s.task(func() {
  55. var challs map[int64]*model.Chall
  56. if challs, err = s.dao.Challs(context.Background(), bep.Cids); err != nil {
  57. log.Error("s.dao.Challs(%v) error(%v)", bep.Cids, err)
  58. return
  59. }
  60. s.afterAddMultiReply(bep, challs)
  61. })
  62. return
  63. }
  64. // ListEvent will add a set of events by challenge id
  65. func (s *Service) ListEvent(c context.Context, cid int64) (eventList model.EventSlice, err error) {
  66. var (
  67. events map[int64]*model.Event
  68. )
  69. if events, err = s.dao.EventsByCid(c, cid); err != nil {
  70. log.Error("Failed to s.dao.Events(%d): %v", cid, err)
  71. return
  72. }
  73. eventList = make(model.EventSlice, 0, len(events))
  74. for _, e := range events {
  75. eventList = append(eventList, e)
  76. }
  77. sort.Slice(eventList, func(i, j int) bool {
  78. return eventList[i].CTime < eventList[j].CTime
  79. })
  80. return
  81. }
  82. // batchLastEvent will return the last log on specified targets
  83. func (s *Service) batchLastEvent(c context.Context, cids []int64) (cEvents map[int64]*model.Event, err error) {
  84. var (
  85. eids []int64
  86. events map[int64]*model.Event
  87. )
  88. if eids, err = s.dao.BatchLastEventIDs(c, cids); err != nil {
  89. log.Error("s.dao.BatchLastEventIDs(%d) error(%v)", cids, err)
  90. return
  91. }
  92. if events, err = s.dao.EventsByIDs(c, eids); err != nil {
  93. log.Error("s.dao.EventsByIDs(%d) error(%v)", eids, err)
  94. return
  95. }
  96. cEvents = make(map[int64]*model.Event, len(eids))
  97. for _, e := range events {
  98. cEvents[e.Cid] = e
  99. }
  100. return
  101. }