field_optional_values.go
3.6 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
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/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"
)
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))
}
if table.TableType == domain.SubTable.ToString() && field.Flag == domain.ManualField {
return empty, nil
}
match := cmd.Match
if !domain.SQLType(field.SQLType).IsString() {
match = ""
}
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.Condition != nil {
options.Where = append(options.Where, starrocks.Condition{
Condition: *cmd.Condition,
})
}
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
}