pg_choose_successful_bidder_service.go 3.2 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/dao"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/repository"
	"time"
)

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

func (service *ChooseSuccessfulBidderService) Choose(taskId int64, successfulBidderUid int64, operatorUid int64, plannedCompletionTime time.Time) (*domain.Task, error) {
	var employeeRepository domain.EmployeeRepository
	var taskRepository domain.TaskRepository
	var taskDao *dao.TaskDao
	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
	}
	if dao, err := dao.NewTaskDao(service.transactionContext); err != nil {
		return nil, err
	} else {
		taskDao = dao
	}
	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("无效的操作者")
	}
	successfulBidder, err := employeeRepository.FindOne(map[string]interface{}{
		"uid": successfulBidderUid,
	})
	if err != nil {
		return nil, err
	}
	if successfulBidder == nil {
		return nil, fmt.Errorf("无效的中标人")
	}
	if err := task.ChooseSuccessfulBidder(successfulBidder.EmployeeInfo, plannedCompletionTime); err != nil {
		return nil, err
	}
	if err := taskDao.SetSuccessfulBidder(taskId, task.BidInfo.SuccessfulBidder, task.BidInfo.WinBidTime); err != nil {
		return nil, err
	}
	if task, err := taskRepository.Save(task); err != nil {
		return nil, err
	} else {
		taskBiddedEvent := new(event.TaskBidded)
		taskBiddedEvent.OccurredOn = time.Now()
		taskBiddedEvent.TaskId = task.TaskId
		taskBiddedEvent.CompanyId = task.CompanyId
		taskBiddedEvent.TaskName = task.TaskName
		taskBiddedEvent.TaskType = task.TaskType
		taskBiddedEvent.Sponsor = task.Sponsor
		taskBiddedEvent.TaskStatus = task.TaskStatus
		taskBiddedEvent.ReferenceResource = task.ReferenceResource
		taskBiddedEvent.BidInfo = task.BidInfo
		taskBiddedEvent.Operator = operator.EmployeeInfo
		if err := service.Publish(taskBiddedEvent); err != nil {
			println(err.Error())
		}
		return task, nil
	}
}

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