...
|
...
|
@@ -6,16 +6,28 @@ import ( |
|
|
"fmt"
|
|
|
"oppmg/common/log"
|
|
|
"oppmg/models"
|
|
|
"oppmg/protocol"
|
|
|
"oppmg/utils"
|
|
|
"time"
|
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
)
|
|
|
|
|
|
type CompanyBase struct {
|
|
|
Id int64 `json:"id"` //id
|
|
|
Name string `json:"name"` //公司名称名称
|
|
|
AdminCompanyId int64 `json:"admin_company_id"`
|
|
|
Logo string `json:"logo"`
|
|
|
}
|
|
|
|
|
|
// ModuleCompanytData 主管理平台发送过来的数据
|
|
|
type ModuleCompanytData struct {
|
|
|
Id int64 `json:"id"` //id
|
|
|
Name string `json:"name"` //公司名称名称
|
|
|
//AdminId int64 `json:"admin_id"` //主管理员id
|
|
|
Logo string `json:"logo"`
|
|
|
Company CompanyBase `json:"company"`
|
|
|
User ModuleEmployee `json:"user"`
|
|
|
}
|
|
|
|
|
|
type CompanyCharge struct {
|
|
|
Id int64 `json:"id"`
|
|
|
Charge []int64 `json:"charge"`
|
|
|
}
|
|
|
|
...
|
...
|
@@ -23,7 +35,7 @@ var _ PlatformAction = ModuleCompanytData{} |
|
|
|
|
|
func (m ModuleCompanytData) DoAction(code string, jsondata []byte) error {
|
|
|
switch code {
|
|
|
case "edit":
|
|
|
case "add":
|
|
|
var (
|
|
|
data ModuleCompanytData
|
|
|
err error
|
...
|
...
|
@@ -32,10 +44,21 @@ func (m ModuleCompanytData) DoAction(code string, jsondata []byte) error { |
|
|
if err != nil {
|
|
|
return fmt.Errorf("数据解析失败:%s", err)
|
|
|
}
|
|
|
return UpdateCompanyData(data)
|
|
|
return AddCompanyData(data)
|
|
|
case "edit":
|
|
|
// var (
|
|
|
// data ModuleCompanytData
|
|
|
// err error
|
|
|
// )
|
|
|
// err = json.Unmarshal(jsondata, &data)
|
|
|
// if err != nil {
|
|
|
// return fmt.Errorf("数据解析失败:%s", err)
|
|
|
// }
|
|
|
// return UpdateCompanyData(data)
|
|
|
return nil
|
|
|
case "setCompanyCharge":
|
|
|
var (
|
|
|
data ModuleCompanytData
|
|
|
data CompanyCharge
|
|
|
err error
|
|
|
)
|
|
|
err = json.Unmarshal(jsondata, &data)
|
...
|
...
|
@@ -50,7 +73,7 @@ func (m ModuleCompanytData) DoAction(code string, jsondata []byte) error { |
|
|
}
|
|
|
|
|
|
// SetCompanyCharge 更新公司那一级的部门的管理员
|
|
|
func SetCompanyCharge(data ModuleCompanytData) error {
|
|
|
func SetCompanyCharge(data CompanyCharge) error {
|
|
|
var (
|
|
|
err error
|
|
|
companyData *models.Company
|
...
|
...
|
@@ -89,21 +112,600 @@ func SetCompanyCharge(data ModuleCompanytData) error { |
|
|
}
|
|
|
|
|
|
func UpdateCompanyData(data ModuleCompanytData) error {
|
|
|
// var (
|
|
|
// companyData *models.Company
|
|
|
// err error
|
|
|
// )
|
|
|
// companyData, err = models.GetCompanyByUCenter(data.Id)
|
|
|
// if err != nil {
|
|
|
// log.Error("获取企业数据失败,user_center_id:%d ,err:%s", data.Id, err)
|
|
|
// return errors.New("获取企业数据失败")
|
|
|
// }
|
|
|
// companyData.Logo = data.Logo
|
|
|
// companyData.Name = data.Name
|
|
|
// err = models.UpdateCompanyById(companyData, []string{"Logo", "Name"})
|
|
|
// if err != nil {
|
|
|
// log.Error("更新公司数据失败:%s", err)
|
|
|
// return errors.New("更新公司数据失败")
|
|
|
// }
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
//AddCompanyData 新增公司 初始化公司
|
|
|
func AddCompanyData(data ModuleCompanytData) error {
|
|
|
var (
|
|
|
companyData *models.Company
|
|
|
err error
|
|
|
newDeparment *models.Department
|
|
|
newRoleGroup *models.Role
|
|
|
newRole *models.Role
|
|
|
newUser *models.User
|
|
|
newCompany *models.Company
|
|
|
newUserCompany *models.UserCompany
|
|
|
err error
|
|
|
)
|
|
|
companyData, err = models.GetCompanyByUCenter(data.Id)
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
//处理用户数据
|
|
|
newUser, err = AddAdminUser(&data, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("处理公司主管数据失败:%s", err)
|
|
|
return errors.New("处理公司主管数据失败")
|
|
|
}
|
|
|
//处理公司数据
|
|
|
newCompany, err = AddCompanyBase(&data, newUser, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("处理公司数据失败:%s", err)
|
|
|
return errors.New("处理公司数据失败")
|
|
|
}
|
|
|
//处理评分配置数据 初始化
|
|
|
err = iniSysConfig(int(newCompany.Id), o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("处理评分配置数据失败:%s", err)
|
|
|
return errors.New("处理评分配置数据")
|
|
|
}
|
|
|
//添加user_company
|
|
|
newUserCompany, err = AddUserCompanyData(&data, newCompany, newUser, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("处理user_company数据失败:%s", err)
|
|
|
return errors.New("处理user_company数据失败")
|
|
|
}
|
|
|
//添加部门
|
|
|
newDeparment, err = AddFirstDepartment(newCompany, newUserCompany, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("处理部门数据失败:%s", err)
|
|
|
return errors.New("处理部门数据失败")
|
|
|
}
|
|
|
//添加用户部门数据
|
|
|
_, err = initUserDedaprtmet(newDeparment, newUserCompany, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("处理user_department数据失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//添加部门主管
|
|
|
err = AddFirstDepartmentCharge(newDeparment, newUserCompany, newCompany, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("处理部门主管数据,err:%s", err)
|
|
|
return errors.New("处理部门主管数据")
|
|
|
}
|
|
|
//添加角色组
|
|
|
newRoleGroup, err = initRoleGroup(newCompany, o)
|
|
|
if err != nil {
|
|
|
log.Error("获取企业数据失败,user_center_id:%d ,err:%s", data.Id, err)
|
|
|
return errors.New("获取企业数据失败")
|
|
|
o.Rollback()
|
|
|
log.Error("处理角色组数据失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
companyData.Logo = data.Logo
|
|
|
companyData.Name = data.Name
|
|
|
err = models.UpdateCompanyById(companyData, []string{"Logo", "Name"})
|
|
|
//添加角色
|
|
|
newRole, err = initRole(newCompany, newRoleGroup, o)
|
|
|
if err != nil {
|
|
|
log.Error("更新公司数据失败:%s", err)
|
|
|
return errors.New("更新公司数据失败")
|
|
|
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 AddAdminUser(centerCompany *ModuleCompanytData, o orm.Ormer) (*models.User, error) {
|
|
|
var (
|
|
|
err error
|
|
|
)
|
|
|
companyAdminData := centerCompany.User
|
|
|
//添加用户
|
|
|
newUser := &models.User{
|
|
|
Id: companyAdminData.Id,
|
|
|
Phone: companyAdminData.Phone,
|
|
|
NickName: companyAdminData.Name,
|
|
|
UserCenterId: companyAdminData.OpenId,
|
|
|
}
|
|
|
_, err = models.AddUser(newUser, o)
|
|
|
if err != nil {
|
|
|
log.Error("添加用户数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
return newUser, nil
|
|
|
}
|
|
|
|
|
|
func AddCompanyBase(centerCompany *ModuleCompanytData, admininfo *models.User, o orm.Ormer) (*models.Company, error) {
|
|
|
var (
|
|
|
err error
|
|
|
)
|
|
|
companybase := centerCompany.Company
|
|
|
newCompany := &models.Company{
|
|
|
Id: companybase.Id,
|
|
|
Name: companybase.Name,
|
|
|
AdminId: admininfo.Id,
|
|
|
UserCenterId: companybase.AdminCompanyId,
|
|
|
Enable: models.COMPANY_ENABLE_NO,
|
|
|
}
|
|
|
_, err = models.AddCompany(newCompany, o)
|
|
|
if err != nil {
|
|
|
log.Error("添加公司数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
return nil, nil
|
|
|
}
|
|
|
|
|
|
func AddUserCompanyData(centerCompany *ModuleCompanytData, newcompany *models.Company, newuser *models.User, o orm.Ormer) (*models.UserCompany, error) {
|
|
|
var (
|
|
|
err error
|
|
|
)
|
|
|
adminUserData := centerCompany.User
|
|
|
uc := &models.UserCompany{
|
|
|
UserId: newuser.Id,
|
|
|
CompanyId: newcompany.Id,
|
|
|
Enable: models.USERCOMPANY_ENABLE_YES,
|
|
|
ChargeStatus: models.USERCOMPANY_CHARGE_YES,
|
|
|
Id: adminUserData.Id,
|
|
|
NickName: adminUserData.Name,
|
|
|
OpenId: adminUserData.OpenId,
|
|
|
Sex: adminUserData.Sex,
|
|
|
JobNum: adminUserData.JobNum,
|
|
|
Phone: adminUserData.Phone,
|
|
|
PrivatePhone: adminUserData.PrivatePhone,
|
|
|
Email: adminUserData.Email,
|
|
|
ExtensionNum: adminUserData.ExtensionNum,
|
|
|
Workspace: adminUserData.WorkSpace,
|
|
|
IsBusiness: adminUserData.IsBusiness,
|
|
|
Avatar: adminUserData.Avatar,
|
|
|
Remarks: adminUserData.Remarks,
|
|
|
// AdminType: adminUserData.AdminType,
|
|
|
ExtraText: adminUserData.ExtraText,
|
|
|
}
|
|
|
_, err = models.AddUserCompany(uc, o)
|
|
|
if err != nil {
|
|
|
log.Error("添加user_company数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
return uc, nil
|
|
|
}
|
|
|
|
|
|
//AddFirstDepartment 创建公司的一级部门
|
|
|
func AddFirstDepartment(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,
|
|
|
Relation: fmt.Sprint(utils.GenerateIDBySonyflake()),
|
|
|
}
|
|
|
_, 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 AddFirstDepartmentCharge(newDepartment *models.Department, newUserCompany *models.UserCompany, newCompany *models.Company, o orm.Ormer) error {
|
|
|
var (
|
|
|
err error
|
|
|
)
|
|
|
departmentCharg := &models.DepartmentCharge{
|
|
|
CompanyId: newCompany.Id,
|
|
|
DepartmentId: newDepartment.Id,
|
|
|
UserCompanyId: newUserCompany.Id,
|
|
|
}
|
|
|
_, err = models.AddDepartmentCharge(departmentCharg, o)
|
|
|
if err != nil {
|
|
|
log.Error("设置部门主管失败,err:%s", err)
|
|
|
return err
|
|
|
}
|
|
|
newDepartment.SetManages([]int64{newUserCompany.Id})
|
|
|
err = models.UpdateDepartmentById(newDepartment, []string{"Manages"}, o)
|
|
|
if err != nil {
|
|
|
log.Error("设置部门主管失败,err:%s", err)
|
|
|
return err
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func UpdateCompany(centerCompany *ModuleCompanytData, 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.Company.AdminCompanyId).
|
|
|
One(newCompany)
|
|
|
if err == nil {
|
|
|
_, err = o.QueryTable(&models.Company{}).
|
|
|
Filter("user_center_id", centerCompany.Company.AdminCompanyId).
|
|
|
Update(orm.Params{
|
|
|
"name": centerCompany.Company.Name,
|
|
|
"admin_id": admininfo.Id,
|
|
|
"delete_at": 0,
|
|
|
})
|
|
|
if err != nil {
|
|
|
log.Error("更新公司数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
} else {
|
|
|
log.Error("查询公司数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
return newCompany, nil
|
|
|
}
|
|
|
|
|
|
// func initAdminUser(centerCompany *ModuleCompanytData, o orm.Ormer) (*models.User, error) {
|
|
|
// var (
|
|
|
// err error
|
|
|
// newUser = &models.User{}
|
|
|
// )
|
|
|
// companyAdminData := centerCompany.User
|
|
|
// err = o.QueryTable(&models.User{}).
|
|
|
// Filter("phone", companyAdminData.Phone).
|
|
|
// Filter("delete_at", 0).
|
|
|
// One(newUser)
|
|
|
// if err == nil {
|
|
|
// // 更新数据
|
|
|
// _, err = o.QueryTable(&models.User{}).
|
|
|
// Filter("user_center_id", companyAdminData.OpenId).
|
|
|
// Filter("delete_at", 0).
|
|
|
// Update(orm.Params{
|
|
|
// "nick_name": companyAdminData.Name,
|
|
|
// "phone": companyAdminData.Phone,
|
|
|
// "enable_status": models.USER_ENABLE_YES,
|
|
|
// })
|
|
|
// if err != nil {
|
|
|
// log.Error("数据操作失败:%s", err)
|
|
|
// return nil, err
|
|
|
// }
|
|
|
// } else if err == orm.ErrNoRows {
|
|
|
// var (
|
|
|
// ucenterUser = &ucenter.ResponseAddUser{}
|
|
|
// err error
|
|
|
// )
|
|
|
// ucenterUser, err = ucenter.RequestUCenterAddUser(companyAdminData.Phone, companyAdminData.Name, "")
|
|
|
// if err != nil {
|
|
|
// log.Error("获取统一用户中心数据失败 :%s", err)
|
|
|
// return nil, err
|
|
|
// }
|
|
|
// //添加用户
|
|
|
// newUser = &models.User{
|
|
|
// Id: companyAdminData.Id,
|
|
|
// Phone: companyAdminData.Phone,
|
|
|
// NickName: companyAdminData.Name,
|
|
|
// Icon: ucenterUser.Data.Avatar,
|
|
|
// Accid: ucenterUser.Data.Accid,
|
|
|
// UserCenterId: ucenterUser.Data.Id,
|
|
|
// CsAccount: ucenterUser.Data.CustomerAccount,
|
|
|
// }
|
|
|
// _, err = models.AddUser(newUser, o)
|
|
|
// 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,
|
|
|
// OpenId: newuser.UserCenterId,
|
|
|
// Enable: models.USERCOMPANY_ENABLE_YES,
|
|
|
// ChargeStatus: models.USERCOMPANY_CHARGE_YES,
|
|
|
// }
|
|
|
// _, 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 {
|
|
|
// newDepartment.Name = newCompany.Name
|
|
|
// err = models.UpdateDepartmentById(newDepartment, []string{"Name"}, o)
|
|
|
// if err != nil {
|
|
|
// log.Error("更新部门关系树数据失败:%s", err)
|
|
|
// return nil, err
|
|
|
// }
|
|
|
// return newDepartment, nil
|
|
|
// } else if err == orm.ErrNoRows {
|
|
|
// //添加部门
|
|
|
// newDepartment = &models.Department{
|
|
|
// CompanyId: newCompany.Id,
|
|
|
// Name: newCompany.Name,
|
|
|
// IsTop: 1,
|
|
|
// ParentId: 0,
|
|
|
// Relation: fmt.Sprint(utils.GenerateIDBySonyflake()),
|
|
|
// }
|
|
|
// 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
|
|
|
// }
|
|
|
|
|
|
//initUserDedaprtmet 设置用户和部门关系
|
|
|
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
|
|
|
}
|
|
|
|
|
|
//initRoleGroup 设置初始的角色组
|
|
|
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
|
|
|
}
|
|
|
|
|
|
//initRole 初始化公司的角色
|
|
|
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
|
|
|
}
|
|
|
|
|
|
//initUserRole 初始化第一个用户和角色的关系
|
|
|
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,
|
|
|
}
|
|
|
_, 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
|
|
|
}
|
|
|
|
|
|
//iniSysConfig 初始化公司的系统配置
|
|
|
func iniSysConfig(companyid int, o orm.Ormer) error {
|
|
|
var (
|
|
|
m *models.SysConfig
|
|
|
err error
|
|
|
)
|
|
|
m, err = models.GetSysConfigByCompanyId(companyid, models.KeyScore)
|
|
|
if err == nil {
|
|
|
return nil
|
|
|
}
|
|
|
v := protocol.ScoreConfig{
|
|
|
DiscoveryScore: &protocol.DiscoveryScore{
|
|
|
BasicFactor: 1,
|
|
|
ExtraFactor: 1,
|
|
|
ValueFactor: 1,
|
|
|
},
|
|
|
SumScore: &protocol.SumScore{
|
|
|
DiscoveryFactor: 1,
|
|
|
CatchFactor: 1,
|
|
|
},
|
|
|
BasicScore: &protocol.ScoreRange{
|
|
|
Max: 10000,
|
|
|
Min: 0,
|
|
|
Step: 1,
|
|
|
},
|
|
|
ExtraScore: &protocol.ScoreRange{
|
|
|
Max: 10000,
|
|
|
Min: 0,
|
|
|
Step: 1,
|
|
|
},
|
|
|
ValueScore: &protocol.ScoreRange{
|
|
|
Max: 10000,
|
|
|
Min: 0,
|
|
|
Step: 1,
|
|
|
},
|
|
|
}
|
|
|
bt, _ := json.Marshal(v)
|
|
|
m = &models.SysConfig{
|
|
|
Key: models.KeyScore,
|
|
|
CompanyId: companyid,
|
|
|
CreateAt: time.Now(),
|
|
|
UpdateAt: time.Now(),
|
|
|
Content: string(bt),
|
|
|
}
|
|
|
_, err = models.AddSysConfig(m, o)
|
|
|
return err
|
|
|
} |
...
|
...
|
|