product_section.go 9.4 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productSection/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productSection/query"
)

// 工段服务
type ProductSectionService struct {
}

// 创建工段服务
func (productSectionService *ProductSectionService) CreateProductSection(createProductSectionCommand *command.CreateProductSectionCommand) (interface{}, error) {
	if err := createProductSectionCommand.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()
	}()
	//newProductSection := &productSection.ProductSection{
	//	WorkshopId:  createProductSectionCommand.WorkshopId,
	//	LineId:      createProductSectionCommand.LineId,
	//	SectionName: createProductSectionCommand.SectionName,
	//}
	//var productSectionRepository productSection.ProductSectionRepository
	//if value, err := factory.CreateProductSectionRepository(map[string]interface{}{
	//	"transactionContext": transactionContext,
	//}); err != nil {
	//	return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	//} else {
	//	productSectionRepository = value
	//}
	//if productSection, err := productSectionRepository.Save(newProductSection); 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 productSection, nil
	//}
	return nil, nil
}

// 返回工段服务
func (productSectionService *ProductSectionService) GetProductSection(getProductSectionQuery *query.GetProductSectionQuery) (interface{}, error) {
	if err := getProductSectionQuery.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 productSectionRepository productSection.ProductSectionRepository
	//if value, err := factory.CreateProductSectionRepository(map[string]interface{}{
	//	"transactionContext": transactionContext,
	//}); err != nil {
	//	return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	//} else {
	//	productSectionRepository = value
	//}
	//productSection, err := productSectionRepository.FindOne(map[string]interface{}{"productSectionId": getProductSectionQuery.ProductSectionId})
	//if err != nil {
	//	return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	//}
	//if productSection == nil {
	//	return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getProductSectionQuery.ProductSectionId)))
	//} else {
	//	if err := transactionContext.CommitTransaction(); err != nil {
	//		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	//	}
	//	return productSection, nil
	//}
	return nil, nil
}

// 返回工段服务列表
func (productSectionService *ProductSectionService) ListProductSection(listProductSectionQuery *query.ListProductSectionQuery) (interface{}, error) {
	if err := listProductSectionQuery.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 productSectionRepository productSection.ProductSectionRepository
	//if value, err := factory.CreateProductSectionRepository(map[string]interface{}{
	//	"transactionContext": transactionContext,
	//}); err != nil {
	//	return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	//} else {
	//	productSectionRepository = value
	//}
	//if count, productSections, err := productSectionRepository.Find(tool_funs.SimpleStructToMap(listProductSectionQuery)); 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,
	//		"productSections": productSections,
	//	}, nil
	//}
	return nil, nil
}

// 移除工段服务
func (productSectionService *ProductSectionService) RemoveProductSection(removeProductSectionCommand *command.RemoveProductSectionCommand) (interface{}, error) {
	if err := removeProductSectionCommand.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 productSectionRepository productSection.ProductSectionRepository
	//if value, err := factory.CreateProductSectionRepository(map[string]interface{}{
	//	"transactionContext": transactionContext,
	//}); err != nil {
	//	return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	//} else {
	//	productSectionRepository = value
	//}
	//productSection, err := productSectionRepository.FindOne(map[string]interface{}{"productSectionId": removeProductSectionCommand.ProductSectionId})
	//if err != nil {
	//	return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	//}
	//if productSection == nil {
	//	return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeProductSectionCommand.ProductSectionId)))
	//}
	//if productSection, err := productSectionRepository.Remove(productSection); 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 productSection, nil
	//}
	return nil, nil
}

// 更新工段服务
func (productSectionService *ProductSectionService) UpdateProductSection(updateProductSectionCommand *command.UpdateProductSectionCommand) (interface{}, error) {
	if err := updateProductSectionCommand.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 productSectionRepository productSection.ProductSectionRepository
	//if value, err := factory.CreateProductSectionRepository(map[string]interface{}{
	//	"transactionContext": transactionContext,
	//}); err != nil {
	//	return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	//} else {
	//	productSectionRepository = value
	//}
	//productSection, err := productSectionRepository.FindOne(map[string]interface{}{"productSectionId": updateProductSectionCommand.ProductSectionId})
	//if err != nil {
	//	return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	//}
	//if productSection == nil {
	//	return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateProductSectionCommand.ProductSectionId)))
	//}
	//if err := productSection.Update(tool_funs.SimpleStructToMap(updateProductSectionCommand)); err != nil {
	//	return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	//}
	//if productSection, err := productSectionRepository.Save(productSection); 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 productSection, nil
	//}
	return nil, nil
}

func NewProductSectionService(options map[string]interface{}) *ProductSectionService {
	newProductSectionService := &ProductSectionService{}
	return newProductSectionService
}