pg_reject_task_service.go 3.6 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 RejectTaskReceiverService struct {
	coreDomain.BaseEventPublisher
	transactionContext *pgTransaction.TransactionContext
}

func (service *RejectTaskReceiverService) Reject(taskId int64, operatorUid int64, rejectReason string) (*domain.Task, error) {
	var employeeRepository domain.EmployeeRepository
	var taskRepository domain.TaskRepository
	var rejectTaskRecordRepository domain.RejectTaskRecordRepository
	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 repository, err := repository.NewRejectTaskRecordRepository(service.transactionContext); err != nil {
		return nil, err
	} else {
		rejectTaskRecordRepository = 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("无效的任务发布者")
	}
	if err := task.RejectReceiver(); err != nil {
		return nil, err
	}
	newRejectTaskRecord := &domain.RejectTaskRecord{
		Task:         task,
		Operator:     operator.EmployeeInfo,
		RejectReason: rejectReason,
		CreateTime:   time.Now(),
	}
	if task.TaskType == domain.TASK_TYPE_ROB {
		if err := taskDao.DeleteRobInfo(taskId); err != nil {
			return nil, err
		}
	}
	if task.TaskType == domain.TASK_TYPE_BID {
		if err := taskDao.CancelSuccessfulBidder(taskId); err != nil {
			return nil, err
		}
	}
	if rejectTaskRecord, err := rejectTaskRecordRepository.Save(newRejectTaskRecord); err != nil {
		return nil, err
	} else {
		newRejectTaskRecord = rejectTaskRecord
	}
	if task, err := taskRepository.Save(task); err != nil {
		return nil, err
	} else {
		taskRejectedEvent := new(event.TaskRejected)
		taskRejectedEvent.OccurredOn = time.Now()
		taskRejectedEvent.TaskId = task.TaskId
		taskRejectedEvent.CompanyId = task.CompanyId
		taskRejectedEvent.TaskName = task.TaskName
		taskRejectedEvent.TaskType = task.TaskType
		taskRejectedEvent.Sponsor = task.Sponsor
		taskRejectedEvent.TaskStatus = task.TaskStatus
		taskRejectedEvent.RobInfo = task.RobInfo
		taskRejectedEvent.BidInfo = task.BidInfo
		taskRejectedEvent.RejectTaskRecord = newRejectTaskRecord
		if err := service.Publish(taskRejectedEvent); err != nil {
			return nil, err
		}
		return task, nil
	}
}

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