table_object_search.go
5.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package service
import (
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/dto"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/query"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
"sort"
)
func (tableService *TableService) TableObjectSearch(searchQuery *query.SearchTableQuery) (interface{}, error) {
if err := searchQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
tableRepository, _, _ := factory.FastPgTable(transactionContext, 0)
_, tables, err := tableRepository.Find(utils.ObjectToMap(searchQuery))
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var result = make([]*dto.TableObjectDto, 0)
for _, table := range tables {
var item = &dto.TableObjectDto{}
item.Load(table)
if searchQuery.ReturnDetailStructInfo {
item.SetDetailStructInfo(table)
}
item.Flag = domain.FlagSet
if item.TableType == domain.MainTable.ToString() ||
item.TableType == domain.SubTable.ToString() ||
item.TableType == domain.SideTable.ToString() {
item.ParentId = 0
item.Status = domain.StatusOn
}
result = append(result, item)
}
if searchQuery.TableId > 0 {
return map[string]interface{}{
"count": len(result),
"tableObjects": result,
}, nil
}
querySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0)
_, querySets, _ := querySetRepository.Find(map[string]interface{}{"context": searchQuery.Context})
var (
querySetMapById = make(map[int]*domain.QuerySet)
querySetMapByTableId = make(map[int]*domain.QuerySet)
)
if searchQuery.ReturnGroupItem != nil && *searchQuery.ReturnGroupItem {
for _, qs := range querySets {
querySetMapById[qs.QuerySetId] = qs
}
}
for _, qs := range querySets {
if qs.QuerySetInfo.BindTableId == 0 {
continue
}
querySetMapByTableId[qs.QuerySetInfo.BindTableId] = qs
}
var response = make([]*dto.TableObjectDto, 0)
for index, t := range result {
v, ok := querySetMapByTableId[t.TableId]
if !ok {
continue
}
result[index].Update(v)
}
// 分组
querySetMapGroup := make(map[int]bool)
querySetGroups := make([]*domain.QuerySet, 0)
for _, t := range result {
if filterTableByFilterRule(t, searchQuery) {
continue
}
// 无模块权限 并且 关闭状态的都不返回(拆解、计算)
if !moduleHasAuth(searchQuery.CurrentModule, t.TableType) && t.Status == domain.StatusOff {
continue
}
if !domain.TableType(t.TableType).TableIsSplitByGroup() {
response = append(response, t)
continue
}
v, ok := querySetMapByTableId[t.TableId]
if !ok {
response = append(response, t)
continue
}
t.Update(v)
parentGroupId := v.ParentId
response = append(response, t)
for {
if parentGroupId == 0 {
break
}
if _, ok := querySetMapGroup[parentGroupId]; ok {
break
}
querySetMapGroup[parentGroupId] = true
if v, ok := querySetMapById[parentGroupId]; ok {
querySetGroups = append(querySetGroups, v)
parentGroupId = v.ParentId
}
}
}
for _, querySetGroup := range querySetGroups {
var groupItem = &dto.TableObjectDto{}
groupItem.LoadGroup(querySetGroup)
response = append(response, groupItem)
}
sort.Slice(response, func(i, j int) bool {
item1 := response[i]
item2 := response[j]
// 拆解模块按应用时间倒叙排列
if domain.AssertTableType(item1.TableType, domain.MainTable, domain.SubTable, domain.SideTable) &&
domain.AssertTableType(item2.TableType, domain.MainTable, domain.SubTable, domain.SideTable) {
return item1.ApplyAt > item2.ApplyAt
}
return item1.Id < item2.Id
})
return map[string]interface{}{
"count": len(response),
"tableObjects": response,
}, nil
}
// true:代表需要过滤 false:不需要过滤
func filterTableByFilterRule(item *dto.TableObjectDto, searchQuery *query.SearchTableQuery) bool {
filterRules := searchQuery.FilterRules
if len(searchQuery.ExcludeTables) > 0 {
for _, t := range searchQuery.ExcludeTables {
if t == item.TableId {
return true
}
}
}
for _, rule := range filterRules {
if rule.TableType == item.TableType && rule.Status > 0 && rule.Status != item.Status {
return true
}
if rule.TableType == item.TableType && rule.Status == 0 {
return true
}
}
for _, rule := range filterRules {
if rule.TableType == "*" && rule.Status > 0 && rule.Status != item.Status {
return true
}
}
return false
}
// 验证当前模块的权限。属于当前模块可以访问所有
func moduleHasAuth(currentModule int, t string) bool {
if currentModule == 0 {
return false
}
if currentModule == domain.ModuleQuerySetCenter {
if t == domain.SchemaTable.ToString() || t == domain.SubProcessTable.ToString() || t == domain.CalculateTable.ToString() {
return true
}
}
if currentModule == domain.ModuleCalculateCenter {
if t == domain.CalculateItem.ToString() || t == domain.CalculateSet.ToString() {
return true
}
}
return false
}