-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
336 lines (321 loc) · 9.76 KB
/
log.go
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
package BaseApprover
import (
"errors"
"fmt"
CoreFilter "github.com/fotomxq/weeekj_core/v5/core/filter"
CoreSQL2 "github.com/fotomxq/weeekj_core/v5/core/sql2"
Router2SystemConfig "github.com/fotomxq/weeekj_core/v5/router2/system_config"
)
// ArgsGetLogList 获取日志列表参数
type ArgsGetLogList struct {
//分页参数
Pages CoreSQL2.ArgsPages `json:"pages"`
//组织ID
OrgID int64 `db:"org_id" json:"orgID" check:"id" empty:"true"`
//提交组织成员ID
OrgBindID int64 `db:"org_bind_id" json:"orgBindID" check:"id" empty:"true"`
//用户ID
UserID int64 `db:"user_id" json:"userID" check:"id" empty:"true"`
//审批状态
// 0: 未审批; 1: 审批中; 2: 审批通过; 3: 审批拒绝
Status int `db:"status" json:"status" check:"intThan0" empty:"true"`
//关联的模块标识码
// erp_project
ModuleCode string `db:"module_code" json:"moduleCode" check:"des" min:"1" max:"50" empty:"true"`
//审批配置ID
ConfigID int64 `db:"config_id" json:"configID" check:"id" empty:"true"`
//搜索
Search string `json:"search" check:"search" empty:"true"`
}
// GetLogList 获取日志列表
func GetLogList(args *ArgsGetLogList) (dataList []FieldsLog, dataCount int64, err error) {
dataCount, err = configItemDB.Select().SetFieldsList([]string{"id"}).SetFieldsSort([]string{"id", "flow_order"}).SetPages(args.Pages).SetDeleteQuery("delete_at", false).SetIDQuery("org_id", args.OrgID).SetIDQuery("org_bind_id", args.OrgBindID).SetIDQuery("user_id", args.UserID).SetIntQuery("status", args.Status).SetStringQuery("module_code", args.ModuleCode).SetIDQuery("config_id", args.ConfigID).SetSearchQuery([]string{"submitter_name", "approver_remark"}, args.Search).SelectList("").ResultAndCount(&dataList)
if err != nil || len(dataList) < 1 {
return
}
for k, v := range dataList {
vData := getLogByID(v.ID)
if vData.ID < 1 {
continue
}
dataList[k] = vData
}
return
}
// ArgsGetLogByID 获取日志数据包参数
type ArgsGetLogByID struct {
//ID
ID int64 `db:"id" json:"id" check:"id"`
//组织ID
OrgID int64 `db:"org_id" json:"orgID" check:"id" empty:"true"`
//提交组织成员ID
OrgBindID int64 `db:"org_bind_id" json:"orgBindID" check:"id" empty:"true"`
//用户ID
UserID int64 `db:"user_id" json:"userID" check:"id" empty:"true"`
}
// GetLogByID 获取日志数据包
func GetLogByID(args *ArgsGetLogByID) (data DataLog, err error) {
rawData := getLogByID(args.ID)
if rawData.ID < 1 {
err = errors.New("no data")
return
}
if !CoreFilter.EqID2(args.OrgID, rawData.OrgID) {
err = errors.New("no data")
return
}
rawFlows, _, _ := GetLogFlows(&ArgsGetLogFlows{
Pages: CoreSQL2.ArgsPages{
Page: 1,
Max: 999,
Sort: "flow_order",
Desc: false,
},
LogID: rawData.ID,
Status: -1,
OrgBindID: -1,
UserID: -1,
Search: "",
})
var flows DataLogFlows
if len(rawFlows) > 0 {
for _, v := range rawFlows {
flows = append(flows, DataLogFlow{
FlowOrder: v.FlowOrder,
Status: v.Status,
CreateAt: v.CreateAt,
ApproveAt: v.ApproveAt,
OrgBindID: v.OrgBindID,
UserID: v.UserID,
ApproverName: v.ApproverName,
ApproverRemark: v.ApproverRemark,
RejectRemark: v.RejectRemark,
})
}
}
data = DataLog{
ID: rawData.ID,
CreateAt: rawData.CreateAt,
UpdateAt: rawData.UpdateAt,
DeleteAt: rawData.DeleteAt,
OrgID: rawData.OrgID,
OrgBindID: rawData.OrgBindID,
UserID: rawData.UserID,
SubmitterName: rawData.SubmitterName,
Status: rawData.Status,
ModuleCode: rawData.ModuleCode,
ApproverID: rawData.ApproverID,
ConfigID: rawData.ConfigID,
Flows: flows,
}
//反馈
return
}
// ArgsGetLogByModuleAndID 通过模块和ID获取审批参数
type ArgsGetLogByModuleAndID struct {
//组织ID
OrgID int64 `db:"org_id" json:"orgID" check:"id" empty:"true"`
//提交组织成员ID
OrgBindID int64 `db:"org_bind_id" json:"orgBindID" check:"id" empty:"true"`
//用户ID
UserID int64 `db:"user_id" json:"userID" check:"id" empty:"true"`
//关联的模块标识码
// erp_project
ModuleCode string `db:"module_code" json:"moduleCode" check:"des" min:"1" max:"50"`
//审批ID
ApproverID int64 `db:"approver_id" json:"approverID" check:"id"`
}
// GetLogByModuleAndID 通过模块和ID获取审批
func GetLogByModuleAndID(args *ArgsGetLogByModuleAndID) (data DataLog, err error) {
//获取日志数据
var rawLogData FieldsLog
err = logDB.Get().SetFieldsOne([]string{"id"}).SetDeleteQuery("delete_at", false).SetIDQuery("org_id", args.OrgID).SetIDQuery("org_bind_id", args.OrgBindID).SetIDQuery("user_id", args.UserID).SetStringQuery("module_code", args.ModuleCode).SetIDQuery("approver_id", args.ApproverID).Result(&rawLogData)
if err != nil {
return
}
if rawLogData.ID > 0 {
rawLogData = getLogByID(rawLogData.ID)
}
//通过ID获取数据包
data, err = GetLogByID(&ArgsGetLogByID{
ID: rawLogData.ID,
OrgID: -1,
OrgBindID: -1,
UserID: -1,
})
if err != nil {
rawLogData = FieldsLog{}
return
}
//反馈
return
}
// ArgsCreateLog 发起新的审批参数
type ArgsCreateLog struct {
//组织ID
OrgID int64 `db:"org_id" json:"orgID" check:"id" empty:"true"`
//提交组织成员ID
OrgBindID int64 `db:"org_bind_id" json:"orgBindID" check:"id" empty:"true"`
//用户ID
UserID int64 `db:"user_id" json:"userID" check:"id" empty:"true"`
//关联的模块标识码
// erp_project
ModuleCode string `db:"module_code" json:"moduleCode" check:"des" min:"1" max:"50"`
//审批分叉标识码
// 用于识别模块内,不同的审批流程
ForkCode string `db:"fork_code" json:"forkCode" check:"des" min:"1" max:"50"`
//审批ID
ApproverID int64 `db:"approver_id" json:"approverID" check:"id"`
//审批备注
ApproverRemark string `db:"approver_remark" json:"approverRemark" check:"des" min:"1" max:"300"`
}
// CreateLog 发起新的审批
func CreateLog(args *ArgsCreateLog) (errCode string, err error) {
//检查是否存在审批的数据
rawData, _ := GetLogByModuleAndID(&ArgsGetLogByModuleAndID{
OrgID: args.OrgID,
OrgBindID: -1,
UserID: -1,
ModuleCode: args.ModuleCode,
ApproverID: args.ApproverID,
})
if rawData.ID > 0 {
errCode = "err_approver_log_repeat"
err = errors.New("data exists")
return
}
//获取配置数据包
configData := getConfigByModule(args.OrgID, args.ModuleCode, args.ForkCode)
if configData.ID < 1 {
errCode = "err_config"
err = errors.New("no data")
return
}
//构建数据
var newLogID int64
newLogID, err = logDB.Insert().SetFields([]string{"org_id", "org_bind_id", "user_id", "submitter_name", "approver_remark", "status", "module_code", "approver_id", "config_id"}).Add(map[string]any{
"org_id": args.OrgID,
"org_bind_id": args.OrgBindID,
"user_id": args.UserID,
"submitter_name": getApproverName(args.OrgBindID, args.UserID),
"approver_remark": args.ApproverRemark,
"status": 0,
"module_code": configData.ModuleCode,
"approver_id": args.ApproverID,
"config_id": configData.ID,
}).ExecAndResultID()
if err != nil {
errCode = "err_insert"
return
}
//记录最开始的审批流
startFlow := -1
var flowID int64 = -1
//遍历配置行数据
for _, v := range configData.Items {
var vFlowID int64
vFlowID, err = createLogFlow(&argsCreateLogFlow{
LogID: newLogID,
FlowOrder: v.FlowOrder,
OrgBindID: args.OrgBindID,
UserID: args.UserID,
})
if err != nil {
errCode = "err_insert"
return
}
if startFlow < 0 {
startFlow = v.FlowOrder
flowID = vFlowID
} else {
if startFlow > v.FlowOrder {
startFlow = v.FlowOrder
flowID = vFlowID
}
}
}
//修改flowID状态为1
if flowID > 0 {
err = logFlowDB.Update().SetFields([]string{"status"}).NeedUpdateTime().AddWhereID(flowID).NamedExec(map[string]any{
"status": 1,
})
if err != nil {
errCode = "err_update"
return
}
deleteLogFlowCache(flowID)
}
//反馈
return
}
// ArgsDeleteLog 删除审批流参数
type ArgsDeleteLog struct {
//ID
ID int64 `db:"id" json:"id" check:"id"`
//组织ID
OrgID int64 `db:"org_id" json:"orgID" check:"id" empty:"true"`
}
// DeleteLog 删除审批流
func DeleteLog(args *ArgsDeleteLog) (err error) {
//获取日志行数据
flows, _, _ := GetLogFlows(&ArgsGetLogFlows{
Pages: CoreSQL2.ArgsPages{
Page: 1,
Max: 999,
Sort: "flow_order",
Desc: false,
},
LogID: args.ID,
Status: -1,
OrgBindID: -1,
UserID: -1,
Search: "",
})
//删除数据
err = logDB.Delete().NeedSoft(true).AddWhereID(args.ID).AddWhereOrgID(args.OrgID).ExecNamed(nil)
if err != nil {
return
}
//删除缓冲
deleteLogCache(args.ID)
//删除行数据
if len(flows) > 0 {
for _, v := range flows {
deleteLogFlowCache(v.ID)
}
}
_ = clearLogFlow(args.ID)
//反馈
return
}
// 更新审批日志状态
func updateLogStatus(id int64, status int) (err error) {
err = logDB.Update().SetFields([]string{"status"}).NeedUpdateTime().AddWhereID(id).NamedExec(map[string]any{
"status": status,
})
if err != nil {
return
}
deleteLogCache(id)
return
}
// getLogByID 获取审批流程信息
func getLogByID(id int64) (data FieldsLog) {
cacheMark := getLogCacheMark(id)
if err := Router2SystemConfig.MainCache.GetStruct(cacheMark, &data); err == nil && data.ID > 0 {
return
}
err := logDB.Get().SetFieldsOne([]string{"id", "create_at", "update_at", "delete_at", "org_id", "org_bind_id", "user_id", "submitter_name", "approver_remark", "status", "module_code", "approver_id", "config_id"}).GetByID(id).NeedLimit().Result(&data)
if err != nil {
return
}
Router2SystemConfig.MainCache.SetStruct(cacheMark, data, cacheLogTime)
return
}
// 缓冲
func getLogCacheMark(id int64) string {
return fmt.Sprint("base:approver:log:id.", id)
}
func deleteLogCache(id int64) {
Router2SystemConfig.MainCache.DeleteMark(getLogCacheMark(id))
}