event_handler_conflict_status.go 5.1 KB
package service

import (
	"errors"
	"fmt"
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/event/command"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
	"reflect"
	"sort"
)

func (tableEventService *TableEventService) HandlerTableAffectedMarkToConflictStatus(ctx *domain.Context, cmd *command.TableEventCommand) (interface{}, error) {
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	data := cmd.EventTable
	tableId := 0
	switch data.Type {
	case domain.TableStructEditEvent, domain.TableDeleteEvent:
		tableId = data.Table.TableId
	case domain.QuerySetUpdateEvent:
		// 结构变更才报冲突
		if !checkStructChange(cmd) {
			return nil, err
		}
		tableId = data.QuerySet.QuerySetInfo.BindTableId
	case domain.QuerySetUpdateRenameEvent:
		tableId = data.QuerySet.QuerySetInfo.BindTableId
	default:
		return nil, err
	}
	if tableId == 0 {
		return nil, nil
	}
	// tableId 相关联的
	tableRepository, _, _ := factory.FastPgTable(transactionContext, 0)

	_, tables, err := tableRepository.Find(map[string]interface{}{"context": ctx, "dependencyTable": tableId, "tableTypesNotIn": []string{domain.TemporaryTable.ToString()}})
	if errors.Is(err, domain.ErrorNotFound) {
		return nil, nil
	}
	tableIds := make([]int, 0)
	for _, table := range tables {
		tableIds = append(tableIds, table.TableId)
	}
	if len(tableIds) == 0 {
		return nil, nil
	}
	querySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0)
	_, querySets, _ := querySetRepository.Find(map[string]interface{}{"context": ctx, "bindTableIds": tableIds})
	for _, querySet := range querySets {
		log.Logger.Debug(fmt.Sprintf("【集合状态更新】 id:%v name:%v ReadyStatus:1", querySet.QuerySetId, querySet.Name))
		querySet.QuerySetInfo.WithConflictStatus()
		_, err = querySetRepository.Save(querySet)
		if err != nil {
			return nil, err
		}
	}

	//tableDependencyService, _ := domainService.NewTableDependencyService(transactionContext.(*pgTransaction.TransactionContext))
	//tableDependTree := tableDependencyService.TableDependTree(tables, tableId)
	//tree := tableDependTree.Tree
	//
	//querySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0)
	//if len(tree) > 0 {
	//	_, querySets, _ := querySetRepository.Find(map[string]interface{}{
	//		"types":        []string{domain.SchemaTable.ToString(), domain.SubProcessTable.ToString(), domain.CalculateTable.ToString()},
	//		"bindTableIds": tree,
	//	})
	//	for _, querySet := range querySets {
	//		log.Logger.Debug(fmt.Sprintf("【集合状态更新】 id:%v name:%v 标记冲突", querySet.QuerySetId, querySet.Name))
	//		querySet.QuerySetInfo.WithConflictStatus()
	//		_, err = querySetRepository.Save(querySet)
	//		if err != nil {
	//			return nil, err
	//		}
	//	}
	//}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return nil, nil
}

func checkStructChange(cmd *command.TableEventCommand) bool {
	var (
		newSet   = cmd.EventTable.QuerySet
		oldSet   = cmd.EventTable.OldQuerySet
		newTable = cmd.EventTable.Table
		oldTable = cmd.EventTable.OldTable
	)
	if newSet == nil || oldSet == nil {
		return false
	}
	var (
		t string = newSet.Type
	)

	switch t {
	case domain.SchemaTable.ToString(), domain.SubProcessTable.ToString():
		// 第一步表有变更
		newSetDepTables := newSet.GetDependencyTables(newSet.QueryComponents)
		oldSetDepTables := newSet.GetDependencyTables(oldSet.QueryComponents)
		sort.SliceStable(newSetDepTables, func(i, j int) bool {
			return newSetDepTables[i] < newSetDepTables[j]
		})
		sort.SliceStable(oldSetDepTables, func(i, j int) bool {
			return oldSetDepTables[i] < oldSetDepTables[j]
		})
		if !reflect.DeepEqual(newSetDepTables, oldSetDepTables) {
			log.Logger.Debug(fmt.Sprintf("方案/过程:%v 依赖变更 %v -> %v", t, oldSetDepTables, newSetDepTables))
			return true
		}
	case domain.CalculateTable.ToString():

	case domain.CalculateItem.ToString(), domain.CalculateSet.ToString():
		return false
	}
	if newTable == nil || oldTable == nil {
		return false
	}
	// 第二步判断字段是否有变更
	newTableFields := tableFields(newTable)
	oldTableFields := tableFields(oldTable)
	sort.Strings(newTableFields)
	sort.Strings(oldTableFields)
	if !reflect.DeepEqual(newTableFields, oldTableFields) {
		log.Logger.Debug(fmt.Sprintf("计算表:%v 结构变更 %v -> %v", t, oldTableFields, newTableFields))
		return true
	}
	return false
}

func tableFields(t *domain.Table) []string {
	var result = make([]string, 0)
	for _, f := range t.Fields(false) {
		result = append(result, f.Name)
	}
	return result
}