workshop.go 8.5 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-manufacture/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/workshop/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/workshop/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
)

// 车间服务
type WorkshopService struct {
}

// 创建车间服务
func (workshopService *WorkshopService) CreateWorkshop(createWorkshopCommand *command.CreateWorkshopCommand) (interface{}, error) {
	if err := createWorkshopCommand.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()
	}()
	newWorkshop := &domain.Workshop{
		WorkshopName: createWorkshopCommand.WorkshopName,
		//PrincipalId:  createWorkshopCommand.PrincipalId,
	}
	var workshopRepository domain.WorkshopRepository
	if value, err := factory.CreateWorkshopRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		workshopRepository = value
	}
	if workshop, err := workshopRepository.Save(newWorkshop); 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 workshop, nil
	}
}

// 返回车间服务
func (workshopService *WorkshopService) GetWorkshop(getWorkshopQuery *query.GetWorkshopQuery) (interface{}, error) {
	if err := getWorkshopQuery.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 workshopRepository domain.WorkshopRepository
	if value, err := factory.CreateWorkshopRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		workshopRepository = value
	}
	workshop, err := workshopRepository.FindOne(map[string]interface{}{"workshopId": getWorkshopQuery.WorkshopId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if workshop == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getWorkshopQuery.WorkshopId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return workshop, nil
	}
}

// 返回车间服务列表
func (workshopService *WorkshopService) ListWorkshop(listWorkshopQuery *query.ListWorkshopQuery) (interface{}, error) {
	if err := listWorkshopQuery.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 workshopRepository domain.WorkshopRepository
	if value, err := factory.CreateWorkshopRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		workshopRepository = value
	}
	if count, workshops, err := workshopRepository.Find(tool_funs.SimpleStructToMap(listWorkshopQuery)); 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,
			"workshops": workshops,
		}, nil
	}
}

// 移除车间服务
func (workshopService *WorkshopService) RemoveWorkshop(removeWorkshopCommand *command.RemoveWorkshopCommand) (interface{}, error) {
	if err := removeWorkshopCommand.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 workshopRepository domain.WorkshopRepository
	if value, err := factory.CreateWorkshopRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		workshopRepository = value
	}
	workshop, err := workshopRepository.FindOne(map[string]interface{}{"workshopId": removeWorkshopCommand.WorkshopId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if workshop == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeWorkshopCommand.WorkshopId)))
	}
	if workshop, err := workshopRepository.Remove(workshop); 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 workshop, nil
	}
}

// 更新车间服务
func (workshopService *WorkshopService) UpdateWorkshop(updateWorkshopCommand *command.UpdateWorkshopCommand) (interface{}, error) {
	if err := updateWorkshopCommand.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 workshopRepository domain.WorkshopRepository
	if value, err := factory.CreateWorkshopRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		workshopRepository = value
	}
	workshop, err := workshopRepository.FindOne(map[string]interface{}{"workshopId": updateWorkshopCommand.WorkshopId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if workshop == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateWorkshopCommand.WorkshopId)))
	}
	if err := workshop.Update(tool_funs.SimpleStructToMap(updateWorkshopCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if workshop, err := workshopRepository.Save(workshop); 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 workshop, nil
	}
}

func NewWorkshopService(options map[string]interface{}) *WorkshopService {
	newWorkshopService := &WorkshopService{}
	return newWorkshopService
}