作者 唐旭辉

修复错误

@@ -28,8 +28,8 @@ func init() { @@ -28,8 +28,8 @@ func init() {
28 } 28 }
29 29
30 const ( 30 const (
31 - ROLETYPES_GROUP int8 = 1  
32 - ROLETYPES_ROLE int8 = 2 31 + ROLETYPES_GROUP int8 = 1 //角色组
  32 + ROLETYPES_ROLE int8 = 2 //角色
33 ) 33 )
34 34
35 func (t *Role) ValidateTypes() bool { 35 func (t *Role) ValidateTypes() bool {
@@ -42,18 +42,18 @@ func (t *Role) ValidateTypes() bool { @@ -42,18 +42,18 @@ func (t *Role) ValidateTypes() bool {
42 return false 42 return false
43 } 43 }
44 44
45 -func (t *Role) ValidatePid() error { 45 +func (t *Role) ValidatePid() (*Role, error) {
46 roledata, err := GetRoleById(t.Pid) 46 roledata, err := GetRoleById(t.Pid)
47 if err != nil { 47 if err != nil {
48 - return err 48 + return nil, err
49 } 49 }
50 if roledata.Types != ROLETYPES_GROUP { 50 if roledata.Types != ROLETYPES_GROUP {
51 - return errors.New("roledata.Types != ROLETYPES_GROUP") 51 + return nil, errors.New("roledata.Types != ROLETYPES_GROUP")
52 } 52 }
53 if roledata.CompanyId != t.CompanyId { 53 if roledata.CompanyId != t.CompanyId {
54 - return errors.New("validate companyId err") 54 + return nil, errors.New("validate companyId err")
55 } 55 }
56 - return nil 56 + return roledata, nil
57 } 57 }
58 58
59 // AddRole insert a new Role into database and returns 59 // AddRole insert a new Role into database and returns
@@ -93,7 +93,7 @@ func UpdateRoleById(m *Role, col []string) (err error) { @@ -93,7 +93,7 @@ func UpdateRoleById(m *Role, col []string) (err error) {
93 93
94 // DeleteRole deletes Role by Id and returns error if 94 // DeleteRole deletes Role by Id and returns error if
95 // the record to be deleted doesn't exist 95 // the record to be deleted doesn't exist
96 -func DeleteRole(id int) (err error) { 96 +func DeleteRoleByID(id int) (err error) {
97 o := orm.NewOrm() 97 o := orm.NewOrm()
98 v := Role{Id: id} 98 v := Role{Id: id}
99 // ascertain id exists in the database 99 // ascertain id exists in the database
@@ -101,18 +101,19 @@ func DeleteRole(id int) (err error) { @@ -101,18 +101,19 @@ func DeleteRole(id int) (err error) {
101 var num int64 101 var num int64
102 v.DeleteAt = time.Now() 102 v.DeleteAt = time.Now()
103 if num, err = o.Update(&v); err == nil { 103 if num, err = o.Update(&v); err == nil {
104 - log.Debug("Number of records deleted in database:%d", num) 104 + log.Debug("Number of records updated in database:%d", num)
105 } 105 }
106 } 106 }
107 return 107 return
108 } 108 }
109 109
110 func DeleteRoleByPid(pid int) (err error) { 110 func DeleteRoleByPid(pid int) (err error) {
111 - // o := orm.NewOrm()  
112 -  
113 - // var num int64  
114 - // deleteAt = time.Now()  
115 - // o.QueryTable(&Role{}).Filter("pid", pid).Update(orm.Params{})  
116 - 111 + o := orm.NewOrm()
  112 + var num int64
  113 + deleteAt := time.Now()
  114 + num, err = o.QueryTable(&Role{}).Filter("pid", pid).Update(orm.Params{
  115 + "delete_at": deleteAt,
  116 + })
  117 + log.Debug("Number of records updated in database:%d", num)
117 return 118 return
118 } 119 }
@@ -31,11 +31,11 @@ type RequestRoleOne struct { @@ -31,11 +31,11 @@ type RequestRoleOne struct {
31 type ResponseRoleInfo struct { 31 type ResponseRoleInfo struct {
32 ID int `json:"id"` 32 ID int `json:"id"`
33 Pid int `json:"pid"` 33 Pid int `json:"pid"`
  34 + ParentName string `json:"parent_name"`
34 Name string `json:"name"` 35 Name string `json:"name"`
35 Descript string `json:"descript"` 36 Descript string `json:"descript"`
36 - Types int `json:"types"` 37 + Types int8 `json:"types"`
37 CreateTime int64 `json:"create_time` 38 CreateTime int64 `json:"create_time`
38 - UpdateTime int64 `json:"update_time"`  
39 } 39 }
40 40
41 //RequestRoleList 分页获取角色列表 41 //RequestRoleList 分页获取角色列表
@@ -9,10 +9,11 @@ import ( @@ -9,10 +9,11 @@ import (
9 ) 9 )
10 10
11 func RoleAdd(param protocol.RequestRoleAdd) (*protocol.ResponseRoleInfo, error) { 11 func RoleAdd(param protocol.RequestRoleAdd) (*protocol.ResponseRoleInfo, error) {
  12 + nowTime := time.Now()
12 role := models.Role{ 13 role := models.Role{
13 CompanyId: param.CompanyID, 14 CompanyId: param.CompanyID,
14 Name: param.Name, 15 Name: param.Name,
15 - CreateAt: time.Now(), 16 + CreateAt: nowTime,
16 Pid: param.Pid, 17 Pid: param.Pid,
17 Types: param.Types, 18 Types: param.Types,
18 Descript: param.Descript, 19 Descript: param.Descript,
@@ -22,21 +23,30 @@ func RoleAdd(param protocol.RequestRoleAdd) (*protocol.ResponseRoleInfo, error) @@ -22,21 +23,30 @@ func RoleAdd(param protocol.RequestRoleAdd) (*protocol.ResponseRoleInfo, error)
22 log.Error(e.Error()) 23 log.Error(e.Error())
23 return nil, protocol.NewErrWithMessage("1", e) 24 return nil, protocol.NewErrWithMessage("1", e)
24 } 25 }
25 - err := role.ValidatePid() 26 + var (
  27 + parentRole *models.Role
  28 + err error
  29 + roleid int64
  30 + )
  31 + parentRole, err = role.ValidatePid()
26 if err != nil { 32 if err != nil {
27 e := fmt.Errorf("ValidatePid err:%s", err) 33 e := fmt.Errorf("ValidatePid err:%s", err)
28 log.Error(e.Error()) 34 log.Error(e.Error())
29 return nil, protocol.NewErrWithMessage("1", e) 35 return nil, protocol.NewErrWithMessage("1", e)
30 } 36 }
31 - roleid, err := models.AddRole(&role) 37 + roleid, err = models.AddRole(&role)
32 if err != nil { 38 if err != nil {
33 log.Error("AddRole err:%s", err) 39 log.Error("AddRole err:%s", err)
34 return nil, protocol.NewErrWithMessage("1", err) 40 return nil, protocol.NewErrWithMessage("1", err)
35 } 41 }
36 r := &protocol.ResponseRoleInfo{ 42 r := &protocol.ResponseRoleInfo{
37 - ID: int(roleid),  
38 - Name: role.Name,  
39 - Descript: role.Descript, 43 + ID: int(roleid),
  44 + Name: role.Name,
  45 + Descript: role.Descript,
  46 + Pid: role.Pid,
  47 + ParentName: parentRole.Name,
  48 + Types: role.Types,
  49 + CreateTime: role.CreateAt.Unix(), //Format("2006-01-02 15:04:05")
40 } 50 }
41 return r, nil 51 return r, nil
42 } 52 }
@@ -53,11 +63,15 @@ func RoleDelete(param protocol.RequestRoleDelete) error { @@ -53,11 +63,15 @@ func RoleDelete(param protocol.RequestRoleDelete) error {
53 log.Error(e.Error()) 63 log.Error(e.Error())
54 return protocol.NewErrWithMessage("1", e) 64 return protocol.NewErrWithMessage("1", e)
55 } 65 }
56 - //TODO 66 + //
57 if role.Types == models.ROLETYPES_GROUP { 67 if role.Types == models.ROLETYPES_GROUP {
58 - 68 + if err := models.DeleteRoleByPid(param.RoleID); err != nil {
  69 + e := fmt.Errorf("DeleteRoleByPid err:%s", err)
  70 + log.Error(e.Error())
  71 + return protocol.NewErrWithMessage("1", e)
  72 + }
59 } 73 }
60 - if err := models.DeleteRole(param.RoleID); err != nil { 74 + if err := models.DeleteRoleByID(param.RoleID); err != nil {
61 e := fmt.Errorf("DeleteRole err:%s", err) 75 e := fmt.Errorf("DeleteRole err:%s", err)
62 log.Error(e.Error()) 76 log.Error(e.Error())
63 return protocol.NewErrWithMessage("1", e) 77 return protocol.NewErrWithMessage("1", e)
@@ -67,8 +81,9 @@ func RoleDelete(param protocol.RequestRoleDelete) error { @@ -67,8 +81,9 @@ func RoleDelete(param protocol.RequestRoleDelete) error {
67 81
68 func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error) { 82 func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error) {
69 var ( 83 var (
70 - role *models.Role  
71 - err error 84 + parentRole *models.Role
  85 + role *models.Role
  86 + err error
72 ) 87 )
73 role, err = models.GetRoleById(param.ID) 88 role, err = models.GetRoleById(param.ID)
74 if err != nil { 89 if err != nil {
@@ -84,7 +99,7 @@ func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error @@ -84,7 +99,7 @@ func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error
84 role.Pid = param.Pid 99 role.Pid = param.Pid
85 role.Descript = param.Descript 100 role.Descript = param.Descript
86 role.Name = param.Name 101 role.Name = param.Name
87 - if err = role.ValidatePid(); err != nil { 102 + if parentRole, err = role.ValidatePid(); err != nil {
88 e := fmt.Errorf("ValidatePid err:%s", err) 103 e := fmt.Errorf("ValidatePid err:%s", err)
89 log.Error(e.Error()) 104 log.Error(e.Error())
90 return nil, protocol.NewErrWithMessage("1", e) 105 return nil, protocol.NewErrWithMessage("1", e)
@@ -95,15 +110,19 @@ func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error @@ -95,15 +110,19 @@ func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error
95 return nil, protocol.NewErrWithMessage("1", e) 110 return nil, protocol.NewErrWithMessage("1", e)
96 } 111 }
97 r := &protocol.ResponseRoleInfo{ 112 r := &protocol.ResponseRoleInfo{
98 - ID: param.ID,  
99 - Name: param.Name,  
100 - Descript: param.Descript, 113 + ID: role.Id,
  114 + Name: role.Name,
  115 + Descript: role.Descript,
  116 + Pid: role.Pid,
  117 + ParentName: parentRole.Name,
  118 + Types: role.Types,
  119 + CreateTime: role.CreateAt.Unix(),
101 } 120 }
102 return r, nil 121 return r, nil
103 } 122 }
104 123
105 func RoleGetOne(param protocol.RequestRoleOne) (*protocol.ResponseRoleInfo, error) { 124 func RoleGetOne(param protocol.RequestRoleOne) (*protocol.ResponseRoleInfo, error) {
106 - 125 + // roledata, err := models.GetRoleById(param.RoleID)
107 return nil, nil 126 return nil, nil
108 } 127 }
109 128