digital_platform_event_subscribe.go 3.1 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"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/infrastructure/cache"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/domainService"
)

func (tableEventService *TableEventService) DigitalPlatformEventSubscribe(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()
	//}()

	var (
		dataChanged   = true
		structChanged = true
	)

	data := cmd.EventTable
	tableId := 0
	switch data.Type {
	case domain.TableDataImportEvent, domain.TableDataEditEvent, domain.TableDeleteEvent:
		// dataChanged = true
		tableId = data.Table.TableId
	case domain.QuerySetUpdateEvent:
		tableId = data.QuerySet.QuerySetInfo.BindTableId
		// structChanged = true
	}
	if tableId == 0 {
		return nil, nil
	}
	var notifyData = struct {
		DataChanged       bool   `json:"dataChanged"`
		StructChanged     bool   `json:"structChanged"`
		TableId           int    `json:"tableId"`
		Event             string `json:"event"`
		TableAffectedList []int  `json:"tableAffectedList"`
	}{
		DataChanged:   dataChanged,
		StructChanged: structChanged,
		TableId:       tableId,
		Event:         data.Type.ToString(),
	}
	// tableId 相关联的
	tableRepository, _, _ := factory.FastPgTable(transactionContext, 0)
	_, tables, err := tableRepository.Find(map[string]interface{}{"context": data.Context, "tableTypesNotIn": []string{domain.TemporaryTable.ToString(), domain.ExcelTable.ToString()}})
	if err != nil {
		return nil, err
	}

	tableDependencyService, _ := domainService.NewTableDependencyService(transactionContext.(*pgTransaction.TransactionContext))
	tableDependTree := tableDependencyService.TableDependTree(tables, tableId)
	tree := tableDependTree.Tree

	//tableService := tableservice.NewTableService(nil)
	for i := range tree {
		cache.DefaultDataTableCacheService.DeleteDataTable(tree[i])
		// fresh cache
		//tableService.TablePreview(data.Context, &tablecommand.TablePreviewCommand{
		//	TableId:    tree[i],
		//	ObjectType: domain.ObjectMetaTable,
		//	PageSize:   10000,
		//	PageNumber: 0,
		//	UseCache:   true,
		//})
		notifyData.TableAffectedList = append(notifyData.TableAffectedList, tree[i])
	}

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