notification_handle_subscriber.go
4.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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,
}
}