table_object_search.go
4.9 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
package service
import (
"fmt"
"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())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
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})
if !searchQuery.ReturnGroupItem {
querySets = make([]*domain.QuerySet, 0)
}
// BindTableId , parentId
querySetMapByTableId := make(map[int]*domain.QuerySet)
for _, qs := range querySets {
if qs.QuerySetInfo.BindTableId == 0 {
continue
}
querySetMapByTableId[qs.QuerySetInfo.BindTableId] = qs
}
querySetMapById := make(map[int]*domain.QuerySet)
for _, qs := range querySets {
querySetMapById[qs.QuerySetId] = qs
}
var response = make([]*dto.TableObjectDto, 0)
// 分组
querySetMapGroup := make(map[int]bool)
querySetGroups := make([]*domain.QuerySet, 0)
for _, t := range result {
if filterTableByFilterRule(t, searchQuery) {
continue
}
if !domain.TableType(t.TableType).TableHasGroup() {
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)
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
sort.Slice(response, func(i, j int) bool {
item1 := response[i]
item2 := response[j]
k1 := fmt.Sprintf("%v-%v-%v", item1.TableType, item1.ParentId, item1.Id)
k2 := fmt.Sprintf("%v-%v-%v", item2.TableType, item2.ParentId, item2.Id)
return k1 < k2
})
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
}
//if rule.TableType == rule.TableType && rule.Status > 0 && rule.Status != item.Status {
// return true
//}
}
return false
}