event.go
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package domain
import "github.com/gookit/event"
type EventType string
func (et EventType) ToString() string {
return string(et)
}
const (
TableDataEditEvent EventType = "table.data.edit"
TableStructEditEvent EventType = "table.struct.edit"
TableDataImportEvent EventType = "table.data.import"
TableDeleteEvent EventType = "table.delete"
QuerySetUpdateEvent EventType = "table.query.set.update"
QuerySetUpdateRenameEvent EventType = "table.query.set.update.rename"
TableApplyOnEvent EventType = "table.apply-on"
QuerySetUpdateStatusEvent EventType = "table.query.set.update.status" // 禁用启用
QuerySetDeleteEvent EventType = "table.query.set.delete" // 删除
)
// EventTable 表事件
type EventTable struct {
Context *Context
Type EventType
Table *Table
QuerySet *QuerySet
OldQuerySet *QuerySet
OldTable *Table
Metadata map[string]interface{}
}
func NewEventTable(ctx *Context, t EventType) *EventTable {
return &EventTable{
Context: ctx,
Type: t,
Metadata: make(map[string]interface{}),
}
}
func (et *EventTable) WithContext(t *Context) *EventTable {
et.Context = t
return et
}
func (et *EventTable) WithType(t EventType) *EventTable {
et.Type = t
return et
}
func (et *EventTable) WithTable(t *Table) *EventTable {
et.Table = t
return et
}
func (et *EventTable) WithOldTable(t *Table) *EventTable {
et.OldTable = t
return et
}
func (et *EventTable) WithQuerySet(t *QuerySet) *EventTable {
et.QuerySet = t
return et
}
func (et *EventTable) WithOldQuerySet(t *QuerySet) *EventTable {
et.OldQuerySet = t
return et
}
func (et *EventTable) WithMetadata(key string, values interface{}) *EventTable {
et.Metadata[key] = values
return et
}
func (et *EventTable) ResolveEvent(e event.Event) {
et.Context = e.Get("Context").(*Context)
et.Type = e.Get("Type").(EventType)
et.Table = e.Get("Table").(*Table)
et.QuerySet = e.Get("QuerySet").(*QuerySet)
et.OldQuerySet = e.Get("OldQuerySet").(*QuerySet)
et.OldTable = e.Get("OldTable").(*Table)
et.Metadata = e.Get("Metadata").(map[string]interface{})
}
func (et *EventTable) FireEvent() event.Event {
e := event.M{}
e["Context"] = et.Context
e["Type"] = et.Type
e["Table"] = et.Table
e["OldTable"] = et.OldTable
e["QuerySet"] = et.QuerySet
e["OldQuerySet"] = et.OldQuerySet
e["Metadata"] = et.Metadata
return event.MustFire(et.Type.ToString(), e)
}