|
|
package company
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"oppmg/common/log"
|
|
|
"oppmg/models"
|
|
|
"oppmg/protocol"
|
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
)
|
|
|
|
|
|
type CenterCompanyInfo struct {
|
|
|
CompanyId int64
|
|
|
CompanyName string
|
|
|
AdminId int64
|
|
|
AdminAccount string
|
|
|
AdminName string
|
|
|
}
|
|
|
|
|
|
func GetCenterCompanyInfo(companyid int64) (CenterCompanyInfo, error) {
|
|
|
|
|
|
//TODO 调用统一用户中心的数据
|
|
|
data := CenterCompanyInfo{
|
|
|
CompanyId: 9999 + companyid,
|
|
|
CompanyName: "调试用公司数据",
|
|
|
AdminId: 9999 + companyid,
|
|
|
AdminAccount: "调试用自己修改",
|
|
|
AdminName: "调试用自己修改",
|
|
|
}
|
|
|
return data, nil
|
|
|
}
|
|
|
|
|
|
//InitCompanyInfo 初始化公司
|
|
|
//@uCompanyid 从统一用户中心获取的公司id companyid,
|
|
|
func InitCompanyInfo(uCompanyid int64) error {
|
|
|
var (
|
|
|
centerCompany CenterCompanyInfo
|
|
|
err error
|
|
|
)
|
|
|
centerCompany, err = GetCenterCompanyInfo(uCompanyid)
|
|
|
if err != nil {
|
|
|
return protocol.NewErrWithMessage("10051")
|
|
|
}
|
|
|
var (
|
|
|
newDeparment *models.Department
|
|
|
//newUserDepart = &models.UserDepartment{}
|
|
|
newRoleGroup *models.Role
|
|
|
newRole *models.Role
|
|
|
newUser *models.User
|
|
|
newCompany *models.Company
|
|
|
newUserCompany *models.UserCompany
|
|
|
)
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
//处理用户数据
|
|
|
newUser, err = initAdminUser(centerCompany, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("处理公司主管数据失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//处理公司数据
|
|
|
newCompany, err = initCompany(centerCompany, newUser, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("处理公司数据失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//添加user_company
|
|
|
newUserCompany, err = initUserCompany(newCompany, newUser, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("处理user_company数据失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//添加部门
|
|
|
newDeparment, err = initDepartment(newCompany, newUserCompany, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("处理部门数据失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//添加用户部门数据
|
|
|
_, err = initUserDedaprtmet(newDeparment, newUserCompany, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("处理user_department数据失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//添加角色组
|
|
|
newRoleGroup, err = initRoleGroup(newCompany, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("处理角色组数据失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//添加角色
|
|
|
newRole, err = initRole(newCompany, newRoleGroup, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("处理角色数据失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//添加用户的角色
|
|
|
_, err = initUserRole(newUserCompany, newRole, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("处理user_role数据失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
o.Commit()
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func initCompany(centerCompany CenterCompanyInfo, admininfo *models.User, o orm.Ormer) (*models.Company, error) {
|
|
|
var (
|
|
|
err error
|
|
|
newCompany = &models.Company{}
|
|
|
)
|
|
|
err = o.QueryTable(&models.Company{}).
|
|
|
Filter("user_center_id", centerCompany.CompanyId).
|
|
|
One(newCompany)
|
|
|
if err == nil {
|
|
|
_, err = o.QueryTable(&models.Company{}).
|
|
|
Filter("user_center_id", centerCompany.CompanyId).
|
|
|
Update(orm.Params{
|
|
|
"name": centerCompany.CompanyName,
|
|
|
"admin_id": admininfo.Id,
|
|
|
"delete_at": 0,
|
|
|
"enable": models.COMPANY_ENABLE_YES,
|
|
|
})
|
|
|
if err != nil {
|
|
|
log.Error("更新公司数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
} else if err == orm.ErrNoRows {
|
|
|
newCompany = &models.Company{
|
|
|
Name: centerCompany.CompanyName,
|
|
|
AdminId: admininfo.Id,
|
|
|
UserCenterId: centerCompany.CompanyId,
|
|
|
}
|
|
|
_, err = models.AddCompany(newCompany, o)
|
|
|
if err != nil {
|
|
|
log.Error("添加公司数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
log.Error("查询公司数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
return newCompany, nil
|
|
|
}
|
|
|
|
|
|
func initAdminUser(centerCompany CenterCompanyInfo, o orm.Ormer) (*models.User, error) {
|
|
|
var (
|
|
|
err error
|
|
|
newUser = &models.User{}
|
|
|
)
|
|
|
err = o.QueryTable(&models.User{}).
|
|
|
Filter("user_center_id", centerCompany.AdminId).
|
|
|
Filter("delete_at", 0).
|
|
|
One(newUser)
|
|
|
if err == nil {
|
|
|
// 更新数据
|
|
|
_, err = o.QueryTable(&models.User{}).
|
|
|
Filter("user_center_id", centerCompany.CompanyId).
|
|
|
Filter("delete_at", 0).
|
|
|
Update(orm.Params{
|
|
|
"nick_name": centerCompany.AdminName,
|
|
|
"phone": centerCompany.AdminAccount,
|
|
|
"enable_status": models.USER_ENABLE_YES,
|
|
|
})
|
|
|
if err != nil {
|
|
|
log.Error("数据操作失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
} else if err == orm.ErrNoRows {
|
|
|
//添加用户
|
|
|
newUser = &models.User{
|
|
|
Phone: centerCompany.AdminAccount,
|
|
|
UserCenterId: centerCompany.AdminId,
|
|
|
NickName: centerCompany.AdminName,
|
|
|
}
|
|
|
id, err := models.AddUser(newUser, o)
|
|
|
fmt.Println("-=====>user_id=", id)
|
|
|
if err != nil {
|
|
|
|
|
|
log.Error("添加用户数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
} else {
|
|
|
log.Error("查询用户数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
return newUser, nil
|
|
|
}
|
|
|
|
|
|
func initUserCompany(newcompany *models.Company, newuser *models.User, o orm.Ormer) (*models.UserCompany, error) {
|
|
|
var (
|
|
|
err error
|
|
|
uc = &models.UserCompany{}
|
|
|
)
|
|
|
err = o.QueryTable(&models.UserCompany{}).
|
|
|
Filter("user_id", newuser.Id).
|
|
|
Filter("company_id", newcompany.Id).
|
|
|
Filter("delete_at", 0).
|
|
|
One(uc)
|
|
|
if err == nil {
|
|
|
_, err = o.QueryTable(&models.UserCompany{}).
|
|
|
Filter("id", uc.Id).
|
|
|
Update(orm.Params{
|
|
|
"enable": models.USERCOMPANY_ENABLE_YES,
|
|
|
})
|
|
|
if err != nil {
|
|
|
log.Error("更新user_company数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
} else if err == orm.ErrNoRows {
|
|
|
uc = &models.UserCompany{
|
|
|
UserId: newuser.Id,
|
|
|
CompanyId: newcompany.Id,
|
|
|
}
|
|
|
_, err = models.AddUserCompany(uc, o)
|
|
|
if err != nil {
|
|
|
log.Error("添加user_company数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
} else {
|
|
|
log.Error("获取user_company数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
return uc, nil
|
|
|
}
|
|
|
|
|
|
func initDepartment(newCompany *models.Company, newusercompany *models.UserCompany, o orm.Ormer) (*models.Department, error) {
|
|
|
var (
|
|
|
newDepartment = &models.Department{}
|
|
|
err error
|
|
|
)
|
|
|
err = o.QueryTable(&models.Department{}).
|
|
|
Filter("company_id", newCompany.Id).
|
|
|
Filter("is_top", 1).
|
|
|
One(newDepartment)
|
|
|
if err == nil {
|
|
|
return newDepartment, nil
|
|
|
} else if err == orm.ErrNoRows {
|
|
|
//添加部门
|
|
|
newDepartment = &models.Department{
|
|
|
CompanyId: newCompany.Id,
|
|
|
Name: newCompany.Name,
|
|
|
IsTop: 1,
|
|
|
ParentId: 0,
|
|
|
}
|
|
|
newDepartment.SetManages([]int64{newusercompany.Id})
|
|
|
_, err = models.AddDepartment(newDepartment, o)
|
|
|
if err != nil {
|
|
|
log.Error("添加部门数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
newDepartment.SetRelation(nil)
|
|
|
err = models.UpdateDepartmentById(newDepartment, []string{"Relation"}, o)
|
|
|
if err != nil {
|
|
|
log.Error("更新部门关系树数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
} else {
|
|
|
log.Error("查询部门数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
return newDepartment, nil
|
|
|
}
|
|
|
|
|
|
func initUserDedaprtmet(newDepartment *models.Department, newUserCompany *models.UserCompany, o orm.Ormer) (*models.UserDepartment, error) {
|
|
|
var (
|
|
|
newUserDepartment = &models.UserDepartment{}
|
|
|
err error
|
|
|
)
|
|
|
err = o.QueryTable(&models.UserDepartment{}).
|
|
|
Filter("department_id", newDepartment.Id).
|
|
|
Filter("user_company_id", newUserCompany.Id).
|
|
|
Filter("enable_status", models.USER_DEPARTMENT_ENABLE_YES).
|
|
|
One(newUserDepartment)
|
|
|
if err == nil {
|
|
|
return newUserDepartment, nil
|
|
|
} else if err == orm.ErrNoRows {
|
|
|
//添加部门
|
|
|
newUserDepartment = &models.UserDepartment{
|
|
|
UserCompanyId: newUserCompany.Id,
|
|
|
UserId: newUserCompany.UserId,
|
|
|
CompanyId: newUserCompany.CompanyId,
|
|
|
DepartmentId: newDepartment.Id,
|
|
|
}
|
|
|
_, err = models.AddUserDepartment(newUserDepartment, o)
|
|
|
if err != nil {
|
|
|
log.Error("添加部门数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
} else {
|
|
|
log.Error("查询部门数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
return newUserDepartment, nil
|
|
|
}
|
|
|
|
|
|
func initRoleGroup(newCompany *models.Company, o orm.Ormer) (*models.Role, error) {
|
|
|
var (
|
|
|
newRoleGroup = &models.Role{}
|
|
|
err error
|
|
|
)
|
|
|
err = o.QueryTable(&models.Role{}).
|
|
|
Filter("delete_at", 0).
|
|
|
Filter("is_default", models.ROLE_DEFAULR).
|
|
|
Filter("types", models.ROLETYPES_GROUP).
|
|
|
Filter("company_id", newCompany.Id).
|
|
|
One(newRoleGroup)
|
|
|
if err == nil {
|
|
|
return newRoleGroup, nil
|
|
|
} else if err == orm.ErrNoRows {
|
|
|
//添加部门
|
|
|
newRoleGroup = &models.Role{
|
|
|
Types: models.ROLETYPES_GROUP,
|
|
|
CompanyId: newCompany.Id,
|
|
|
IsDefault: models.ROLE_DEFAULR,
|
|
|
Name: "主管组",
|
|
|
}
|
|
|
_, err = models.AddRole(newRoleGroup, o)
|
|
|
if err != nil {
|
|
|
log.Error("添加角色组数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
} else {
|
|
|
log.Error("查询角色组数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
return newRoleGroup, nil
|
|
|
}
|
|
|
|
|
|
func initRole(newCompany *models.Company, newRoleGroup *models.Role, o orm.Ormer) (*models.Role, error) {
|
|
|
var (
|
|
|
newRole = &models.Role{}
|
|
|
err error
|
|
|
)
|
|
|
err = o.QueryTable(&models.Role{}).
|
|
|
Filter("delete_at", 0).
|
|
|
Filter("is_default", models.ROLE_DEFAULR).
|
|
|
Filter("types", models.ROLETYPES_ROLE).
|
|
|
Filter("company_id", newCompany.Id).
|
|
|
One(newRole)
|
|
|
if err == nil {
|
|
|
return newRole, nil
|
|
|
} else if err == orm.ErrNoRows {
|
|
|
//添加部门
|
|
|
newRole = &models.Role{
|
|
|
Types: models.ROLETYPES_ROLE,
|
|
|
CompanyId: newCompany.Id,
|
|
|
IsDefault: models.ROLE_DEFAULR,
|
|
|
Name: "主管理员",
|
|
|
Pid: newRoleGroup.Id,
|
|
|
}
|
|
|
_, err = models.AddRole(newRole, o)
|
|
|
if err != nil {
|
|
|
log.Error("添加角色数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
} else {
|
|
|
log.Error("查询角色数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
return newRole, nil
|
|
|
}
|
|
|
|
|
|
func initUserRole(newUserCompany *models.UserCompany, newRole *models.Role, o orm.Ormer) (*models.UserRole, error) {
|
|
|
var (
|
|
|
newUserRole = &models.UserRole{}
|
|
|
err error
|
|
|
)
|
|
|
err = o.QueryTable(&models.UserRole{}).
|
|
|
Filter("enable_status", models.USER_ROLE_ENABLE_YES).
|
|
|
Filter("user_company_id", newUserCompany.Id).
|
|
|
Filter("role_id", newRole.Id).
|
|
|
One(newUserRole)
|
|
|
if err == nil {
|
|
|
return newUserRole, nil
|
|
|
} else if err == orm.ErrNoRows {
|
|
|
//添加UserRole
|
|
|
newUserRole = &models.UserRole{
|
|
|
RoleId: newRole.Id,
|
|
|
UserCompanyId: newUserCompany.Id,
|
|
|
CompanyId: newUserCompany.CompanyId,
|
|
|
UserId: newUserCompany.UserId,
|
|
|
}
|
|
|
_, err = models.AddUserRole(newUserRole, o)
|
|
|
if err != nil {
|
|
|
log.Error("添加user_role数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
} else {
|
|
|
log.Error("查询user_role数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
return newUserRole, nil
|
|
|
} |
...
|
...
|
|