table_update_table_struct_service.go
3.4 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
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"
"time"
)
type UpdateTableStructService struct {
transactionContext *pgTransaction.TransactionContext
}
func NewUpdateTableStructService(transactionContext *pgTransaction.TransactionContext) (*UpdateTableStructService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &UpdateTableStructService{
transactionContext: transactionContext,
}, nil
}
}
func (ptr *UpdateTableStructService) UpdateTableStruct(ctx *domain.Context, tableId int, fields []*domain.Field, name string) (interface{}, error) {
tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
table, err := tableRepository.FindOne(map[string]interface{}{"tableId": tableId})
if err != nil {
return nil, err
}
if table.TableType != domain.SubTable.ToString() {
return nil, fmt.Errorf("只有分表允许编辑表结构")
}
mainTable, err := tableRepository.FindOne(map[string]interface{}{"tableId": table.ParentId})
if err != nil {
return nil, fmt.Errorf("主表不存在")
}
fields = MappingFields(mainTable, fields)
reserves, deletes, adds := domain.FieldsChange(table.Fields(false), fields)
dataFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": domain.MainTableField})
manualFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": domain.ManualField})
table.DataFieldIndex = mainTable.DataFieldIndex
table.DataFields = dataFields
table.ManualFields = manualFields
table.UpdatedAt = time.Now()
if err = domain.ValidFields(fields); err != nil {
return nil, err
}
if _, err = tableRepository.Save(table); err != nil {
return nil, err
}
if _, err = tableRepository.Save(mainTable); err != nil {
return nil, err
}
// 字段被删除
if len(deletes) > 0 {
tableDependencyService, _ := NewTableDependencyService(ptr.transactionContext)
if err := tableDependencyService.HasDependencyError(ctx, tableId); err != nil {
return nil, err
}
}
// Log
// 日志
if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &SubTableEditLog{
LogEntry: domain.NewLogEntry(table.Name, table.TableType, domain.EditSubTable, ctx),
Reserve: reserves,
Delete: deletes,
Add: adds,
}); err != nil {
return nil, err
}
// 通知底层
if _, err = ByteCore.SplitTable(domain.ReqSplitTable{FromTable: mainTable, ToSubTable: table}); err != nil {
return nil, err
}
return struct{}{}, nil
}
func MappingFields(mainTable *domain.Table, fields []*domain.Field) []*domain.Field {
tableFields := mainTable.Fields(false)
tableFieldsMap := (domain.Fields)(tableFields).ToMap()
for i := range fields {
f := fields[i]
if v, ok := tableFieldsMap[f.Name]; ok {
fields[i].Name = v.Name
fields[i].SQLName = v.SQLName
fields[i].Index = v.Index
fields[i].SQLType = v.SQLType
fields[i].Description = f.Description
fields[i].Flag = v.Flag
} else {
if f.Flag == domain.ManualField && f.Index == 0 {
mainTable.DataFieldIndex += 1
fields[i] = DataField(f.Name, f.SQLType, domain.ManualField, mainTable.DataFieldIndex)
fields[i].Description = f.Description
}
}
}
return fields
}