table_business.go
7.2 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
183
184
185
186
187
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/application/table/query"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/bytelib"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/domainService"
)
func (tableService *TableService) ShowBusinessDatabases(ctx *domain.Context, cmd *query.ShowBusinessDatabasesRequest) (interface{}, error) {
byteCoreService := domainService.ByteCoreService{}
response, err := byteCoreService.ShowBusinessDatabases(bytelib.ReqShowBusinessDatabases{})
return response, err
}
func (tableService *TableService) ShowBusinessTables(ctx *domain.Context, cmd *query.ShowTablesRequest) (interface{}, error) {
byteCoreService := domainService.ByteCoreService{}
response, err := byteCoreService.ShowBusinessTables(bytelib.ReqShowBusinessTables{
DatabaseEnName: cmd.DatabaseEnName,
DatabaseType: cmd.DatabaseType,
})
result := make([]map[string]interface{}, 0)
for _, t := range response.TableFullNames {
result = append(result, map[string]interface{}{
"name": t,
})
}
return map[string]interface{}{
"list": result,
}, err
}
func (tableService *TableService) QueryBusinessTable(ctx *domain.Context, cmd *query.ShowTableDataRequest) (interface{}, error) {
byteCoreService := domainService.ByteCoreService{}
response, err := byteCoreService.QueryBusinessTable(bytelib.ReqQueryBusinessTable{
TableFullName: cmd.TableFullName,
PageNumber: cmd.PageNumber,
PageSize: cmd.PageSize,
})
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
tableRepository, _, _ := factory.FastPgTable(transactionContext, 0)
table, _ := tableRepository.FindBusinessTable(constant.COMPANY_SU_TIAN_XIA, response.TableFullName)
if table != nil {
response.TableRemarkName = table.Name
response.ShowTableNameBy = table.TableInfo.BusinessTableShowTableNameBy
response.ShowTableFieldNameBy = table.TableInfo.BusinessTableShowTableFieldNameBy
for i, f := range response.FieldSchemas {
for j, jf := range table.DataFields {
if jf.SQLName == f.FieldEnName {
response.FieldSchemas[i].FieldZhName = table.DataFields[j].Name
}
}
}
}
return response, err
}
func (tableService *TableService) UpdateBusinessTable(ctx *domain.Context, cmd *command.UpdateBusinessTableRequest) (interface{}, 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 tableRepository domain.TableRepository
if value, err := factory.CreateTableRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
tableRepository = value
}
ctx.CompanyId = int(constant.COMPANY_SU_TIAN_XIA)
var (
fields = make([]*domain.Field, 0)
mainTable *domain.Table
)
for i, f := range cmd.Fields {
fields = append(fields, &domain.Field{
Index: i + 1,
Name: f.FieldZhName,
SQLName: f.FieldEnName,
SQLType: f.FieldType, //TODO:类型转换
Flag: domain.MainTableField,
})
}
table, _ := tableRepository.FindBusinessTable(constant.COMPANY_SU_TIAN_XIA, cmd.TableFullName)
if table == nil {
mainTable = domainService.NewTable(domain.BusinessTable, cmd.TableName, fields, 0).
WithContext(ctx).
WithPrefix(domain.BusinessTable.ToString())
mainTable.SQLName = cmd.TableFullName
mainTable.DataFields = fields
mainTable.TableInfo.BusinessTableShowTableNameBy = cmd.ShowTableNameBy
mainTable.TableInfo.BusinessTableShowTableFieldNameBy = cmd.ShowTableFieldNameBy
mainTable.TableInfo.TableFrom = 1
if mainTable, err = tableRepository.Save(mainTable); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
} else {
table.Name = cmd.TableName
table.DataFields = fields
table.TableInfo.BusinessTableShowTableNameBy = cmd.ShowTableNameBy
table.TableInfo.BusinessTableShowTableFieldNameBy = cmd.ShowTableFieldNameBy
if table, err = tableRepository.Save(table); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
if err = transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return mainTable, nil
}
func (tableService *TableService) GenerateBusinessTable(ctx *domain.Context, cmd *command.GenerateBusinessTableRequest) (interface{}, 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 tableRepository domain.TableRepository
if value, err := factory.CreateTableRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
tableRepository = value
}
ctx.CompanyId = int(constant.COMPANY_SU_TIAN_XIA)
duplicateTable, err := tableRepository.FindOne(map[string]interface{}{"context": ctx, "tableName": cmd.TableName,
"tableTypes": []string{string(domain.MainTable), string(domain.SubTable), string(domain.SideTable)}})
if err == nil && duplicateTable != nil {
return nil, fmt.Errorf("表名称重复")
}
var (
fields = make([]*domain.Field, 0)
mainTable *domain.Table
)
for i, f := range cmd.Fields {
fields = append(fields, &domain.Field{
Index: i + 1,
Name: f.FieldZhName,
SQLName: f.FieldEnName,
SQLType: f.FieldType, //TODO:类型转换
Flag: domain.MainTableField,
})
}
mainTable = domainService.NewTable(domain.MainTable, cmd.TableName, fields, 0).
WithContext(ctx).
WithPrefix(domain.MainTable.ToString()).ApplyDefaultModule()
mainTable.SQLName = cmd.TableFullName
mainTable.TableInfo.TableFrom = 1
mainTable.DataFields = fields
if mainTable, err = tableRepository.Save(mainTable); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err = transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}