|
|
1
|
+package company
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "fmt"
|
|
|
5
|
+ "oppmg/common/log"
|
|
|
6
|
+ "oppmg/models"
|
|
|
7
|
+ "oppmg/protocol"
|
|
|
8
|
+
|
|
|
9
|
+ "github.com/astaxie/beego/orm"
|
|
|
10
|
+)
|
|
|
11
|
+
|
|
|
12
|
+type CenterCompanyInfo struct {
|
|
|
13
|
+ CompanyId int64
|
|
|
14
|
+ CompanyName string
|
|
|
15
|
+ AdminId int64
|
|
|
16
|
+ AdminAccount string
|
|
|
17
|
+ AdminName string
|
|
|
18
|
+}
|
|
|
19
|
+
|
|
|
20
|
+func GetCenterCompanyInfo(companyid int64) (CenterCompanyInfo, error) {
|
|
|
21
|
+
|
|
|
22
|
+ //TODO 调用统一用户中心的数据
|
|
|
23
|
+ data := CenterCompanyInfo{
|
|
|
24
|
+ CompanyId: 9999 + companyid,
|
|
|
25
|
+ CompanyName: "调试用公司数据",
|
|
|
26
|
+ AdminId: 9999 + companyid,
|
|
|
27
|
+ AdminAccount: "调试用自己修改",
|
|
|
28
|
+ AdminName: "调试用自己修改",
|
|
|
29
|
+ }
|
|
|
30
|
+ return data, nil
|
|
|
31
|
+}
|
|
|
32
|
+
|
|
|
33
|
+//InitCompanyInfo 初始化公司
|
|
|
34
|
+//@uCompanyid 从统一用户中心获取的公司id companyid,
|
|
|
35
|
+func InitCompanyInfo(uCompanyid int64) error {
|
|
|
36
|
+ var (
|
|
|
37
|
+ centerCompany CenterCompanyInfo
|
|
|
38
|
+ err error
|
|
|
39
|
+ )
|
|
|
40
|
+ centerCompany, err = GetCenterCompanyInfo(uCompanyid)
|
|
|
41
|
+ if err != nil {
|
|
|
42
|
+ return protocol.NewErrWithMessage("10051")
|
|
|
43
|
+ }
|
|
|
44
|
+ var (
|
|
|
45
|
+ newDeparment *models.Department
|
|
|
46
|
+ //newUserDepart = &models.UserDepartment{}
|
|
|
47
|
+ newRoleGroup *models.Role
|
|
|
48
|
+ newRole *models.Role
|
|
|
49
|
+ newUser *models.User
|
|
|
50
|
+ newCompany *models.Company
|
|
|
51
|
+ newUserCompany *models.UserCompany
|
|
|
52
|
+ )
|
|
|
53
|
+ o := orm.NewOrm()
|
|
|
54
|
+ o.Begin()
|
|
|
55
|
+ //处理用户数据
|
|
|
56
|
+ newUser, err = initAdminUser(centerCompany, o)
|
|
|
57
|
+ if err != nil {
|
|
|
58
|
+ o.Rollback()
|
|
|
59
|
+ log.Error("处理公司主管数据失败:%s", err)
|
|
|
60
|
+ return protocol.NewErrWithMessage("1")
|
|
|
61
|
+ }
|
|
|
62
|
+ //处理公司数据
|
|
|
63
|
+ newCompany, err = initCompany(centerCompany, newUser, o)
|
|
|
64
|
+ if err != nil {
|
|
|
65
|
+ o.Rollback()
|
|
|
66
|
+ log.Error("处理公司数据失败:%s", err)
|
|
|
67
|
+ return protocol.NewErrWithMessage("1")
|
|
|
68
|
+ }
|
|
|
69
|
+ //添加user_company
|
|
|
70
|
+ newUserCompany, err = initUserCompany(newCompany, newUser, o)
|
|
|
71
|
+ if err != nil {
|
|
|
72
|
+ o.Rollback()
|
|
|
73
|
+ log.Error("处理user_company数据失败:%s", err)
|
|
|
74
|
+ return protocol.NewErrWithMessage("1")
|
|
|
75
|
+ }
|
|
|
76
|
+ //添加部门
|
|
|
77
|
+ newDeparment, err = initDepartment(newCompany, newUserCompany, o)
|
|
|
78
|
+ if err != nil {
|
|
|
79
|
+ o.Rollback()
|
|
|
80
|
+ log.Error("处理部门数据失败:%s", err)
|
|
|
81
|
+ return protocol.NewErrWithMessage("1")
|
|
|
82
|
+ }
|
|
|
83
|
+ //添加用户部门数据
|
|
|
84
|
+ _, err = initUserDedaprtmet(newDeparment, newUserCompany, o)
|
|
|
85
|
+ if err != nil {
|
|
|
86
|
+ o.Rollback()
|
|
|
87
|
+ log.Error("处理user_department数据失败:%s", err)
|
|
|
88
|
+ return protocol.NewErrWithMessage("1")
|
|
|
89
|
+ }
|
|
|
90
|
+ //添加角色组
|
|
|
91
|
+ newRoleGroup, err = initRoleGroup(newCompany, o)
|
|
|
92
|
+ if err != nil {
|
|
|
93
|
+ o.Rollback()
|
|
|
94
|
+ log.Error("处理角色组数据失败:%s", err)
|
|
|
95
|
+ return protocol.NewErrWithMessage("1")
|
|
|
96
|
+ }
|
|
|
97
|
+ //添加角色
|
|
|
98
|
+ newRole, err = initRole(newCompany, newRoleGroup, o)
|
|
|
99
|
+ if err != nil {
|
|
|
100
|
+ o.Rollback()
|
|
|
101
|
+ log.Error("处理角色数据失败:%s", err)
|
|
|
102
|
+ return protocol.NewErrWithMessage("1")
|
|
|
103
|
+ }
|
|
|
104
|
+ //添加用户的角色
|
|
|
105
|
+ _, err = initUserRole(newUserCompany, newRole, o)
|
|
|
106
|
+ if err != nil {
|
|
|
107
|
+ o.Rollback()
|
|
|
108
|
+ log.Error("处理user_role数据失败:%s", err)
|
|
|
109
|
+ return protocol.NewErrWithMessage("1")
|
|
|
110
|
+ }
|
|
|
111
|
+ o.Commit()
|
|
|
112
|
+ return nil
|
|
|
113
|
+}
|
|
|
114
|
+
|
|
|
115
|
+func initCompany(centerCompany CenterCompanyInfo, admininfo *models.User, o orm.Ormer) (*models.Company, error) {
|
|
|
116
|
+ var (
|
|
|
117
|
+ err error
|
|
|
118
|
+ newCompany = &models.Company{}
|
|
|
119
|
+ )
|
|
|
120
|
+ err = o.QueryTable(&models.Company{}).
|
|
|
121
|
+ Filter("user_center_id", centerCompany.CompanyId).
|
|
|
122
|
+ One(newCompany)
|
|
|
123
|
+ if err == nil {
|
|
|
124
|
+ _, err = o.QueryTable(&models.Company{}).
|
|
|
125
|
+ Filter("user_center_id", centerCompany.CompanyId).
|
|
|
126
|
+ Update(orm.Params{
|
|
|
127
|
+ "name": centerCompany.CompanyName,
|
|
|
128
|
+ "admin_id": admininfo.Id,
|
|
|
129
|
+ "delete_at": 0,
|
|
|
130
|
+ "enable": models.COMPANY_ENABLE_YES,
|
|
|
131
|
+ })
|
|
|
132
|
+ if err != nil {
|
|
|
133
|
+ log.Error("更新公司数据失败:%s", err)
|
|
|
134
|
+ return nil, err
|
|
|
135
|
+ }
|
|
|
136
|
+ } else if err == orm.ErrNoRows {
|
|
|
137
|
+ newCompany = &models.Company{
|
|
|
138
|
+ Name: centerCompany.CompanyName,
|
|
|
139
|
+ AdminId: admininfo.Id,
|
|
|
140
|
+ UserCenterId: centerCompany.CompanyId,
|
|
|
141
|
+ }
|
|
|
142
|
+ _, err = models.AddCompany(newCompany, o)
|
|
|
143
|
+ if err != nil {
|
|
|
144
|
+ log.Error("添加公司数据失败:%s", err)
|
|
|
145
|
+ return nil, err
|
|
|
146
|
+ }
|
|
|
147
|
+
|
|
|
148
|
+ } else {
|
|
|
149
|
+ log.Error("查询公司数据失败:%s", err)
|
|
|
150
|
+ return nil, err
|
|
|
151
|
+ }
|
|
|
152
|
+ return newCompany, nil
|
|
|
153
|
+}
|
|
|
154
|
+
|
|
|
155
|
+func initAdminUser(centerCompany CenterCompanyInfo, o orm.Ormer) (*models.User, error) {
|
|
|
156
|
+ var (
|
|
|
157
|
+ err error
|
|
|
158
|
+ newUser = &models.User{}
|
|
|
159
|
+ )
|
|
|
160
|
+ err = o.QueryTable(&models.User{}).
|
|
|
161
|
+ Filter("user_center_id", centerCompany.AdminId).
|
|
|
162
|
+ Filter("delete_at", 0).
|
|
|
163
|
+ One(newUser)
|
|
|
164
|
+ if err == nil {
|
|
|
165
|
+ // 更新数据
|
|
|
166
|
+ _, err = o.QueryTable(&models.User{}).
|
|
|
167
|
+ Filter("user_center_id", centerCompany.CompanyId).
|
|
|
168
|
+ Filter("delete_at", 0).
|
|
|
169
|
+ Update(orm.Params{
|
|
|
170
|
+ "nick_name": centerCompany.AdminName,
|
|
|
171
|
+ "phone": centerCompany.AdminAccount,
|
|
|
172
|
+ "enable_status": models.USER_ENABLE_YES,
|
|
|
173
|
+ })
|
|
|
174
|
+ if err != nil {
|
|
|
175
|
+ log.Error("数据操作失败:%s", err)
|
|
|
176
|
+ return nil, err
|
|
|
177
|
+ }
|
|
|
178
|
+ } else if err == orm.ErrNoRows {
|
|
|
179
|
+ //添加用户
|
|
|
180
|
+ newUser = &models.User{
|
|
|
181
|
+ Phone: centerCompany.AdminAccount,
|
|
|
182
|
+ UserCenterId: centerCompany.AdminId,
|
|
|
183
|
+ NickName: centerCompany.AdminName,
|
|
|
184
|
+ }
|
|
|
185
|
+ id, err := models.AddUser(newUser, o)
|
|
|
186
|
+ fmt.Println("-=====>user_id=", id)
|
|
|
187
|
+ if err != nil {
|
|
|
188
|
+
|
|
|
189
|
+ log.Error("添加用户数据失败:%s", err)
|
|
|
190
|
+ return nil, err
|
|
|
191
|
+ }
|
|
|
192
|
+ } else {
|
|
|
193
|
+ log.Error("查询用户数据失败:%s", err)
|
|
|
194
|
+ return nil, err
|
|
|
195
|
+ }
|
|
|
196
|
+ return newUser, nil
|
|
|
197
|
+}
|
|
|
198
|
+
|
|
|
199
|
+func initUserCompany(newcompany *models.Company, newuser *models.User, o orm.Ormer) (*models.UserCompany, error) {
|
|
|
200
|
+ var (
|
|
|
201
|
+ err error
|
|
|
202
|
+ uc = &models.UserCompany{}
|
|
|
203
|
+ )
|
|
|
204
|
+ err = o.QueryTable(&models.UserCompany{}).
|
|
|
205
|
+ Filter("user_id", newuser.Id).
|
|
|
206
|
+ Filter("company_id", newcompany.Id).
|
|
|
207
|
+ Filter("delete_at", 0).
|
|
|
208
|
+ One(uc)
|
|
|
209
|
+ if err == nil {
|
|
|
210
|
+ _, err = o.QueryTable(&models.UserCompany{}).
|
|
|
211
|
+ Filter("id", uc.Id).
|
|
|
212
|
+ Update(orm.Params{
|
|
|
213
|
+ "enable": models.USERCOMPANY_ENABLE_YES,
|
|
|
214
|
+ })
|
|
|
215
|
+ if err != nil {
|
|
|
216
|
+ log.Error("更新user_company数据失败:%s", err)
|
|
|
217
|
+ return nil, err
|
|
|
218
|
+ }
|
|
|
219
|
+ } else if err == orm.ErrNoRows {
|
|
|
220
|
+ uc = &models.UserCompany{
|
|
|
221
|
+ UserId: newuser.Id,
|
|
|
222
|
+ CompanyId: newcompany.Id,
|
|
|
223
|
+ }
|
|
|
224
|
+ _, err = models.AddUserCompany(uc, o)
|
|
|
225
|
+ if err != nil {
|
|
|
226
|
+ log.Error("添加user_company数据失败:%s", err)
|
|
|
227
|
+ return nil, err
|
|
|
228
|
+ }
|
|
|
229
|
+ } else {
|
|
|
230
|
+ log.Error("获取user_company数据失败:%s", err)
|
|
|
231
|
+ return nil, err
|
|
|
232
|
+ }
|
|
|
233
|
+ return uc, nil
|
|
|
234
|
+}
|
|
|
235
|
+
|
|
|
236
|
+func initDepartment(newCompany *models.Company, newusercompany *models.UserCompany, o orm.Ormer) (*models.Department, error) {
|
|
|
237
|
+ var (
|
|
|
238
|
+ newDepartment = &models.Department{}
|
|
|
239
|
+ err error
|
|
|
240
|
+ )
|
|
|
241
|
+ err = o.QueryTable(&models.Department{}).
|
|
|
242
|
+ Filter("company_id", newCompany.Id).
|
|
|
243
|
+ Filter("is_top", 1).
|
|
|
244
|
+ One(newDepartment)
|
|
|
245
|
+ if err == nil {
|
|
|
246
|
+ return newDepartment, nil
|
|
|
247
|
+ } else if err == orm.ErrNoRows {
|
|
|
248
|
+ //添加部门
|
|
|
249
|
+ newDepartment = &models.Department{
|
|
|
250
|
+ CompanyId: newCompany.Id,
|
|
|
251
|
+ Name: newCompany.Name,
|
|
|
252
|
+ IsTop: 1,
|
|
|
253
|
+ ParentId: 0,
|
|
|
254
|
+ }
|
|
|
255
|
+ newDepartment.SetManages([]int64{newusercompany.Id})
|
|
|
256
|
+ _, err = models.AddDepartment(newDepartment, o)
|
|
|
257
|
+ if err != nil {
|
|
|
258
|
+ log.Error("添加部门数据失败:%s", err)
|
|
|
259
|
+ return nil, err
|
|
|
260
|
+ }
|
|
|
261
|
+ newDepartment.SetRelation(nil)
|
|
|
262
|
+ err = models.UpdateDepartmentById(newDepartment, []string{"Relation"}, o)
|
|
|
263
|
+ if err != nil {
|
|
|
264
|
+ log.Error("更新部门关系树数据失败:%s", err)
|
|
|
265
|
+ return nil, err
|
|
|
266
|
+ }
|
|
|
267
|
+ } else {
|
|
|
268
|
+ log.Error("查询部门数据失败:%s", err)
|
|
|
269
|
+ return nil, err
|
|
|
270
|
+ }
|
|
|
271
|
+
|
|
|
272
|
+ return newDepartment, nil
|
|
|
273
|
+}
|
|
|
274
|
+
|
|
|
275
|
+func initUserDedaprtmet(newDepartment *models.Department, newUserCompany *models.UserCompany, o orm.Ormer) (*models.UserDepartment, error) {
|
|
|
276
|
+ var (
|
|
|
277
|
+ newUserDepartment = &models.UserDepartment{}
|
|
|
278
|
+ err error
|
|
|
279
|
+ )
|
|
|
280
|
+ err = o.QueryTable(&models.UserDepartment{}).
|
|
|
281
|
+ Filter("department_id", newDepartment.Id).
|
|
|
282
|
+ Filter("user_company_id", newUserCompany.Id).
|
|
|
283
|
+ Filter("enable_status", models.USER_DEPARTMENT_ENABLE_YES).
|
|
|
284
|
+ One(newUserDepartment)
|
|
|
285
|
+ if err == nil {
|
|
|
286
|
+ return newUserDepartment, nil
|
|
|
287
|
+ } else if err == orm.ErrNoRows {
|
|
|
288
|
+ //添加部门
|
|
|
289
|
+ newUserDepartment = &models.UserDepartment{
|
|
|
290
|
+ UserCompanyId: newUserCompany.Id,
|
|
|
291
|
+ UserId: newUserCompany.UserId,
|
|
|
292
|
+ CompanyId: newUserCompany.CompanyId,
|
|
|
293
|
+ DepartmentId: newDepartment.Id,
|
|
|
294
|
+ }
|
|
|
295
|
+ _, err = models.AddUserDepartment(newUserDepartment, o)
|
|
|
296
|
+ if err != nil {
|
|
|
297
|
+ log.Error("添加部门数据失败:%s", err)
|
|
|
298
|
+ return nil, err
|
|
|
299
|
+ }
|
|
|
300
|
+ } else {
|
|
|
301
|
+ log.Error("查询部门数据失败:%s", err)
|
|
|
302
|
+ return nil, err
|
|
|
303
|
+ }
|
|
|
304
|
+
|
|
|
305
|
+ return newUserDepartment, nil
|
|
|
306
|
+}
|
|
|
307
|
+
|
|
|
308
|
+func initRoleGroup(newCompany *models.Company, o orm.Ormer) (*models.Role, error) {
|
|
|
309
|
+ var (
|
|
|
310
|
+ newRoleGroup = &models.Role{}
|
|
|
311
|
+ err error
|
|
|
312
|
+ )
|
|
|
313
|
+ err = o.QueryTable(&models.Role{}).
|
|
|
314
|
+ Filter("delete_at", 0).
|
|
|
315
|
+ Filter("is_default", models.ROLE_DEFAULR).
|
|
|
316
|
+ Filter("types", models.ROLETYPES_GROUP).
|
|
|
317
|
+ Filter("company_id", newCompany.Id).
|
|
|
318
|
+ One(newRoleGroup)
|
|
|
319
|
+ if err == nil {
|
|
|
320
|
+ return newRoleGroup, nil
|
|
|
321
|
+ } else if err == orm.ErrNoRows {
|
|
|
322
|
+ //添加部门
|
|
|
323
|
+ newRoleGroup = &models.Role{
|
|
|
324
|
+ Types: models.ROLETYPES_GROUP,
|
|
|
325
|
+ CompanyId: newCompany.Id,
|
|
|
326
|
+ IsDefault: models.ROLE_DEFAULR,
|
|
|
327
|
+ Name: "主管组",
|
|
|
328
|
+ }
|
|
|
329
|
+ _, err = models.AddRole(newRoleGroup, o)
|
|
|
330
|
+ if err != nil {
|
|
|
331
|
+ log.Error("添加角色组数据失败:%s", err)
|
|
|
332
|
+ return nil, err
|
|
|
333
|
+ }
|
|
|
334
|
+ } else {
|
|
|
335
|
+ log.Error("查询角色组数据失败:%s", err)
|
|
|
336
|
+ return nil, err
|
|
|
337
|
+ }
|
|
|
338
|
+
|
|
|
339
|
+ return newRoleGroup, nil
|
|
|
340
|
+}
|
|
|
341
|
+
|
|
|
342
|
+func initRole(newCompany *models.Company, newRoleGroup *models.Role, o orm.Ormer) (*models.Role, error) {
|
|
|
343
|
+ var (
|
|
|
344
|
+ newRole = &models.Role{}
|
|
|
345
|
+ err error
|
|
|
346
|
+ )
|
|
|
347
|
+ err = o.QueryTable(&models.Role{}).
|
|
|
348
|
+ Filter("delete_at", 0).
|
|
|
349
|
+ Filter("is_default", models.ROLE_DEFAULR).
|
|
|
350
|
+ Filter("types", models.ROLETYPES_ROLE).
|
|
|
351
|
+ Filter("company_id", newCompany.Id).
|
|
|
352
|
+ One(newRole)
|
|
|
353
|
+ if err == nil {
|
|
|
354
|
+ return newRole, nil
|
|
|
355
|
+ } else if err == orm.ErrNoRows {
|
|
|
356
|
+ //添加部门
|
|
|
357
|
+ newRole = &models.Role{
|
|
|
358
|
+ Types: models.ROLETYPES_ROLE,
|
|
|
359
|
+ CompanyId: newCompany.Id,
|
|
|
360
|
+ IsDefault: models.ROLE_DEFAULR,
|
|
|
361
|
+ Name: "主管理员",
|
|
|
362
|
+ Pid: newRoleGroup.Id,
|
|
|
363
|
+ }
|
|
|
364
|
+ _, err = models.AddRole(newRole, o)
|
|
|
365
|
+ if err != nil {
|
|
|
366
|
+ log.Error("添加角色数据失败:%s", err)
|
|
|
367
|
+ return nil, err
|
|
|
368
|
+ }
|
|
|
369
|
+ } else {
|
|
|
370
|
+ log.Error("查询角色数据失败:%s", err)
|
|
|
371
|
+ return nil, err
|
|
|
372
|
+ }
|
|
|
373
|
+
|
|
|
374
|
+ return newRole, nil
|
|
|
375
|
+}
|
|
|
376
|
+
|
|
|
377
|
+func initUserRole(newUserCompany *models.UserCompany, newRole *models.Role, o orm.Ormer) (*models.UserRole, error) {
|
|
|
378
|
+ var (
|
|
|
379
|
+ newUserRole = &models.UserRole{}
|
|
|
380
|
+ err error
|
|
|
381
|
+ )
|
|
|
382
|
+ err = o.QueryTable(&models.UserRole{}).
|
|
|
383
|
+ Filter("enable_status", models.USER_ROLE_ENABLE_YES).
|
|
|
384
|
+ Filter("user_company_id", newUserCompany.Id).
|
|
|
385
|
+ Filter("role_id", newRole.Id).
|
|
|
386
|
+ One(newUserRole)
|
|
|
387
|
+ if err == nil {
|
|
|
388
|
+ return newUserRole, nil
|
|
|
389
|
+ } else if err == orm.ErrNoRows {
|
|
|
390
|
+ //添加UserRole
|
|
|
391
|
+ newUserRole = &models.UserRole{
|
|
|
392
|
+ RoleId: newRole.Id,
|
|
|
393
|
+ UserCompanyId: newUserCompany.Id,
|
|
|
394
|
+ CompanyId: newUserCompany.CompanyId,
|
|
|
395
|
+ UserId: newUserCompany.UserId,
|
|
|
396
|
+ }
|
|
|
397
|
+ _, err = models.AddUserRole(newUserRole, o)
|
|
|
398
|
+ if err != nil {
|
|
|
399
|
+ log.Error("添加user_role数据失败:%s", err)
|
|
|
400
|
+ return nil, err
|
|
|
401
|
+ }
|
|
|
402
|
+ } else {
|
|
|
403
|
+ log.Error("查询user_role数据失败:%s", err)
|
|
|
404
|
+ return nil, err
|
|
|
405
|
+ }
|
|
|
406
|
+
|
|
|
407
|
+ return newUserRole, nil
|
|
|
408
|
+} |