作者 yangfu

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

... ... @@ -12,8 +12,8 @@ AdminPort = 8088
##数据库连接
# sqlconn ="${MYSQL_CONN||root:sutianxia2015@tcp(115.29.205.99:3306)/opportunity?charset=utf8&loc=Asia%2FShanghai}"
mysql_user = "${MYSQL_USER||root}"
mysql_password = "${MYSQL_PASSWORD||sutianxia2015}"
mysql_host = "${MYSQL_HOST||115.29.205.99}"
mysql_password = "${MYSQL_PASSWORD||root}"
mysql_host = "${MYSQL_HOST||127.0.0.1}"
mysql_port = "${MYSQL_PORT||3306}"
mysql_db_name = "${MYSQL_DB_NAME||opportunity}"
##redis相关配置
... ...
... ... @@ -390,5 +390,23 @@ func (c *CompanyController) UserList() {
return
}
//Regist
// func
//InitCompany 初始化企业
//@router /ucenter/company [post]
func (c *CompanyController) InitCompany() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
type Parameter struct {
CompanyId int64 `json:"company_id"`
}
var param Parameter
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败 err:%s", err)
msg = protocol.BadRequestParam("1")
return
}
err := servecompany.InitCompanyInfo(param.CompanyId)
msg = protocol.NewReturnResponse(nil, err)
return
}
... ...
... ... @@ -54,8 +54,17 @@ func init() {
// AddCompany insert a new Company into database and returns
// last inserted Id on success.
func AddCompany(m *Company) (id int64, err error) {
o := orm.NewOrm()
func AddCompany(m *Company, om ...orm.Ormer) (id int64, err error) {
var o orm.Ormer
if len(om) > 0 {
o = om[0]
} else {
o = orm.NewOrm()
}
m.CreateAt = time.Now()
m.UpdateAt = time.Now()
m.Enable = COMPANY_ENABLE_YES
m.DeleteAt = time.Unix(0, 0)
id, err = o.Insert(m)
return
}
... ...
... ... @@ -122,6 +122,8 @@ func AddDepartment(m *Department, om ...orm.Ormer) (id int64, err error) {
} else {
o = orm.NewOrm()
}
m.CreateAt = time.Now()
m.UpdateAt = time.Now()
m.DeleteAt = time.Unix(0, 0)
id, err = o.Insert(m)
return
... ... @@ -147,14 +149,10 @@ func UpdateDepartmentById(m *Department, col []string, om ...orm.Ormer) (err err
} else {
o = orm.NewOrm()
}
v := Department{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
m.UpdateAt = time.Now()
if num, err = o.Update(m, col...); err == nil {
fmt.Println("Number of records updated in database:", num)
}
var num int64
m.UpdateAt = time.Now()
if num, err = o.Update(m, col...); err == nil {
_ = num
}
return
}
... ...
... ... @@ -76,8 +76,13 @@ func (t *Role) ValidatePid() (*Role, error) {
// AddRole insert a new Role into database and returns
// last inserted Id on success.
func AddRole(m *Role) (id int64, err error) {
o := orm.NewOrm()
func AddRole(m *Role, om ...orm.Ormer) (id int64, err error) {
var o orm.Ormer
if len(om) > 0 {
o = om[0]
} else {
o = orm.NewOrm()
}
m.CreateAt = time.Now()
m.DeleteAt = time.Unix(0, 0)
m.UpdateAt = time.Now()
... ...
... ... @@ -8,7 +8,7 @@ import (
)
type User struct {
Id int64 `orm:"column(id);pk" description:"用户id"`
Id int64 `orm:"column(id)" description:"用户id"`
NickName string `orm:"column(nick_name);size(100)" description:"昵称"`
Phone string `orm:"column(phone);size(40)" description:"手机号码"`
Passwd string `orm:"column(passwd);size(128)" description:"密码"`
... ... @@ -20,6 +20,7 @@ type User struct {
CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now_add" description:"创建时间"`
EnableStatus int8 `orm:"column(enable_status)" description:"是否有效"`
DeleteAt time.Time `orm:"column(delete_at);type(timestamp)"`
UserCenterId int64 `orm:"column(user_center_id)"`
}
func (t *User) TableName() string {
... ...
... ... @@ -55,8 +55,15 @@ func init() {
// AddUserDepartment insert a new UserDepartment into database and returns
// last inserted Id on success.
func AddUserDepartment(m *UserDepartment) (id int64, err error) {
o := orm.NewOrm()
func AddUserDepartment(m *UserDepartment, om ...orm.Ormer) (id int64, err error) {
var o orm.Ormer
if len(om) > 0 {
o = om[0]
} else {
o = orm.NewOrm()
}
m.CreateTime = time.Now()
m.EnableStatus = USER_DEPARTMENT_ENABLE_YES
id, err = o.Insert(m)
return
}
... ...
... ... @@ -8,7 +8,7 @@ import (
)
type UserPosition struct {
Id int64 `orm:"column(id);pk" description:"唯一键值"`
Id int64 `orm:"column(id)" description:"唯一键值"`
UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"`
PositionId int64 `orm:"column(position_id)" description:"表position.id 职位编号"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
... ...
... ... @@ -7,7 +7,7 @@ import (
)
type UserRole struct {
Id int `orm:"column(id);pk"`
Id int `orm:"column(id)"`
RoleId int64 `orm:"column(role_id)"`
UserId int64 `orm:"column(user_id)"`
EnableStatus int8 `orm:"column(enable_status)" description:"是否有效"`
... ... @@ -53,8 +53,14 @@ func init() {
// AddUserRole insert a new UserRole into database and returns
// last inserted Id on success.
func AddUserRole(m *UserRole) (id int64, err error) {
o := orm.NewOrm()
func AddUserRole(m *UserRole, om ...orm.Ormer) (id int64, err error) {
var o orm.Ormer
if len(om) == 0 {
o = om[0]
} else {
o = orm.NewOrm()
}
m.EnableStatus = USER_ROLE_ENABLE_YES
id, err = o.Insert(m)
return
}
... ...
... ... @@ -18,6 +18,7 @@ func init() {
beego.NSRouter("/add", &controllers.CompanyController{}, "post:DepartmentAdd"),
beego.NSRouter("/edit", &controllers.CompanyController{}, "post:DepartmentUpdate"),
beego.NSRouter("/delete", &controllers.CompanyController{}, "post:DepartmentDelete"),
// beego.NSRouter("/user", &controllers.CompanyController{}, "post:DepartmentDelete"),
),
beego.NSNamespace("position",
beego.NSRouter("/list", &controllers.CompanyController{}, "post:PositionList"),
... ... @@ -73,8 +74,13 @@ func init() {
beego.NSBefore(middleware.LogRequestData),
beego.NSRouter("/login", &controllers.AuthController{}, "post:Login"),
)
nsUcenter := beego.NewNamespace("/ucenter",
beego.NSBefore(middleware.LogRequestData),
beego.NSRouter("/company", &controllers.CompanyController{}, "post:InitCompany"),
)
beego.AddNamespace(nsV1)
beego.AddNamespace(nsAuth)
beego.AddNamespace(nsUcenter)
}
//routerPermission 路由对应的权限
... ...
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
}
... ...