notice_setting.go
11.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package service
import (
"fmt"
"log"
"time"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/noticeSetting/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/noticeSetting/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/domain"
)
// 编排消息通知内容
type NoticeSettingService struct {
}
// 返回编排消息通知内容
func (noticeSettingService *NoticeSettingService) GetNoticeSetting(getNoticeSettingQuery *query.GetNoticeSettingQuery) (interface{}, error) {
if err := getNoticeSettingQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var noticeSettingRepository domain.NoticeSettingRepository
if value, err := factory.CreateNoticeSettingRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
noticeSettingRepository = value
}
noticeSetting, err := noticeSettingRepository.FindOne(map[string]interface{}{
"noticeSettingId": getNoticeSettingQuery.NoticeSettingId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if noticeSetting == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%d", getNoticeSettingQuery.NoticeSettingId))
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return noticeSetting, nil
}
}
// 返回编排消息通知内容列表
func (noticeSettingService *NoticeSettingService) ListNoticeSetting(listNoticeSettingQuery *query.ListNoticeSettingQuery) (int64, interface{}, error) {
if err := listNoticeSettingQuery.ValidateQuery(); err != nil {
return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var noticeSettingRepository domain.NoticeSettingRepository
if value, err := factory.CreateNoticeSettingRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
noticeSettingRepository = value
}
queryOption := map[string]interface{}{
"pageIndex": listNoticeSettingQuery.PageIndex,
"pageSize": listNoticeSettingQuery.PageSize,
"companyId": listNoticeSettingQuery.CompanyId,
}
if listNoticeSettingQuery.OrgId > 0 {
queryOption["orgId"] = listNoticeSettingQuery.OrgId
}
if count, noticeSettings, err := noticeSettingRepository.Find(queryOption); err != nil {
return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return count,
noticeSettings,
nil
}
}
// 更新编排消息通知内容
func (noticeSettingService *NoticeSettingService) UpdateNoticeSetting(updateNoticeSettingCommand *command.UpdateNoticeSettingCommand) (interface{}, error) {
if err := updateNoticeSettingCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var noticeSettingRepository domain.NoticeSettingRepository
if value, err := factory.CreateNoticeSettingRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
noticeSettingRepository = value
}
var (
noticeSettings []*domain.NoticeSetting
noticeSetting *domain.NoticeSetting
)
_, noticeSettings, err = noticeSettingRepository.Find(map[string]interface{}{
"companyId": updateNoticeSettingCommand.CompanyId,
"orgId": updateNoticeSettingCommand.OrgId,
"moduleAction": updateNoticeSettingCommand.ModuleAction,
})
log.Println("+++++======>", updateNoticeSettingCommand)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if len(noticeSettings) > 1 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "配置信息重复")
}
if len(noticeSettings) > 0 {
//存在旧的数据
noticeSetting = noticeSettings[0]
if noticeSetting.NoticeSettingId != updateNoticeSettingCommand.NoticeSettingId {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "该环节配置已存在")
}
}
noticeSetting, err = noticeSettingRepository.FindOne(map[string]interface{}{
"noticeSettingId": updateNoticeSettingCommand.NoticeSettingId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
err = noticeSetting.Update(map[string]interface{}{
"content": updateNoticeSettingCommand.Content,
"isPush": updateNoticeSettingCommand.IsPush,
"updatedAt": time.Now(),
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if noticeSetting, err := noticeSettingRepository.Save(noticeSetting); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return noticeSetting, nil
}
}
// 更新编排消息通知内容
func (noticeSettingService *NoticeSettingService) AddNoticeSetting(addNoticeSettingCommand *command.AddNoticeSettingCommand) (interface{}, error) {
if err := addNoticeSettingCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var noticeSettingRepository domain.NoticeSettingRepository
if value, err := factory.CreateNoticeSettingRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
noticeSettingRepository = value
}
var (
noticeSettings []*domain.NoticeSetting
noticeSetting *domain.NoticeSetting
)
_, noticeSettings, err = noticeSettingRepository.Find(map[string]interface{}{
"companyId": addNoticeSettingCommand.CompanyId,
"orgId": addNoticeSettingCommand.OrgId,
"moduleAction": addNoticeSettingCommand.ModuleAction,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if len(noticeSettings) > 0 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "该环节配置已存在")
}
noticeSetting = &domain.NoticeSetting{
CompanyId: addNoticeSettingCommand.CompanyId,
Content: addNoticeSettingCommand.Content,
IsPush: addNoticeSettingCommand.IsPush,
Module: addNoticeSettingCommand.Module,
ModuleAction: addNoticeSettingCommand.ModuleAction,
OrgId: addNoticeSettingCommand.OrgId,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
if noticeSetting, err := noticeSettingRepository.Save(noticeSetting); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return noticeSetting, nil
}
}
// //InitNoticeSetting 为企业初始化消息列表,填充空白的消息模板
// func (noticeSettingService *NoticeSettingService) InitNoticeSetting(initCommand *command.InitNoticeSettingCommand) error {
// if err := initCommand.ValidateCommand(); err != nil {
// return application.ThrowError(application.ARG_ERROR, err.Error())
// }
// transactionContext, err := factory.CreateTransactionContext(nil)
// if err != nil {
// return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// if err := transactionContext.StartTransaction(); err != nil {
// return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// defer func() {
// transactionContext.RollbackTransaction()
// }()
// var noticeSettingRepository domain.NoticeSettingRepository
// if value, err := factory.CreateNoticeSettingRepository(map[string]interface{}{
// "transactionContext": transactionContext,
// }); err != nil {
// return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
// } else {
// noticeSettingRepository = value
// }
// _, noticeSets, err := noticeSettingRepository.Find(map[string]interface{}{
// "companyId": initCommand.CompanyId,
// "orgId": initCommand.OrgId,
// })
// if err != nil {
// return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
// }
// notticeExist := make(map[string]int)
// for i := range noticeSets {
// notticeExist[noticeSets[i].ModuleAction] = 1
// }
// defaultModuleAction := domain.GetNoticeModuleActionList()
// newEmptySetting := []domain.NoticeSetting{}
// for _, act := range defaultModuleAction {
// if _, ok := notticeExist[act.ActionCode]; !ok {
// newEmptySetting = append(newEmptySetting, domain.NoticeSetting{
// CompanyId: initCommand.CompanyId,
// OrgId: initCommand.OrgId,
// Module: act.ModuleCode,
// ModuleAction: act.ActionCode,
// CreatedAt: time.Now(),
// UpdatedAt: time.Now(),
// Content: "",
// IsPush: domain.NoticeSettingIsNotPush,
// })
// }
// }
// for i := range newEmptySetting {
// _, err = noticeSettingRepository.Save(&newEmptySetting[i])
// if err != nil {
// return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// }
// if err := transactionContext.CommitTransaction(); err != nil {
// return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// return nil
// }
func NewNoticeSettingService(options map[string]interface{}) *NoticeSettingService {
newNoticeSettingService := &NoticeSettingService{}
return newNoticeSettingService
}