table_copy_data_table_service.go 1.9 KB
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
	}
	if table.TableType != domain.MainTable.ToString() {
		return nil, fmt.Errorf("主表才允许复制")
	}
	// 验证表名是否重复
	duplicateTable, err := tableRepository.FindOne(map[string]interface{}{"context": ctx, "tableName": tableName})
	if err == nil && duplicateTable != nil {
		return nil, fmt.Errorf("表名称重复")
	}

	sideTable := NewTable(domain.SideTable, tableName, table.DataFields, table.RowCount).WithContext(ctx)
	sideTable.ParentId = table.TableId
	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
	}

	// 通知底层
	return nil, nil
}