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

// 生产计划服务
type ProductPlanService struct {
}

// 创建生产计划服务
func (productPlanService *ProductPlanService) CreateProductPlan(createProductPlanCommand *command.CreateProductPlanCommand) (interface{}, error) {
	if err := createProductPlanCommand.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()
	}()
	newProductPlan := &domain.ProductPlan{
		CompanyId: createProductPlanCommand.CompanyId,
		OrgId:     createProductPlanCommand.OrgId,
		//WorkshopId:      createProductPlanCommand.WorkshopId,
		BatchNumber:     createProductPlanCommand.BatchNumber,
		ProductDate:     createProductPlanCommand.ProductDate,
		WorkOn:          createProductPlanCommand.WorkOn,
		Machine:         createProductPlanCommand.Machine,
		PlanProductName: createProductPlanCommand.PlanProductName,
		//PlanDevoted:     createProductPlanCommand.PlanDevoted,
		Remark: createProductPlanCommand.Remark,
	}
	var productPlanRepository domain.ProductPlanRepository
	if value, err := factory.CreateProductPlanRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productPlanRepository = value
	}
	if productPlan, err := productPlanRepository.Save(newProductPlan); 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 productPlan, nil
	}
}

// 返回生产计划服务
func (productPlanService *ProductPlanService) GetProductPlan(getProductPlanQuery *query.GetProductPlanQuery) (interface{}, error) {
	if err := getProductPlanQuery.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 productPlanRepository domain.ProductPlanRepository
	if value, err := factory.CreateProductPlanRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productPlanRepository = value
	}
	productPlan, err := productPlanRepository.FindOne(map[string]interface{}{"productPlanId": getProductPlanQuery.ProductPlanId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if productPlan == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getProductPlanQuery.ProductPlanId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return productPlan, nil
	}
}

// 返回生产计划服务列表
func (productPlanService *ProductPlanService) ListProductPlan(listProductPlanQuery *query.ListProductPlanQuery) (interface{}, error) {
	if err := listProductPlanQuery.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 productPlanRepository domain.ProductPlanRepository
	if value, err := factory.CreateProductPlanRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productPlanRepository = value
	}
	if count, productPlans, err := productPlanRepository.Find(tool_funs.SimpleStructToMap(listProductPlanQuery)); 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,
			"productPlans": productPlans,
		}, nil
	}
}

// 领料
func (productPlanService *ProductPlanService) ReceiveMaterial(receiveMaterialCommand *command.ReceiveMaterialCommand) (interface{}, error) {
	if err := receiveMaterialCommand.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()
	}()
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return nil, nil
}

// 移除生产计划服务
func (productPlanService *ProductPlanService) RemoveProductPlan(removeProductPlanCommand *command.RemoveProductPlanCommand) (interface{}, error) {
	if err := removeProductPlanCommand.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 productPlanRepository domain.ProductPlanRepository
	if value, err := factory.CreateProductPlanRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productPlanRepository = value
	}
	productPlan, err := productPlanRepository.FindOne(map[string]interface{}{"productPlanId": removeProductPlanCommand.ProductPlanId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if productPlan == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeProductPlanCommand.ProductPlanId)))
	}
	if productPlan, err := productPlanRepository.Remove(productPlan); 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 productPlan, nil
	}
}

// 退料
func (productPlanService *ProductPlanService) ReturnMaterial(returnMaterialCommand *command.ReturnMaterialCommand) (interface{}, error) {
	if err := returnMaterialCommand.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()
	}()
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return nil, nil
}

// 计划下线
func (productPlanService *ProductPlanService) SetOffline(setOfflineCommand *command.SetOfflineCommand) (interface{}, error) {
	if err := setOfflineCommand.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()
	}()
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return nil, nil
}

// 计划上线
func (productPlanService *ProductPlanService) SetOnline(setOnlineCommand *command.SetOnlineCommand) (interface{}, error) {
	if err := setOnlineCommand.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()
	}()
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return nil, nil
}

// 提交成品记录 (成品 二级品)
func (productPlanService *ProductPlanService) SubmitProductRecord(submitProductRecordCommand *command.SubmitProductRecordCommand) (interface{}, error) {
	if err := submitProductRecordCommand.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()
	}()
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return nil, nil
}

// 换单
func (productPlanService *ProductPlanService) Switch(switchCommand *command.SwitchCommand) (interface{}, error) {
	if err := switchCommand.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()
	}()
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return nil, nil
}

// 更新生产计划服务
func (productPlanService *ProductPlanService) UpdateProductPlan(updateProductPlanCommand *command.UpdateProductPlanCommand) (interface{}, error) {
	if err := updateProductPlanCommand.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 productPlanRepository domain.ProductPlanRepository
	if value, err := factory.CreateProductPlanRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productPlanRepository = value
	}
	productPlan, err := productPlanRepository.FindOne(map[string]interface{}{"productPlanId": updateProductPlanCommand.ProductPlanId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if productPlan == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateProductPlanCommand.ProductPlanId)))
	}
	if err := productPlan.Update(tool_funs.SimpleStructToMap(updateProductPlanCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if productPlan, err := productPlanRepository.Save(productPlan); 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 productPlan, nil
	}
}

func NewProductPlanService(options map[string]interface{}) *ProductPlanService {
	newProductPlanService := &ProductPlanService{}
	return newProductPlanService
}