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

// 单位换算服务
type UnitConversionService struct {
}

// 创建单位换算服务
func (unitConversionService *UnitConversionService) CreateUnitConversion(createUnitConversionCommand *command.CreateUnitConversionCommand) (interface{}, error) {
	if err := createUnitConversionCommand.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()
	}()
	newUnitConversion := &domain.UnitConversion{
		CompanyId: createUnitConversionCommand.CompanyId,
		OrgId:     createUnitConversionCommand.OrgId,
		//WorkshopId:              createUnitConversionCommand.WorkshopId,
		//LineId:                  createUnitConversionCommand.LineId,
		//SectionId:               createUnitConversionCommand.SectionId,
		//Material:                createUnitConversionCommand.Material,
		//FromUnitQuantity:        createUnitConversionCommand.FromUnitQuantity,
		//ToUnitQuantity:          createUnitConversionCommand.ToUnitQuantity,
		IntelligentWeighingFlag: createUnitConversionCommand.IntelligentWeighingFlag,
	}
	var unitConversionRepository domain.UnitConversionRepository
	if value, err := factory.CreateUnitConversionRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		unitConversionRepository = value
	}
	if unitConversion, err := unitConversionRepository.Save(newUnitConversion); 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 unitConversion, nil
	}
}

// 返回单位换算服务
func (unitConversionService *UnitConversionService) GetUnitConversion(getUnitConversionQuery *query.GetUnitConversionQuery) (interface{}, error) {
	if err := getUnitConversionQuery.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 unitConversionRepository domain.UnitConversionRepository
	if value, err := factory.CreateUnitConversionRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		unitConversionRepository = value
	}
	unitConversion, err := unitConversionRepository.FindOne(map[string]interface{}{"unitConversionId": getUnitConversionQuery.UnitConversionId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if unitConversion == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getUnitConversionQuery.UnitConversionId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return unitConversion, nil
	}
}

// 返回单位换算服务列表
func (unitConversionService *UnitConversionService) ListUnitConversion(listUnitConversionQuery *query.ListUnitConversionQuery) (interface{}, error) {
	if err := listUnitConversionQuery.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 unitConversionRepository domain.UnitConversionRepository
	if value, err := factory.CreateUnitConversionRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		unitConversionRepository = value
	}
	if count, unitConversions, err := unitConversionRepository.Find(tool_funs.SimpleStructToMap(listUnitConversionQuery)); 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,
			"unitConversions": unitConversions,
		}, nil
	}
}

// 移除单位换算服务
func (unitConversionService *UnitConversionService) RemoveUnitConversion(removeUnitConversionCommand *command.RemoveUnitConversionCommand) (interface{}, error) {
	if err := removeUnitConversionCommand.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 unitConversionRepository domain.UnitConversionRepository
	if value, err := factory.CreateUnitConversionRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		unitConversionRepository = value
	}
	unitConversion, err := unitConversionRepository.FindOne(map[string]interface{}{"unitConversionId": removeUnitConversionCommand.UnitConversionId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if unitConversion == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeUnitConversionCommand.UnitConversionId)))
	}
	if unitConversion, err := unitConversionRepository.Remove(unitConversion); 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 unitConversion, nil
	}
}

// 更新单位换算服务
func (unitConversionService *UnitConversionService) UpdateUnitConversion(updateUnitConversionCommand *command.UpdateUnitConversionCommand) (interface{}, error) {
	if err := updateUnitConversionCommand.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 unitConversionRepository domain.UnitConversionRepository
	if value, err := factory.CreateUnitConversionRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		unitConversionRepository = value
	}
	unitConversion, err := unitConversionRepository.FindOne(map[string]interface{}{"unitConversionId": updateUnitConversionCommand.UnitConversionId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if unitConversion == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateUnitConversionCommand.UnitConversionId)))
	}
	if err := unitConversion.Update(tool_funs.SimpleStructToMap(updateUnitConversionCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if unitConversion, err := unitConversionRepository.Save(unitConversion); 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 unitConversion, nil
	}
}

func NewUnitConversionService(options map[string]interface{}) *UnitConversionService {
	newUnitConversionService := &UnitConversionService{}
	return newUnitConversionService
}