|
|
package company
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"oppmg/common/log"
|
|
|
"oppmg/models"
|
|
|
"oppmg/protocol"
|
|
|
"oppmg/services/ucenter"
|
|
|
"oppmg/utils"
|
|
|
"strings"
|
|
|
"time"
|
...
|
...
|
@@ -15,445 +12,445 @@ import ( |
|
|
)
|
|
|
|
|
|
//InitCompanyInfo 初始化公司
|
|
|
func InitCompanyInfo(centerCompany protocol.CenterCompanyInfo) error {
|
|
|
var (
|
|
|
newDeparment *models.Department
|
|
|
newRoleGroup *models.Role
|
|
|
newRole *models.Role
|
|
|
newUser *models.User
|
|
|
newCompany *models.Company
|
|
|
newUserCompany *models.UserCompany
|
|
|
err error
|
|
|
)
|
|
|
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")
|
|
|
}
|
|
|
err = iniSysConfig(int(newCompany.Id), 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 InitCompanyInfo(centerCompany protocol.CenterCompanyInfo) error {
|
|
|
// var (
|
|
|
// newDeparment *models.Department
|
|
|
// newRoleGroup *models.Role
|
|
|
// newRole *models.Role
|
|
|
// newUser *models.User
|
|
|
// newCompany *models.Company
|
|
|
// newUserCompany *models.UserCompany
|
|
|
// err error
|
|
|
// )
|
|
|
// 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")
|
|
|
// }
|
|
|
// err = iniSysConfig(int(newCompany.Id), 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 protocol.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{
|
|
|
Id: centerCompany.CompanyId,
|
|
|
Name: centerCompany.CompanyName,
|
|
|
AdminId: admininfo.Id,
|
|
|
UserCenterId: centerCompany.CompanyId,
|
|
|
}
|
|
|
_, err = models.AddCompany(newCompany, o)
|
|
|
if err != nil {
|
|
|
log.Error("添加公司数据失败:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
// func initCompany(centerCompany protocol.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{
|
|
|
// Id: centerCompany.CompanyId,
|
|
|
// 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
|
|
|
}
|
|
|
// } else {
|
|
|
// log.Error("查询公司数据失败:%s", err)
|
|
|
// return nil, err
|
|
|
// }
|
|
|
// return newCompany, nil
|
|
|
// }
|
|
|
|
|
|
func initAdminUser(centerCompany protocol.CenterCompanyInfo, o orm.Ormer) (*models.User, error) {
|
|
|
var (
|
|
|
err error
|
|
|
newUser = &models.User{}
|
|
|
)
|
|
|
err = o.QueryTable(&models.User{}).
|
|
|
Filter("phone", centerCompany.AdminAccount).
|
|
|
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 {
|
|
|
var (
|
|
|
ucenterUser = &ucenter.ResponseAddUser{}
|
|
|
err error
|
|
|
)
|
|
|
ucenterUser, err = ucenter.RequestUCenterAddUser(centerCompany.AdminAccount, centerCompany.AdminName, "")
|
|
|
if err != nil {
|
|
|
log.Error("获取统一用户中心数据失败 :%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
//添加用户
|
|
|
newUser = &models.User{
|
|
|
Phone: centerCompany.AdminAccount,
|
|
|
NickName: centerCompany.AdminName,
|
|
|
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 initAdminUser(centerCompany protocol.CenterCompanyInfo, o orm.Ormer) (*models.User, error) {
|
|
|
// var (
|
|
|
// err error
|
|
|
// newUser = &models.User{}
|
|
|
// )
|
|
|
// err = o.QueryTable(&models.User{}).
|
|
|
// Filter("phone", centerCompany.AdminAccount).
|
|
|
// 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 {
|
|
|
// var (
|
|
|
// ucenterUser = &ucenter.ResponseAddUser{}
|
|
|
// err error
|
|
|
// )
|
|
|
// ucenterUser, err = ucenter.RequestUCenterAddUser(centerCompany.AdminAccount, centerCompany.AdminName, "")
|
|
|
// if err != nil {
|
|
|
// log.Error("获取统一用户中心数据失败 :%s", err)
|
|
|
// return nil, err
|
|
|
// }
|
|
|
// //添加用户
|
|
|
// newUser = &models.User{
|
|
|
// Phone: centerCompany.AdminAccount,
|
|
|
// NickName: centerCompany.AdminName,
|
|
|
// 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 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
|
|
|
}
|
|
|
// 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
|
|
|
}
|
|
|
// 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
|
|
|
}
|
|
|
// 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
|
|
|
}
|
|
|
// 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
|
|
|
}
|
|
|
// 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
|
|
|
}
|
|
|
// 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
|
|
|
}
|
|
|
// 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
|
|
|
}
|
|
|
// 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,
|
|
|
}
|
|
|
_, 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
|
|
|
}
|
|
|
// 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
|
|
|
}
|
|
|
// return newUserRole, nil
|
|
|
// }
|
|
|
|
|
|
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
|
|
|
}
|
|
|
// 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
|
|
|
// }
|
|
|
|
|
|
func CompanyInfoEdit(companyid int64, logo string) error {
|
|
|
data := models.Company{
|
...
|
...
|
|