table_update_table_struct_service.go 3.4 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"
	"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
}