query_set.go 12.9 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/tool_funs"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/querySet/command"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/querySet/dto"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/querySet/query"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
)

// 查询集合服务
type QuerySetService struct {
}

// 修改状态
func (querySetService *QuerySetService) ChangeStatus(ctx *domain.Context, changeStatusCommand *command.ChangeStatusCommand) (interface{}, error) {
	if err := changeStatusCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.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()
	}()
	svr, _ := factory.FastQuerySetServices(transactionContext)
	if err := svr.ChangeStatus(ctx, changeStatusCommand.QuerySetId, changeStatusCommand.Status); err != nil {
		return nil, factory.FastError(err)
	}

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

// 移动
func (querySetService *QuerySetService) Copy(ctx *domain.Context, copyCommand *command.CopyCommand) (interface{}, error) {
	if err := copyCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.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()
	}()

	svr, _ := factory.FastQuerySetServices(transactionContext)
	if err := svr.Copy(ctx, copyCommand.QuerySetId, copyCommand.Type, copyCommand.ParentId, copyCommand.Name); err != nil {
		return nil, factory.FastError(err)
	}

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

// 创建查询集合服务
func (querySetService *QuerySetService) CreateQuerySet(ctx *domain.Context, createQuerySetCommand *command.CreateQuerySetCommand) (interface{}, error) {
	if err := createQuerySetCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.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()
	}()
	newQuerySet := &domain.QuerySet{
		Type:     createQuerySetCommand.Type,
		Flag:     createQuerySetCommand.Flag,
		Name:     createQuerySetCommand.Name,
		ParentId: createQuerySetCommand.ParentId,
	}

	svr, _ := factory.FastQuerySetServices(transactionContext)
	if err := svr.Create(ctx, newQuerySet); err != nil {
		return nil, factory.FastError(err)
	}

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

// 依赖关系图
func (querySetService *QuerySetService) DependencyGraph(ctx *domain.Context, dependencyGraphQuery *query.DependencyGraphQuery) (interface{}, error) {
	if err := dependencyGraphQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.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()
	}()

	svr, _ := factory.FastQuerySetServices(transactionContext)
	data, err := svr.DependencyGraph(ctx, dependencyGraphQuery.QuerySetId)
	if err != nil {
		return nil, factory.FastError(err)
	}

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

// 返回查询集合服务
func (querySetService *QuerySetService) GetQuerySet(ctx *domain.Context, getQuerySetQuery *query.GetQuerySetQuery) (interface{}, error) {
	if err := getQuerySetQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.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()
	}()
	_, querySet, err := factory.FastPgQuerySet(transactionContext, getQuerySetQuery.QuerySetId)
	if err != nil {
		return nil, factory.FastError(err)
	}
	return (&dto.QuerySetDetailDto{}).Load(querySet), nil
}

// 返回查询集合服务列表
func (querySetService *QuerySetService) ListQuerySet(ctx *domain.Context, listQuerySetQuery *query.ListQuerySetQuery) (interface{}, error) {
	if err := listQuerySetQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.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 querySetRepository domain.QuerySetRepository
	if value, err := factory.CreateQuerySetRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		querySetRepository = value
	}
	if count, querySets, err := querySetRepository.Find(tool_funs.SimpleStructToMap(listQuerySetQuery)); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return map[string]interface{}{
			"count":     count,
			"querySets": querySets,
		}, nil
	}
}

// 移动
func (querySetService *QuerySetService) Move(ctx *domain.Context, moveCommand *command.MoveCommand) (interface{}, error) {
	if err := moveCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.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()
	}()

	svr, _ := factory.FastQuerySetServices(transactionContext)
	if err := svr.Move(ctx, moveCommand.QuerySetId, moveCommand.ParentId, moveCommand.Index); err != nil {
		return nil, factory.FastError(err)
	}

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

// 移除查询集合服务
func (querySetService *QuerySetService) RemoveQuerySet(ctx *domain.Context, removeQuerySetCommand *command.RemoveQuerySetCommand) (interface{}, error) {
	if err := removeQuerySetCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.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()
	}()
	svr, _ := factory.FastQuerySetServices(transactionContext)
	if err := svr.Delete(ctx, removeQuerySetCommand.QuerySetId); err != nil {
		return nil, factory.FastError(err)
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return struct{}{}, nil
}

// 重命名
func (querySetService *QuerySetService) Rename(ctx *domain.Context, renameCommand *command.RenameCommand) (interface{}, error) {
	if err := renameCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.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()
	}()

	svr, _ := factory.FastQuerySetServices(transactionContext)
	if err := svr.Rename(ctx, renameCommand.QuerySetId, renameCommand.Name); err != nil {
		return nil, factory.FastError(err)
	}

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

// 返回查询集合服务列表
func (querySetService *QuerySetService) SearchQuerySet(ctx *domain.Context, searchQuerySetQuery *query.SearchQuerySetQuery) (interface{}, error) {
	if err := searchQuerySetQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.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()
	}()

	QuerySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0)
	options := utils.ObjectToMap(searchQuerySetQuery)
	options["context"] = ctx
	count, querySets, err := QuerySetRepository.Find(options)
	if err != nil {
		return nil, factory.FastError(err)
	}

	var result = make([]*dto.QuerySetDto, 0)
	for _, set := range querySets {
		var item = &dto.QuerySetDto{}
		item.Load(set)
		result = append(result, item)
	}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return map[string]interface{}{
		"count":     count,
		"querySets": result,
	}, nil
}

// 更新查询集合服务
func (querySetService *QuerySetService) UpdateQuerySet(ctx *domain.Context, updateQuerySetCommand *command.UpdateQuerySetCommand) (interface{}, error) {
	if err := updateQuerySetCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.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()
	}()
	svr, _ := factory.FastQuerySetServices(transactionContext)
	if err := svr.Update(ctx, updateQuerySetCommand.QuerySetId, updateQuerySetCommand.QueryComponents); err != nil {
		return nil, factory.FastError(err)
	}

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

func NewQuerySetService(options map[string]interface{}) *QuerySetService {
	newQuerySetService := &QuerySetService{}
	return newQuerySetService
}