field_optional_values.go
5.1 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
package service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"github.com/zeromicro/go-zero/core/collection"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/command"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/pg"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
"gorm.io/gorm"
"sort"
)
func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd *command.FieldOptionalValuesCommand) (interface{}, error) {
if err := cmd.ValidateCommand(); 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()
}()
var dataTable *domain.DataTable
var empty = map[string]interface{}{
"values": []string{},
"total": 0,
}
tableRepository, _, _ := factory.FastPgTable(transactionContext, 0)
var table *domain.Table
var db *gorm.DB
switch cmd.ObjectType {
case domain.ObjectFile:
byteCore, _ := factory.CreateByteCoreService(transactionContext)
response, err := byteCore.FieldOptionalValues(domain.ReqFieldOptionalValues{
FileId: cmd.ObjectId,
Field: &cmd.Field,
Match: cmd.Match,
Where: domain.Where{
PageNumber: cmd.PageNumber,
PageSize: cmd.PageSize,
},
})
if err != nil {
return nil, factory.FastError(err)
}
return map[string]interface{}{
"values": response.Values,
"total": response.Total,
}, nil
case domain.ObjectMetaTable:
table, err = tableRepository.FindOne(map[string]interface{}{"ctx": ctx, "tableId": cmd.ObjectId})
if err != nil {
return nil, factory.FastError(err)
}
db = starrocks.DB
case domain.ObjectDBTable:
table = domain.DBTables[cmd.ObjectId]
db = pg.GormDB
}
field, ok := table.MatchField(&cmd.Field)
if !ok {
return nil, factory.FastError(fmt.Errorf("列:%v 不存在", cmd.Field.Name))
}
// 字段只传name时,补齐sqlName
for i, c := range cmd.Where.Conditions {
if c.Field != nil && c.Field.SQLName == "" {
if v, ok := table.MatchField(c.Field); ok {
cmd.Where.Conditions[i].Field.SQLName = v.SQLName
cmd.Where.Conditions[i].Field.SQLType = v.SQLType
}
}
}
if table.TableType == domain.SubTable.ToString() && field.Flag == domain.ManualField {
return empty, nil
}
match := cmd.Match
if !domain.SQLType(field.SQLType).IsString() {
match = ""
}
if table.TableType == domain.CalculateSet.ToString() {
var querySet *domain.QuerySet
querySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0)
querySet, err = querySetRepository.FindOne(map[string]interface{}{"BindTableId": cmd.ObjectId})
if err != nil {
return nil, factory.FastError(err)
}
svr, _ := factory.FastQuerySetServices(transactionContext)
dataTable, err = svr.LoadCalculateSetData(ctx, querySet, querySet.QueryComponents)
if err != nil {
return nil, factory.FastError(err)
}
if cmd.Where != nil {
data, length := dataTable.FilterByWhere(*cmd.Where)
dataTable.Data = data
dataTable.Total = length
}
values := removeDuplicate(dataTable.Values(field))
sort.SliceStable(values, func(i, j int) bool {
return values[i] < values[j]
})
return map[string]interface{}{
"values": values,
"total": len(values),
}, nil
}
options := &starrocks.QueryOptions{
Table: table,
TableName: table.SQLName,
Select: []*domain.Field{field},
Where: []starrocks.Condition{
{
Condition: domain.Condition{
Field: field,
Like: match,
Order: "ASC",
},
Distinct: true,
},
},
}
if cmd.Where != nil && len(cmd.Where.Conditions) > 0 {
for _, c := range cmd.Where.Conditions {
options.Where = append(options.Where, starrocks.Condition{
Condition: c,
})
}
}
options.AdditionOptionsByTable(table)
options.SetOffsetLimit(cmd.PageNumber, cmd.PageSize)
dataTable, err = starrocks.Query(*options, starrocks.WrapQueryFuncWithDB(db))
if err != nil {
return nil, factory.FastError(err)
}
dataTable.Total, err = starrocks.WrapQueryCountWithDB(*options, db)()
if err != nil {
return nil, factory.FastError(err)
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
values := dataTable.OptionalValue(cmd.Match, field.SQLType)
return map[string]interface{}{
"values": values,
"total": len(values),
}, nil
}
func removeDuplicate(inputs []string) []string {
s := collection.NewSet()
var result = make([]string, 0)
for _, input := range inputs {
if !s.Contains(input) {
s.Add(input)
result = append(result, input)
}
}
return result
}