ability_service_subscriber.go 4.0 KB
package subscriber

import (
	"fmt"
	"github.com/linmadan/egglib-go/core/domain"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/factory"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain/event"
)

type AbilityServiceSubscriber struct {
}

func (subscriber *AbilityServiceSubscriber) HandleEvent(domainEvent domain.DomainEvent) error {
	abilityServiceGateway, err := factory.CreateAbilityServiceGateway(nil)
	if err != nil {
		fmt.Println(err.Error())
		return nil
	}
	switch domainEvent.EventType() {
	case event.TASK_RELEASED_EVENT:
		taskReleasedEvent := domainEvent.(*event.TaskReleased)
		var serials []int64
		for _, referenceResourceItem := range taskReleasedEvent.ReferenceResource.ReferenceResourceItems {
			serials = append(serials, referenceResourceItem.SerialNumber)
		}
		data, err := abilityServiceGateway.CommitQuestionQuotes(taskReleasedEvent.Sponsor.Uid, taskReleasedEvent.TaskId, serials)
		if err != nil {
			fmt.Println(err.Error())
			return nil
		}
		fmt.Println(data)
		break
	case event.TASK_CLOSED_EVENT:
		taskClosedEvent := domainEvent.(*event.TaskClosed)
		var referenceResourceIds []int64
		for _, referenceResourceItem := range taskClosedEvent.ReferenceResource.ReferenceResourceItems {
			referenceResourceIds = append(referenceResourceIds, referenceResourceItem.ReferenceResourceId)
		}
		data, err := abilityServiceGateway.CloseTaskCallback(taskClosedEvent.TaskId, referenceResourceIds)
		if err != nil {
			fmt.Println(err.Error())
			return nil
		}
		fmt.Println(data)
		break
	case event.TASK_ROBED_EVENT:
		taskRobedEvent := domainEvent.(*event.TaskRobed)
		var referenceResourceIds []int64
		for _, referenceResourceItem := range taskRobedEvent.ReferenceResource.ReferenceResourceItems {
			referenceResourceIds = append(referenceResourceIds, referenceResourceItem.ReferenceResourceId)
		}
		data, err := abilityServiceGateway.ReceiverTaskCallback(taskRobedEvent.RobInfo.Receiver.Uid, taskRobedEvent.TaskId, referenceResourceIds, taskRobedEvent.RobInfo.Receiver.Uid, taskRobedEvent.RobInfo.ReceiveTime)
		if err != nil {
			fmt.Println(err.Error())
			return nil
		}
		fmt.Println(data)
		break
	case event.TASK_BIDDED_EVENT:
		taskBiddedEvent := domainEvent.(*event.TaskBidded)
		var referenceResourceIds []int64
		for _, referenceResourceItem := range taskBiddedEvent.ReferenceResource.ReferenceResourceItems {
			referenceResourceIds = append(referenceResourceIds, referenceResourceItem.ReferenceResourceId)
		}
		data, err := abilityServiceGateway.ReceiverTaskCallback(taskBiddedEvent.BidInfo.SuccessfulBidder.Uid, taskBiddedEvent.TaskId, referenceResourceIds, taskBiddedEvent.Operator.Uid, taskBiddedEvent.BidInfo.WinBidTime)
		if err != nil {
			fmt.Println(err.Error())
			return nil
		}
		fmt.Println(data)
		break
	case event.TASK_ACCEPTANCED_EVENT:
		taskAcceptancedEvent := domainEvent.(*event.TaskAcceptanced)
		if taskAcceptancedEvent.ReferenceResource != nil {
			var solveUid int64
			var scoreSolve float64
			for _, referenceResourceItem := range taskAcceptancedEvent.ReferenceResource.ReferenceResourceItems {
				var partners []map[string]interface{}
				for _, solveScore := range referenceResourceItem.SolveScore {
					if solveScore.IsReceiver {
						solveUid = solveScore.Contributor.Uid
						scoreSolve = solveScore.Score
					} else {
						partners = append(partners, map[string]interface{}{
							"uid":           solveScore.Contributor.Uid,
							"scoreSolution": solveScore.Score,
						})
					}
				}
				data, err := abilityServiceGateway.CommitQuestionSolution(referenceResourceItem.ReferenceResourceId, taskAcceptancedEvent.Sponsor.Uid, solveUid, taskAcceptancedEvent.SolveReport, scoreSolve, taskAcceptancedEvent.SolvePictureUrls, partners)
				if err != nil {
					fmt.Println(err.Error())
					return nil
				}
				fmt.Println(data)
			}
		}
		break
	}
	return nil
}

func (subscriber *AbilityServiceSubscriber) SubscribedToEventTypes() []string {
	return [] string{
		event.TASK_RELEASED_EVENT,
		event.TASK_ACCEPTANCED_EVENT,
		event.TASK_CLOSED_EVENT,
		event.TASK_ROBED_EVENT,
		event.TASK_BIDDED_EVENT,
	}
}