作者 唐旭辉

日常提交存储

### 接口完成进度
| 功能 | 完成状态 | 时间 | 接口路径 | 请求方式 |
| ---------------- | ------------------ | --------------- | -------------------------- | -------- |
| 登录获取 token | 完成 | 2018/12/11 | /auth/login | post |
| 短信验证码 | 未开始 | | |
| 刷新 token | 完成 | 2018/12/11 | /v1/auth/refresh_token | get |
| 切换公司 | 完成 | 2018/12/11 | /v1/auth/change_company | post |
| 获取个人基础数据 | 未开始 | | /v1/auth/me | get |
| 获取角色列表 | 完成 | | /v1/rbac/role | get |
| 添加角色 | 完成 | 2019.11.26 | /v1/rbac/role/add | post |
| 编辑角色 | 完成 | 2019.11.26 | /v1/rbac/role/edit | post |
| ~~删除角色~~ | ~~完成(待变更)~~ | ~~ 2019.11.26~~ | /v1/rbac/role/delete | post |
| 删除角色 | 完成 | 2019.12.04 | /v1/rbac/role/delete | post |
| 添加角色组 | 完成 | 2019.12.04 | /v1/rbac/role_group/add | post |
| 编辑角色组 | 完成 | 2019.12.04 | /v1/rbac/role_group/edit | post |
| 删除角色组 | 完成 | 2019.12.04 | /v1/rbac/role_group/delete | post |
| 获取部门列表 | 完成 | 2019.12.5 | /v1/department | get |
| 添加部门 | 完成 | 2019.11.28 | /v1/department/add | post |
| 编辑部门 | 完成 | 2019.11.29 | /v1/department/edit | post |
| ~~删除部门~~ | ~~完成(待变更)~~ | ~~2019.11.29~~ | ~~/v1/department~~ | post |
| 删除部门 | 完成 | 2019.12.2 | /v1/department/delete | post |
| 获取职位列表 | 进行中 | | /v1/position | get |
| 添加职位 | 完成 | 2019.12.2 | /v1/position/add | post |
| 编辑职位 | 完成 | 2019.12.2 | /v1/position/edit | post |
| 删除职位 | 完成 | 2019.12.2 | /v1/position/delete | post |
| 添加员工 | 未开始 | | /v1/company/user | post |
| 编辑员工 | 未开始 | | /v1/company/user | post |
| 删除员工 | 未开始 | | /v1/company/user | post |
| 公告管理 | 未开始 | | | |
| 公告管理 | 未开始 | | | |
| 公告管理 | 未开始 | | | |
| 机会&审批 模板 | 未开始 | | | |
| 机会&审批 模板 | 未开始 | | | |
| 机会&审批 模板 | 未开始 | | | |
| 机会&审批 模板 | 未开始 | | | |
| 机会类型设置 | 未开始 | | | |
| 机会类型设置 | 未开始 | | | |
| 机会类型设置 | 未开始 | | | |
| 机会类型设置 | 未开始 | | | |
| 功能 | 完成状态 | 时间 | 接口路径 | 请求方式 |
| ---------------- | -------- | ---------- | -------------------------- | -------- |
| 登录获取 token | 完成 | 2018/12/11 | /auth/login | post |
| 短信验证码 | 未开始 | | |
| 刷新 token | 完成 | 2018/12/11 | /v1/auth/refresh_token | get |
| 切换公司 | 完成 | 2018/12/11 | /v1/auth/change_company | post |
| 获取个人基础数据 | (暂停) | | /v1/auth/me | get |
| 获取角色列表 | 完成 | | /v1/rbac/role | get |
| 添加角色 | 完成 | 2019.11.26 | /v1/rbac/role/add | post |
| 编辑角色 | 完成 | 2019.11.26 | /v1/rbac/role/edit | post |
| 删除角色 | 完成 | 2019.12.04 | /v1/rbac/role/delete | post |
| 添加角色组 | 完成 | 2019.12.04 | /v1/rbac/role_group/add | post |
| 编辑角色组 | 完成 | 2019.12.04 | /v1/rbac/role_group/edit | post |
| 删除角色组 | 完成 | 2019.12.04 | /v1/rbac/role_group/delete | post |
| 获取部门列表 | 完成 | 2019.12.5 | /v1/department | get |
| 添加部门 | 完成 | 2019.11.28 | /v1/department/add | post |
| 编辑部门 | 完成 | 2019.11.29 | /v1/department/edit | post |
| 删除部门 | 完成 | 2019.12.2 | /v1/department/delete | post |
| 获取职位列表 | 进行中 | | /v1/position | get |
| 添加职位 | 完成 | 2019.12.2 | /v1/position/add | post |
| 编辑职位 | 完成 | 2019.12.2 | /v1/position/edit | post |
| 删除职位 | 完成 | 2019.12.2 | /v1/position/delete | post |
| 添加员工 | 未开始 | | /v1/company/user | post |
| 编辑员工 | 未开始 | | /v1/company/user | post |
| 删除员工 | 未开始 | | /v1/company/user | post |
| 公告管理 | 未开始 | | | |
| 公告管理 | 未开始 | | | |
| 公告管理 | 未开始 | | | |
| 机会&审批 模板 | 未开始 | | | |
| 机会&审批 模板 | 未开始 | | | |
| 机会&审批 模板 | 未开始 | | | |
| 机会&审批 模板 | 未开始 | | | |
| 机会类型设置 | 未开始 | | | |
| 机会类型设置 | 未开始 | | | |
| 机会类型设置 | 未开始 | | | |
| 机会类型设置 | 未开始 | | | |
... ...
... ... @@ -96,13 +96,16 @@ func (c *AuthController) ChangeCompany() {
return
}
//me
//@router /me [get]
func (c *AuthController) Me() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
// userid := c.GetUserId()
// companyid := c.GetCompanyId()
userid := c.GetUserId()
companyid := c.GetCompanyId()
userinfo, err := serveauth.UserBaseInfo(userid, companyid)
msg = protocol.NewReturnResponse(userinfo, err)
return
}
... ...
... ... @@ -63,7 +63,7 @@ func (c *CompanyController) DepartmentOne() {
}
// DepartmentAdd 添加部门
// @router /department [post]
// @router /department/add [post]
func (c *CompanyController) DepartmentAdd() {
var msg *protocol.ResponseMessage
defer func() {
... ... @@ -87,7 +87,7 @@ func (c *CompanyController) DepartmentAdd() {
}
// DepartmentUpdate 更新部门
// @router /department [put]
// @router /department/edit [post]
func (c *CompanyController) DepartmentUpdate() {
var msg *protocol.ResponseMessage
defer func() {
... ... @@ -112,7 +112,7 @@ func (c *CompanyController) DepartmentUpdate() {
}
// DepartmentUpdate 删除部门
// @router /department [delete]
// @router /department/delete [post]
func (c *CompanyController) DepartmentDelete() {
var msg *protocol.ResponseMessage
defer func() {
... ... @@ -132,7 +132,7 @@ func (c *CompanyController) DepartmentDelete() {
}
// PositionAdd 添加职位
// @router /position [post]
// @router /position/add [post]
func (c *CompanyController) PositionAdd() {
var msg *protocol.ResponseMessage
defer func() {
... ... @@ -152,7 +152,7 @@ func (c *CompanyController) PositionAdd() {
}
// PositionEdit 编辑职位
// @router /position [put]
// @router /position/edit [post]
func (c *CompanyController) PositionEdit() {
var msg *protocol.ResponseMessage
defer func() {
... ... @@ -171,7 +171,7 @@ func (c *CompanyController) PositionEdit() {
}
// PositionDelete 删除职位
// @router /position [delete]
// @router /position/delete [post]
func (c *CompanyController) PositionDelete() {
var msg *protocol.ResponseMessage
defer func() {
... ... @@ -213,7 +213,40 @@ func (c *CompanyController) PositionList() {
return
}
//获取公司的基本信息
func (c *CompanyController) BaseInfo() {
//UserAdd 添加用户
//@Router /user/add [post]
func (c *CompanyController) UserAdd() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
var param protocol.RequestUserAdd
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败 err:%s", err)
msg = protocol.BadRequestParam("1")
return
}
param.CompanyId = c.GetCompanyId()
err := servecompany.UserAdd(param)
msg = protocol.NewReturnResponse(nil, err)
return
}
//UserAdd 添加用户
//@Router /user/edit [post]
func (c *CompanyController) UserEdit() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
var param protocol.RequestUserEdit
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败 err:%s", err)
msg = protocol.BadRequestParam("1")
return
}
param.CompanyId = c.GetCompanyId()
err := servecompany.UserEdit(param)
msg = protocol.NewReturnResponse(nil, err)
return
}
... ...
package models
import (
"errors"
"fmt"
"time"
... ... @@ -43,6 +42,13 @@ func (t *User) IsEnable() bool {
return false
}
func (t *User) IsDelete() bool {
if t.DeleteAt.Unix() < 0 {
return false
}
return true
}
func init() {
orm.RegisterModel(new(User))
}
... ... @@ -90,12 +96,13 @@ func UpdateUserById(m *User) (err error) {
func GetUserByPhone(phone string) (v *User, err error) {
o := orm.NewOrm()
v = &User{Phone: phone}
if err = o.Read(v, "Phone"); err == nil {
err = o.QueryTable(&User{}).
Filter("phone", phone).
Filter("delete_at", 0).
One(v)
if err == nil {
return v, nil
}
if v.DeleteAt.Unix() > 0 {
return nil, errors.New("user is deleted")
}
return nil, err
}
... ...
package models
import (
"errors"
"fmt"
"oppmg/common/log"
"time"
... ... @@ -54,8 +55,10 @@ func init() {
// AddUserCompany insert a new UserCompany into database and returns
// last inserted Id on success.
func AddUserCompany(m *UserCompany) (id int64, err error) {
o := orm.NewOrm()
func AddUserCompany(m *UserCompany, o orm.Ormer) (id int64, err error) {
m.CreateAt = time.Now()
m.DeleteAt = time.Unix(0, 0)
m.UpdateAt = time.Now()
id, err = o.Insert(m)
return
}
... ... @@ -77,13 +80,15 @@ func UpdateUserCompanyById(m *UserCompany) (err error) {
func GetUserCompanyBy(userid int64, companyId int64) (*UserCompany, error) {
o := orm.NewOrm()
v := &UserCompany{
UserId: userid,
CompanyId: companyId,
}
err := o.Read(v, "UserId", "CompanyId")
var data []*UserCompany
_, err := o.QueryTable(&UserCompany{}).Filter("UserId", userid).
Filter("CompanyId", companyId).
All(&data)
if err != nil {
return nil, err
}
return v, nil
if len(data) == 0 {
return nil, errors.New("UserCompany not found")
}
return data[0], nil
}
... ...
... ... @@ -8,10 +8,10 @@ import (
)
type UserDepartment struct {
Id int `orm:"column(id);auto" description:"主键"`
Id int64 `orm:"column(id);auto" description:"主键"`
UserId int64 `orm:"column(user_id)" description:"用户id"`
CompanyId int `orm:"column(company_id)" description:"公司id"`
DepartmentId int `orm:"column(department_id)" description:"部门id"`
CompanyId int64 `orm:"column(company_id)" description:"公司id"`
DepartmentId int64 `orm:"column(department_id)" description:"部门id"`
CreateTime time.Time `orm:"column(create_time);type(timestamp);null" description:"创建时间"`
EnableStatus int8 `orm:"column(enable_status)" description:"是否有效"`
}
... ... @@ -20,6 +20,34 @@ func (t *UserDepartment) TableName() string {
return "user_department"
}
//EnableStatus 是否有效
const (
USER_DEPARTMENT_ENABLE_YES int8 = 1 //有效
USER_DEPARTMENT_ENABLE_NO int8 = 2 //无效
)
func (t *UserDepartment) IsEnable() bool {
switch t.EnableStatus {
case USER_DEPARTMENT_ENABLE_YES:
return true
case USER_DEPARTMENT_ENABLE_NO:
return false
}
return false
}
func (t *UserDepartment) ValidCompanyDepart() error {
depart, err := GetDepartmentById(t.DepartmentId)
if err != nil {
return err
}
if depart.CompanyId != t.CompanyId {
e := fmt.Errorf(" depart.CompanyId != param.CompanyId ")
return e
}
return nil
}
func init() {
orm.RegisterModel(new(UserDepartment))
}
... ... @@ -34,7 +62,7 @@ func AddUserDepartment(m *UserDepartment) (id int64, err error) {
// GetUserDepartmentById retrieves UserDepartment by Id. Returns error if
// Id doesn't exist
func GetUserDepartmentById(id int) (v *UserDepartment, err error) {
func GetUserDepartmentById(id int64) (v *UserDepartment, err error) {
o := orm.NewOrm()
v = &UserDepartment{Id: id}
if err = o.Read(v); err == nil {
... ... @@ -60,7 +88,7 @@ func UpdateUserDepartmentById(m *UserDepartment) (err error) {
// DeleteUserDepartment deletes UserDepartment by Id and returns error if
// the record to be deleted doesn't exist
func DeleteUserDepartment(id int) (err error) {
func DeleteUserDepartment(id int64) (err error) {
o := orm.NewOrm()
v := UserDepartment{Id: id}
// ascertain id exists in the database
... ...
... ... @@ -8,18 +8,46 @@ import (
)
type UserPosition struct {
Id int `orm:"column(id);pk" description:"唯一键值"`
Id int64 `orm:"column(id);pk" description:"唯一键值"`
UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"`
PositionId int `orm:"column(position_id)" description:"表position.id 职位编号"`
PositionId int64 `orm:"column(position_id)" description:"表position.id 职位编号"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
CompanyId int `orm:"column(company_id)" description:"表company.id 公司编号"`
EnableStatus int8 `orm:"column(enable_status);null" description:"是否有效 1:有效 0:无效"`
CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"`
EnableStatus int8 `orm:"column(enable_status);null" description:"是否有效 1:有效 2:无效"`
}
func (t *UserPosition) TableName() string {
return "user_position"
}
//EnableStatus 是否有效
const (
USER_POSITION_ENABLE_YES int8 = 1 //有效
USER_POSITION_ENABLE_NO int8 = 2 //无效
)
func (t *UserPosition) IsEnable() bool {
switch t.EnableStatus {
case USER_POSITION_ENABLE_YES:
return true
case USER_POSITION_ENABLE_NO:
return false
}
return false
}
func (t *UserPosition) ValidCompanyPosition() error {
depart, err := GetPositionById(t.PositionId)
if err != nil {
return err
}
if depart.CompanyId != t.CompanyId {
e := fmt.Errorf(" position.CompanyId != param.CompanyId ")
return e
}
return nil
}
func init() {
orm.RegisterModel(new(UserPosition))
}
... ... @@ -34,7 +62,7 @@ func AddUserPosition(m *UserPosition) (id int64, err error) {
// GetUserPositionById retrieves UserPosition by Id. Returns error if
// Id doesn't exist
func GetUserPositionById(id int) (v *UserPosition, err error) {
func GetUserPositionById(id int64) (v *UserPosition, err error) {
o := orm.NewOrm()
v = &UserPosition{Id: id}
if err = o.Read(v); err == nil {
... ... @@ -60,7 +88,7 @@ func UpdateUserPositionById(m *UserPosition) (err error) {
// DeleteUserPosition deletes UserPosition by Id and returns error if
// the record to be deleted doesn't exist
func DeleteUserPosition(id int) (err error) {
func DeleteUserPosition(id int64) (err error) {
o := orm.NewOrm()
v := UserPosition{Id: id}
// ascertain id exists in the database
... ...
package models
import (
"fmt"
"github.com/astaxie/beego/orm"
)
type UserRole struct {
Id int `orm:"column(id);pk"`
RoleId int64 `orm:"column(role_id)"`
UserId int64 `orm:"column(user_id)"`
EnableStatus int8 `orm:"column(enable_status)" description:"是否有效"`
CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"`
}
func (t *UserRole) TableName() string {
return "user_role"
}
//EnableStatus 是否有效
const (
USER_ROLE_ENABLE_YES int8 = 1 //有效
USER_ROLE_ENABLE_NO int8 = 2 //无效
)
func (t *UserRole) IsEnable() bool {
switch t.EnableStatus {
case USER_ROLE_ENABLE_YES:
return true
case USER_ROLE_ENABLE_NO:
return false
}
return false
}
func (t *UserRole) ValidCompanyRole() error {
depart, err := GetRoleById(t.RoleId)
if err != nil {
return err
}
if depart.CompanyId != t.CompanyId {
e := fmt.Errorf("role.CompanyId != param.CompanyId ")
return e
}
return nil
}
func init() {
orm.RegisterModel(new(UserRole))
}
// 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()
id, err = o.Insert(m)
return
}
// GetUserRoleById retrieves UserRole by Id. Returns error if
// Id doesn't exist
func GetUserRoleById(id int) (v *UserRole, err error) {
o := orm.NewOrm()
v = &UserRole{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateUserRole updates UserRole by Id and returns error if
// the record to be updated doesn't exist
func UpdateUserRoleById(m *UserRole) (err error) {
o := orm.NewOrm()
v := UserRole{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
// DeleteUserRole deletes UserRole by Id and returns error if
// the record to be deleted doesn't exist
func DeleteUserRole(id int) (err error) {
o := orm.NewOrm()
v := UserRole{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&UserRole{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
... ...
... ... @@ -70,6 +70,7 @@ type RequestPositionDelete struct {
IDs []int64 `json:"ids"`
}
//RequestUserAdd 添加用户
type RequestUserAdd struct {
Name string `json:"name"`
CompanyId int64 `json:"company_id"`
... ... @@ -79,6 +80,7 @@ type RequestUserAdd struct {
Roles []int64 `json:"roles"`
}
//RequestUserEdit 编辑用户
type RequestUserEdit struct {
ID int64 `json:"id"`
RequestUserAdd
... ...
package protocol
import (
"encoding/json"
)
//CustomErrParse 解析自定义错误结构体
type CustomErrParse interface {
ParseToMessage() *ResponseMessage
... ... @@ -72,8 +68,8 @@ func NewErrWithMessage(code string, eRR ...error) *ErrWithMessage {
//Error 实现接口error 中的方法
//将ErrorCode转为json数据,建议用于日志记录
func (e ErrWithMessage) Error() string {
bt, _ := json.Marshal(e.ErrorCode)
return string(bt)
return e.Errmsg
}
//Unwrap 接口实现
... ...
... ... @@ -17,6 +17,10 @@ var errmessge ErrorMap = map[string]string{
"10025": "该账号已在其他地方登录",
"10026": "登录凭证过期",
"10027": "无操作权限",
//用户相关
"10031": "无效角色",
"10032": "无效部门",
"10033": "无效职位",
}
//错误码转换 ,兼容需要
... ...
... ... @@ -34,6 +34,12 @@ func init() {
beego.NSRouter("/role_group/delete", &controllers.RbacController{}, "post:RoleGroupDelete"),
beego.NSRouter("/role", &controllers.RbacController{}, "get:RoleList"),
),
beego.NSNamespace("/user/",
// beego.NSRouter("/list", &controllers.CompanyController{}, "post:UserList"),
beego.NSRouter("/add", &controllers.CompanyController{}, "post:UserAdd"),
beego.NSRouter("/edit", &controllers.CompanyController{}, "post:UserEdit"),
// beego.NSRouter("/delete", &controllers.CompanyController{}, "post:PositionDelete"),
),
beego.NSNamespace("/auth",
beego.NSRouter("/change_company", &controllers.AuthController{}, "post:ChangeCompany"),
beego.NSRouter("/refresh_token", &controllers.AuthController{}, "get:RefreshToken"),
... ...
... ... @@ -43,44 +43,44 @@ func validatePassword(from, to string) bool {
//LoginAuth 登录认证
//TODO 登录校验逻辑修改
func LoginAuthByPassword(account, password string) (protocol.LoginAuthToken, error) {
var (
user *models.User
companys []models.Company
mcompany models.Company
loginToken protocol.LoginAuthToken
err error
)
user, err = models.GetUserByPhone(account)
if err != nil {
log.Error(err.Error())
return loginToken, protocol.NewErrWithMessage("10021", err)
}
if ok := validatePassword(password, user.Passwd); !ok {
return loginToken, protocol.NewErrWithMessage("10021", err)
}
if ok := user.IsEnable(); !ok {
return loginToken, protocol.NewErrWithMessage("10022")
}
companys, err = models.GetCompanyByUser(user.Id)
if err != nil {
e := fmt.Errorf("GetCompanyByUser(%d) err:%s", user.Id, err)
log.Error(e.Error())
return loginToken, protocol.NewErrWithMessage("1")
}
if len(companys) <= 0 {
log.Error("can not found company")
return loginToken, protocol.NewErrWithMessage("1")
}
mcompany = companys[0]
loginToken, err = GenerateAuthToken(user.Id, mcompany.Id)
if err != nil {
e := fmt.Errorf("GenerateAuthToken err:%s", err)
log.Error(e.Error())
return loginToken, protocol.NewErrWithMessage("1")
}
return loginToken, nil
}
// func LoginAuthByPassword(account, password string) (protocol.LoginAuthToken, error) {
// var (
// user *models.User
// companys []models.Company
// mcompany models.Company
// loginToken protocol.LoginAuthToken
// err error
// )
// user, err = models.GetUserByPhone(account)
// if err != nil {
// log.Error(err.Error())
// return loginToken, protocol.NewErrWithMessage("10021", err)
// }
// if ok := validatePassword(password, user.Passwd); !ok {
// return loginToken, protocol.NewErrWithMessage("10021", err)
// }
// if ok := user.IsEnable(); !ok {
// return loginToken, protocol.NewErrWithMessage("10022")
// }
// companys, err = models.GetCompanyByUser(user.Id)
// if err != nil {
// e := fmt.Errorf("GetCompanyByUser(%d) err:%s", user.Id, err)
// log.Error(e.Error())
// return loginToken, protocol.NewErrWithMessage("1")
// }
// if len(companys) <= 0 {
// log.Error("can not found company")
// return loginToken, protocol.NewErrWithMessage("1")
// }
// mcompany = companys[0]
// loginToken, err = GenerateAuthToken(user.Id, mcompany.Id)
// if err != nil {
// e := fmt.Errorf("GenerateAuthToken err:%s", err)
// log.Error(e.Error())
// return loginToken, protocol.NewErrWithMessage("1")
// }
// return loginToken, nil
// }
//ResetLoginToken token存数据库
func ResetLoginToken(loginToken protocol.LoginAuthToken) error {
... ... @@ -236,6 +236,10 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro
log.Debug("GetUserByPhone(%s) err:%s", account, err)
return logintoken, protocol.NewErrWithMessage("10021")
}
if ok := userdata.IsDelete(); ok {
log.Debug("userdata.IsDelete()==true")
return logintoken, protocol.NewErrWithMessage("10021")
}
if ok := userdata.IsEnable(); !ok {
log.Debug("userdata.IsEnable()==false")
return logintoken, protocol.NewErrWithMessage("10021")
... ...
... ... @@ -5,80 +5,336 @@ import (
"oppmg/common/log"
"oppmg/models"
"oppmg/protocol"
"oppmg/utils"
"time"
"github.com/astaxie/beego/orm"
)
func UserAdd(param protocol.RequestUserAdd) error {
var (
// user models.User
// userRoles []models.RoleRelated
// userPositions []models.UserPosition
// userDeparts []models.UserPosition
userm *models.User
err error
)
//校验角色
validCompanyRole(param.CompanyId, param.Roles)
err = validCompanyRole(param.CompanyId, param.Roles)
if err != nil {
log.Error(err.Error())
return protocol.NewErrWithMessage("10031")
}
//校验部门
validCompanyDepart(param.CompanyId, param.Departments)
err = validCompanyDepart(param.CompanyId, param.Departments)
if err != nil {
log.Error(err.Error())
return protocol.NewErrWithMessage("10032")
}
//校验职位
validCompanyPositions(param.CompanyId, param.Positions)
//添加用户
err = validCompanyPosition(param.CompanyId, param.Positions)
if err != nil {
log.Error(err.Error())
return protocol.NewErrWithMessage("10033")
}
userm = &models.User{
NickName: param.Name,
Phone: param.Phone,
}
o := orm.NewOrm()
o.Begin()
err = registUser(userm, param.CompanyId, o)
if err != nil {
o.Rollback()
log.Error("registUser err:%s", err)
return protocol.NewErrWithMessage("1")
}
//添加角色
err = editUserRole(userm.Id, param.CompanyId, param.Roles, o)
if err != nil {
o.Rollback()
log.Error("editUserRole err:%s", err)
return protocol.NewErrWithMessage("1")
}
//添加部门
err = editUserDepart(userm.Id, param.CompanyId, param.Departments, o)
if err != nil {
o.Rollback()
log.Error("editUserDepart err:%s", err)
return protocol.NewErrWithMessage("1")
}
//添加职位
err = editUserDepart(userm.Id, param.CompanyId, param.Positions, o)
if err != nil {
o.Rollback()
log.Error("editUserDepart err:%s", err)
return protocol.NewErrWithMessage("1")
}
o.Commit()
return nil
}
func validCompanyRole(companyid int64, roleid []int64) error {
var (
err error
)
for _, v := range roleid {
ur := models.UserRole{
CompanyId: companyid,
RoleId: v,
EnableStatus: models.USER_ROLE_ENABLE_YES,
}
err = ur.ValidCompanyRole()
if err != nil {
e := fmt.Errorf("ValidCompanyRole err:%s [company:%d,role:%d]", err, companyid, v)
log.Error(e.Error())
return protocol.NewErrWithMessage("10031")
}
}
return nil
}
func validCompanyRole(companyId int64, roleIds []int64) error {
for _, v := range roleIds {
role, err := models.GetRoleById(v)
func validCompanyDepart(companyid int64, departid []int64) error {
var (
err error
)
for _, v := range departid {
ud := models.UserDepartment{
CompanyId: companyid,
DepartmentId: v,
EnableStatus: models.USER_DEPARTMENT_ENABLE_YES,
}
err = ud.ValidCompanyDepart()
if err != nil {
e := fmt.Errorf("GetRoleById(%d) err:%s", v, err)
e := fmt.Errorf("ValidCompanyDepart err:%s [company:%d,department:%d]", err, companyid, v)
log.Error(e.Error())
return protocol.NewErrWithMessage("1", err)
return protocol.NewErrWithMessage("10032")
}
if role.CompanyId != companyId {
e := fmt.Errorf(" role.CompanyId != param.CompanyId ")
}
return nil
}
func validCompanyPosition(companyid int64, positionid []int64) error {
var (
err error
)
for _, v := range positionid {
ud := models.UserPosition{
CompanyId: companyid,
PositionId: v,
EnableStatus: models.USER_DEPARTMENT_ENABLE_YES,
}
err = ud.ValidCompanyPosition()
if err != nil {
e := fmt.Errorf("ValidCompanyPosition err:%s [company:%d,department:%d]", err, companyid, v)
log.Error(e.Error())
return protocol.NewErrWithMessage("1", err)
return protocol.NewErrWithMessage("10033")
}
}
return nil
}
//registUser 注册用户
func registUser(userIn *models.User, companyid int64, o orm.Ormer) error {
var (
err error
// usrData *models.User
)
_, err = models.GetUserByPhone(userIn.Phone)
if err == nil {
return nil
}
if err != nil && err != orm.ErrNoRows {
return err
}
if err == orm.ErrNoRows {
_, err := models.AddUser(userIn, o)
if err != nil {
return err
}
}
musercompany := &models.UserCompany{
CompanyId: companyid,
UserId: userIn.Id,
}
_, err = models.AddUserCompany(musercompany, o)
if err != nil {
return err
}
//更新
return nil
}
func validCompanyDepart(companyId int64, departIds []int64) error {
for _, v := range departIds {
depart, err := models.GetDepartmentById(v)
func editUserDepart(userid int64, companyid int64, departids []int64, o orm.Ormer) error {
var (
oldDepartIds []int64
err error
delDepartIds []int64
addDepartIds []int64
)
dataSql := `SELECT department_id FROM user_department WHERE enable_status=1 AND company_id=? AND user_id=?`
dataSql2 := `UPDATE user_department SET enable_status = 2 WHERE company_id = ? AND user_id=? AND department_id=?`
datasql3 := `INSERT INTO user_department (user_id,department_id,company_id,enable_status,create_at) VALUES (?,?,?,?,?)`
err = utils.ExecuteQueryAllWithOrmer(o, &oldDepartIds, dataSql, companyid, userid)
if err != nil {
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return e
}
delDepartIds = utils.ArrayInt64Diff(oldDepartIds, departids)
addDepartIds = utils.ArrayInt64Diff(departids, oldDepartIds)
for _, v := range delDepartIds {
err = utils.ExecuteSQLWithOrmer(o, dataSql2, companyid, userid, v)
if err != nil {
e := fmt.Errorf("GetDepartmentById(%d) err:%s", v, err)
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return protocol.NewErrWithMessage("1", err)
return e
}
if depart.CompanyId != companyId {
e := fmt.Errorf(" depart.CompanyId != param.CompanyId ")
}
nowTime := time.Now().String()
for _, v := range addDepartIds {
err = utils.ExecuteSQLWithOrmer(o, datasql3, userid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime)
if err != nil {
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return protocol.NewErrWithMessage("1", err)
return e
}
}
return nil
}
func validCompanyPositions(companyId int64, positionIds []int64) error {
for _, v := range positionIds {
pos, err := models.GetPositionById(v)
func editUserPosition(userid int64, companyid int64, positionids []int64, o orm.Ormer) error {
var (
oldPositionIds []int64
err error
delPositionIds []int64
addPositionIds []int64
)
dataSql := `SELECT position_id FROM user_position WHERE enable_status=1 AND company_id=? AND user_id=?`
dataSql2 := `UPDATE user_position SET enable_status = 2 WHERE company_id = ? AND user_id=? AND position_id=?`
datasql3 := `INSERT INTO user_position (user_id,position_id,company_id,enable_status,create_at) VALUES (?,?,?,?,?)`
err = utils.ExecuteQueryAllWithOrmer(o, &oldPositionIds, dataSql, companyid, userid)
if err != nil {
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return e
}
delPositionIds = utils.ArrayInt64Diff(oldPositionIds, positionids)
addPositionIds = utils.ArrayInt64Diff(positionids, oldPositionIds)
for _, v := range delPositionIds {
err = utils.ExecuteSQLWithOrmer(o, dataSql2, companyid, userid, v)
if err != nil {
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return e
}
}
nowTime := time.Now().String()
for _, v := range addPositionIds {
err = utils.ExecuteSQLWithOrmer(o, datasql3, userid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime)
if err != nil {
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return e
}
}
return nil
}
func editUserRole(userid int64, companyid int64, roleids []int64, o orm.Ormer) error {
var (
oldRoleIds []int64
err error
delRoleIds []int64
addRoleIds []int64
)
dataSql := `SELECT role_id FROM user_role WHERE enable_status=1 AND company_id=? AND user_id=?`
dataSql2 := `UPDATE user_position SET enable_status = 2 WHERE company_id =? AND user_id=? AND role_id=?`
datasql3 := `INSERT INTO user_role (user_id,role_id,company_id,enable_status) VALUES (?,?,?,?)`
err = utils.ExecuteQueryAllWithOrmer(o, &oldRoleIds, dataSql, companyid, userid)
if err != nil {
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return e
}
delRoleIds = utils.ArrayInt64Diff(oldRoleIds, roleids)
addRoleIds = utils.ArrayInt64Diff(roleids, oldRoleIds)
for _, v := range delRoleIds {
err = utils.ExecuteSQLWithOrmer(o, dataSql2, companyid, userid, v)
if err != nil {
e := fmt.Errorf("GetDepartmentById(%d) err:%s", v, err)
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return protocol.NewErrWithMessage("1", err)
return e
}
if pos.CompanyId != companyId {
e := fmt.Errorf("pos.CompanyId != pos.CompanyId")
}
for _, v := range addRoleIds {
err = utils.ExecuteSQLWithOrmer(o, datasql3, userid, v, companyid, models.USER_POSITION_ENABLE_YES)
if err != nil {
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return protocol.NewErrWithMessage("1", err)
return e
}
}
return nil
}
func UserEdit(param protocol.RequestUserEdit) error {
var (
userm *models.User
err error
)
//校验角色
err = validCompanyRole(param.CompanyId, param.Roles)
if err != nil {
log.Error(err.Error())
return protocol.NewErrWithMessage("10031")
}
//校验部门
err = validCompanyDepart(param.CompanyId, param.Departments)
if err != nil {
log.Error(err.Error())
return protocol.NewErrWithMessage("10032")
}
//校验职位
err = validCompanyPosition(param.CompanyId, param.Positions)
if err != nil {
log.Error(err.Error())
return protocol.NewErrWithMessage("10033")
}
userm = &models.User{
Id: param.ID,
NickName: param.Name,
Phone: param.Phone,
}
o := orm.NewOrm()
o.Begin()
err = registUser(userm, param.CompanyId, o)
if err != nil {
o.Rollback()
log.Error("registUser err:%s", err)
return protocol.NewErrWithMessage("1")
}
//添加角色
err = editUserRole(userm.Id, param.CompanyId, param.Roles, o)
if err != nil {
o.Rollback()
log.Error("editUserRole err:%s", err)
return protocol.NewErrWithMessage("1")
}
//添加部门
err = editUserDepart(userm.Id, param.CompanyId, param.Departments, o)
if err != nil {
o.Rollback()
log.Error("editUserDepart err:%s", err)
return protocol.NewErrWithMessage("1")
}
//添加职位
err = editUserDepart(userm.Id, param.CompanyId, param.Positions, o)
if err != nil {
o.Rollback()
log.Error("editUserDepart err:%s", err)
return protocol.NewErrWithMessage("1")
}
o.Commit()
return nil
}
... ...
... ... @@ -9,6 +9,9 @@ type ArrayCmpare interface {
//ArrayInt64Diff 返回切片的差集:arr1-arr2
func ArrayInt64Diff(arr1 []int64, arr2 []int64) []int64 {
if len(arr2) == 0 {
return arr1
}
setmap := make(map[int64]bool)
for i := range arr2 {
setmap[arr1[i]] = true
... ...