dictionary.go 8.6 KB
package service

import (
	"fmt"

	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/tool_funs"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/dictionary/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/dictionary/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/domain"
)

// 数据字典设置
type DictionaryService struct {
}

// 创建数据字典设置
func (dictionaryService *DictionaryService) CreateDictionary(createDictionaryCommand *command.CreateDictionaryCommand) (interface{}, error) {
	if err := createDictionaryCommand.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()
	}()
	newDictionary := &domain.Dictionary{} //TODO
	var dictionaryRepository domain.DictionaryRepository
	if value, err := factory.CreateDictionaryRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		dictionaryRepository = value
	}
	if dictionary, err := dictionaryRepository.Save(newDictionary); 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 dictionary, nil
	}
}

// 返回数据字典设置
func (dictionaryService *DictionaryService) GetDictionary(getDictionaryQuery *query.GetDictionaryQuery) (interface{}, error) {
	if err := getDictionaryQuery.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 dictionaryRepository domain.DictionaryRepository
	if value, err := factory.CreateDictionaryRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		dictionaryRepository = value
	}
	dictionary, err := dictionaryRepository.FindOne(map[string]interface{}{"dictionaryId": getDictionaryQuery.DictCode})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if dictionary == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getDictionaryQuery.DictCode)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return dictionary, nil
	}
}

// 返回数据字典设置列表
func (dictionaryService *DictionaryService) ListDictionary(listDictionaryQuery *query.ListDictionaryQuery) (interface{}, error) {
	if err := listDictionaryQuery.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 dictionaryRepository domain.DictionaryRepository
	if value, err := factory.CreateDictionaryRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		dictionaryRepository = value
	}
	if count, dictionarys, err := dictionaryRepository.Find(tool_funs.SimpleStructToMap(listDictionaryQuery)); 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,
			"dictionarys": dictionarys,
		}, nil
	}
}

// 移除数据字典设置
func (dictionaryService *DictionaryService) RemoveDictionary(removeDictionaryCommand *command.RemoveDictionaryCommand) (interface{}, error) {
	if err := removeDictionaryCommand.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()
	}()
	var dictionaryRepository domain.DictionaryRepository
	if value, err := factory.CreateDictionaryRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		dictionaryRepository = value
	}
	dictionary, err := dictionaryRepository.FindOne(map[string]interface{}{"dictionaryId": removeDictionaryCommand.DictionaryId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if dictionary == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeDictionaryCommand.DictionaryId)))
	}
	if dictionary, err := dictionaryRepository.Remove(dictionary); 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 dictionary, nil
	}
}

// 更新数据字典设置
func (dictionaryService *DictionaryService) UpdateDictionary(updateDictionaryCommand *command.UpdateDictionaryCommand) (interface{}, error) {
	if err := updateDictionaryCommand.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()
	}()
	var dictionaryRepository domain.DictionaryRepository
	if value, err := factory.CreateDictionaryRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		dictionaryRepository = value
	}
	dictionary, err := dictionaryRepository.FindOne(map[string]interface{}{"dictionaryId": updateDictionaryCommand.DictionaryId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if dictionary == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateDictionaryCommand.DictionaryId)))
	}
	if err := dictionary.Update(tool_funs.SimpleStructToMap(updateDictionaryCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if dictionary, err := dictionaryRepository.Save(dictionary); 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 dictionary, nil
	}
}

func NewDictionaryService(options map[string]interface{}) *DictionaryService {
	newDictionaryService := &DictionaryService{}
	return newDictionaryService
}