作者 唐旭辉

修复错误

... ... @@ -28,8 +28,8 @@ func init() {
}
const (
ROLETYPES_GROUP int8 = 1
ROLETYPES_ROLE int8 = 2
ROLETYPES_GROUP int8 = 1 //角色组
ROLETYPES_ROLE int8 = 2 //角色
)
func (t *Role) ValidateTypes() bool {
... ... @@ -42,18 +42,18 @@ func (t *Role) ValidateTypes() bool {
return false
}
func (t *Role) ValidatePid() error {
func (t *Role) ValidatePid() (*Role, error) {
roledata, err := GetRoleById(t.Pid)
if err != nil {
return err
return nil, err
}
if roledata.Types != ROLETYPES_GROUP {
return errors.New("roledata.Types != ROLETYPES_GROUP")
return nil, errors.New("roledata.Types != ROLETYPES_GROUP")
}
if roledata.CompanyId != t.CompanyId {
return errors.New("validate companyId err")
return nil, errors.New("validate companyId err")
}
return nil
return roledata, nil
}
// AddRole insert a new Role into database and returns
... ... @@ -93,7 +93,7 @@ func UpdateRoleById(m *Role, col []string) (err error) {
// DeleteRole deletes Role by Id and returns error if
// the record to be deleted doesn't exist
func DeleteRole(id int) (err error) {
func DeleteRoleByID(id int) (err error) {
o := orm.NewOrm()
v := Role{Id: id}
// ascertain id exists in the database
... ... @@ -101,18 +101,19 @@ func DeleteRole(id int) (err error) {
var num int64
v.DeleteAt = time.Now()
if num, err = o.Update(&v); err == nil {
log.Debug("Number of records deleted in database:%d", num)
log.Debug("Number of records updated in database:%d", num)
}
}
return
}
func DeleteRoleByPid(pid int) (err error) {
// o := orm.NewOrm()
// var num int64
// deleteAt = time.Now()
// o.QueryTable(&Role{}).Filter("pid", pid).Update(orm.Params{})
o := orm.NewOrm()
var num int64
deleteAt := time.Now()
num, err = o.QueryTable(&Role{}).Filter("pid", pid).Update(orm.Params{
"delete_at": deleteAt,
})
log.Debug("Number of records updated in database:%d", num)
return
}
... ...
... ... @@ -31,11 +31,11 @@ type RequestRoleOne struct {
type ResponseRoleInfo struct {
ID int `json:"id"`
Pid int `json:"pid"`
ParentName string `json:"parent_name"`
Name string `json:"name"`
Descript string `json:"descript"`
Types int `json:"types"`
Types int8 `json:"types"`
CreateTime int64 `json:"create_time`
UpdateTime int64 `json:"update_time"`
}
//RequestRoleList 分页获取角色列表
... ...
... ... @@ -9,10 +9,11 @@ import (
)
func RoleAdd(param protocol.RequestRoleAdd) (*protocol.ResponseRoleInfo, error) {
nowTime := time.Now()
role := models.Role{
CompanyId: param.CompanyID,
Name: param.Name,
CreateAt: time.Now(),
CreateAt: nowTime,
Pid: param.Pid,
Types: param.Types,
Descript: param.Descript,
... ... @@ -22,21 +23,30 @@ func RoleAdd(param protocol.RequestRoleAdd) (*protocol.ResponseRoleInfo, error)
log.Error(e.Error())
return nil, protocol.NewErrWithMessage("1", e)
}
err := role.ValidatePid()
var (
parentRole *models.Role
err error
roleid int64
)
parentRole, 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)
roleid, err = models.AddRole(&role)
if err != nil {
log.Error("AddRole err:%s", err)
return nil, protocol.NewErrWithMessage("1", err)
}
r := &protocol.ResponseRoleInfo{
ID: int(roleid),
Name: role.Name,
Descript: role.Descript,
ID: int(roleid),
Name: role.Name,
Descript: role.Descript,
Pid: role.Pid,
ParentName: parentRole.Name,
Types: role.Types,
CreateTime: role.CreateAt.Unix(), //Format("2006-01-02 15:04:05")
}
return r, nil
}
... ... @@ -53,11 +63,15 @@ func RoleDelete(param protocol.RequestRoleDelete) error {
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
}
//TODO
//
if role.Types == models.ROLETYPES_GROUP {
if err := models.DeleteRoleByPid(param.RoleID); err != nil {
e := fmt.Errorf("DeleteRoleByPid err:%s", err)
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
}
}
if err := models.DeleteRole(param.RoleID); err != nil {
if err := models.DeleteRoleByID(param.RoleID); err != nil {
e := fmt.Errorf("DeleteRole err:%s", err)
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
... ... @@ -67,8 +81,9 @@ func RoleDelete(param protocol.RequestRoleDelete) error {
func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error) {
var (
role *models.Role
err error
parentRole *models.Role
role *models.Role
err error
)
role, err = models.GetRoleById(param.ID)
if err != nil {
... ... @@ -84,7 +99,7 @@ func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error
role.Pid = param.Pid
role.Descript = param.Descript
role.Name = param.Name
if err = role.ValidatePid(); err != nil {
if parentRole, err = role.ValidatePid(); err != nil {
e := fmt.Errorf("ValidatePid err:%s", err)
log.Error(e.Error())
return nil, protocol.NewErrWithMessage("1", e)
... ... @@ -95,15 +110,19 @@ func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error
return nil, protocol.NewErrWithMessage("1", e)
}
r := &protocol.ResponseRoleInfo{
ID: param.ID,
Name: param.Name,
Descript: param.Descript,
ID: role.Id,
Name: role.Name,
Descript: role.Descript,
Pid: role.Pid,
ParentName: parentRole.Name,
Types: role.Types,
CreateTime: role.CreateAt.Unix(),
}
return r, nil
}
func RoleGetOne(param protocol.RequestRoleOne) (*protocol.ResponseRoleInfo, error) {
// roledata, err := models.GetRoleById(param.RoleID)
return nil, nil
}
... ...