pg_acceptance_task_service.go 7.1 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/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, suMoney float64, taskPercentage []*domain.TaskPercentageItem, referenceResourceScore []*domain.ReferenceResourceItem, solveReport string, solvePictureUrls []string, actualCompletionTime time.Time) (*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,
		"status": 1,
	})
	if err != nil {
		return nil, err
	}
	if operator == nil {
		// 获取无效的发布人员信息
		operatorInvalid, err := employeeRepository.FindOne(map[string]interface{}{
			"uid": operatorUid,
		})
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		if operatorInvalid == nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的操作者")
		}
		return nil, fmt.Errorf("无效的操作者:" + operatorInvalid.EmployeeInfo.EmployeeName)
	}
	task, err := taskRepository.FindOne(map[string]interface{}{
		"taskId": taskId,
	})

	if err != nil {
		return nil, err
	}
	if task == nil {
		return nil, fmt.Errorf("无效的任务")
	}
	participatorMap := make(map[int64]*domain.Employee)
	var participatorInfos []*domain.EmployeeInfo
	for _, participatorUid := range participators {
		if participator, err := employeeRepository.FindOne(map[string]interface{}{
			"uid": participatorUid,
			"status": 1,
		}); err != nil {
			return nil, err
		} else {
			if participator == nil {
				// 获取无效的发布人员信息
				participatorInvalid, err := employeeRepository.FindOne(map[string]interface{}{
					"uid": participatorUid,
				})
				if err != nil {
					return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
				}
				if participatorInvalid == nil {
					return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的参与人")
				}
				return nil, fmt.Errorf("无效的参与人:" + participatorInvalid.EmployeeInfo.EmployeeName)
			}
			participatorMap[participator.EmployeeInfo.Uid] = participator
			participatorInfos = append(participatorInfos, participator.EmployeeInfo)
		}
	}
	if err := task.Acceptance(participatorInfos, suMoney, taskPercentage, referenceResourceScore, solveReport, solvePictureUrls, actualCompletionTime); err != nil {
		return nil, err
	}
	for _, taskPercentageItem := range taskPercentage {
		var taskTypeName string
		if task.TaskType == domain.TASK_TYPE_BID {
			taskTypeName = "竞标任务"
		}
		if task.TaskType == domain.TASK_TYPE_ROB {
			taskTypeName = "抢单任务"
		}
		if task.TaskType == domain.TASK_TYPE_DESIGNATE {
			taskTypeName = "指派任务"
		}

		var employee *domain.Employee
		if taskPercentageItem.IsReceiver {
			receiver, err := employeeRepository.FindOne(map[string]interface{}{
				"uid": taskPercentageItem.Contributor.Uid,
				"status": 1,
			})
			if err != nil {
				return nil, err
			}
			if receiver == nil {
				// 获取无效的任务领取人员信息
				receiverInvalid, err := employeeRepository.FindOne(map[string]interface{}{
					"uid": operatorUid,
				})
				if err != nil {
					return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
				}
				if receiverInvalid == nil {
					return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的任务领取者")
				}
				return nil, fmt.Errorf("无效的任务领取者:" + receiverInvalid.EmployeeInfo.EmployeeName)
			}
			employee = receiver
		} else {
			participator, ok := participatorMap[taskPercentageItem.Contributor.Uid]
			if !ok {
				return nil, fmt.Errorf("无效的参与人")
			}
			employee = participator
		}
		suMoneyTransactionRecord := &domain.SuMoneyTransactionRecord{
			RecordType:               domain.SU_MONEY_TRANSACTION_RECORD_TYPE_AWARD,
			Employee:                 taskPercentageItem.Contributor,
			SuMoneyBeforeTransaction: employee.SuMoney,
			CurrentSuMoney:           employee.SuMoney + taskPercentageItem.SuMoney,
			SuMoney:                  taskPercentageItem.SuMoney,
			Operator:                 operator.EmployeeInfo,
			RecordDescription:        fmt.Sprintf("%s-%s", taskTypeName, task.TaskName),
			CreateTime:               time.Now(),
		}
		if _, err := suMoneyTransactionRecordRepository.Save(suMoneyTransactionRecord); err != nil {
			return nil, err
		}
		if err := employeeDao.TransferSuMoney(taskPercentageItem.Contributor.Uid, taskPercentageItem.SuMoney); err != nil {
			return nil, err
		}
	}
	if task, err := taskRepository.Save(task); err != nil {
		return nil, err
	} else {
		taskAcceptancedEvent := new(event.TaskAcceptanced)
		taskAcceptancedEvent.OccurredOn = time.Now()
		taskAcceptancedEvent.TaskId = task.TaskId
		taskAcceptancedEvent.CompanyId = task.CompanyId
		taskAcceptancedEvent.TaskName = task.TaskName
		taskAcceptancedEvent.TaskType = task.TaskType
		taskAcceptancedEvent.Sponsor = task.Sponsor
		taskAcceptancedEvent.TaskStatus = task.TaskStatus
		taskAcceptancedEvent.ReferenceResource = task.ReferenceResource
		taskAcceptancedEvent.AcceptanceStandard = task.AcceptanceStandard
		taskAcceptancedEvent.Participators = task.Participators
		taskAcceptancedEvent.TaskPercentage = task.TaskPercentage
		taskAcceptancedEvent.SolveReport = task.SolveReport
		taskAcceptancedEvent.SolvePictureUrls = task.SolvePictureUrls
		if err := service.Publish(taskAcceptancedEvent); err != nil {
			return nil, err
		}
		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
	}
}