作者 yangfu

Merge branch 'dev' of http://gitlab.fjmaimaimai.com/mmm-go/oppmg into dev

@@ -12,8 +12,8 @@ AdminPort = 8088 @@ -12,8 +12,8 @@ AdminPort = 8088
12 ##数据库连接 12 ##数据库连接
13 # sqlconn ="${MYSQL_CONN||root:sutianxia2015@tcp(115.29.205.99:3306)/opportunity?charset=utf8&loc=Asia%2FShanghai}" 13 # sqlconn ="${MYSQL_CONN||root:sutianxia2015@tcp(115.29.205.99:3306)/opportunity?charset=utf8&loc=Asia%2FShanghai}"
14 mysql_user = "${MYSQL_USER||root}" 14 mysql_user = "${MYSQL_USER||root}"
15 -mysql_password = "${MYSQL_PASSWORD||sutianxia2015}"  
16 -mysql_host = "${MYSQL_HOST||115.29.205.99}" 15 +mysql_password = "${MYSQL_PASSWORD||root}"
  16 +mysql_host = "${MYSQL_HOST||127.0.0.1}"
17 mysql_port = "${MYSQL_PORT||3306}" 17 mysql_port = "${MYSQL_PORT||3306}"
18 mysql_db_name = "${MYSQL_DB_NAME||opportunity}" 18 mysql_db_name = "${MYSQL_DB_NAME||opportunity}"
19 ##redis相关配置 19 ##redis相关配置
@@ -390,5 +390,23 @@ func (c *CompanyController) UserList() { @@ -390,5 +390,23 @@ func (c *CompanyController) UserList() {
390 return 390 return
391 } 391 }
392 392
393 -//Regist  
394 -// func 393 +//InitCompany 初始化企业
  394 +//@router /ucenter/company [post]
  395 +func (c *CompanyController) InitCompany() {
  396 + var msg *protocol.ResponseMessage
  397 + defer func() {
  398 + c.ResposeJson(msg)
  399 + }()
  400 + type Parameter struct {
  401 + CompanyId int64 `json:"company_id"`
  402 + }
  403 + var param Parameter
  404 + if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
  405 + log.Error("json 解析失败 err:%s", err)
  406 + msg = protocol.BadRequestParam("1")
  407 + return
  408 + }
  409 + err := servecompany.InitCompanyInfo(param.CompanyId)
  410 + msg = protocol.NewReturnResponse(nil, err)
  411 + return
  412 +}
@@ -54,8 +54,17 @@ func init() { @@ -54,8 +54,17 @@ func init() {
54 54
55 // AddCompany insert a new Company into database and returns 55 // AddCompany insert a new Company into database and returns
56 // last inserted Id on success. 56 // last inserted Id on success.
57 -func AddCompany(m *Company) (id int64, err error) {  
58 - o := orm.NewOrm() 57 +func AddCompany(m *Company, om ...orm.Ormer) (id int64, err error) {
  58 + var o orm.Ormer
  59 + if len(om) > 0 {
  60 + o = om[0]
  61 + } else {
  62 + o = orm.NewOrm()
  63 + }
  64 + m.CreateAt = time.Now()
  65 + m.UpdateAt = time.Now()
  66 + m.Enable = COMPANY_ENABLE_YES
  67 + m.DeleteAt = time.Unix(0, 0)
59 id, err = o.Insert(m) 68 id, err = o.Insert(m)
60 return 69 return
61 } 70 }
@@ -122,6 +122,8 @@ func AddDepartment(m *Department, om ...orm.Ormer) (id int64, err error) { @@ -122,6 +122,8 @@ func AddDepartment(m *Department, om ...orm.Ormer) (id int64, err error) {
122 } else { 122 } else {
123 o = orm.NewOrm() 123 o = orm.NewOrm()
124 } 124 }
  125 + m.CreateAt = time.Now()
  126 + m.UpdateAt = time.Now()
125 m.DeleteAt = time.Unix(0, 0) 127 m.DeleteAt = time.Unix(0, 0)
126 id, err = o.Insert(m) 128 id, err = o.Insert(m)
127 return 129 return
@@ -147,14 +149,10 @@ func UpdateDepartmentById(m *Department, col []string, om ...orm.Ormer) (err err @@ -147,14 +149,10 @@ func UpdateDepartmentById(m *Department, col []string, om ...orm.Ormer) (err err
147 } else { 149 } else {
148 o = orm.NewOrm() 150 o = orm.NewOrm()
149 } 151 }
150 - v := Department{Id: m.Id}  
151 - // ascertain id exists in the database  
152 - if err = o.Read(&v); err == nil {  
153 var num int64 152 var num int64
154 m.UpdateAt = time.Now() 153 m.UpdateAt = time.Now()
155 if num, err = o.Update(m, col...); err == nil { 154 if num, err = o.Update(m, col...); err == nil {
156 - fmt.Println("Number of records updated in database:", num)  
157 - } 155 + _ = num
158 } 156 }
159 return 157 return
160 } 158 }
@@ -76,8 +76,13 @@ func (t *Role) ValidatePid() (*Role, error) { @@ -76,8 +76,13 @@ func (t *Role) ValidatePid() (*Role, error) {
76 76
77 // AddRole insert a new Role into database and returns 77 // AddRole insert a new Role into database and returns
78 // last inserted Id on success. 78 // last inserted Id on success.
79 -func AddRole(m *Role) (id int64, err error) {  
80 - o := orm.NewOrm() 79 +func AddRole(m *Role, om ...orm.Ormer) (id int64, err error) {
  80 + var o orm.Ormer
  81 + if len(om) > 0 {
  82 + o = om[0]
  83 + } else {
  84 + o = orm.NewOrm()
  85 + }
81 m.CreateAt = time.Now() 86 m.CreateAt = time.Now()
82 m.DeleteAt = time.Unix(0, 0) 87 m.DeleteAt = time.Unix(0, 0)
83 m.UpdateAt = time.Now() 88 m.UpdateAt = time.Now()
@@ -8,7 +8,7 @@ import ( @@ -8,7 +8,7 @@ import (
8 ) 8 )
9 9
10 type User struct { 10 type User struct {
11 - Id int64 `orm:"column(id);pk" description:"用户id"` 11 + Id int64 `orm:"column(id)" description:"用户id"`
12 NickName string `orm:"column(nick_name);size(100)" description:"昵称"` 12 NickName string `orm:"column(nick_name);size(100)" description:"昵称"`
13 Phone string `orm:"column(phone);size(40)" description:"手机号码"` 13 Phone string `orm:"column(phone);size(40)" description:"手机号码"`
14 Passwd string `orm:"column(passwd);size(128)" description:"密码"` 14 Passwd string `orm:"column(passwd);size(128)" description:"密码"`
@@ -20,6 +20,7 @@ type User struct { @@ -20,6 +20,7 @@ type User struct {
20 CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now_add" description:"创建时间"` 20 CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now_add" description:"创建时间"`
21 EnableStatus int8 `orm:"column(enable_status)" description:"是否有效"` 21 EnableStatus int8 `orm:"column(enable_status)" description:"是否有效"`
22 DeleteAt time.Time `orm:"column(delete_at);type(timestamp)"` 22 DeleteAt time.Time `orm:"column(delete_at);type(timestamp)"`
  23 + UserCenterId int64 `orm:"column(user_center_id)"`
23 } 24 }
24 25
25 func (t *User) TableName() string { 26 func (t *User) TableName() string {
@@ -55,8 +55,15 @@ func init() { @@ -55,8 +55,15 @@ func init() {
55 55
56 // AddUserDepartment insert a new UserDepartment into database and returns 56 // AddUserDepartment insert a new UserDepartment into database and returns
57 // last inserted Id on success. 57 // last inserted Id on success.
58 -func AddUserDepartment(m *UserDepartment) (id int64, err error) {  
59 - o := orm.NewOrm() 58 +func AddUserDepartment(m *UserDepartment, om ...orm.Ormer) (id int64, err error) {
  59 + var o orm.Ormer
  60 + if len(om) > 0 {
  61 + o = om[0]
  62 + } else {
  63 + o = orm.NewOrm()
  64 + }
  65 + m.CreateTime = time.Now()
  66 + m.EnableStatus = USER_DEPARTMENT_ENABLE_YES
60 id, err = o.Insert(m) 67 id, err = o.Insert(m)
61 return 68 return
62 } 69 }
@@ -8,7 +8,7 @@ import ( @@ -8,7 +8,7 @@ import (
8 ) 8 )
9 9
10 type UserPosition struct { 10 type UserPosition struct {
11 - Id int64 `orm:"column(id);pk" description:"唯一键值"` 11 + Id int64 `orm:"column(id)" description:"唯一键值"`
12 UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"` 12 UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"`
13 PositionId int64 `orm:"column(position_id)" description:"表position.id 职位编号"` 13 PositionId int64 `orm:"column(position_id)" description:"表position.id 职位编号"`
14 CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"` 14 CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
@@ -7,7 +7,7 @@ import ( @@ -7,7 +7,7 @@ import (
7 ) 7 )
8 8
9 type UserRole struct { 9 type UserRole struct {
10 - Id int `orm:"column(id);pk"` 10 + Id int `orm:"column(id)"`
11 RoleId int64 `orm:"column(role_id)"` 11 RoleId int64 `orm:"column(role_id)"`
12 UserId int64 `orm:"column(user_id)"` 12 UserId int64 `orm:"column(user_id)"`
13 EnableStatus int8 `orm:"column(enable_status)" description:"是否有效"` 13 EnableStatus int8 `orm:"column(enable_status)" description:"是否有效"`
@@ -53,8 +53,14 @@ func init() { @@ -53,8 +53,14 @@ func init() {
53 53
54 // AddUserRole insert a new UserRole into database and returns 54 // AddUserRole insert a new UserRole into database and returns
55 // last inserted Id on success. 55 // last inserted Id on success.
56 -func AddUserRole(m *UserRole) (id int64, err error) {  
57 - o := orm.NewOrm() 56 +func AddUserRole(m *UserRole, om ...orm.Ormer) (id int64, err error) {
  57 + var o orm.Ormer
  58 + if len(om) == 0 {
  59 + o = om[0]
  60 + } else {
  61 + o = orm.NewOrm()
  62 + }
  63 + m.EnableStatus = USER_ROLE_ENABLE_YES
58 id, err = o.Insert(m) 64 id, err = o.Insert(m)
59 return 65 return
60 } 66 }
@@ -18,6 +18,7 @@ func init() { @@ -18,6 +18,7 @@ func init() {
18 beego.NSRouter("/add", &controllers.CompanyController{}, "post:DepartmentAdd"), 18 beego.NSRouter("/add", &controllers.CompanyController{}, "post:DepartmentAdd"),
19 beego.NSRouter("/edit", &controllers.CompanyController{}, "post:DepartmentUpdate"), 19 beego.NSRouter("/edit", &controllers.CompanyController{}, "post:DepartmentUpdate"),
20 beego.NSRouter("/delete", &controllers.CompanyController{}, "post:DepartmentDelete"), 20 beego.NSRouter("/delete", &controllers.CompanyController{}, "post:DepartmentDelete"),
  21 + // beego.NSRouter("/user", &controllers.CompanyController{}, "post:DepartmentDelete"),
21 ), 22 ),
22 beego.NSNamespace("position", 23 beego.NSNamespace("position",
23 beego.NSRouter("/list", &controllers.CompanyController{}, "post:PositionList"), 24 beego.NSRouter("/list", &controllers.CompanyController{}, "post:PositionList"),
@@ -73,8 +74,13 @@ func init() { @@ -73,8 +74,13 @@ func init() {
73 beego.NSBefore(middleware.LogRequestData), 74 beego.NSBefore(middleware.LogRequestData),
74 beego.NSRouter("/login", &controllers.AuthController{}, "post:Login"), 75 beego.NSRouter("/login", &controllers.AuthController{}, "post:Login"),
75 ) 76 )
  77 + nsUcenter := beego.NewNamespace("/ucenter",
  78 + beego.NSBefore(middleware.LogRequestData),
  79 + beego.NSRouter("/company", &controllers.CompanyController{}, "post:InitCompany"),
  80 + )
76 beego.AddNamespace(nsV1) 81 beego.AddNamespace(nsV1)
77 beego.AddNamespace(nsAuth) 82 beego.AddNamespace(nsAuth)
  83 + beego.AddNamespace(nsUcenter)
78 } 84 }
79 85
80 //routerPermission 路由对应的权限 86 //routerPermission 路由对应的权限
  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 +}