log_opt.go
8.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
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
package log_opt
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
"strconv"
"time"
)
func CreateTask(u *domain.UserAuth, task domain.Task) error {
transactionContext, err := factory.StartTransaction()
if err != nil {
return err
}
defer func() {
_ = transactionContext.RollbackTransaction()
if err := recover(); err != nil {
log.Logger.Error(application.ThrowError(application.BUSINESS_ERROR, fmt.Sprintf("创建任务日志异常:%s", err)).Error())
}
}()
logOptRepo := factory.CreateLogOptRepository(map[string]interface{}{"transactionContext": transactionContext})
logOpt := createdLogOpt(u, strconv.Itoa(task.Id))
logOpt.OptMethod = domain.CREATE
logOpt.OptField = "创建了任务"
logOpt.OptValue = task.Alias
_, err = logOptRepo.Insert(logOpt)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil
}
func UpdateTask(u *domain.UserAuth,
srcTask *domain.Task,
newTask *domain.Task,
srcStage []*domain.TaskStage,
newStage []*domain.TaskStage,
) error {
transactionContext, err := factory.StartTransaction()
if err != nil {
return err
}
defer func() {
_ = transactionContext.RollbackTransaction()
if err := recover(); err != nil {
log.Logger.Error(application.ThrowError(application.BUSINESS_ERROR, fmt.Sprintf("更新任务日志异常:%s", err)).Error())
}
}()
logOptRepo := factory.CreateLogOptRepository(map[string]interface{}{"transactionContext": transactionContext})
var logArray = compareTask(u, srcTask, newTask)
var logArray2 = compareStage(u, newTask.Id, srcStage, newStage)
logArray = append(logArray, logArray2...)
if len(logArray) == 0 {
return nil
}
for _, v := range logArray {
_, err = logOptRepo.Insert(v)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil
}
func compareTask(u *domain.UserAuth, srcTask *domain.Task, newTask *domain.Task) []*domain.LogOpt {
var taskId = strconv.Itoa(newTask.Id)
logArray := make([]*domain.LogOpt, 0)
if srcTask.Alias != newTask.Alias {
logOpt := createdLogOpt(u, taskId)
logOpt.OptField = "修改了任务名称"
logOpt.OptValue = newTask.Alias
logArray = append(logArray, logOpt)
}
if srcTask.EndTime != newTask.EndTime {
logOpt := createdLogOpt(u, taskId)
logOpt.OptField = "修改了任务截止时间"
if newTask.EndTime == 0 {
logOpt.OptValue = "未设置时间"
} else {
logOpt.OptValue = time.Unix(newTask.EndTime, 0).Local().Format("2006-01-02")
//logOpt.OptValue = time.Unix(newTask.EndTime, 0).Local().Format("2006-01-02 15:04:05")
}
logArray = append(logArray, logOpt)
}
if srcTask.UseEndTime != newTask.UseEndTime {
logOpt := createdLogOpt(u, taskId)
logOpt.OptField = "修改了任务截止时间应用到日评中"
if newTask.UseEndTime == 0 {
logOpt.OptValue = "不应用"
} else {
logOpt.OptValue = "应用"
}
logArray = append(logArray, logOpt)
}
if srcTask.LevelName != newTask.LevelName {
logOpt := createdLogOpt(u, taskId)
logOpt.OptField = "修改了任务类型"
logOpt.OptValue = newTask.LevelName
logArray = append(logArray, logOpt)
}
if srcTask.SortBy != newTask.SortBy {
logOpt := createdLogOpt(u, taskId)
logOpt.OptField = "修改了任务优先级"
logOpt.OptValue = newTask.SortBy.Named()
logArray = append(logArray, logOpt)
}
if srcTask.AssistFlagMax != newTask.AssistFlagMax {
logOpt := createdLogOpt(u, taskId)
logOpt.OptField = "修改了上级辅导时间"
logOpt.OptValue = strconv.Itoa(newTask.AssistFlagMax) + "天"
logArray = append(logArray, logOpt)
}
// 新的任务相关方
uidNewMap := map[int]int{}
for _, id := range newTask.RelatedUser {
uidNewMap[id] = id
}
// 被移除的任务相关方
removedIds := make([]int, 0)
for _, id := range srcTask.RelatedUser {
if _, ok := uidNewMap[id]; ok {
delete(uidNewMap, id) // 删除后,剩余的都是新增的
} else {
removedIds = append(removedIds, id)
}
}
// 变动的任务相关方
changeIds := make([]int, 0)
for _, v := range removedIds {
changeIds = append(changeIds, v)
}
for _, v := range uidNewMap {
changeIds = append(changeIds, v)
}
userMap, _ := getUserMap(changeIds)
for _, v := range removedIds {
logOpt := createdLogOpt(u, taskId)
logOpt.OptMethod = domain.DELETE
logOpt.OptField = "移除了任务相关方"
if v, ok := userMap[v]; ok {
logOpt.OptValue = v.Name
}
logArray = append(logArray, logOpt)
}
for _, v := range uidNewMap {
logOpt := createdLogOpt(u, taskId)
logOpt.OptMethod = domain.CREATE
logOpt.OptField = "添加了任务相关方"
if v, ok := userMap[v]; ok {
logOpt.OptValue = v.Name
}
logArray = append(logArray, logOpt)
}
return logArray
}
func compareStage(u *domain.UserAuth, id int, srcStage []*domain.TaskStage, newStage []*domain.TaskStage) []*domain.LogOpt {
taskId := strconv.Itoa(id)
logArray := make([]*domain.LogOpt, 0)
srcMap := map[int]*domain.TaskStage{}
for i := range srcStage {
it := srcStage[i]
srcMap[it.Id] = it
}
for i := range newStage {
it := newStage[i]
if it.DeletedAt != nil {
logOpt := createdLogOpt(u, taskId)
logOpt.OptMethod = domain.DELETE
logOpt.OptField = "删除了里程碑"
logOpt.OptValue = it.Name
logArray = append(logArray, logOpt)
continue
}
// 新旧比对
if v, ok := srcMap[it.Id]; ok {
if it.Name != v.Name {
logOpt := createdLogOpt(u, taskId)
logOpt.OptField = "修改了里程碑名称"
logOpt.OptValue = it.Name
logArray = append(logArray, logOpt)
}
if it.PlanCompletedAt != v.PlanCompletedAt {
logOpt := createdLogOpt(u, taskId)
logOpt.OptField = "修改了里程碑" + it.Name + "的计划完成时间"
if it.PlanCompletedAt == 0 {
logOpt.OptValue = "未设置时间"
} else {
logOpt.OptValue = time.Unix(it.PlanCompletedAt, 0).Local().Format("2006-01-02")
//logOpt.OptValue = time.Unix(it.PlanCompletedAt, 0).Local().Format("2006-01-02 15:04:05")
}
logArray = append(logArray, logOpt)
}
} else {
logOpt := createdLogOpt(u, taskId)
logOpt.OptMethod = domain.CREATE
logOpt.OptField = "创建了里程碑"
logOpt.OptValue = it.Name
logArray = append(logArray, logOpt)
}
}
return logArray
}
func getUserMap(ids []int) (map[int]*domain.User, error) {
userMap := map[int]*domain.User{}
if len(ids) == 0 {
return userMap, nil
}
transactionContext, err := factory.StartTransaction()
if err != nil {
return userMap, err
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
userRepo := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
_, users, err := userRepo.Find(map[string]interface{}{"ids": ids, "limit": len(ids)})
if err != nil {
return userMap, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
for i := range users {
user := users[i]
userMap[int(user.Id)] = user
}
return userMap, nil
}
func createdLogOpt(u *domain.UserAuth, taskId string) *domain.LogOpt {
var createdAt = time.Now()
var updatedAt = createdAt
operator := domain.StaffDesc{
UserId: int(u.UserId),
CompanyName: u.CompanyName,
Account: u.Phone,
UserName: u.Name,
}
return &domain.LogOpt{
Id: 0,
CompanyId: int(u.CompanyId),
Operator: operator,
OptMethod: domain.UPDATE,
OptTargetId: taskId,
OptField: "",
OptValue: "",
CreatedAt: createdAt,
UpdatedAt: updatedAt,
}
}