pg_confirm_rob_task_service.go 2.4 KB
package domain_service

import (
	"fmt"
	coreDomain "github.com/linmadan/egglib-go/core/domain"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain/event"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/repository"
	"time"
)

type ConfirmRobTaskService struct {
	coreDomain.BaseEventPublisher
	transactionContext *pgTransaction.TransactionContext
}

func (service *ConfirmRobTaskService) Confirm(taskId int64, operatorUid int64, plannedCompletionTime time.Time) (*domain.Task, error) {
	var employeeRepository domain.EmployeeRepository
	var taskRepository domain.TaskRepository
	if repository, err := repository.NewEmployeeRepository(service.transactionContext); err != nil {
		return nil, err
	} else {
		employeeRepository = repository
	}
	if repository, err := repository.NewTaskRepository(service.transactionContext); err != nil {
		return nil, err
	} else {
		taskRepository = repository
	}
	operator, err := employeeRepository.FindOne(map[string]interface{}{
		"uid": operatorUid,
	})
	if err != nil {
		return nil, err
	}
	if operator == nil {
		return nil, fmt.Errorf("无效的操作者")
	}
	task, err := taskRepository.FindOne(map[string]interface{}{
		"taskId": taskId,
	})
	if err != nil {
		return nil, err
	}
	if task == nil {
		return nil, fmt.Errorf("无效的任务")
	}
	if operator.EmployeeInfo.Uid != task.Sponsor.Uid {
		return nil, fmt.Errorf("无效的任务发布者")
	}
	if err := task.ConfirmTask(plannedCompletionTime); err != nil {
		return nil, err
	}
	if task, err := taskRepository.Save(task); err != nil {
		return nil, err
	} else {
		taskConfirmed := new(event.TaskConfirmed)
		taskConfirmed.OccurredOn = time.Now()
		taskConfirmed.TaskId = task.TaskId
		taskConfirmed.CompanyId = task.CompanyId
		taskConfirmed.TaskName = task.TaskName
		taskConfirmed.TaskType = task.TaskType
		taskConfirmed.Sponsor = task.Sponsor
		taskConfirmed.TaskStatus = task.TaskStatus
		taskConfirmed.RobInfo = task.RobInfo
		if err := service.Publish(taskConfirmed); err != nil {
			return nil, err
		}
		return task, nil
	}
}

func NewConfirmRobTaskService(transactionContext *pgTransaction.TransactionContext) (*ConfirmRobTaskService, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &ConfirmRobTaskService{
			transactionContext: transactionContext,
		}, nil
	}
}