作者 唐旭辉

角色,部门相关修改

... ... @@ -2,6 +2,7 @@ package models
import (
"encoding/json"
"errors"
"fmt"
"oppmg/common/log"
"time"
... ... @@ -44,6 +45,18 @@ func (t *Department) ParseManagesIds(v []int64) string {
return string(bt)
}
func (t *Department) SetRelation(parent *Department) error {
if t.Id == 0 {
return errors.New("Id==0")
}
if parent == nil {
t.Relation = fmt.Sprintf("%d", t.Id)
} else {
t.Relation = fmt.Sprintf("%s/%d", parent.Relation, t.Id)
}
return nil
}
// AddDepartment insert a new Department into database and returns
// last inserted Id on success.
func AddDepartment(m *Department) (id int64, err error) {
... ...
package models
import (
"errors"
"oppmg/common/log"
"time"
... ... @@ -26,6 +27,35 @@ func init() {
orm.RegisterModel(new(Role))
}
const (
ROLETYPES_GROUP int8 = 1
ROLETYPES_ROLE int8 = 2
)
func (t *Role) ValidateTypes() bool {
switch i {
case ROLETYPES_GROUP:
return true
case ROLETYPES_ROLE:
return true
}
return false
}
func (t *Role) ValidatePid() error {
roledata, err := GetRoleById(t.Pid)
if err != nil {
return err
}
if roledata.Types != ROLETYPES_GROUP {
return errors.New("roledata.Types != ROLETYPES_GROUP")
}
if roledata.CompanyId != t.CompanyId {
return errors.New("validate companyId err")
}
return nil
}
// AddRole insert a new Role into database and returns
// last inserted Id on success.
func AddRole(m *Role) (id int64, err error) {
... ... @@ -47,13 +77,13 @@ func GetRoleById(id int) (v *Role, err error) {
// UpdateRole updates Role by Id and returns error if
// the record to be updated doesn't exist
func UpdateRoleById(m *Role) (err error) {
func UpdateRoleById(m *Role, col []string) (err error) {
o := orm.NewOrm()
v := Role{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 {
if num, err = o.Update(m, col...); err == nil {
log.Debug("Number of records updated in database:%d", num)
}
}
... ...
... ... @@ -11,8 +11,6 @@ type UserCompany struct {
Id int64 `orm:"column(id);auto" description:"唯一标识"`
CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"`
UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"`
DepartmentId int `orm:"column(department_id)" description:"部门id"`
PositionId int `orm:"column(position_id)" description:"职位id"`
ChanceTotal int `orm:"column(chance_total)" description:"发表机会数"`
CommentTotal int `orm:"column(comment_total)" description:"发表评论总数"`
CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
... ...
... ... @@ -3,7 +3,9 @@ package protocol
//RequestRoleAdd 添加角色信息操作入参
type RequestRoleAdd struct {
CompanyID int `json:"company"`
Pid int `json:"pid"`
Name string `json:"name"`
Types int8 `json:"types"`
Descript string `json:"descript"`
}
... ... @@ -28,8 +30,10 @@ type RequestRoleOne struct {
//ResponseRoleInfo 响应数据
type ResponseRoleInfo struct {
ID int `json:"id"`
Pid int `json:"pid"`
Name string `json:"name"`
Descript string `json:"descript"`
Types int `json:"types"`
CreateTime int64 `json:"create_time`
UpdateTime int64 `json:"update_time"`
}
... ...
... ... @@ -12,10 +12,47 @@ import (
"github.com/astaxie/beego/orm"
)
func DepartmentAdd(param protocol.RequestDepartmentAdd) (protocol.ResponseDepartmentInfo, error) {
r := protocol.ResponseDepartmentInfo{}
return r, nil
func DepartmentAdd(param protocol.RequestDepartmentAdd) error {
var (
parentDepart *models.Department
err error
)
if param.ParantID > 0 {
parentDepart, err = models.GetDepartmentById(param.ParantID)
if err != nil {
e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ParantID, err)
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
}
if parentDepart.CompanyId != param.CompanyID {
e := fmt.Errorf("parentDepart.CompanyId != param.CompanyID")
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
}
}
for _, v := range param.Managers {
_, err = models.GetUserCompanyBy(v, param.CompanyID)
if err != nil {
e := fmt.Errorf("GetUserCompanyBy(%d, %d)", v, param.CompanyID)
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
}
}
departmentAdd := &models.Department{
CompanyId: param.CompanyID,
Name: param.Name,
CreateAt: time.Now(),
ParentId: param.ParantID,
Member: 0,
}
departmentAdd.SetRelation(parentDepart)
_, err = models.AddDepartment(departmentAdd)
if err != nil {
e := fmt.Errorf("AddDepartment err:%s", err)
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
}
return nil
}
... ... @@ -74,7 +111,7 @@ func DepartmentEdit(param protocol.RequestDepartmentEdit) error {
//更新部门关系数据
err = departmentRelationUpdate(departUpdate, newparent)
if err != nil {
e := fmt.Errorf(" departmentRelationUpdate err:%s", err)
e := fmt.Errorf("departmentRelationUpdate err:%s", err)
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
}
... ... @@ -107,7 +144,16 @@ func departmentRelationUpdate(old *models.Department, newparent *models.Departme
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
}
for i := range departSubset {
if departSubset[i].Id == newparent.Id {
//确认新的父级id是否合法
o.Rollback()
e := fmt.Errorf("departSubset[i].Id == newparent.Id")
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
}
//重建关系树
s := strings.TrimPrefix(departSubset[i].Relation, oldrelation)
departSubset[i].Relation = strings.TrimSpace(fmt.Sprintf("%s%s", newRelation, s))
err := utils.ExcuteSQLWithOrmer(o, dataSql2, departSubset[i].Relation, departSubset[i].Id)
... ...
... ... @@ -14,8 +14,21 @@ func RoleAdd(param protocol.RequestRoleAdd) (*protocol.ResponseRoleInfo, error)
CompanyId: param.CompanyID,
Name: param.Name,
CreateAt: time.Now(),
Pid: param.Pid,
Types: param.Types,
Descript: param.Descript,
}
if ok := role.ValidateTypes(); !ok {
e := fmt.Errorf("ValidateTypes err")
log.Error(e.Error())
return nil, protocol.NewErrWithMessage("1", e)
}
err := role.ValidatePid()
if err != nil {
e := fmt.Errorf("ValidatePid err:%s", err)
log.Error(e.Error())
return nil, protocol.NewErrWithMessage("1", e)
}
roleid, err := models.AddRole(&role)
if err != nil {
log.Error("AddRole err:%s", err)
... ... @@ -68,7 +81,7 @@ func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error
}
role.Descript = param.Descript
role.Name = param.Name
if err = models.UpdateRoleById(role); err != nil {
if err = models.UpdateRoleById(role, []string{"Descript", "Name"}); err != nil {
e := fmt.Errorf("UpdateRoleById err:%s", err)
log.Error(e.Error())
return nil, protocol.NewErrWithMessage("1", e)
... ...