notification_handle_subscriber.go 4.0 KB
package subscriber

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/application/factory"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain/event"
	"time"
)

type NotificationHandleSubscriber struct {
	TransactionContext *pgTransaction.TransactionContext
}

func (subscriber *NotificationHandleSubscriber) HandleEvent(domainEvent coreDomain.DomainEvent) error {
	var notificationRepository domain.NotificationRepository
	if value, err := factory.CreateNotificationRepository(map[string]interface{}{
		"transactionContext": subscriber.TransactionContext,
	}); err != nil {
		return err
	} else {
		notificationRepository = value
	}
	var sentNotificationRepository domain.SentNotificationRepository
	if value, err := factory.CreateSentNotificationRepository(map[string]interface{}{
		"transactionContext": subscriber.TransactionContext,
	}); err != nil {
		return err
	} else {
		sentNotificationRepository = value
	}
	switch domainEvent.EventType() {
	case event.TASK_ROBED_EVENT:
		taskRobedEvent := domainEvent.(*event.TaskRobed)
		notification := &domain.Notification{
			NotificationType:     domain.NOTIFICATION_TYPE_INTERACTION,
			NotificationTitle:    fmt.Sprintf("%s领取了任务", taskRobedEvent.RobInfo.Receiver.EmployeeName),
			NotificationContent:  taskRobedEvent.TaskName,
			NotificationTime:     time.Now(),
			ExternalResourceType: domain.EXTERNAL_RESOURCE_TYPE_TASK,
			ExternalResource:     taskRobedEvent.TaskId,
		}
		if notification, err := notificationRepository.Save(notification); err != nil {
			return err
		} else {
			sentNotification := &domain.SentNotification{
				Notification: notification,
				Receiver:     taskRobedEvent.Sponsor,
				IsRead:       false,
				ReadTime:     time.Time{},
			}
			if _, err := sentNotificationRepository.Save(sentNotification); err != nil {
				return err
			}
		}
		break
	case event.TASK_CONFIRMED_EVENT:
		taskConfirmedEvent := domainEvent.(*event.TaskConfirmed)
		notification := &domain.Notification{
			NotificationType:     domain.NOTIFICATION_TYPE_INTERACTION,
			NotificationTitle:    "你已成功领取了任务",
			NotificationContent:  taskConfirmedEvent.TaskName,
			NotificationTime:     time.Now(),
			ExternalResourceType: domain.EXTERNAL_RESOURCE_TYPE_TASK,
			ExternalResource:     taskConfirmedEvent.TaskId,
		}
		if notification, err := notificationRepository.Save(notification); err != nil {
			return err
		} else {
			sentNotification := &domain.SentNotification{
				Notification: notification,
				Receiver:     taskConfirmedEvent.RobInfo.Receiver,
				IsRead:       false,
				ReadTime:     time.Time{},
			}
			if _, err := sentNotificationRepository.Save(sentNotification); err != nil {
				return err
			}
		}
		break
	case event.TASK_REJECTED_EVENT:
		taskRejectedEvent := domainEvent.(*event.TaskRejected)
		notification := &domain.Notification{
			NotificationType:     domain.NOTIFICATION_TYPE_INTERACTION,
			NotificationTitle:    "您领取的任务被驳回了,查看理由",
			NotificationContent:  taskRejectedEvent.TaskName,
			NotificationTime:     time.Now(),
			ExternalResourceType: domain.EXTERNAL_RESOURCE_TYPE_REJECT_TASK_RECORD,
			ExternalResource:     taskRejectedEvent.RejectTaskRecord.RejectTaskRecordId,
		}
		if notification, err := notificationRepository.Save(notification); err != nil {
			return err
		} else {
			sentNotification := &domain.SentNotification{
				Notification: notification,
				Receiver:     taskRejectedEvent.BeRejectedPerson,
				IsRead:       false,
				ReadTime:     time.Time{},
			}
			if _, err := sentNotificationRepository.Save(sentNotification); err != nil {
				return err
			}
		}
		break
	}
	return nil
}

func (subscriber *NotificationHandleSubscriber) SubscribedToEventTypes() []string {
	return [] string{
		event.TASK_ROBED_EVENT,
		event.TASK_CONFIRMED_EVENT,
		event.TASK_REJECTED_EVENT,
	}
}