dictionary.go 11.4 KB
package service

import (
	"fmt"
	"sort"

	"github.com/linmadan/egglib-go/core/application"
	"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()
	}()

	var items []domain.DictionaryItem
	for _, item := range createDictionaryCommand.DictItems {
		i := domain.DictionaryItem{
			ItemCode:  item.ItemCode,
			ItemLabel: item.ItemLabel,
			ItemValue: item.ItemValue,
			IsShow:    item.IsShow,
			Sort:      item.Sort,
		}
		items = append(items, i)
	}
	sort.Sort(domain.DictionaryItems(items))
	newDictionary := &domain.Dictionary{
		DictCode:  createDictionaryCommand.DictCode,
		DictName:  createDictionaryCommand.DictName,
		Describe:  createDictionaryCommand.Describe,
		DictItems: items,
	}
	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
	}
	queryCondition := make(map[string]interface{})
	if len(getDictionaryQuery.DictCode) > 0 {
		queryCondition["dictCode"] = getDictionaryQuery.DictCode
	}
	if getDictionaryQuery.DictionaryId > 0 {
		queryCondition["dictionaryId"] = getDictionaryQuery.DictionaryId
	}
	dictionary, err := dictionaryRepository.FindOne(queryCondition)
	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) (int64, interface{}, error) {
	if err := listDictionaryQuery.ValidateQuery(); err != nil {
		return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return 0, 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 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		dictionaryRepository = value
	}
	offset := listDictionaryQuery.PageSize * (listDictionaryQuery.Pageindex - 1)
	queryCondition := map[string]interface{}{
		"limit":  listDictionaryQuery.PageSize,
		"offset": offset,
	}
	if count, dictionarys, err := dictionaryRepository.Find(queryCondition); err != nil {
		return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return count,
			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
	}
	for _, dictionaryId := range removeDictionaryCommand.DictionaryIds {
		if dictionaryId == 0 {
			continue
		}
		dictionary, err := dictionaryRepository.FindOne(map[string]interface{}{"dictionaryId": 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("%d", dictionaryId))
		}
		if _, err := dictionaryRepository.Remove(dictionary); err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
	}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return removeDictionaryCommand, 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("%d", updateDictionaryCommand.DictionaryId))
	}
	var items []domain.DictionaryItem
	for _, item := range updateDictionaryCommand.DictItems {
		i := domain.DictionaryItem{
			ItemCode:  item.ItemCode,
			ItemLabel: item.ItemLabel,
			ItemValue: item.ItemValue,
			IsShow:    item.IsShow,
			Sort:      item.Sort,
		}
		items = append(items, i)
	}
	sort.Sort(domain.DictionaryItems(items))
	err = dictionary.Update(map[string]interface{}{
		"dictCode":  updateDictionaryCommand.DictCode,
		"dictName":  updateDictionaryCommand.DictName,
		"describe":  updateDictionaryCommand.Describe,
		"dictItems": items,
	})
	if 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 (dictionaryService *DictionaryService) GetDictionaryByCode(getDictionaryQuery *query.GetDictionaryByCodeQuery) (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
	}
	queryCondition := make(map[string]interface{})
	if len(getDictionaryQuery.DictCodes) > 0 {
		queryCondition["dictCodeIn"] = getDictionaryQuery.DictCodes
	}
	_, dictionarys, err := dictionaryRepository.Find(queryCondition)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

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

}

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