pg_apply_complete_task_service.go 2.5 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/infrastructure/repository"
)

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

func (service *ApplyCompleteTaskService) ApplyComplete(taskId int64, receiverUid 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
	}
	receiver, err := employeeRepository.FindOne(map[string]interface{}{
		"uid": receiverUid,
	})
	if err != nil {
		return nil, err
	}
	if receiver == 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 task.TaskType == domain.TASK_TYPE_ROB {
		if task.RobInfo != nil && task.RobInfo.Receiver != nil {
			if receiver.EmployeeInfo.Uid != task.RobInfo.Receiver.Uid {
				return nil, fmt.Errorf("无法申请完成别人领取的任务")
			}
		} else {
			return nil, fmt.Errorf("无法申请完成任务,任务可能已经被驳回")
		}
	}
	if task.TaskType == domain.TASK_TYPE_BID {
		if task.BidInfo != nil && task.BidInfo.SuccessfulBidder != nil {
			if receiver.EmployeeInfo.Uid != task.BidInfo.SuccessfulBidder.Uid {
				return nil, fmt.Errorf("无法申请完成别人领取的任务")
			}
		} else {
			return nil, fmt.Errorf("无法申请完成任务,任务可能已经被驳回")
		}
	}
	if err := task.ApplyComplete(); err != nil {
		return nil, err
	}
	if task, err := taskRepository.Save(task); err != nil {
		return nil, err
	} else {
		return task, nil
	}
}

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