fast_domain_repository.go 4.2 KB
package factory

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

// FastPgWorkshop  快速返回车间对象
//
// transactionContext 事务
// id  对象唯一标识
func FastPgWorkshop(transactionContext application.TransactionContext, id int, options ...option) (domain.WorkshopRepository, *domain.Workshop, error) {
	var rep domain.WorkshopRepository
	var mod *domain.Workshop
	var err error
	if value, err := CreateWorkshopRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		rep = value
	}
	if id > 0 {
		if mod, err = rep.FindOne(map[string]interface{}{"workshopId": id}); err != nil {
			if err == domain.ErrorNotFound {
				return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该车间不存在")
			}
			return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
	}
	//if err = fastPgDataAuth(transactionContext, mod, options...); err != nil {
	//	return nil, nil, err
	//}
	return rep, mod, err
}

// FastPgWorkstation  快速返回工作定位对象
//
// transactionContext 事务
// workshopId  车间ID
// lineId      生产线ID
// sectionId   对象ID
func FastPgWorkstation(transactionContext application.TransactionContext, workshopId, lineId, sectionId int, options ...option) (domain.WorkshopRepository, *domain.WorkStation, error) {
	var rep domain.WorkshopRepository
	var mod *domain.Workshop
	var err error
	if value, err := CreateWorkshopRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		rep = value
	}

	if mod, err = rep.FindOne(map[string]interface{}{"workshopId": workshopId}); err != nil {
		if err == domain.ErrorNotFound {
			return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该车间不存在")
		}
		return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}

	workStation, err := mod.FindWorkStation(workshopId, lineId, sectionId)
	if err != nil {
		return rep, nil, err
	}
	o := NewFastOptions(options...)
	if o.SetPrincipal {
		workStation.Principal = mod.Principal
	}
	return rep, workStation, err
}

// FastPgProductJob  快速返回工位对象
//
// transactionContext 事务
// id  对象唯一标识
func FastPgProductJob(transactionContext application.TransactionContext, id int, options ...option) (domain.ProductJobRepository, *domain.ProductJob, error) {
	var rep domain.ProductJobRepository
	var mod *domain.ProductJob
	var err error
	if value, err := CreateProductJobRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		rep = value
	}
	if id > 0 {
		if mod, err = rep.FindOne(map[string]interface{}{"productJobId": id}); err != nil {
			if err == domain.ErrorNotFound {
				return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该车间不存在")
			}
			return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
	}
	//if err = fastPgDataAuth(transactionContext, mod, options...); err != nil {
	//	return nil, nil, err
	//}
	return rep, mod, err
}

/*****   2.配置  *****/

type FastOptions struct {
	DataAuthRequired bool
	SetPrincipal     bool
	OperateInfo      *domain.OperateInfo
}

func NewFastOptions(options ...option) *FastOptions {
	o := &FastOptions{
		DataAuthRequired: false,
	}
	for i := 0; i < len(options); i++ {
		options[i](o)
	}
	return o
}

type option func(options *FastOptions)

// 需要数据权限
func WithDataAuthRequired() option {
	return func(options *FastOptions) {
		options.DataAuthRequired = true
	}
}

// WithOperator 操作人
func WithOperator(op *domain.OperateInfo) option {
	return func(options *FastOptions) {
		options.OperateInfo = op
	}
}

// WithOperator 操作人
func WithSetPrincipal() option {
	return func(options *FastOptions) {
		options.SetPrincipal = true
	}
}