approveflow/app/provider/flow_instance/service.go

189 lines
6.1 KiB
Go

package flow_instance
import (
"approveflow/app/provider/flow_definition"
"approveflow/app/provider/flow_instance/model"
"context"
"github.com/Superdanda/hade/framework"
"github.com/Superdanda/hade/framework/contract"
"github.com/Superdanda/hade/framework/gin"
"github.com/pkg/errors"
)
type FlowInstanceService struct {
container framework.Container
}
func (f *FlowInstanceService) CreateInstance(ctx *gin.Context, approvalFlow *flow_definition.ApprovalFlow, data map[string]interface{}) (*model.ApprovalInstance, error) {
instance, err := model.NewApprovalInstance(ctx, approvalFlow, data)
if err != nil {
return nil, err
}
err = f.GetFlowInstanceRepository().SaveInstance(ctx, instance)
if err != nil {
return nil, err
}
return instance, nil
}
func (f *FlowInstanceService) StartInstance(ctx context.Context, instanceID int64) (*model.ApprovalInstance, error) {
return f.handlerInstance(ctx, instanceID, func(ctx context.Context, instance *model.ApprovalInstance) error {
err := instance.Start(ctx)
return err
})
}
func (f *FlowInstanceService) GetInstance(ctx context.Context, instanceID int64) (*model.ApprovalInstance, error) {
return f.GetFlowInstanceRepository().GetInstanceByID(ctx, instanceID)
}
func (f *FlowInstanceService) GetInstancePage(ctx context.Context, pageNum, pageSize int, instance *model.ApprovalInstance) ([]*model.ApprovalInstance, int64, error) {
return f.GetFlowInstanceRepository().GetInstancePage(ctx, pageNum, pageSize, instance)
}
func (f *FlowInstanceService) CancelInstance(ctx context.Context, instanceID int64) error {
//TODO implement me
panic("implement me")
}
func (f *FlowInstanceService) ApproveStep(ctx context.Context, instanceID int64, stepID int64, comments string) error {
return f.handlerStep(ctx, instanceID, stepID, func(ctx context.Context, instance *model.ApprovalInstance, step *model.InstanceStep) error {
err := step.Approve(comments)
if err != nil {
return err
}
return nil
})
}
func (f *FlowInstanceService) RejectStep(ctx context.Context, instanceID int64, stepID int64, comments string) error {
return f.handlerStep(ctx, instanceID, stepID, func(ctx context.Context, instance *model.ApprovalInstance, step *model.InstanceStep) error {
err := step.Reject(comments)
if err != nil {
return err
}
return nil
})
}
func (f *FlowInstanceService) GetCurrentStep(ctx context.Context, instanceID int64) ([]*model.InstanceStep, error) {
instance, err := f.GetInstance(ctx, instanceID)
if err != nil {
return nil, err
}
return instance.GetCurrentSteps(), nil
}
func (f *FlowInstanceService) AddCustomStep(ctx context.Context, instanceID int64, step *model.InstanceStep, fromStepKey, toStepKey string) (*model.ApprovalInstance, error) {
return f.handlerInstance(ctx, instanceID, func(ctx context.Context, instance *model.ApprovalInstance) error {
fromStep, err := instance.GetStepByKey(fromStepKey)
if err != nil {
return err
}
// 如果是 自定义阶段 那么它的路径配置能够实例对象中找到
if fromStep.IsDynamic {
//instance.InstancePathConfigs
}
return nil
})
}
func (f *FlowInstanceService) GetCustomSteps(ctx context.Context, instanceID int64) ([]*model.InstanceStep, error) {
//TODO implement me
panic("implement me")
}
func (f *FlowInstanceService) RemoveCustomStep(ctx context.Context, instanceID int64, stepID int64) error {
//TODO implement me
panic("implement me")
}
func (f *FlowInstanceService) PerformReversal(ctx context.Context, instanceID int64, stepID int64, reason string) error {
//TODO implement me
panic("implement me")
}
func (f *FlowInstanceService) GetReversalInfo(instanceID int64) (model.ApprovalReversal, error) {
//TODO implement me
panic("implement me")
}
func (f *FlowInstanceService) CreateBatchApprovalTask(ctx context.Context, approverKey string, stepIDs []int64) (*model.BatchApprovalTask, error) {
//TODO implement me
panic("implement me")
}
func (f *FlowInstanceService) ExecuteBatchApprovalTask(ctx context.Context, taskID int64, approverKey string, comments string) error {
//TODO implement me
panic("implement me")
}
func (f *FlowInstanceService) GetBatchApprovalTasks(ctx context.Context, approverKey string) ([]model.BatchApprovalTask, error) {
//TODO implement me
panic("implement me")
}
func (f *FlowInstanceService) GetApprovalRecords(ctx context.Context, instanceID int64) ([]*model.ApprovalRecord, error) {
//TODO implement me
panic("implement me")
}
func NewFlowInstanceService(params ...interface{}) (interface{}, error) {
container := params[0].(framework.Container)
return &FlowInstanceService{container: container}, nil
}
func (f *FlowInstanceService) GetFlowInstanceRepository() FlowInstanceRepository {
infrastructureService := f.container.MustMake(contract.InfrastructureKey).(contract.InfrastructureService)
return infrastructureService.GetModuleOrmRepository(InstanceRepositoryKey).(FlowInstanceRepository)
}
func (f *FlowInstanceService) handlerInstance(ctx context.Context, instanceID int64,
handler func(ctx context.Context, instance *model.ApprovalInstance) error,
) (*model.ApprovalInstance, error) {
instance, err := f.GetInstance(ctx, instanceID)
if err != nil {
return nil, err
}
err = handler(ctx, instance)
if err != nil {
return nil, err
}
return instance, nil
}
func (f *FlowInstanceService) handlerStep(ctx context.Context, instanceID int64, stepID int64,
handler func(ctx context.Context, instance *model.ApprovalInstance, step *model.InstanceStep) error,
) error {
instance, instanceStep, err := f.GetInstanceAndCurrentStep(ctx, instanceID, stepID)
if err != nil {
return err
}
err = handler(ctx, instance, instanceStep)
if err != nil {
return err
}
err = f.GetFlowInstanceRepository().SaveInstance(ctx, instance)
if err != nil {
return err
}
return nil
}
func (f *FlowInstanceService) GetInstanceAndCurrentStep(ctx context.Context, instanceID int64, stepID int64) (*model.ApprovalInstance, *model.InstanceStep, error) {
instance, err := f.GetInstance(ctx, instanceID)
if err != nil {
return nil, nil, err
}
step := instance.GetCurrentStepsById(stepID)
if step == nil {
return instance, nil, errors.New("step not found")
}
return instance, step, nil
}