system_setting.go
7.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
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
package service
import (
"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/systemSetting/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/systemSetting/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/domain"
)
// 用户的系统设置
type SystemSettingService struct {
}
// 返回用户的系统设置
func (systemSettingService *SystemSettingService) GetSystemSetting(getSystemSettingQuery *query.GetSystemSettingQuery) (interface{}, error) {
if err := getSystemSettingQuery.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 systemSettingRepository domain.SystemSettingRepository
if value, err := factory.CreateSystemSettingRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
systemSettingRepository = value
}
systemSetting, err := systemSettingRepository.FindOne(map[string]interface{}{
"companyId": getSystemSettingQuery.CompanyId,
"settingCode": getSystemSettingQuery.SettingCode,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if systemSetting == nil {
//获取默认设置项
setting := domain.SystemSetting{}
defaultSet := setting.GetDefaultSetting(getSystemSettingQuery.SettingCode)
if len(defaultSet) == 0 {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "获取设置项失败,settingCode="+getSystemSettingQuery.SettingCode)
}
defaultSet[0].CompanyId = getSystemSettingQuery.CompanyId
systemSetting = &defaultSet[0]
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return systemSetting, nil
}
// 返回用户的系统设置列表
func (systemSettingService *SystemSettingService) ListSystemSetting(listSystemSettingQuery *query.ListSystemSettingQuery) (interface{}, error) {
if err := listSystemSettingQuery.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 systemSettingRepository domain.SystemSettingRepository
if value, err := factory.CreateSystemSettingRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
systemSettingRepository = value
}
queryOption := map[string]interface{}{
"companyId": listSystemSettingQuery.CompanyId,
}
if len(listSystemSettingQuery.SettingCode) > 0 {
queryOption["settingCode"] = listSystemSettingQuery.SettingCode
}
_, systemSettings, err := systemSettingRepository.Find(queryOption)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
settingMap := map[string]*domain.SystemSetting{}
for i := range systemSettings {
code := systemSettings[i].SettingCode
settingMap[code] = systemSettings[i]
}
defaultSettings := new(domain.SystemSetting).GetDefaultSetting("")
for i := range defaultSettings {
if v, ok := settingMap[defaultSettings[i].SettingCode]; ok {
defaultSettings[i].Value = v.Value
}
defaultSettings[i].CompanyId = listSystemSettingQuery.CompanyId
}
return map[string]interface{}{
"systemSettings": defaultSettings,
}, nil
}
// 更新用户的系统设置
func (systemSettingService *SystemSettingService) UpdateSystemSetting(updateSystemSettingCommand *command.UpdateSystemSettingCommand) (interface{}, error) {
if err := updateSystemSettingCommand.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 systemSettingRepository domain.SystemSettingRepository
if value, err := factory.CreateSystemSettingRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
systemSettingRepository = value
}
_, systemSettings, err := systemSettingRepository.Find(map[string]interface{}{
"settingCode": updateSystemSettingCommand.SettingCode,
"companyId": updateSystemSettingCommand.CompanyId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var systemSetting *domain.SystemSetting
if len(systemSettings) == 0 {
defaultSet := new(domain.SystemSetting).GetDefaultSetting(updateSystemSettingCommand.SettingCode)
if len(defaultSet) > 0 {
systemSetting = &defaultSet[0]
systemSetting.CompanyId = updateSystemSettingCommand.CompanyId
}
} else {
systemSetting = systemSettings[0]
if err := systemSetting.Update(map[string]interface{}{
"value": updateSystemSettingCommand.Value,
}); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
}
if systemSetting == nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, "settingCode错误,settingCode="+updateSystemSettingCommand.SettingCode)
}
if systemSetting, err := systemSettingRepository.Save(systemSetting); 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 systemSetting, nil
}
}
func NewSystemSettingService(options map[string]interface{}) *SystemSettingService {
newSystemSettingService := &SystemSettingService{}
return newSystemSettingService
}