table_copy_data_table_service.go
2.5 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
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 CopyDataTableService struct {
transactionContext *pgTransaction.TransactionContext
}
func NewCopyDataTableService(transactionContext *pgTransaction.TransactionContext) (*CopyDataTableService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &CopyDataTableService{
transactionContext: transactionContext,
}, nil
}
}
// CopyTable 表复制 【data-table】
func (ptr *CopyDataTableService) CopyTable(ctx *domain.Context, tableId int, tableName string) (interface{}, error) {
tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
table, err := tableRepository.FindOne(map[string]interface{}{"tableId": tableId})
if err != nil {
return nil, err
}
var mainTable *domain.Table
dataFields := table.DataFields
if table.TableType == domain.SubTable.ToString() {
mainTable, err = tableRepository.FindOne(map[string]interface{}{"tableId": table.ParentId})
if err != nil {
return nil, err
}
for i := range table.ManualFields {
f := table.ManualFields[i].Copy()
f.Flag = domain.MainTableField
dataFields = append(dataFields, f)
}
}
// 验证表名是否重复
duplicateTable, err := tableRepository.FindOne(map[string]interface{}{
"context": ctx,
"tableName": tableName,
"tableTypes": domain.TableTypesToStringList(domain.MainTable, domain.SubTable, domain.SideTable)})
if err == nil && duplicateTable != nil {
return nil, fmt.Errorf("表名称重复")
}
sideTable := NewCopyTable(domain.SideTable, tableName, dataFields, table.RowCount).
WithContext(ctx).
WithParentId(table.TableId).
WithDataFieldIndex(table.DataFieldIndex).
WithPrefix(domain.SideTable.ToString()).
WithTableInfo(table)
if sideTable, err = tableRepository.Save(sideTable); err != nil {
return nil, err
}
// 日志
if err = FastLog(ptr.transactionContext, domain.CommonLog, sideTable.TableId, &CopyTableLog{
LogEntry: domain.NewLogEntry(sideTable.Name, sideTable.TableType, domain.CopyTable, ctx),
SourceTableName: table.Name,
}); err != nil {
return nil, err
}
// 通知底层
if _, err = ByteCore.CopyTable(domain.ReqCopyTable{Table: table, MainTable: mainTable, CopyToTable: sideTable}); err != nil {
return nil, err
}
return nil, nil
}