order_log_subscriber.go
5.5 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package subscriber
import (
"fmt"
"time"
coreDomain "github.com/linmadan/egglib-go/core/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain/event"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/repository"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
//订单数据修改触发的订阅事件
type OrderLogSubscriber struct {
TransactionContext *transaction.TransactionContext
}
var _ coreDomain.DomainEventSubscriber = (*OrderLogSubscriber)(nil)
func (subscriber *OrderLogSubscriber) HandleEvent(domainEvent coreDomain.DomainEvent) error {
var (
orderLogRepository domain.OrderLogRepository
userRepository domain.UsersRepository
adminUser domain.Users
err error
)
if orderLogRepository, err = repository.NewOrderLogRepository(subscriber.TransactionContext); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if userRepository, err = repository.NewUsersRepository(subscriber.TransactionContext); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
switch domainEvent.EventType() {
//订单分红因为货品的数量变动而发送改变
case event.UPDATE_BONUS_BY_GOOD_NUMBER_EVENT:
currentEvent := domainEvent.(event.UpdateBonusByGoodNumber)
adminUser, err = userRepository.FindOne(domain.UsersFindOneQuery{Id: currentEvent.AdminId})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
orderLog := domain.OrderLog{
OperatorType: domain.ORDER_LOG_OPERATOR_ADMIN,
OperatorId: currentEvent.AdminId,
Operator: adminUser.Name,
AlterTime: time.Now(),
DataFrom: domain.ORDER_LOG_FROM,
LogAction: "修改",
OrderId: currentEvent.OrderId,
GoodId: currentEvent.GoodId,
Descript: []domain.OrderLogDescript{
domain.OrderLogDescript{
Title: "调整商品数量",
Item: currentEvent.GoodName,
Action: []string{
fmt.Sprintf("购买数量由%s调整为%s", currentEvent.FormerNumber, currentEvent.NewNumber),
fmt.Sprintf("商品总价由¥%s调整为¥%s", currentEvent.FormerAmount, currentEvent.NewAmount),
},
},
},
}
err = orderLogRepository.Add(&orderLog)
break
//订单分红因为合伙人分红比例变动而发送改变
case event.UPDATE_BONUS_BY_PARTENT_BONUS_PERCENT_EVENT:
currentEvent := domainEvent.(event.UpdateBounsByPartnerBonusPercent)
adminUser, err = userRepository.FindOne(domain.UsersFindOneQuery{Id: currentEvent.AdminId})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
orderLog := domain.OrderLog{
OperatorType: domain.ORDER_LOG_OPERATOR_ADMIN,
OperatorId: currentEvent.AdminId,
Operator: adminUser.Name,
AlterTime: time.Now(),
DataFrom: domain.ORDER_LOG_FROM,
LogAction: "修改",
OrderId: currentEvent.OrderId,
GoodId: currentEvent.GoodId,
Descript: []domain.OrderLogDescript{
domain.OrderLogDescript{
Title: "合伙人分红比例",
Item: currentEvent.GoodName,
Action: []string{
fmt.Sprintf("分红比例由%s调整为%s", currentEvent.FormerPartnerBonusPercent, currentEvent.NewPartnerBonusPercent),
fmt.Sprintf("应收分红由¥%s调整为¥%s", currentEvent.FormerPartnerBonus, currentEvent.NewPartnerBonus),
},
},
},
}
err = orderLogRepository.Add(&orderLog)
break
//更新订单的备注
case event.UPDATE_ORDER_REMARK:
currentEvent := domainEvent.(event.UpdateOrderRemark)
adminUser, err = userRepository.FindOne(domain.UsersFindOneQuery{Id: currentEvent.AdminId})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
orderLog := domain.OrderLog{
OperatorType: domain.ORDER_LOG_OPERATOR_ADMIN,
OperatorId: currentEvent.AdminId,
Operator: adminUser.Name,
AlterTime: time.Now(),
DataFrom: domain.ORDER_LOG_FROM,
LogAction: "修改",
OrderId: currentEvent.OrderId,
GoodId: 0,
Descript: []domain.OrderLogDescript{
domain.OrderLogDescript{
Title: "编辑备注",
Item: currentEvent.NewRemark,
Action: []string{},
},
},
}
err = orderLogRepository.Add(&orderLog)
break
// 支付订单中货品的分红
case event.PAY_ORDER_GOOD_BONUS_EVENT:
currentEvent := domainEvent.(event.PayOrderGoodBonus)
adminUser, err = userRepository.FindOne(domain.UsersFindOneQuery{Id: currentEvent.AdminId})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
orderLog := domain.OrderLog{
OperatorType: domain.ORDER_LOG_OPERATOR_ADMIN,
OperatorId: currentEvent.AdminId,
Operator: adminUser.Name,
AlterTime: time.Now(),
DataFrom: domain.ORDER_LOG_FROM,
LogAction: "支付",
OrderId: currentEvent.OrderId,
GoodId: currentEvent.GoodId,
Descript: []domain.OrderLogDescript{
domain.OrderLogDescript{
Title: "支付分红",
Item: currentEvent.GoodName,
Action: []string{
fmt.Sprintf("支付分红¥%.2f", currentEvent.PartnerBonus),
},
},
},
}
err = orderLogRepository.Add(&orderLog)
break
}
return err
}
func (subscriber *OrderLogSubscriber) SubscribedToEventTypes() []string {
return []string{
event.UPDATE_BONUS_BY_GOOD_NUMBER_EVENT,
event.UPDATE_BONUS_BY_PARTENT_BONUS_PERCENT_EVENT,
event.UPDATE_ORDER_REMARK,
event.PAY_ORDER_GOOD_BONUS_EVENT,
}
}