-
Notifications
You must be signed in to change notification settings - Fork 0
/
org_create.go
136 lines (133 loc) · 3.93 KB
/
org_create.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
package OrgCoreCore
import (
"errors"
"fmt"
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"
Router2SystemConfig "github.com/fotomxq/weeekj_core/v5/router2/system_config"
UserCore "github.com/fotomxq/weeekj_core/v5/user/core"
UserMessageMod "github.com/fotomxq/weeekj_core/v5/user/message/mod"
"github.com/lib/pq"
"time"
)
// ArgsCreateOrg 创建组织参数
type ArgsCreateOrg 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"`
//分类ID
SortID int64 `db:"sort_id" json:"sortID" check:"id" empty:"true"`
}
// CreateOrg 创建组织
func CreateOrg(args *ArgsCreateOrg) (orgData FieldsOrg, errCode string, err error) {
//生成key
if args.Key == "" {
args.Key = makeKey(args.Name)
}
//获取用户信息
if args.UserID < 1 {
errCode = "err_user"
err = errors.New("user id is empty")
return
}
var userData UserCore.FieldsUserType
userData, 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
}
//检查重名
oldOrgID := getOrgByName(args.Name)
if oldOrgID > 0 {
errCode = "err_name"
err = errors.New("org name exist")
return
}
//如果开通功能为空,则默认给开通功能
if len(args.OpenFunc) < 1 {
args.OpenFunc = Router2SystemConfig.GlobConfig.Org.DefaultOpenFunc
}
//检查上一级
if args.ParentID > 0 {
//获取上一级
var parentData FieldsOrg
parentData, err = GetOrg(&ArgsGetOrg{
ID: args.ParentID,
})
if err != nil {
errCode = "err_parent"
return
}
//检查上一级功能和本次开通功能
err = checkOrgInParentFunc(parentData.OpenFunc, args.OpenFunc)
if err != nil {
errCode = "err_func_not_in_area"
return
}
}
//检查key
if args.Key == "" {
args.Key, err = CoreFilter.GetRandStr3(10)
if err != nil {
errCode = "err_key"
return
}
}
orgData, err = GetOrgByKey(&ArgsGetOrgByKey{
Key: args.Key,
})
if err == nil && orgData.ID > 0 {
errCode = "err_key"
err = errors.New("key is exist")
return
}
//生成数据
if err = CoreSQL.CreateOneAndData(Router2SystemConfig.MainDB.DB, "org_core", "INSERT INTO org_core (user_id, name, key, des, parent_id, parent_func, open_func, sort_id) VALUES (:user_id, :name, :key, :des, :parent_id, :parent_func, :open_func, :sort_id)", args, &orgData); err != nil {
errCode = "err_insert"
return
}
//建立绑定关系
if _, err = SetBind(&ArgsSetBind{
UserID: args.UserID,
Avatar: 0,
Name: userData.Name,
OrgID: orgData.ID,
GroupIDs: []int64{},
Manager: []string{"member", "all"},
NationCode: userData.NationCode,
Phone: userData.Phone,
Email: userData.Email,
SyncSystem: "",
SyncID: 0,
SyncHash: "",
Params: CoreSQLConfig.FieldsConfigsType{},
}); err != nil {
errCode = "err_org_bind"
return
}
//发送用户消息
if orgData.UserID > 0 {
UserMessageMod.CreateSystemToUser(time.Time{}, orgData.UserID, "成功开通商户", fmt.Sprint("您已经成功开通了商户(", orgData.Name, ")。"), nil, nil)
}
//反馈
return
}