-
Notifications
You must be signed in to change notification settings - Fork 0
/
warning.go
337 lines (330 loc) · 10 KB
/
warning.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
337
package OrgCert
import (
"errors"
"fmt"
BaseSMS "github.com/fotomxq/weeekj_core/v5/base/sms"
CoreFilter "github.com/fotomxq/weeekj_core/v5/core/filter"
CoreSQL "github.com/fotomxq/weeekj_core/v5/core/sql"
CoreSQLConfig "github.com/fotomxq/weeekj_core/v5/core/sql/config"
CoreSQLFrom "github.com/fotomxq/weeekj_core/v5/core/sql/from"
CoreSQLPages "github.com/fotomxq/weeekj_core/v5/core/sql/pages"
CoreSQLTime "github.com/fotomxq/weeekj_core/v5/core/sql/time"
OrgCoreCore "github.com/fotomxq/weeekj_core/v5/org/core"
Router2SystemConfig "github.com/fotomxq/weeekj_core/v5/router2/system_config"
UserCore "github.com/fotomxq/weeekj_core/v5/user/core"
UserMessage "github.com/fotomxq/weeekj_core/v5/user/message"
"github.com/lib/pq"
"time"
)
// ArgsGetWarningList 获取异常列表参数
type ArgsGetWarningList struct {
//分页
Pages CoreSQLPages.ArgsDataList `json:"pages"`
//组织ID
OrgID int64 `db:"org_id" json:"orgID" check:"id" empty:"true"`
//子组织ID
ChildOrgID int64 `db:"child_org_id" json:"childOrgID" check:"id" empty:"true"`
//是否反馈
NeedIsFinish bool `db:"need_is_finish" json:"needIsFinish" check:"bool"`
IsFinish bool `db:"is_finish" json:"isFinish" check:"bool"`
//证件标识码
ConfigMarks pq.StringArray `db:"config_marks" json:"configMarks" check:"marks" empty:"true"`
//证件配置ID
ConfigID int64 `db:"config_id" json:"configID" check:"id" empty:"true"`
//时间段
TimeBetween CoreSQLTime.DataCoreTime `json:"timeBetween"`
//搜索
Search string `json:"search" check:"search" empty:"true"`
}
// GetWarningList 获取异常列表参数
func GetWarningList(args *ArgsGetWarningList) (dataList []FieldsWarning, dataCount int64, err error) {
maps := map[string]interface{}{}
where := ""
if args.OrgID > 0 {
where = where + "org_id = :org_id"
maps["org_id"] = args.OrgID
}
if args.ChildOrgID > 0 {
if where != "" {
where = where + " AND "
}
where = where + "child_org_id = :child_org_id"
maps["child_org_id"] = args.ChildOrgID
}
where = CoreSQL.GetNeedChange(where, "finish_at", args.NeedIsFinish, args.IsFinish)
if len(args.ConfigMarks) > 0 {
if where != "" {
where = where + " AND "
}
where = where + "config_mark = ANY(:config_marks)"
maps["config_marks"] = args.ConfigMarks
}
if args.ConfigID > 0 {
if where != "" {
where = where + " AND "
}
where = where + "config_id = :config_id"
maps["config_id"] = args.ConfigID
}
if args.TimeBetween.MinTime != "" && args.TimeBetween.MaxTime != "" {
var timeBetween CoreSQLTime.FieldsCoreTime
timeBetween, err = CoreSQLTime.GetBetweenByISO(args.TimeBetween)
if err != nil {
return
}
var newWhere string
newWhere, maps = CoreSQLTime.GetBetweenByTime("create_at", timeBetween, maps)
if newWhere != "" {
if where != "" {
where = where + " AND "
}
where = where + newWhere
}
}
if args.Search != "" {
if where != "" {
where = where + " AND "
}
where = where + "(msg ILIKE '%' || :search || '%')"
maps["search"] = args.Search
}
if where == "" {
where = "true"
}
tableName := "org_cert_warning"
dataCount, err = CoreSQL.GetListPageAndCount(
Router2SystemConfig.MainDB.DB,
&dataList,
tableName,
"id",
"SELECT id, create_at, finish_at, org_id, child_org_id, cert_id, config_id, config_mark, msg FROM "+tableName+" WHERE "+where,
where,
maps,
&args.Pages,
[]string{"id", "create_at"},
)
return
}
// argsCreateWarning 创建新的记录参数
type argsCreateWarning struct {
//组织ID
OrgID int64 `db:"org_id" json:"orgID" check:"id"`
//子商户
ChildOrgID int64 `db:"child_org_id" json:"childOrgID" check:"id" empty:"true"`
//异常证件ID
CertID int64 `db:"cert_id" json:"certID" check:"id"`
//消息
Msg string `db:"msg" json:"msg" check:"des" min:"1" max:"600"`
//扩展参数
Params CoreSQLConfig.FieldsConfigsType `db:"params" json:"params"`
//配置标题
ConfigName string `json:"configName"`
//绑定来源
// user 用户 / org 商户 / org_bind 商户成员 / finance_assets 财务资产 /
BindFrom string `db:"bind_from" json:"bindFrom"`
//绑定ID
// 根据配置决定,可能是用户ID\组织ID\或其他任意主体
BindID int64 `db:"bind_id" json:"bindID"`
}
// createWarning 创建新的记录
func createWarning(args *argsCreateWarning) (err error) {
//获取证件信息
var certData FieldsCert
certData, err = GetCert(&ArgsGetCert{
ID: args.CertID,
OrgID: args.OrgID,
ChildOrgID: args.ChildOrgID,
})
if err != nil {
return
}
//获取配置
var configData FieldsConfig
configData, err = GetConfigByID(&ArgsGetConfigByID{
ID: certData.ConfigID,
OrgID: -1,
})
if err != nil {
return
}
//检查是否存在未处理
var data FieldsWarning
err = Router2SystemConfig.MainDB.Get(&data, "SELECT id, create_at FROM org_cert_warning WHERE org_id = $1 AND child_org_id = $2 AND cert_id = $3 AND finish_at < to_timestamp(1000000)", args.OrgID, args.ChildOrgID, args.CertID)
if err == nil && data.ID > 0 {
//检查是否存在每年提醒开关?
needTipEveryYear, b := configData.Params.GetValBool("needTipEveryYear")
if !b {
needTipEveryYear = false
}
if !needTipEveryYear {
err = errors.New("have warning")
return
} else {
//检查是否达到一年
if data.CreateAt.Unix() >= CoreFilter.GetNowTimeCarbon().SubMonths(11).Time.Unix() {
err = errors.New("have warning")
return
}
}
}
//创建记录
var warningData FieldsWarning
err = CoreSQL.CreateOneAndData(Router2SystemConfig.MainDB.DB, "org_cert_warning", "INSERT INTO org_cert_warning (org_id, child_org_id, cert_id, config_id, config_mark, msg, send_msg_at, send_sms_at, params) VALUES (:org_id, :child_org_id, :cert_id, :config_id, :config_mark, :msg, to_timestamp(0), to_timestamp(0), :params)", map[string]interface{}{
"org_id": args.OrgID,
"child_org_id": args.ChildOrgID,
"cert_id": args.CertID,
"config_id": configData.ID,
"config_mark": configData.Mark,
"msg": args.Msg,
"params": args.Params,
}, &warningData)
if err == nil {
//根据需求,发送用户消息和短信消息
needUserMessage, b := args.Params.GetValBool("needUserMessage")
if b && needUserMessage {
switch args.BindFrom {
case "org":
var orgData OrgCoreCore.FieldsOrg
orgData, err = OrgCoreCore.GetOrg(&OrgCoreCore.ArgsGetOrg{
ID: args.BindID,
})
if err != nil {
break
}
var params CoreSQLConfig.FieldsConfigsType
params = append(params, CoreSQLConfig.FieldsConfigType{
Mark: "orgCertWarningID",
Val: fmt.Sprint(warningData.ID),
})
_, err = UserMessage.Create(&UserMessage.ArgsCreate{
WaitSendAt: time.Now(),
SendUserID: 0,
ReceiveUserID: orgData.UserID,
Title: args.Msg,
Content: args.Msg,
Files: []int64{},
Params: params,
})
if err != nil {
break
}
case "user":
var params CoreSQLConfig.FieldsConfigsType
params = append(params, CoreSQLConfig.FieldsConfigType{
Mark: "orgCertWarningID",
Val: fmt.Sprint(warningData.ID),
})
_, err = UserMessage.Create(&UserMessage.ArgsCreate{
WaitSendAt: time.Now(),
SendUserID: 0,
ReceiveUserID: args.BindID,
Title: args.Msg,
Content: args.Msg,
Files: []int64{},
Params: params,
})
if err != nil {
break
}
}
}
needSMS, b := args.Params.GetValBool("needSMS")
if b && needSMS {
smsConfigID, b := args.Params.GetValInt64("smsConfigID")
if b && smsConfigID > 0 {
switch args.BindFrom {
case "org":
var orgData OrgCoreCore.FieldsOrg
orgData, err = OrgCoreCore.GetOrg(&OrgCoreCore.ArgsGetOrg{
ID: args.BindID,
})
if err != nil {
break
}
var userData UserCore.FieldsUserType
userData, err = UserCore.GetUserByID(&UserCore.ArgsGetUserByID{
ID: orgData.UserID,
OrgID: -1,
})
if err != nil {
break
}
if userData.NationCode == "" || userData.Phone == "" {
break
}
var params CoreSQLConfig.FieldsConfigsType
params = append(params, CoreSQLConfig.FieldsConfigType{
Mark: "$1",
Val: args.ConfigName,
})
_, err = BaseSMS.CreateSMS(&BaseSMS.ArgsCreateSMS{
OrgID: orgData.ID,
ConfigID: smsConfigID,
Token: 0,
NationCode: userData.NationCode,
Phone: userData.Phone,
Params: params,
FromInfo: CoreSQLFrom.FieldsFrom{
System: "",
ID: 0,
Mark: "",
Name: "组织证件",
},
})
if err != nil {
break
}
case "user":
var userData UserCore.FieldsUserType
userData, err = UserCore.GetUserByID(&UserCore.ArgsGetUserByID{
ID: args.BindID,
OrgID: -1,
})
if err != nil {
break
}
if userData.NationCode == "" || userData.Phone == "" {
break
}
var params CoreSQLConfig.FieldsConfigsType
params = append(params, CoreSQLConfig.FieldsConfigType{
Mark: "$1",
Val: args.ConfigName,
})
_, err = BaseSMS.CreateSMS(&BaseSMS.ArgsCreateSMS{
OrgID: userData.OrgID,
ConfigID: smsConfigID,
Token: 0,
NationCode: userData.NationCode,
Phone: userData.Phone,
Params: params,
FromInfo: CoreSQLFrom.FieldsFrom{
System: "",
ID: 0,
Mark: "",
Name: "组织证件",
},
})
if err != nil {
break
}
}
}
}
}
return
}
// ArgsUpdateWarningFinish 标记已经处理参数
type ArgsUpdateWarningFinish struct {
//ID
ID int64 `db:"id" json:"id" check:"id"`
//组织ID
OrgID int64 `db:"org_id" json:"orgID" check:"id" empty:"true"`
//子商户
ChildOrgID int64 `db:"child_org_id" json:"childOrgID" check:"id" empty:"true"`
}
// UpdateWarningFinish 标记已经处理
func UpdateWarningFinish(args *ArgsUpdateWarningFinish) (err error) {
_, err = CoreSQL.UpdateOne(Router2SystemConfig.MainDB.DB, "UPDATE org_cert_warning SET finish_at = NOW() WHERE id = :id AND (:org_id < 1 OR org_id = :org_id) AND (:child_org_id < 1 OR child_org_id = :child_org_id)", args)
return
}