table_add_table_struct_service.go
3.0 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
package domainService
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
)
type AddTableStructService struct {
transactionContext *pgTransaction.TransactionContext
}
func NewAddTableStructService(transactionContext *pgTransaction.TransactionContext) (*AddTableStructService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &AddTableStructService{
transactionContext: transactionContext,
}, nil
}
}
// AddTableStruct 添加表结构(新建分表)
func (ptr *AddTableStructService) AddTableStruct(ctx *domain.Context, parentTableId int, fields []*domain.Field, name string) (*domain.Table, error) {
tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
mainTable, err := tableRepository.FindOne(map[string]interface{}{"tableId": parentTableId})
if err != nil {
return nil, err
}
if !(mainTable.TableType == domain.MainTable.ToString() || mainTable.TableType == domain.SideTable.ToString()) {
return nil, fmt.Errorf("只有主表、副表允许新建分表")
}
// 验证表名是否重复
duplicateTable, err := tableRepository.FindOne(map[string]interface{}{"context": ctx, "tableName": name, "tableTypes": []string{
domain.MainTable.ToString(),
domain.SubTable.ToString(),
domain.SideTable.ToString(),
}})
if err == nil && duplicateTable != nil {
return nil, fmt.Errorf("表名称重复")
}
fields = MappingFieldsV2(mainTable, fields)
dataFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": domain.MainTableField})
manualFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": domain.ManualField})
table := NewTable(domain.SubTable, name, fields, mainTable.RowCount).WithContext(ctx).WithPrefix(string(domain.SubTable)).WithTableInfo(mainTable)
table.DataFieldIndex = mainTable.DataFieldIndex
table.DataFields = dataFields
table.ManualFields = manualFields
table.ParentId = parentTableId
reserves, deletes, adds := domain.FieldsChange(table.Fields(false), fields)
if err = domain.ValidFields(fields); err != nil {
return nil, err
}
if table, err = tableRepository.Save(table); err != nil {
return nil, err
}
if _, err = tableRepository.Save(mainTable); err != nil {
return nil, err
}
// Log
// 日志
if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &SpiltMainTableLog{
LogEntry: domain.NewLogEntry(table.Name, table.TableType, domain.SpiltMainTable, ctx),
Reserve: reserves,
Delete: deletes,
Add: adds,
SourceTableName: mainTable.Name,
}); err != nil {
return nil, err
}
// 通知底层
if _, err = ByteCore.SplitTable(domain.ReqSplitTable{FromTable: mainTable, ToSubTable: table}); err != nil {
return nil, err
}
return table, nil
}