column_setting.go 9.9 KB
package service

import (
	"fmt"
	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/tool_funs"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/columnSetting/command"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/columnSetting/query"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
)

// 栏目设置
type ColumnSettingService struct {
}

// 创建栏目设置增删改查
func (columnSettingService *ColumnSettingService) CreateColumnSetting(createColumnSettingCommand *command.CreateColumnSettingCommand) (interface{}, error) {
	if err := createColumnSettingCommand.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()
	}()
	newColumnSetting := &domain.ColumnSetting{
		Description: createColumnSettingCommand.Description,
		UserName:    createColumnSettingCommand.UserName,
	}
	var columnSettingRepository domain.ColumnSettingRepository
	if value, err := factory.CreateColumnSettingRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		columnSettingRepository = value
	}
	if columnSetting, err := columnSettingRepository.Save(newColumnSetting); 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 columnSetting, nil
	}
}

// 返回栏目设置增删改查
func (columnSettingService *ColumnSettingService) GetColumnSetting(getColumnSettingQuery *query.GetColumnSettingQuery) (interface{}, error) {
	if err := getColumnSettingQuery.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 columnSettingRepository domain.ColumnSettingRepository
	if value, err := factory.CreateColumnSettingRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		columnSettingRepository = value
	}
	columnSetting, err := columnSettingRepository.FindOne(map[string]interface{}{"column_settingId": getColumnSettingQuery.ColumnSettingId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if columnSetting == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getColumnSettingQuery.ColumnSettingId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return columnSetting, nil
	}
}

// 返回栏目设置增删改查列表
func (columnSettingService *ColumnSettingService) ListColumnSetting(listColumnSettingQuery *query.ListColumnSettingQuery) (interface{}, error) {
	if err := listColumnSettingQuery.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 columnSettingRepository domain.ColumnSettingRepository
	if value, err := factory.CreateColumnSettingRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		columnSettingRepository = value
	}
	if count, columnSettings, err := columnSettingRepository.Find(tool_funs.SimpleStructToMap(listColumnSettingQuery)); 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,
			"column_settings": columnSettings,
		}, nil
	}
}

// 移除栏目设置增删改查
func (columnSettingService *ColumnSettingService) RemoveColumnSetting(removeColumnSettingCommand *command.RemoveColumnSettingCommand) (interface{}, error) {
	if err := removeColumnSettingCommand.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 errSt := transactionContext.StartTransaction(); errSt != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, errSt.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var columnSettingRepository domain.ColumnSettingRepository
	if value, errFac := factory.CreateColumnSettingRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); errFac != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errFac.Error())
	} else {
		columnSettingRepository = value
	}
	columnSetting, err := columnSettingRepository.FindOne(map[string]interface{}{"column_settingId": removeColumnSettingCommand.ColumnSettingId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if columnSetting == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeColumnSettingCommand.ColumnSettingId)))
	}
	if columnSettingRemoved, errRm := columnSettingRepository.Remove(columnSetting); errRm != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errRm.Error())
	} else {
		if errTr := transactionContext.CommitTransaction(); errTr != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, errRm.Error())
		}
		return columnSettingRemoved, nil
	}
}

// 重置栏目设置
func (columnSettingService *ColumnSettingService) ResetColumn(resetColumnCommand *command.ResetColumnCommand) (interface{}, error) {
	if err := resetColumnCommand.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()
	}()
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return nil, nil
}

// 更新栏目设置增删改查
func (columnSettingService *ColumnSettingService) UpdateColumnSetting(updateColumnSettingCommand *command.UpdateColumnSettingCommand) (interface{}, error) {
	if err := updateColumnSettingCommand.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 columnSettingRepository domain.ColumnSettingRepository
	if value, err := factory.CreateColumnSettingRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		columnSettingRepository = value
	}
	columnSetting, err := columnSettingRepository.FindOne(map[string]interface{}{"column_settingId": updateColumnSettingCommand.ColumnSettingId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if columnSetting == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateColumnSettingCommand.ColumnSettingId)))
	}
	if err := columnSetting.Update(tool_funs.SimpleStructToMap(updateColumnSettingCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if columnSettingSaved, err := columnSettingRepository.Save(columnSetting); 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 columnSettingSaved, nil
	}
}

func NewColumnSettingService(options map[string]interface{}) *ColumnSettingService {
	newColumnSettingService := &ColumnSettingService{}
	return newColumnSettingService
}