/
org_audit.go
387 lines (372 loc) · 12.6 KB
/
org_audit.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package OrgCoreCore
import (
"errors"
"fmt"
BaseConfig "github.com/fotomxq/weeekj_core/v5/base/config"
CoreFilter "github.com/fotomxq/weeekj_core/v5/core/filter"
CoreLog "github.com/fotomxq/weeekj_core/v5/core/log"
CoreSQL "github.com/fotomxq/weeekj_core/v5/core/sql"
CoreSQLConfig "github.com/fotomxq/weeekj_core/v5/core/sql/config"
CoreSQLPages "github.com/fotomxq/weeekj_core/v5/core/sql/pages"
OrgMap "github.com/fotomxq/weeekj_core/v5/org/map"
Router2SystemConfig "github.com/fotomxq/weeekj_core/v5/router2/system_config"
UserCore "github.com/fotomxq/weeekj_core/v5/user/core"
"github.com/lib/pq"
"github.com/mozillazg/go-pinyin"
"strings"
)
// ArgsGetOrgAuditList 获取审核列表参数
type ArgsGetOrgAuditList struct {
//分页
Pages CoreSQLPages.ArgsDataList `json:"pages"`
//用户ID
UserID int64 `json:"userID" check:"id" empty:"true"`
//上级组织ID
ParentID int64 `json:"parentID" check:"id" empty:"true"`
//上级控制权限限制
ParentFunc pq.StringArray `db:"parent_func" json:"parentFunc" check:"marks" empty:"true"`
//开通业务
// 该内容只有总管理员或订阅能进行控制
OpenFunc pq.StringArray `db:"open_func" json:"openFunc" check:"marks" empty:"true"`
//是否审核
NeedIsAudit bool `db:"need_is_audit" json:"needIsAudit" check:"bool" empty:"true"`
IsAudit bool `db:"is_audit" json:"isAudit" check:"bool" empty:"true"`
//是否拒绝
IsBan bool `db:"is_ban" json:"isBan" check:"bool" empty:"true"`
//审核人员
AuditUserID int64 `db:"audit_user_id" json:"auditUserID" check:"id" empty:"true"`
//是否删除
IsRemove bool `json:"isRemove" check:"bool" empty:"true"`
//搜索
Search string `json:"search" check:"search" empty:"true"`
}
// GetOrgAuditList 获取审核列表
func GetOrgAuditList(args *ArgsGetOrgAuditList) (dataList []FieldsOrgAudit, dataCount int64, err error) {
where := ""
maps := map[string]interface{}{}
where = CoreSQL.GetDeleteSQL(args.IsRemove, where)
if args.ParentID > -1 {
where = where + " AND :parent_id = :parent_id"
maps["parent_id"] = args.ParentID
}
if args.UserID > 0 {
where = where + " AND user_id = :user_id"
maps["user_id"] = args.UserID
}
if len(args.ParentFunc) > 0 {
where = where + " AND parent_func @> :parent_func"
maps["parent_func"] = args.ParentFunc
}
if len(args.OpenFunc) > 0 {
where = where + " AND open_func @> :open_func"
maps["open_func"] = args.OpenFunc
}
if args.NeedIsAudit {
if args.IsAudit {
where = where + " AND audit_at > to_timestamp(1000000) AND ban_at <= to_timestamp(1000000)"
} else {
if args.IsBan {
where = where + " AND ban_at > to_timestamp(1000000)"
} else {
where = where + " AND audit_at <= to_timestamp(1000000) AND ban_at <= to_timestamp(1000000)"
}
}
}
if args.AuditUserID > 0 {
where = where + " AND audit_user_id = :audit_user_id"
maps["audit_user_id"] = args.AuditUserID
}
if args.Search != "" {
where = where + " AND (name ILIKE '%' || :search || '%' OR des ILIKE '%' || :search || '%' OR ban_des ILIKE '%' || :search || '%')"
maps["search"] = args.Search
}
tableName := "org_core_audit"
dataCount, err = CoreSQL.GetListPageAndCount(
Router2SystemConfig.MainDB.DB,
&dataList,
tableName,
"id",
"SELECT id, create_at, delete_at, audit_at, ban_at, ban_des, audit_user_id, user_id, key, name, des, parent_id, parent_func, open_func, params "+"FROM "+tableName+" WHERE "+where,
where,
maps,
&args.Pages,
[]string{"id", "create_at", "update_at", "delete_at", "audit_at", "ban_at"},
)
return
}
// ArgsCreateOrgAudit 创建新审核请求参数
type ArgsCreateOrgAudit struct {
//所属用户
// 掌管该数据的用户,创建人和根管理员,不可删除只能更换
UserID int64 `db:"user_id" json:"userID" check:"id"`
//企业唯一标识码
// 用于特殊识别和登陆识别等操作
Key string `db:"key" json:"key" check:"mark" empty:"true"`
//构架名称,或组织名称
Name string `db:"name" json:"name" check:"name"`
//组织描述
Des string `db:"des" json:"des" check:"des" min:"1" max:"3000"`
//上级ID
ParentID int64 `db:"parent_id" json:"parentID" check:"id" empty:"true"`
//上级控制权限限制
ParentFunc pq.StringArray `db:"parent_func" json:"parentFunc" check:"marks" empty:"true"`
//开通业务
// 该内容只有总管理员或订阅能进行控制
OpenFunc pq.StringArray `db:"open_func" json:"openFunc" check:"marks" empty:"true"`
//附加参数
Params CoreSQLConfig.FieldsConfigsType `db:"params" json:"params"`
}
// CreateOrgAudit 创建新审核请求
func CreateOrgAudit(args *ArgsCreateOrgAudit) (auditData FieldsOrgAudit, errCode string, err error) {
//修正参数
if args.ParentID < 1 {
args.ParentID = 0
}
//用户禁止重复申请
err = Router2SystemConfig.MainDB.Get(&auditData, "SELECT id FROM org_core_audit WHERE delete_at < to_timestamp(1000000) AND audit_at < to_timestamp(1000000) AND ban_at < to_timestamp(1000000) AND user_id = $1 LIMIT 1", args.UserID)
if err == nil && auditData.ID > 0 {
errCode = "err_org_replace_audit"
err = errors.New("user replace audit")
return
}
//生成key
if args.Key == "" {
args.Key = makeKey(args.Name)
}
//获取用户信息
if args.UserID < 1 {
errCode = "err_user"
err = errors.New("user id is empty")
return
}
_, err = UserCore.GetUserByID(&UserCore.ArgsGetUserByID{
ID: args.UserID,
OrgID: -1,
})
if err != nil {
errCode = "err_user"
err = errors.New("get user by id, " + err.Error())
return
}
//检查上一级
if args.ParentID > 0 {
//获取上一级
var parentData FieldsOrg
parentData, err = GetOrg(&ArgsGetOrg{
ID: args.ParentID,
})
if err != nil {
errCode = "err_org_parent_not_exist"
return
}
//检查上一级功能和本次开通功能
err = checkOrgInParentFunc(parentData.OpenFunc, args.OpenFunc)
if err != nil {
errCode = "err_org_func_not_in_area"
return
}
}
//检查key
if args.Key == "" {
args.Key, err = CoreFilter.GetRandStr3(10)
if err != nil {
errCode = "err_key"
return
}
}
var orgData FieldsOrg
orgData, err = GetOrgByKey(&ArgsGetOrgByKey{
Key: args.Key,
})
if err == nil && orgData.ID > 0 {
errCode = "err_org_replace"
err = errors.New("key is exist")
return
}
//生成数据
if err = CoreSQL.CreateOneAndData(Router2SystemConfig.MainDB.DB, "org_core_audit", "INSERT INTO org_core_audit (audit_at, ban_at, ban_des, audit_user_id, user_id, key, name, des, parent_id, parent_func, open_func, params) VALUES (to_timestamp(0), to_timestamp(0), '', 0, :user_id, :key, :name, :des, :parent_id, :parent_func, :open_func, :params)", args, &auditData); err != nil {
errCode = "err_insert"
return
}
return
}
// ArgsUpdateOrgAuditPass 通过审核参数
type ArgsUpdateOrgAuditPass struct {
//ID
ID int64 `db:"id" json:"id" check:"id"`
//上级组织ID
ParentID int64 `db:"parent_id" json:"parentID" check:"id" empty:"true"`
//操作人
AuditUserID int64 `db:"audit_user_id" json:"auditUserID" check:"id" empty:"true"`
}
// AuditOrgCreateMap 新增特殊逻辑判断平台是否开始同步创建组织地图开关
func AuditOrgCreateMap(auditData FieldsOrgAudit, orgID int64) {
// 通过扩展参数获取创建地图相关数据
orgMapProvince := auditData.Params.GetValIntNoBool("OrgMapProvince")
orgMapCity := auditData.Params.GetValIntNoBool("OrgMapCity")
newIndexShowGps := auditData.Params.GetValNoErr("IndexShowGPS")
newGPSArr := strings.Split(newIndexShowGps, ",")
var longitude, latitude float64
if newIndexShowGps != "" && len(newGPSArr) >= 3 {
longitude = CoreFilter.GetFloat64ByStringNoErr(newGPSArr[1])
latitude = CoreFilter.GetFloat64ByStringNoErr(newGPSArr[2])
}
_, err := OrgMap.CreateMap(&OrgMap.ArgsCreateMap{
OrgID: orgID,
UserID: auditData.UserID,
ParentID: 0,
CoverFileID: auditData.Params.GetValInt64NoBool("CoverFileID"),
CoverFileIDs: []int64{},
Name: auditData.Name,
Des: auditData.Des,
Country: 86,
Province: orgMapProvince,
City: orgMapCity,
Address: auditData.Params.GetValNoErr("IndexShowAddress"),
MapType: 1,
Longitude: longitude,
Latitude: latitude,
AdCountLimit: -1,
ViewTimeLimit: 0,
Params: CoreSQLConfig.FieldsConfigsType{},
})
if err != nil {
CoreLog.Error(err)
}
}
// UpdateOrgAuditPass 通过审核
func UpdateOrgAuditPass(args *ArgsUpdateOrgAuditPass) (errCode string, err error) {
_, err = CoreSQL.UpdateOneSoft(Router2SystemConfig.MainDB.DB, "UPDATE org_core_audit SET audit_at = NOW(), ban_at = to_timestamp(0), audit_user_id = :audit_user_id WHERE id = :id AND (:parent_id < 1 OR parent_id = :parent_id)", args)
if err == nil {
var auditData FieldsOrgAudit
err = Router2SystemConfig.MainDB.Get(&auditData, "SELECT id, create_at, delete_at, audit_at, ban_at, ban_des, audit_user_id, user_id, key, name, des, parent_id, parent_func, open_func, params FROM org_core_audit WHERE id = $1 LIMIT 1", args.ID)
if err != nil {
errCode = "audit_empty"
return
}
var newOrgData FieldsOrg
newOrgData, errCode, err = CreateOrg(&ArgsCreateOrg{
UserID: auditData.UserID,
Key: auditData.Key,
Name: auditData.Name,
Des: auditData.Des,
ParentID: auditData.ParentID,
ParentFunc: auditData.ParentFunc,
OpenFunc: auditData.OpenFunc,
SortID: 0,
})
if err != nil {
return
}
// 判断审核组织后是否同步创建组织地图
orgAuditCreateMap := BaseConfig.GetDataBoolNoErr("OrgAuditCreateMap")
// 如果开启了同步创建组织地图,则创建组织地图
if orgAuditCreateMap {
AuditOrgCreateMap(auditData, newOrgData.ID)
}
for _, v := range auditData.Params {
if strings.Contains(v.Mark, "*") {
continue
}
isFind := true
switch v.Mark {
case "CoverFileID":
case "OrgPhone":
case "OrgDes":
case "OrgBusinessHours":
case "IndexShowAddress":
case "IndexShowGPS":
case "OrgAuditLicenseFileID":
case "OrgAuditIDCardAFileID":
case "OrgAuditIDCardBFileID":
case "OrgAuditLegalName":
default:
isFind = false
}
if isFind {
_ = Config.SetConfigValSimple(newOrgData.ID, v.Mark, v.Val)
}
}
auditData.Params = CoreSQLConfig.Set(auditData.Params, "NewOrgID", newOrgData.ID)
_, err = CoreSQL.UpdateOneSoft(Router2SystemConfig.MainDB.DB, "UPDATE org_core_audit SET params = :params WHERE id = :id", map[string]interface{}{
"id": args.ID,
"params": auditData.Params,
})
if err != nil {
errCode = "err_update"
return
}
return
} else {
errCode = "no_audit"
}
return
}
// ArgsUpdateOrgAuditBan 拒绝审核参数
type ArgsUpdateOrgAuditBan struct {
//ID
ID int64 `db:"id" json:"id" check:"id"`
//上级组织ID
ParentID int64 `db:"parent_id" json:"parentID" check:"id" empty:"true"`
//操作人
AuditUserID int64 `db:"audit_user_id" json:"auditUserID" check:"id" empty:"true"`
//拒绝审核原因
BanDes string `db:"ban_des" json:"banDes" check:"des" min:"1" max:"600" empty:"true"`
}
// UpdateOrgAuditBan 拒绝审核
func UpdateOrgAuditBan(args *ArgsUpdateOrgAuditBan) (err error) {
_, err = CoreSQL.UpdateOneSoft(Router2SystemConfig.MainDB.DB, "UPDATE org_core_audit SET audit_at = to_timestamp(0), ban_at = NOW(), audit_user_id = :audit_user_id, ban_des = :ban_des WHERE id = :id AND (:parent_id < 1 OR parent_id = :parent_id)", args)
return
}
// ArgsDeleteOrgAudit 删除审核参数
type ArgsDeleteOrgAudit struct {
//ID
ID int64 `db:"id" json:"id" check:"id"`
//上级组织ID
ParentID int64 `db:"parent_id" json:"parentID" check:"id" empty:"true"`
}
// DeleteOrgAudit 删除审核
func DeleteOrgAudit(args *ArgsDeleteOrgAudit) (err error) {
_, err = CoreSQL.DeleteAllSoft(Router2SystemConfig.MainDB.DB, "org_core_audit", "id = :id AND (:parent_id < 1 OR parent_id = :parent_id)", args)
return
}
// 生成新的key
func makeKey(orgName string) string {
//生成拼音
newKeys := pinyin.LazyPinyin(orgName, pinyin.NewArgs())
var newKey string
for k := 0; k < len(newKeys); k++ {
newKey = newKey + newKeys[k]
}
//检查key是否存在
if newKey != "" {
var id int64
err := Router2SystemConfig.MainDB.Get(&id, "SELECT id FROM org_core WHERE key = $1", newKey)
if err != nil || id < 1 {
err = Router2SystemConfig.MainDB.Get(&id, "SELECT id FROM org_core_audit WHERE key = $1", newKey)
if err == nil && id > 0 {
return newKey
}
}
}
step := 1
for {
if step > 100 {
break
}
step += 1
var id int64
newKey2 := newKey + fmt.Sprint(step)
err := Router2SystemConfig.MainDB.Get(&id, "SELECT id FROM org_core WHERE key = $1", newKey2)
if err == nil && id > 0 {
continue
}
err = Router2SystemConfig.MainDB.Get(&id, "SELECT id FROM org_core_audit WHERE key = $1", newKey2)
if err == nil && id > 0 {
continue
}
break
}
//反馈数据
return newKey
}