pg_release_task_service.go 3.7 KB
package domain_service

import (
	"fmt"
	"github.com/linmadan/egglib-go/core/application"
	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 ReleaseTaskService struct {
	coreDomain.BaseEventPublisher
	transactionContext *pgTransaction.TransactionContext
}

func (service *ReleaseTaskService) Release(taskId int64, operatorUid int64) (*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 task.ReferenceResource != nil && len(task.ReferenceResource.ReferenceResourceItems) > 0 {
		var referenceResourceIds []int64
		for _, referenceResourceItem := range task.ReferenceResource.ReferenceResourceItems {
			referenceResourceIds = append(referenceResourceIds, referenceResourceItem.ReferenceResourceId)
		}
		queryOptions := make(map[string]interface{})
		queryOptions["referenceResourceIds"] = referenceResourceIds
		queryOptions["isFilterCloseStatus"] = true
		queryOptions["isFilterUnReleasedStatus"] = true
		if count, tasks, err := taskRepository.Find(queryOptions); err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		} else {
			if count > 1 {
				return nil, fmt.Errorf("问题资源已经被引用,发布任务失败")
			}
			if count == 1 {
				if tasks[0].TaskId != task.TaskId {
					return nil, fmt.Errorf("问题资源已经被引用,发布任务失败")
				}
			}
		}
	}
	if err := task.Release(); err != nil {
		return nil, err
	}
	if task, err := taskRepository.Save(task); err != nil {
		return nil, err
	} else {
		taskReleasedEvent := new(event.TaskReleased)
		taskReleasedEvent.OccurredOn = time.Now()
		taskReleasedEvent.TaskId = task.TaskId
		taskReleasedEvent.CompanyId = task.CompanyId
		taskReleasedEvent.TaskName = task.TaskName
		taskReleasedEvent.TaskType = task.TaskType
		taskReleasedEvent.Sponsor = task.Sponsor
		taskReleasedEvent.TaskStatus = task.TaskStatus
		taskReleasedEvent.ReferenceResource = task.ReferenceResource
		taskReleasedEvent.CustomerValue = task.CustomerValue
		taskReleasedEvent.TaskNature = task.TaskNature
		taskReleasedEvent.SuMoney = task.SuMoney
		taskReleasedEvent.AcceptanceStandard = task.AcceptanceStandard
		taskReleasedEvent.TaskDescription = task.TaskDescription
		taskReleasedEvent.BidInfo = task.BidInfo
		if err := service.Publish(taskReleasedEvent); err != nil {
			println(err.Error())
		}
		return task, nil
	}
}

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