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

// 工位服务
type ProductJobService struct {
}

// 创建工位服务
func (productJobService *ProductJobService) CreateProductJob(createProductJobCommand *command.CreateProductJobCommand) (interface{}, error) {
	if err := createProductJobCommand.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()
	}()
	newProductJob := &domain.ProductJob{
		CompanyId: createProductJobCommand.CompanyId,
		OrgId:     createProductJobCommand.OrgId,
		JobName:   createProductJobCommand.JobName,
		//WorkshopId:     createProductJobCommand.WorkshopId,
		//LineId:         createProductJobCommand.LineId,
		//SectionId:      createProductJobCommand.SectionId,
		RelatedDevices: createProductJobCommand.RelatedDevices,
	}
	var productJobRepository domain.ProductJobRepository
	if value, err := factory.CreateProductJobRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productJobRepository = value
	}
	if productJob, err := productJobRepository.Save(newProductJob); 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 productJob, nil
	}
}

// 返回工位服务
func (productJobService *ProductJobService) GetProductJob(getProductJobQuery *query.GetProductJobQuery) (interface{}, error) {
	if err := getProductJobQuery.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 productJobRepository domain.ProductJobRepository
	if value, err := factory.CreateProductJobRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productJobRepository = value
	}
	productJob, err := productJobRepository.FindOne(map[string]interface{}{"productJobId": getProductJobQuery.ProductJobId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if productJob == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getProductJobQuery.ProductJobId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return productJob, nil
	}
}

// 返回工位服务列表
func (productJobService *ProductJobService) ListProductJob(listProductJobQuery *query.ListProductJobQuery) (interface{}, error) {
	if err := listProductJobQuery.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 productJobRepository domain.ProductJobRepository
	if value, err := factory.CreateProductJobRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productJobRepository = value
	}
	if count, productJobs, err := productJobRepository.Find(tool_funs.SimpleStructToMap(listProductJobQuery)); 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,
			"productJobs": productJobs,
		}, nil
	}
}

// 移除工位服务
func (productJobService *ProductJobService) RemoveProductJob(removeProductJobCommand *command.RemoveProductJobCommand) (interface{}, error) {
	if err := removeProductJobCommand.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 productJobRepository domain.ProductJobRepository
	if value, err := factory.CreateProductJobRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productJobRepository = value
	}
	productJob, err := productJobRepository.FindOne(map[string]interface{}{"productJobId": removeProductJobCommand.ProductJobId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if productJob == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeProductJobCommand.ProductJobId)))
	}
	if productJob, err := productJobRepository.Remove(productJob); 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 productJob, nil
	}
}

// 更新工位服务
func (productJobService *ProductJobService) UpdateProductJob(updateProductJobCommand *command.UpdateProductJobCommand) (interface{}, error) {
	if err := updateProductJobCommand.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 productJobRepository domain.ProductJobRepository
	if value, err := factory.CreateProductJobRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productJobRepository = value
	}
	productJob, err := productJobRepository.FindOne(map[string]interface{}{"productJobId": updateProductJobCommand.ProductJobId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if productJob == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateProductJobCommand.ProductJobId)))
	}
	if err := productJob.Update(tool_funs.SimpleStructToMap(updateProductJobCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if productJob, err := productJobRepository.Save(productJob); 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 productJob, nil
	}
}

func NewProductJobService(options map[string]interface{}) *ProductJobService {
	newProductJobService := &ProductJobService{}
	return newProductJobService
}