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

// 生产班组服务
type ProductGroupService struct {
}

// 创建生产班组服务
func (productGroupService *ProductGroupService) CreateProductGroup(createProductGroupCommand *command.CreateProductGroupCommand) (interface{}, error) {
	if err := createProductGroupCommand.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()
	}()
	newProductGroup := &domain.ProductGroup{
		CompanyId: createProductGroupCommand.CompanyId,
		OrgId:     createProductGroupCommand.OrgId,
		//WorkshopId:   createProductGroupCommand.WorkshopId,
		//LineId:       createProductGroupCommand.LineId,
		//SectionId:    createProductGroupCommand.SectionId,
		GroupName: createProductGroupCommand.GroupName,
		//GroupLeader:  createProductGroupCommand.GroupLeader,
		GroupMembers: createProductGroupCommand.GroupMembers,
		WorkOn:       createProductGroupCommand.WorkOn,
	}
	var productGroupRepository domain.ProductGroupRepository
	if value, err := factory.CreateProductGroupRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productGroupRepository = value
	}
	if productGroup, err := productGroupRepository.Save(newProductGroup); 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 productGroup, nil
	}
}

// 返回生产班组服务
func (productGroupService *ProductGroupService) GetProductGroup(getProductGroupQuery *query.GetProductGroupQuery) (interface{}, error) {
	if err := getProductGroupQuery.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 productGroupRepository domain.ProductGroupRepository
	if value, err := factory.CreateProductGroupRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productGroupRepository = value
	}
	productGroup, err := productGroupRepository.FindOne(map[string]interface{}{"productGroupId": getProductGroupQuery.ProductGroupId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if productGroup == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getProductGroupQuery.ProductGroupId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return productGroup, nil
	}
}

// 返回生产班组服务列表
func (productGroupService *ProductGroupService) ListProductGroup(listProductGroupQuery *query.ListProductGroupQuery) (interface{}, error) {
	if err := listProductGroupQuery.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 productGroupRepository domain.ProductGroupRepository
	if value, err := factory.CreateProductGroupRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productGroupRepository = value
	}
	if count, productGroups, err := productGroupRepository.Find(tool_funs.SimpleStructToMap(listProductGroupQuery)); 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,
			"productGroups": productGroups,
		}, nil
	}
}

// 移除生产班组服务
func (productGroupService *ProductGroupService) RemoveProductGroup(removeProductGroupCommand *command.RemoveProductGroupCommand) (interface{}, error) {
	if err := removeProductGroupCommand.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 productGroupRepository domain.ProductGroupRepository
	if value, err := factory.CreateProductGroupRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productGroupRepository = value
	}
	productGroup, err := productGroupRepository.FindOne(map[string]interface{}{"productGroupId": removeProductGroupCommand.ProductGroupId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if productGroup == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeProductGroupCommand.ProductGroupId)))
	}
	if productGroup, err := productGroupRepository.Remove(productGroup); 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 productGroup, nil
	}
}

// 更新生产班组服务
func (productGroupService *ProductGroupService) UpdateProductGroup(updateProductGroupCommand *command.UpdateProductGroupCommand) (interface{}, error) {
	if err := updateProductGroupCommand.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 productGroupRepository domain.ProductGroupRepository
	if value, err := factory.CreateProductGroupRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productGroupRepository = value
	}
	productGroup, err := productGroupRepository.FindOne(map[string]interface{}{"productGroupId": updateProductGroupCommand.ProductGroupId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if productGroup == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateProductGroupCommand.ProductGroupId)))
	}
	if err := productGroup.Update(tool_funs.SimpleStructToMap(updateProductGroupCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if productGroup, err := productGroupRepository.Save(productGroup); 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 productGroup, nil
	}
}

func NewProductGroupService(options map[string]interface{}) *ProductGroupService {
	newProductGroupService := &ProductGroupService{}
	return newProductGroupService
}