作者 tangxvhui

数据同步 添加公司

@@ -32,8 +32,7 @@ const ( @@ -32,8 +32,7 @@ const (
32 32
33 // AddDepartmentCharge insert a new DepartmentCharge into database and returns 33 // AddDepartmentCharge insert a new DepartmentCharge into database and returns
34 // last inserted Id on success. 34 // last inserted Id on success.
35 -func AddDepartmentCharge(m *DepartmentCharge) (id int64, err error) {  
36 - o := orm.NewOrm() 35 +func AddDepartmentCharge(m *DepartmentCharge, o orm.Ormer) (id int64, err error) {
37 id, err = o.Insert(m) 36 id, err = o.Insert(m)
38 return 37 return
39 } 38 }
@@ -6,16 +6,28 @@ import ( @@ -6,16 +6,28 @@ import (
6 "fmt" 6 "fmt"
7 "oppmg/common/log" 7 "oppmg/common/log"
8 "oppmg/models" 8 "oppmg/models"
  9 + "oppmg/protocol"
  10 + "oppmg/utils"
  11 + "time"
9 12
10 "github.com/astaxie/beego/orm" 13 "github.com/astaxie/beego/orm"
11 ) 14 )
12 15
  16 +type CompanyBase struct {
  17 + Id int64 `json:"id"` //id
  18 + Name string `json:"name"` //公司名称名称
  19 + AdminCompanyId int64 `json:"admin_company_id"`
  20 + Logo string `json:"logo"`
  21 +}
  22 +
13 // ModuleCompanytData 主管理平台发送过来的数据 23 // ModuleCompanytData 主管理平台发送过来的数据
14 type ModuleCompanytData struct { 24 type ModuleCompanytData struct {
15 - Id int64 `json:"id"` //id  
16 - Name string `json:"name"` //公司名称名称  
17 - //AdminId int64 `json:"admin_id"` //主管理员id  
18 - Logo string `json:"logo"` 25 + Company CompanyBase `json:"company"`
  26 + User ModuleEmployee `json:"user"`
  27 +}
  28 +
  29 +type CompanyCharge struct {
  30 + Id int64 `json:"id"`
19 Charge []int64 `json:"charge"` 31 Charge []int64 `json:"charge"`
20 } 32 }
21 33
@@ -23,7 +35,7 @@ var _ PlatformAction = ModuleCompanytData{} @@ -23,7 +35,7 @@ var _ PlatformAction = ModuleCompanytData{}
23 35
24 func (m ModuleCompanytData) DoAction(code string, jsondata []byte) error { 36 func (m ModuleCompanytData) DoAction(code string, jsondata []byte) error {
25 switch code { 37 switch code {
26 - case "edit": 38 + case "add":
27 var ( 39 var (
28 data ModuleCompanytData 40 data ModuleCompanytData
29 err error 41 err error
@@ -32,10 +44,21 @@ func (m ModuleCompanytData) DoAction(code string, jsondata []byte) error { @@ -32,10 +44,21 @@ func (m ModuleCompanytData) DoAction(code string, jsondata []byte) error {
32 if err != nil { 44 if err != nil {
33 return fmt.Errorf("数据解析失败:%s", err) 45 return fmt.Errorf("数据解析失败:%s", err)
34 } 46 }
35 - return UpdateCompanyData(data) 47 + return AddCompanyData(data)
  48 + case "edit":
  49 + // var (
  50 + // data ModuleCompanytData
  51 + // err error
  52 + // )
  53 + // err = json.Unmarshal(jsondata, &data)
  54 + // if err != nil {
  55 + // return fmt.Errorf("数据解析失败:%s", err)
  56 + // }
  57 + // return UpdateCompanyData(data)
  58 + return nil
36 case "setCompanyCharge": 59 case "setCompanyCharge":
37 var ( 60 var (
38 - data ModuleCompanytData 61 + data CompanyCharge
39 err error 62 err error
40 ) 63 )
41 err = json.Unmarshal(jsondata, &data) 64 err = json.Unmarshal(jsondata, &data)
@@ -50,7 +73,7 @@ func (m ModuleCompanytData) DoAction(code string, jsondata []byte) error { @@ -50,7 +73,7 @@ func (m ModuleCompanytData) DoAction(code string, jsondata []byte) error {
50 } 73 }
51 74
52 // SetCompanyCharge 更新公司那一级的部门的管理员 75 // SetCompanyCharge 更新公司那一级的部门的管理员
53 -func SetCompanyCharge(data ModuleCompanytData) error { 76 +func SetCompanyCharge(data CompanyCharge) error {
54 var ( 77 var (
55 err error 78 err error
56 companyData *models.Company 79 companyData *models.Company
@@ -89,21 +112,600 @@ func SetCompanyCharge(data ModuleCompanytData) error { @@ -89,21 +112,600 @@ func SetCompanyCharge(data ModuleCompanytData) error {
89 } 112 }
90 113
91 func UpdateCompanyData(data ModuleCompanytData) error { 114 func UpdateCompanyData(data ModuleCompanytData) error {
  115 + // var (
  116 + // companyData *models.Company
  117 + // err error
  118 + // )
  119 + // companyData, err = models.GetCompanyByUCenter(data.Id)
  120 + // if err != nil {
  121 + // log.Error("获取企业数据失败,user_center_id:%d ,err:%s", data.Id, err)
  122 + // return errors.New("获取企业数据失败")
  123 + // }
  124 + // companyData.Logo = data.Logo
  125 + // companyData.Name = data.Name
  126 + // err = models.UpdateCompanyById(companyData, []string{"Logo", "Name"})
  127 + // if err != nil {
  128 + // log.Error("更新公司数据失败:%s", err)
  129 + // return errors.New("更新公司数据失败")
  130 + // }
  131 + return nil
  132 +}
  133 +
  134 +//AddCompanyData 新增公司 初始化公司
  135 +func AddCompanyData(data ModuleCompanytData) error {
92 var ( 136 var (
93 - companyData *models.Company  
94 - err error 137 + newDeparment *models.Department
  138 + newRoleGroup *models.Role
  139 + newRole *models.Role
  140 + newUser *models.User
  141 + newCompany *models.Company
  142 + newUserCompany *models.UserCompany
  143 + err error
95 ) 144 )
96 - companyData, err = models.GetCompanyByUCenter(data.Id) 145 + o := orm.NewOrm()
  146 + o.Begin()
  147 + //处理用户数据
  148 + newUser, err = AddAdminUser(&data, o)
  149 + if err != nil {
  150 + o.Rollback()
  151 + log.Error("处理公司主管数据失败:%s", err)
  152 + return errors.New("处理公司主管数据失败")
  153 + }
  154 + //处理公司数据
  155 + newCompany, err = AddCompanyBase(&data, newUser, o)
  156 + if err != nil {
  157 + o.Rollback()
  158 + log.Error("处理公司数据失败:%s", err)
  159 + return errors.New("处理公司数据失败")
  160 + }
  161 + //处理评分配置数据 初始化
  162 + err = iniSysConfig(int(newCompany.Id), o)
  163 + if err != nil {
  164 + o.Rollback()
  165 + log.Error("处理评分配置数据失败:%s", err)
  166 + return errors.New("处理评分配置数据")
  167 + }
  168 + //添加user_company
  169 + newUserCompany, err = AddUserCompanyData(&data, newCompany, newUser, o)
  170 + if err != nil {
  171 + o.Rollback()
  172 + log.Error("处理user_company数据失败:%s", err)
  173 + return errors.New("处理user_company数据失败")
  174 + }
  175 + //添加部门
  176 + newDeparment, err = AddFirstDepartment(newCompany, newUserCompany, o)
  177 + if err != nil {
  178 + o.Rollback()
  179 + log.Error("处理部门数据失败:%s", err)
  180 + return errors.New("处理部门数据失败")
  181 + }
  182 + //添加用户部门数据
  183 + _, err = initUserDedaprtmet(newDeparment, newUserCompany, o)
  184 + if err != nil {
  185 + o.Rollback()
  186 + log.Error("处理user_department数据失败:%s", err)
  187 + return protocol.NewErrWithMessage("1")
  188 + }
  189 + //添加部门主管
  190 + err = AddFirstDepartmentCharge(newDeparment, newUserCompany, newCompany, o)
  191 + if err != nil {
  192 + o.Rollback()
  193 + log.Error("处理部门主管数据,err:%s", err)
  194 + return errors.New("处理部门主管数据")
  195 + }
  196 + //添加角色组
  197 + newRoleGroup, err = initRoleGroup(newCompany, o)
97 if err != nil { 198 if err != nil {
98 - log.Error("获取企业数据失败,user_center_id:%d ,err:%s", data.Id, err)  
99 - return errors.New("获取企业数据失败") 199 + o.Rollback()
  200 + log.Error("处理角色组数据失败:%s", err)
  201 + return protocol.NewErrWithMessage("1")
100 } 202 }
101 - companyData.Logo = data.Logo  
102 - companyData.Name = data.Name  
103 - err = models.UpdateCompanyById(companyData, []string{"Logo", "Name"}) 203 + //添加角色
  204 + newRole, err = initRole(newCompany, newRoleGroup, o)
104 if err != nil { 205 if err != nil {
105 - log.Error("更新公司数据失败:%s", err)  
106 - return errors.New("更新公司数据失败") 206 + o.Rollback()
  207 + log.Error("处理角色数据失败:%s", err)
  208 + return protocol.NewErrWithMessage("1")
107 } 209 }
  210 + //添加用户的角色
  211 + _, err = initUserRole(newUserCompany, newRole, o)
  212 + if err != nil {
  213 + o.Rollback()
  214 + log.Error("处理user_role数据失败:%s", err)
  215 + return protocol.NewErrWithMessage("1")
  216 + }
  217 + o.Commit()
108 return nil 218 return nil
109 } 219 }
  220 +
  221 +func AddAdminUser(centerCompany *ModuleCompanytData, o orm.Ormer) (*models.User, error) {
  222 + var (
  223 + err error
  224 + )
  225 + companyAdminData := centerCompany.User
  226 + //添加用户
  227 + newUser := &models.User{
  228 + Id: companyAdminData.Id,
  229 + Phone: companyAdminData.Phone,
  230 + NickName: companyAdminData.Name,
  231 + UserCenterId: companyAdminData.OpenId,
  232 + }
  233 + _, err = models.AddUser(newUser, o)
  234 + if err != nil {
  235 + log.Error("添加用户数据失败:%s", err)
  236 + return nil, err
  237 + }
  238 + return newUser, nil
  239 +}
  240 +
  241 +func AddCompanyBase(centerCompany *ModuleCompanytData, admininfo *models.User, o orm.Ormer) (*models.Company, error) {
  242 + var (
  243 + err error
  244 + )
  245 + companybase := centerCompany.Company
  246 + newCompany := &models.Company{
  247 + Id: companybase.Id,
  248 + Name: companybase.Name,
  249 + AdminId: admininfo.Id,
  250 + UserCenterId: companybase.AdminCompanyId,
  251 + Enable: models.COMPANY_ENABLE_NO,
  252 + }
  253 + _, err = models.AddCompany(newCompany, o)
  254 + if err != nil {
  255 + log.Error("添加公司数据失败:%s", err)
  256 + return nil, err
  257 + }
  258 + return nil, nil
  259 +}
  260 +
  261 +func AddUserCompanyData(centerCompany *ModuleCompanytData, newcompany *models.Company, newuser *models.User, o orm.Ormer) (*models.UserCompany, error) {
  262 + var (
  263 + err error
  264 + )
  265 + adminUserData := centerCompany.User
  266 + uc := &models.UserCompany{
  267 + UserId: newuser.Id,
  268 + CompanyId: newcompany.Id,
  269 + Enable: models.USERCOMPANY_ENABLE_YES,
  270 + ChargeStatus: models.USERCOMPANY_CHARGE_YES,
  271 + Id: adminUserData.Id,
  272 + NickName: adminUserData.Name,
  273 + OpenId: adminUserData.OpenId,
  274 + Sex: adminUserData.Sex,
  275 + JobNum: adminUserData.JobNum,
  276 + Phone: adminUserData.Phone,
  277 + PrivatePhone: adminUserData.PrivatePhone,
  278 + Email: adminUserData.Email,
  279 + ExtensionNum: adminUserData.ExtensionNum,
  280 + Workspace: adminUserData.WorkSpace,
  281 + IsBusiness: adminUserData.IsBusiness,
  282 + Avatar: adminUserData.Avatar,
  283 + Remarks: adminUserData.Remarks,
  284 + // AdminType: adminUserData.AdminType,
  285 + ExtraText: adminUserData.ExtraText,
  286 + }
  287 + _, err = models.AddUserCompany(uc, o)
  288 + if err != nil {
  289 + log.Error("添加user_company数据失败:%s", err)
  290 + return nil, err
  291 + }
  292 + return uc, nil
  293 +}
  294 +
  295 +//AddFirstDepartment 创建公司的一级部门
  296 +func AddFirstDepartment(newCompany *models.Company, newusercompany *models.UserCompany, o orm.Ormer) (*models.Department, error) {
  297 + var (
  298 + newDepartment = &models.Department{}
  299 + err error
  300 + )
  301 + err = o.QueryTable(&models.Department{}).
  302 + Filter("company_id", newCompany.Id).
  303 + Filter("is_top", 1).
  304 + One(newDepartment)
  305 + if err == nil {
  306 + return newDepartment, nil
  307 + } else if err == orm.ErrNoRows {
  308 + //添加部门
  309 + newDepartment = &models.Department{
  310 + CompanyId: newCompany.Id,
  311 + Name: newCompany.Name,
  312 + IsTop: 1,
  313 + ParentId: 0,
  314 + Relation: fmt.Sprint(utils.GenerateIDBySonyflake()),
  315 + }
  316 + _, err = models.AddDepartment(newDepartment, o)
  317 + if err != nil {
  318 + log.Error("添加部门数据失败:%s", err)
  319 + return nil, err
  320 + }
  321 + newDepartment.SetRelation(nil)
  322 + err = models.UpdateDepartmentById(newDepartment, []string{"Relation"}, o)
  323 + if err != nil {
  324 + log.Error("更新部门关系树数据失败:%s", err)
  325 + return nil, err
  326 + }
  327 + } else {
  328 + log.Error("查询部门数据失败:%s", err)
  329 + return nil, err
  330 + }
  331 +
  332 + return newDepartment, nil
  333 +}
  334 +
  335 +func AddFirstDepartmentCharge(newDepartment *models.Department, newUserCompany *models.UserCompany, newCompany *models.Company, o orm.Ormer) error {
  336 + var (
  337 + err error
  338 + )
  339 + departmentCharg := &models.DepartmentCharge{
  340 + CompanyId: newCompany.Id,
  341 + DepartmentId: newDepartment.Id,
  342 + UserCompanyId: newUserCompany.Id,
  343 + }
  344 + _, err = models.AddDepartmentCharge(departmentCharg, o)
  345 + if err != nil {
  346 + log.Error("设置部门主管失败,err:%s", err)
  347 + return err
  348 + }
  349 + newDepartment.SetManages([]int64{newUserCompany.Id})
  350 + err = models.UpdateDepartmentById(newDepartment, []string{"Manages"}, o)
  351 + if err != nil {
  352 + log.Error("设置部门主管失败,err:%s", err)
  353 + return err
  354 + }
  355 + return nil
  356 +}
  357 +
  358 +func UpdateCompany(centerCompany *ModuleCompanytData, admininfo *models.User, o orm.Ormer) (*models.Company, error) {
  359 + var (
  360 + err error
  361 + newCompany = &models.Company{}
  362 + )
  363 + err = o.QueryTable(&models.Company{}).
  364 + Filter("user_center_id", centerCompany.Company.AdminCompanyId).
  365 + One(newCompany)
  366 + if err == nil {
  367 + _, err = o.QueryTable(&models.Company{}).
  368 + Filter("user_center_id", centerCompany.Company.AdminCompanyId).
  369 + Update(orm.Params{
  370 + "name": centerCompany.Company.Name,
  371 + "admin_id": admininfo.Id,
  372 + "delete_at": 0,
  373 + })
  374 + if err != nil {
  375 + log.Error("更新公司数据失败:%s", err)
  376 + return nil, err
  377 + }
  378 + } else {
  379 + log.Error("查询公司数据失败:%s", err)
  380 + return nil, err
  381 + }
  382 + return newCompany, nil
  383 +}
  384 +
  385 +// func initAdminUser(centerCompany *ModuleCompanytData, o orm.Ormer) (*models.User, error) {
  386 +// var (
  387 +// err error
  388 +// newUser = &models.User{}
  389 +// )
  390 +// companyAdminData := centerCompany.User
  391 +// err = o.QueryTable(&models.User{}).
  392 +// Filter("phone", companyAdminData.Phone).
  393 +// Filter("delete_at", 0).
  394 +// One(newUser)
  395 +// if err == nil {
  396 +// // 更新数据
  397 +// _, err = o.QueryTable(&models.User{}).
  398 +// Filter("user_center_id", companyAdminData.OpenId).
  399 +// Filter("delete_at", 0).
  400 +// Update(orm.Params{
  401 +// "nick_name": companyAdminData.Name,
  402 +// "phone": companyAdminData.Phone,
  403 +// "enable_status": models.USER_ENABLE_YES,
  404 +// })
  405 +// if err != nil {
  406 +// log.Error("数据操作失败:%s", err)
  407 +// return nil, err
  408 +// }
  409 +// } else if err == orm.ErrNoRows {
  410 +// var (
  411 +// ucenterUser = &ucenter.ResponseAddUser{}
  412 +// err error
  413 +// )
  414 +// ucenterUser, err = ucenter.RequestUCenterAddUser(companyAdminData.Phone, companyAdminData.Name, "")
  415 +// if err != nil {
  416 +// log.Error("获取统一用户中心数据失败 :%s", err)
  417 +// return nil, err
  418 +// }
  419 +// //添加用户
  420 +// newUser = &models.User{
  421 +// Id: companyAdminData.Id,
  422 +// Phone: companyAdminData.Phone,
  423 +// NickName: companyAdminData.Name,
  424 +// Icon: ucenterUser.Data.Avatar,
  425 +// Accid: ucenterUser.Data.Accid,
  426 +// UserCenterId: ucenterUser.Data.Id,
  427 +// CsAccount: ucenterUser.Data.CustomerAccount,
  428 +// }
  429 +// _, err = models.AddUser(newUser, o)
  430 +// if err != nil {
  431 +// log.Error("添加用户数据失败:%s", err)
  432 +// return nil, err
  433 +// }
  434 +// } else {
  435 +// log.Error("查询用户数据失败:%s", err)
  436 +// return nil, err
  437 +// }
  438 +// return newUser, nil
  439 +// }
  440 +
  441 +// func initUserCompany(newcompany *models.Company, newuser *models.User, o orm.Ormer) (*models.UserCompany, error) {
  442 +// var (
  443 +// err error
  444 +// uc = &models.UserCompany{}
  445 +// )
  446 +// err = o.QueryTable(&models.UserCompany{}).
  447 +// Filter("user_id", newuser.Id).
  448 +// Filter("company_id", newcompany.Id).
  449 +// Filter("delete_at", 0).
  450 +// One(uc)
  451 +// if err == nil {
  452 +// _, err = o.QueryTable(&models.UserCompany{}).
  453 +// Filter("id", uc.Id).
  454 +// Update(orm.Params{
  455 +// "enable": models.USERCOMPANY_ENABLE_YES,
  456 +// })
  457 +// if err != nil {
  458 +// log.Error("更新user_company数据失败:%s", err)
  459 +// return nil, err
  460 +// }
  461 +// } else if err == orm.ErrNoRows {
  462 +// uc = &models.UserCompany{
  463 +// UserId: newuser.Id,
  464 +// CompanyId: newcompany.Id,
  465 +// OpenId: newuser.UserCenterId,
  466 +// Enable: models.USERCOMPANY_ENABLE_YES,
  467 +// ChargeStatus: models.USERCOMPANY_CHARGE_YES,
  468 +// }
  469 +// _, err = models.AddUserCompany(uc, o)
  470 +// if err != nil {
  471 +// log.Error("添加user_company数据失败:%s", err)
  472 +// return nil, err
  473 +// }
  474 +// } else {
  475 +// log.Error("获取user_company数据失败:%s", err)
  476 +// return nil, err
  477 +// }
  478 +// return uc, nil
  479 +// }
  480 +
  481 +// func initDepartment(newCompany *models.Company, newusercompany *models.UserCompany, o orm.Ormer) (*models.Department, error) {
  482 +// var (
  483 +// newDepartment = &models.Department{}
  484 +// err error
  485 +// )
  486 +// err = o.QueryTable(&models.Department{}).
  487 +// Filter("company_id", newCompany.Id).
  488 +// Filter("is_top", 1).
  489 +// One(newDepartment)
  490 +// if err == nil {
  491 +// newDepartment.Name = newCompany.Name
  492 +// err = models.UpdateDepartmentById(newDepartment, []string{"Name"}, o)
  493 +// if err != nil {
  494 +// log.Error("更新部门关系树数据失败:%s", err)
  495 +// return nil, err
  496 +// }
  497 +// return newDepartment, nil
  498 +// } else if err == orm.ErrNoRows {
  499 +// //添加部门
  500 +// newDepartment = &models.Department{
  501 +// CompanyId: newCompany.Id,
  502 +// Name: newCompany.Name,
  503 +// IsTop: 1,
  504 +// ParentId: 0,
  505 +// Relation: fmt.Sprint(utils.GenerateIDBySonyflake()),
  506 +// }
  507 +// newDepartment.SetManages([]int64{newusercompany.Id})
  508 +// _, err = models.AddDepartment(newDepartment, o)
  509 +// if err != nil {
  510 +// log.Error("添加部门数据失败:%s", err)
  511 +// return nil, err
  512 +// }
  513 +// newDepartment.SetRelation(nil)
  514 +// err = models.UpdateDepartmentById(newDepartment, []string{"Relation"}, o)
  515 +// if err != nil {
  516 +// log.Error("更新部门关系树数据失败:%s", err)
  517 +// return nil, err
  518 +// }
  519 +// } else {
  520 +// log.Error("查询部门数据失败:%s", err)
  521 +// return nil, err
  522 +// }
  523 +
  524 +// return newDepartment, nil
  525 +// }
  526 +
  527 +//initUserDedaprtmet 设置用户和部门关系
  528 +func initUserDedaprtmet(newDepartment *models.Department, newUserCompany *models.UserCompany, o orm.Ormer) (*models.UserDepartment, error) {
  529 + var (
  530 + newUserDepartment = &models.UserDepartment{}
  531 + err error
  532 + )
  533 + err = o.QueryTable(&models.UserDepartment{}).
  534 + Filter("department_id", newDepartment.Id).
  535 + Filter("user_company_id", newUserCompany.Id).
  536 + Filter("enable_status", models.USER_DEPARTMENT_ENABLE_YES).
  537 + One(newUserDepartment)
  538 + if err == nil {
  539 + return newUserDepartment, nil
  540 + } else if err == orm.ErrNoRows {
  541 + //添加部门
  542 + newUserDepartment = &models.UserDepartment{
  543 + UserCompanyId: newUserCompany.Id,
  544 + //UserId: newUserCompany.UserId,
  545 + CompanyId: newUserCompany.CompanyId,
  546 + DepartmentId: newDepartment.Id,
  547 + }
  548 + _, err = models.AddUserDepartment(newUserDepartment, o)
  549 + if err != nil {
  550 + log.Error("添加部门数据失败:%s", err)
  551 + return nil, err
  552 + }
  553 + } else {
  554 + log.Error("查询部门数据失败:%s", err)
  555 + return nil, err
  556 + }
  557 +
  558 + return newUserDepartment, nil
  559 +}
  560 +
  561 +//initRoleGroup 设置初始的角色组
  562 +func initRoleGroup(newCompany *models.Company, o orm.Ormer) (*models.Role, error) {
  563 + var (
  564 + newRoleGroup = &models.Role{}
  565 + err error
  566 + )
  567 + err = o.QueryTable(&models.Role{}).
  568 + Filter("delete_at", 0).
  569 + Filter("is_default", models.ROLE_DEFAULR).
  570 + Filter("types", models.ROLETYPES_GROUP).
  571 + Filter("company_id", newCompany.Id).
  572 + One(newRoleGroup)
  573 + if err == nil {
  574 + return newRoleGroup, nil
  575 + } else if err == orm.ErrNoRows {
  576 + //添加部角色组
  577 + newRoleGroup = &models.Role{
  578 + Types: models.ROLETYPES_GROUP,
  579 + CompanyId: newCompany.Id,
  580 + IsDefault: models.ROLE_DEFAULR,
  581 + Name: "管理员",
  582 + }
  583 + _, err = models.AddRole(newRoleGroup, o)
  584 + if err != nil {
  585 + log.Error("添加角色组数据失败:%s", err)
  586 + return nil, err
  587 + }
  588 + } else {
  589 + log.Error("查询角色组数据失败:%s", err)
  590 + return nil, err
  591 + }
  592 +
  593 + return newRoleGroup, nil
  594 +}
  595 +
  596 +//initRole 初始化公司的角色
  597 +func initRole(newCompany *models.Company, newRoleGroup *models.Role, o orm.Ormer) (*models.Role, error) {
  598 + var (
  599 + newRole = &models.Role{}
  600 + err error
  601 + )
  602 + err = o.QueryTable(&models.Role{}).
  603 + Filter("delete_at", 0).
  604 + Filter("is_default", models.ROLE_DEFAULR).
  605 + Filter("types", models.ROLETYPES_ROLE).
  606 + Filter("company_id", newCompany.Id).
  607 + One(newRole)
  608 + if err == nil {
  609 + return newRole, nil
  610 + } else if err == orm.ErrNoRows {
  611 + //添加部门
  612 + newRole = &models.Role{
  613 + Types: models.ROLETYPES_ROLE,
  614 + CompanyId: newCompany.Id,
  615 + IsDefault: models.ROLE_DEFAULR,
  616 + Name: "主管理员",
  617 + Pid: newRoleGroup.Id,
  618 + }
  619 + _, err = models.AddRole(newRole, o)
  620 + if err != nil {
  621 + log.Error("添加角色数据失败:%s", err)
  622 + return nil, err
  623 + }
  624 + } else {
  625 + log.Error("查询角色数据失败:%s", err)
  626 + return nil, err
  627 + }
  628 +
  629 + return newRole, nil
  630 +}
  631 +
  632 +//initUserRole 初始化第一个用户和角色的关系
  633 +func initUserRole(newUserCompany *models.UserCompany, newRole *models.Role, o orm.Ormer) (*models.UserRole, error) {
  634 + var (
  635 + newUserRole = &models.UserRole{}
  636 + err error
  637 + )
  638 + err = o.QueryTable(&models.UserRole{}).
  639 + Filter("enable_status", models.USER_ROLE_ENABLE_YES).
  640 + Filter("user_company_id", newUserCompany.Id).
  641 + Filter("role_id", newRole.Id).
  642 + One(newUserRole)
  643 + if err == nil {
  644 + return newUserRole, nil
  645 + } else if err == orm.ErrNoRows {
  646 + //添加UserRole
  647 + newUserRole = &models.UserRole{
  648 + RoleId: newRole.Id,
  649 + UserCompanyId: newUserCompany.Id,
  650 + CompanyId: newUserCompany.CompanyId,
  651 + }
  652 + _, err = models.AddUserRole(newUserRole, o)
  653 + if err != nil {
  654 + log.Error("添加user_role数据失败:%s", err)
  655 + return nil, err
  656 + }
  657 + } else {
  658 + log.Error("查询user_role数据失败:%s", err)
  659 + return nil, err
  660 + }
  661 +
  662 + return newUserRole, nil
  663 +}
  664 +
  665 +//iniSysConfig 初始化公司的系统配置
  666 +func iniSysConfig(companyid int, o orm.Ormer) error {
  667 + var (
  668 + m *models.SysConfig
  669 + err error
  670 + )
  671 + m, err = models.GetSysConfigByCompanyId(companyid, models.KeyScore)
  672 + if err == nil {
  673 + return nil
  674 + }
  675 + v := protocol.ScoreConfig{
  676 + DiscoveryScore: &protocol.DiscoveryScore{
  677 + BasicFactor: 1,
  678 + ExtraFactor: 1,
  679 + ValueFactor: 1,
  680 + },
  681 + SumScore: &protocol.SumScore{
  682 + DiscoveryFactor: 1,
  683 + CatchFactor: 1,
  684 + },
  685 + BasicScore: &protocol.ScoreRange{
  686 + Max: 10000,
  687 + Min: 0,
  688 + Step: 1,
  689 + },
  690 + ExtraScore: &protocol.ScoreRange{
  691 + Max: 10000,
  692 + Min: 0,
  693 + Step: 1,
  694 + },
  695 + ValueScore: &protocol.ScoreRange{
  696 + Max: 10000,
  697 + Min: 0,
  698 + Step: 1,
  699 + },
  700 + }
  701 + bt, _ := json.Marshal(v)
  702 + m = &models.SysConfig{
  703 + Key: models.KeyScore,
  704 + CompanyId: companyid,
  705 + CreateAt: time.Now(),
  706 + UpdateAt: time.Now(),
  707 + Content: string(bt),
  708 + }
  709 + _, err = models.AddSysConfig(m, o)
  710 + return err
  711 +}