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

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

func (service *AcceptanceTaskService) Acceptance(taskId int64, operatorUid int64, participators []int64, taskPercentage []*domain.TaskPercentageItem, referenceResourceScore []*domain.ReferenceResourceItem, solveReport string, solvePictureUrls []string) (*domain.Task, error) {
	var employeeRepository domain.EmployeeRepository
	var taskRepository domain.TaskRepository
	var suMoneyTransactionRecordRepository domain.SuMoneyTransactionRecordRepository
	var employeeDao *dao.EmployeeDao
	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.NewSuMoneyTransactionRecordRepository(service.transactionContext); err != nil {
		return nil, err
	} else {
		suMoneyTransactionRecordRepository = repository
	}
	if dao, err := dao.NewEmployeeDao(service.transactionContext); err != nil {
		return nil, err
	} else {
		employeeDao = 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("无效的任务")
	}
	var participatorInfos []*domain.EmployeeInfo
	for _, participatorUid := range participators {
		if participator, err := employeeRepository.FindOne(map[string]interface{}{
			"uid": participatorUid,
		}); err != nil {
			return nil, err
		} else {
			if participator == nil {
				return nil, fmt.Errorf("无效的参与人")
			}
			participatorInfos = append(participatorInfos, participator.EmployeeInfo)
		}
	}
	if err := task.Acceptance(participatorInfos, taskPercentage, referenceResourceScore, solveReport, solvePictureUrls); err != nil {
		return nil, err
	}
	for _, taskPercentageItem := range taskPercentage {
		suMoneyTransactionRecord := &domain.SuMoneyTransactionRecord{
			RecordType:        domain.SU_MONEY_TRANSACTION_RECORD_TYPE_AWARD,
			Employee:          taskPercentageItem.Contributor,
			SuMoney:           taskPercentageItem.SuMoney,
			Operator:          operator.EmployeeInfo,
			RecordDescription: fmt.Sprintf("完成[%s]任务奖励", task.TaskName),
			CreateTime:        time.Now(),
		}
		if _, err := suMoneyTransactionRecordRepository.Save(suMoneyTransactionRecord); err != nil {
			return nil, err
		}
		if err := employeeDao.TransferSuMoney(taskPercentageItem.Contributor.Uid, task.SuMoney); err != nil {
			return nil, err
		}
	}
	if task, err := taskRepository.Save(task); err != nil {
		return nil, err
	} else {
		return task, nil
	}
}

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