column_setting.go
13.4 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/columnSetting/command"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/columnSetting/query"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/constant"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"strconv"
"time"
)
// 栏目设置
type ColumnSettingService struct {
}
/**
* @Author SteveChan
* @Description //新增用户时,初始化栏目设置
* @Date 23:13 2021/1/26
* @Param
* @return
**/
func (columnSettingService *ColumnSettingService) CreateColumnSetting(createColumnSettingCommand *command.CreateColumnSettingCommand) (interface{}, error) {
if err := createColumnSettingCommand.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 errStart := transactionContext.StartTransaction(); errStart != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, errStart.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
newColumnSetting := &domain.ColumnSetting{
Description: createColumnSettingCommand.Description,
UserName: createColumnSettingCommand.UserName,
CompanyId: createColumnSettingCommand.CompanyId,
Uid: createColumnSettingCommand.Uid,
Key: constant.ORDER_BASE, // 订单栏目模块名称
Value: domain.DefaultColumns, // 栏目值数组,创建用户时使用默认栏目设置
InvalidValue: domain.DefaultInvalidColumns, // 初始化无效栏目数组
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
var columnSettingRepository domain.ColumnSettingRepository
if value, errFac := factory.CreateColumnSettingRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); errFac != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errFac.Error())
} else {
columnSettingRepository = value
}
if columnSettingSaved, errSaved := columnSettingRepository.Save(newColumnSetting); errSaved != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errSaved.Error())
} else {
if errCommit := transactionContext.CommitTransaction(); errCommit != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, errCommit.Error())
}
return columnSettingSaved, nil
}
}
/**
* @Author SteveChan
* @Description // 返回栏目设置
* @Date 23:14 2021/1/26
* @Param
* @return
**/
func (columnSettingService *ColumnSettingService) GetColumnSetting(getColumnSettingQuery *query.GetColumnSettingQuery) (interface{}, error) {
if err := getColumnSettingQuery.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 columnSettingRepository domain.ColumnSettingRepository
if value, errFact := factory.CreateColumnSettingRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); errFact != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errFact.Error())
} else {
columnSettingRepository = value
}
columnSettingFound, err := columnSettingRepository.FindOne(map[string]interface{}{"uid": getColumnSettingQuery.Uid, "companyId": getColumnSettingQuery.CompanyId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if columnSettingFound == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(getColumnSettingQuery.Uid, 10)))
} else {
if errCommit := transactionContext.CommitTransaction(); errCommit != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, errCommit.Error())
}
// 数据mapper
orderBaseColumns := map[string]interface{}{
"orderBaseColumns": map[string]interface{}{
"selected": columnSettingFound.Value,
"unselected": columnSettingFound.InvalidValue,
},
}
return orderBaseColumns, nil
}
}
// 返回栏目设置增删改查列表
func (columnSettingService *ColumnSettingService) ListColumnSetting(listColumnSettingQuery *query.ListColumnSettingQuery) (interface{}, error) {
if err := listColumnSettingQuery.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 columnSettingRepository domain.ColumnSettingRepository
if value, err := factory.CreateColumnSettingRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
columnSettingRepository = value
}
if count, columnSettings, err := columnSettingRepository.Find(domain.ColumnSettingFindQuery{
Offset: listColumnSettingQuery.Offset,
Limit: listColumnSettingQuery.Limit,
}); 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 map[string]interface{}{
"count": count,
"column_settings": columnSettings,
}, nil
}
}
/**
* @Author SteveChan
* @Description // 删除用户时移除栏目设置
* @Date 23:14 2021/1/26
* @Param
* @return
**/
func (columnSettingService *ColumnSettingService) RemoveColumnSetting(removeColumnSettingCommand *command.RemoveColumnSettingCommand) (interface{}, error) {
if err := removeColumnSettingCommand.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 errSt := transactionContext.StartTransaction(); errSt != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, errSt.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
var columnSettingRepository domain.ColumnSettingRepository
if value, errFac := factory.CreateColumnSettingRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); errFac != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errFac.Error())
} else {
columnSettingRepository = value
}
columnSettingFound, err := columnSettingRepository.FindOne(map[string]interface{}{"uid": removeColumnSettingCommand.Uid, "companyId": removeColumnSettingCommand.CompanyId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if columnSettingFound == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(removeColumnSettingCommand.Uid, 10)))
}
if columnSettingRemoved, _, errRm := columnSettingRepository.Remove(columnSettingFound, nil); errRm != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errRm.Error())
} else {
if errTr := transactionContext.CommitTransaction(); errTr != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, errRm.Error())
}
// 数据mapper
orderBaseColumns := map[string]interface{}{
"orderBaseColumns": map[string]interface{}{
"selected": columnSettingRemoved.Value,
"unselected": columnSettingRemoved.InvalidValue,
},
}
return orderBaseColumns, nil
}
}
/**
* @Author SteveChan
* @Description // 重置栏目设置
* @Date 23:15 2021/1/26
* @Param
* @return
**/
func (columnSettingService *ColumnSettingService) ResetColumn(resetColumnCommand *command.ResetColumnCommand) (interface{}, error) {
if err := resetColumnCommand.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 errStart := transactionContext.StartTransaction(); errStart != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, errStart.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
var columnSettingRepository domain.ColumnSettingRepository
if value, errFact := factory.CreateColumnSettingRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); errFact != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errFact.Error())
} else {
columnSettingRepository = value
}
columnSettingFound, err := columnSettingRepository.FindOne(map[string]interface{}{"uid": resetColumnCommand.Uid, "companyId": resetColumnCommand.CompanyId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if columnSettingFound == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(resetColumnCommand.Uid, 10)))
}
if errUpdated := columnSettingFound.Update(tool_funs.SimpleStructToMap(resetColumnCommand)); errUpdated != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, errUpdated.Error())
}
if columnSettingSaved, errSaved := columnSettingRepository.Save(columnSettingFound); errSaved != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errSaved.Error())
} else {
if errCommit := transactionContext.CommitTransaction(); errCommit != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, errCommit.Error())
}
// 数据mapper
orderBaseColumns := map[string]interface{}{
"orderBaseColumns": map[string]interface{}{
"selected": columnSettingSaved.Value,
"unselected": columnSettingSaved.InvalidValue,
},
}
return orderBaseColumns, nil
}
}
/**
* @Author SteveChan
* @Description // 更新栏目设置
* @Date 23:15 2021/1/26
* @Param
* @return
**/
func (columnSettingService *ColumnSettingService) UpdateColumnSetting(updateColumnSettingCommand *command.UpdateColumnSettingCommand) (interface{}, error) {
if err := updateColumnSettingCommand.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 errStart := transactionContext.StartTransaction(); errStart != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, errStart.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
var columnSettingRepository domain.ColumnSettingRepository
if value, errFact := factory.CreateColumnSettingRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); errFact != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errFact.Error())
} else {
columnSettingRepository = value
}
updateColumnSettingCommand.Value = updateColumnSettingCommand.Selected
updateColumnSettingCommand.InvalidValue = updateColumnSettingCommand.UnSelected
columnSetting, err := columnSettingRepository.FindOne(map[string]interface{}{"uid": updateColumnSettingCommand.Uid, "companyId": updateColumnSettingCommand.CompanyId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if columnSetting == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(updateColumnSettingCommand.Uid, 10)))
}
if errUpdated := columnSetting.Update(tool_funs.SimpleStructToMap(updateColumnSettingCommand)); errUpdated != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, errUpdated.Error())
}
if columnSettingSaved, errSaved := columnSettingRepository.Save(columnSetting); errSaved != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errSaved.Error())
} else {
if errCommit := transactionContext.CommitTransaction(); errCommit != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, errCommit.Error())
}
// 数据mapper
orderBaseColumns := map[string]interface{}{
"orderBaseColumns": map[string]interface{}{
"selected": columnSettingSaved.Value,
"unselected": columnSettingSaved.InvalidValue,
},
}
return orderBaseColumns, nil
}
}
func NewColumnSettingService(options map[string]interface{}) *ColumnSettingService {
newColumnSettingService := &ColumnSettingService{}
return newColumnSettingService
}