product_section.go 9.7 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"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"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/dao"
)

// 工段服务
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()
	}()

	uniqueIdDao, _ := dao.NewUniqueIdDao(transactionContext.(*pgTransaction.TransactionContext))
	uniqueId, err := uniqueIdDao.GenerateUniqueId()
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	newProductSection := &domain.ProductSection{
		SectionId:   uniqueId,
		SectionName: createProductSectionCommand.SectionName,
		Removed:     domain.NotDeleted,
	}
	var workshopRepository domain.WorkshopRepository
	var workshop *domain.Workshop
	workshopRepository, workshop, err = factory.FastPgWorkshop(transactionContext, createProductSectionCommand.WorkshopId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err = workshop.AddSection(createProductSectionCommand.LineId, newProductSection); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if workshop, err = workshopRepository.Save(workshop); 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{}{
		"workshopId":  workshop.WorkshopId,
		"lineId":      createProductSectionCommand.LineId,
		"sectionId":   newProductSection.SectionId,
		"sectionName": newProductSection.SectionName,
	}, 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(cmd *command.RemoveProductSectionCommand) (interface{}, error) {
	if err := cmd.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
	var workshop *domain.Workshop
	workshopRepository, workshop, err = factory.FastPgWorkshop(transactionContext, cmd.WorkshopId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	workStation, err := workshop.FindWorkStation(workshop.WorkshopId, cmd.LineId, cmd.SectionId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	var productJobRepository domain.ProductJobRepository
	productJobRepository, _, _ = factory.FastPgProductJob(transactionContext, 0)
	if count, _, err := productJobRepository.Find(map[string]interface{}{"workStationId": workStation.WorkStationId, "limit": 1}); err == nil && count > 0 {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "该工段下存在工位")
	}

	if err = workshop.RemoveSection(cmd.LineId, cmd.SectionId); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	if workshop, err = workshopRepository.Save(workshop); 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 struct {
	}{}, 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 workshopRepository domain.WorkshopRepository
	var workshop *domain.Workshop
	workshopRepository, workshop, err = factory.FastPgWorkshop(transactionContext, updateProductSectionCommand.WorkshopId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err = workshop.UpdateSection(updateProductSectionCommand.LineId, updateProductSectionCommand.SectionId, updateProductSectionCommand.SectionName); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if workshop, err = workshopRepository.Save(workshop); 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 updateProductSectionCommand, nil
}

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