作者 唐旭辉

职位,角色相关功能

1 ### 接口完成进度 1 ### 接口完成进度
2 2
3 -| 功能 | 完成状态 | 时间 | 接口路径 | 请求方式 |  
4 -| ---------- | -------- | ---------- | ---------------------- | -------- |  
5 -| 登录 | 进行中 | | |  
6 -| 短信验证码 | 未开始 | | |  
7 -| 获取 token | 未开始 | | |  
8 -| 刷新 token | 未开始 | | |  
9 -| 添加角色 | 完成 | 2019.11.26 | /v1/rbac/role | post |  
10 -| 编辑角色 | 完成 | 2019.11.26 | /v1/rbac/role | put |  
11 -| 删除角色 | 完成 | 2019.11.26 | /v1/rbac/role | delete |  
12 -| 添加部门 | 完成 | 2019.11.28 | /v1/company/department | post |  
13 -| 编辑部门 | 完成 | 2019.11.29 | /v1/company/department | put |  
14 -| 删除部门 | 完成 | 2019.11.29 | /v1/company/department | delete |  
15 -| 添加职位 | 进行中 | | /v1/company/position | post |  
16 -| 编辑职位 | 进行中 | | /v1/company/position | put |  
17 -| 删除职位 | 进行中 | | | delete |  
18 -| 添加员工 | 未开始 | | | post |  
19 -| 编辑员工 | 未开始 | | | put |  
20 -| 删除员工 | 未开始 | | | delete |  
21 -| 公告管理 | 未开始 | | | |  
22 -| 公告管理 | 未开始 | | | |  
23 -| 公告管理 | 未开始 | | | | 3 +| 功能 | 完成状态 | 时间 | 接口路径 | 请求方式 |
  4 +| -------------- | ------------------ | -------------- | -------------------------- | -------- |
  5 +| 登录 | 进行中 | | |
  6 +| 短信验证码 | 未开始 | | |
  7 +| 获取 token | 未开始 | | |
  8 +| 刷新 token | 未开始 | | |
  9 +| 添加角色 | 完成 | 2019.11.26 | /v1/rbac/role | post |
  10 +| 编辑角色 | 完成 | 2019.11.26 | /v1/rbac/role | put |
  11 +| 删除角色 | 完成 | 2019.11.26 | /v1/rbac/role | delete |
  12 +| 添加部门 | 完成 | 2019.11.28 | /v1/company/department | post |
  13 +| 编辑部门 | 完成 | 2019.11.29 | /v1/company/department | put |
  14 +| ~~删除部门~~ | ~~完成(待变更)~~ | ~~2019.11.29~~ | ~~/v1/company/department~~ | delete |
  15 +| 删除部门 | 完成 | 2019.12.2 | /v1/company/department | delete |
  16 +| 添加职位 | 完成 | 2019.12.2 | /v1/company/position | post |
  17 +| 编辑职位 | 完成 | 2019.12.2 | /v1/company/position | put |
  18 +| 删除职位 | 完成 | 2019.12.2 | /v1/company/position | delete |
  19 +| 添加员工 | 未开始 | | /v1/company/user | post |
  20 +| 编辑员工 | 未开始 | | /v1/company/user | put |
  21 +| 删除员工 | 未开始 | | /v1/company/user | delete |
  22 +| 公告管理 | 未开始 | | | |
  23 +| 公告管理 | 未开始 | | | |
  24 +| 公告管理 | 未开始 | | | |
  25 +| 机会&审批 模板 | 未开始 | | | |
  26 +| 机会&审批 模板 | 未开始 | | | |
  27 +| 机会&审批 模板 | 未开始 | | | |
  28 +| 机会&审批 模板 | 未开始 | | | |
1 -机会导向项目  
  1 +机会导向项目
@@ -78,12 +78,62 @@ func (c *CompanyController) DepartmentDelete() { @@ -78,12 +78,62 @@ func (c *CompanyController) DepartmentDelete() {
78 msg = protocol.BadRequestParam("1") 78 msg = protocol.BadRequestParam("1")
79 return 79 return
80 } 80 }
81 - if param.ID <= 0 {  
82 - log.Error(" param.ID <= 0 ") 81 +
  82 + err := servecompany.DepartmentDelete(param)
  83 + msg = protocol.NewReturnResponse(nil, err)
  84 + return
  85 +}
  86 +
  87 +// PositionAdd 添加职位
  88 +// @router /position [post]
  89 +func (c *CompanyController) PositionAdd() {
  90 + var msg *protocol.ResponseMessage
  91 + defer func() {
  92 + c.ResposeJson(msg)
  93 + }()
  94 + var param protocol.RequestPositionAdd
  95 + if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
  96 + log.Error("json 解析失败 err:%s", err)
83 msg = protocol.BadRequestParam("1") 97 msg = protocol.BadRequestParam("1")
84 return 98 return
85 } 99 }
86 - err := servecompany.DepartmentDelete(param) 100 + positioninfo, err := servecompany.PositionAdd(param)
  101 + msg = protocol.NewReturnResponse(positioninfo, err)
  102 + return
  103 +}
  104 +
  105 +// PositionEdit 编辑职位
  106 +// @router /position [put]
  107 +func (c *CompanyController) PositionEdit() {
  108 + var msg *protocol.ResponseMessage
  109 + defer func() {
  110 + c.ResposeJson(msg)
  111 + }()
  112 + var param protocol.RequestPositionEdit
  113 + if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
  114 + log.Error("json 解析失败 err:%s", err)
  115 + msg = protocol.BadRequestParam("1")
  116 + return
  117 + }
  118 + positioninfo, err := servecompany.PositionEdit(param)
  119 + msg = protocol.NewReturnResponse(positioninfo, err)
  120 + return
  121 +}
  122 +
  123 +// PositionDelete 删除职位
  124 +// @router /position [delete]
  125 +func (c *CompanyController) PositionDelete() {
  126 + var msg *protocol.ResponseMessage
  127 + defer func() {
  128 + c.ResposeJson(msg)
  129 + }()
  130 + var param protocol.RequestPositionDelete
  131 + if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
  132 + log.Error("json 解析失败 err:%s", err)
  133 + msg = protocol.BadRequestParam("1")
  134 + return
  135 + }
  136 + err := servecompany.PositionDelete(param)
87 msg = protocol.NewReturnResponse(nil, err) 137 msg = protocol.NewReturnResponse(nil, err)
88 return 138 return
89 } 139 }
@@ -16,7 +16,9 @@ type Position struct { @@ -16,7 +16,9 @@ type Position struct {
16 Relation string `orm:"column(relation);size(1000)" description:"父子级关系树"` 16 Relation string `orm:"column(relation);size(1000)" description:"父子级关系树"`
17 CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"` 17 CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
18 UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"` 18 UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
  19 + DeleteAt time.Time `orm:"column(delete_at);type(timestamp)" description:"删除时间"`
19 EnableStatus string `orm:"column(enable_status);size(255)" description:"有效状态 1:有效 0:无效"` 20 EnableStatus string `orm:"column(enable_status);size(255)" description:"有效状态 1:有效 0:无效"`
  21 + Member int `orm:"column(member)"`
20 } 22 }
21 23
22 func (t *Position) TableName() string { 24 func (t *Position) TableName() string {
@@ -38,16 +40,6 @@ func (t *Position) SetRelation(parent *Position) error { @@ -38,16 +40,6 @@ func (t *Position) SetRelation(parent *Position) error {
38 } 40 }
39 return nil 41 return nil
40 } 42 }
41 -func (t *Position) ValidatePid() error {  
42 - roledata, err := GetPositionById(t.ParentId)  
43 - if err != nil {  
44 - return err  
45 - }  
46 - if roledata.CompanyId != t.CompanyId {  
47 - return errors.New("validate companyId err")  
48 - }  
49 - return nil  
50 -}  
51 43
52 // AddPosition insert a new Position into database and returns 44 // AddPosition insert a new Position into database and returns
53 // last inserted Id on success. 45 // last inserted Id on success.
@@ -17,6 +17,7 @@ type Role struct { @@ -17,6 +17,7 @@ type Role struct {
17 Descript string `orm:"column(descript)"` 17 Descript string `orm:"column(descript)"`
18 CreateAt time.Time `orm:"column(create_at);type(timestamp)"` 18 CreateAt time.Time `orm:"column(create_at);type(timestamp)"`
19 DeleteAt time.Time `orm:"column(delete_at);type(timestamp)"` 19 DeleteAt time.Time `orm:"column(delete_at);type(timestamp)"`
  20 + IsDefault int8 `orm:"column(is_default)"`
20 } 21 }
21 22
22 func (t *Role) TableName() string { 23 func (t *Role) TableName() string {
@@ -27,11 +28,18 @@ func init() { @@ -27,11 +28,18 @@ func init() {
27 orm.RegisterModel(new(Role)) 28 orm.RegisterModel(new(Role))
28 } 29 }
29 30
  31 +//角色类型
30 const ( 32 const (
31 ROLETYPES_GROUP int8 = 1 //角色组 33 ROLETYPES_GROUP int8 = 1 //角色组
32 ROLETYPES_ROLE int8 = 2 //角色 34 ROLETYPES_ROLE int8 = 2 //角色
33 ) 35 )
34 36
  37 +//默认角色
  38 +const (
  39 + ROLE_DEFAULR int8 = 1 //默认
  40 + ROLE_DEFAULR_NOT int8 = 0 // 非默认
  41 +)
  42 +
35 func (t *Role) ValidateTypes() bool { 43 func (t *Role) ValidateTypes() bool {
36 switch t.Types { 44 switch t.Types {
37 case ROLETYPES_GROUP: 45 case ROLETYPES_GROUP:
@@ -43,6 +51,9 @@ func (t *Role) ValidateTypes() bool { @@ -43,6 +51,9 @@ func (t *Role) ValidateTypes() bool {
43 } 51 }
44 52
45 func (t *Role) ValidatePid() (*Role, error) { 53 func (t *Role) ValidatePid() (*Role, error) {
  54 + if t.Pid == 0 {
  55 + return nil, nil
  56 + }
46 roledata, err := GetRoleById(t.Pid) 57 roledata, err := GetRoleById(t.Pid)
47 if err != nil { 58 if err != nil {
48 return nil, err 59 return nil, err
@@ -32,8 +32,8 @@ type RequestDepartmentEdit struct { @@ -32,8 +32,8 @@ type RequestDepartmentEdit struct {
32 32
33 //RequestDepartmentDelete ... 33 //RequestDepartmentDelete ...
34 type RequestDepartmentDelete struct { 34 type RequestDepartmentDelete struct {
35 - ID int64 `json:"id"`  
36 - CompanyID int64 `json:"company_id"` //公司 35 + IDs []int64 `json:"ids"`
  36 + CompanyID int64 `json:"company_id"` //公司
37 } 37 }
38 38
39 //ResponseDepartmentList .... 39 //ResponseDepartmentList ....
@@ -50,6 +50,21 @@ type RequestPositionAdd struct { @@ -50,6 +50,21 @@ type RequestPositionAdd struct {
50 50
51 //RequestPositionEdit 编辑职位 51 //RequestPositionEdit 编辑职位
52 type RequestPositionEdit struct { 52 type RequestPositionEdit struct {
53 - ID int `json:"id"` 53 + ID int64 `json:"id"`
54 RequestPositionAdd 54 RequestPositionAdd
55 } 55 }
  56 +
  57 +//ResponsePositionInfo ...
  58 +type ResponsePositionInfo struct {
  59 + ID int64 `json:"id"`
  60 + CompanyID int64 `json:"company_id"`
  61 + Name string `json:"name"`
  62 + ParentID int64 `json:"parent_id"`
  63 + ParemtName string `json:"parent_name`
  64 + CreateTime int64 `json:"create_time`
  65 +}
  66 +
  67 +type RequestPositionDelete struct {
  68 + CompanyID int64 `json:"company_id"`
  69 + IDs []int64 `json:"ids"`
  70 +}
@@ -15,6 +15,8 @@ func init() { @@ -15,6 +15,8 @@ func init() {
15 beego.NSRouter("/department", &controllers.CompanyController{}, "post:DepartmentAdd"), 15 beego.NSRouter("/department", &controllers.CompanyController{}, "post:DepartmentAdd"),
16 beego.NSRouter("/department", &controllers.CompanyController{}, "put:DepartmentUpdate"), 16 beego.NSRouter("/department", &controllers.CompanyController{}, "put:DepartmentUpdate"),
17 beego.NSRouter("/department", &controllers.CompanyController{}, "delete:DepartmentDelete"), 17 beego.NSRouter("/department", &controllers.CompanyController{}, "delete:DepartmentDelete"),
  18 + beego.NSRouter("/position", &controllers.CompanyController{}, "post:PositionAdd"),
  19 + beego.NSRouter("/position", &controllers.CompanyController{}, "put:PositionEdit"),
18 ), 20 ),
19 beego.NSNamespace("/rbac", 21 beego.NSNamespace("/rbac",
20 beego.NSRouter("/role", &controllers.RbacController{}, "post:RoleAdd"), 22 beego.NSRouter("/role", &controllers.RbacController{}, "post:RoleAdd"),
@@ -126,6 +126,11 @@ func DepartmentEdit(param protocol.RequestDepartmentEdit) error { @@ -126,6 +126,11 @@ func DepartmentEdit(param protocol.RequestDepartmentEdit) error {
126 log.Error(e.Error()) 126 log.Error(e.Error())
127 return protocol.NewErrWithMessage("1", e) 127 return protocol.NewErrWithMessage("1", e)
128 } 128 }
  129 + if newparent.CompanyId != departUpdate.CompanyId {
  130 + e := fmt.Errorf("ParentID err")
  131 + log.Error(e.Error())
  132 + return protocol.NewErrWithMessage("1", e)
  133 + }
129 } 134 }
130 //更新部门关系数据 135 //更新部门关系数据
131 err = departmentRelationUpdate(departUpdate, newparent) 136 err = departmentRelationUpdate(departUpdate, newparent)
@@ -201,56 +206,122 @@ func departmentRelationUpdate(departUpdate *models.Department, newparent *models @@ -201,56 +206,122 @@ func departmentRelationUpdate(departUpdate *models.Department, newparent *models
201 } 206 }
202 207
203 //DepartmentDelete 部门删除,删除整个分枝 208 //DepartmentDelete 部门删除,删除整个分枝
  209 +// func DepartmentDelete(param protocol.RequestDepartmentDelete) error {
  210 +// var (
  211 +// departDelete *models.Department
  212 +// err error
  213 +// )
  214 +// departDelete, err = models.GetDepartmentById(param.ID)
  215 +// if err != nil {
  216 +// e := fmt.Errorf("GetDepartmentById err:%s", err)
  217 +// log.Error(e.Error())
  218 +// return protocol.NewErrWithMessage("1", e)
  219 +// }
  220 +// if departDelete.CompanyId != param.CompanyID {
  221 +// e := fmt.Errorf("departDelete.CompanyId != param.CompanyID")
  222 +// log.Error(e.Error())
  223 +// return protocol.NewErrWithMessage("1", e)
  224 +// }
  225 +// const (
  226 +// //获取部门子集 锁数据 select ... for update,
  227 +// dataSql0 string = `SELECT id,relation,member FROM department WHERE relation LIKE ? AND delete_at = 0 FOR UPDATE`
  228 +// dataSql2 string = `update department set delete_at=? where relation LIKE ?`
  229 +// )
  230 +// var (
  231 +// departSubset []models.Department
  232 +// relationLike string = departDelete.Relation + "%"
  233 +// nowTime string = time.Now().String()
  234 +// )
  235 +// o := orm.NewOrm()
  236 +// o.Begin()
  237 +// err = utils.ExecuteQueryAllWithOrmer(o, &departSubset, dataSql0, relationLike)
  238 +// if err != nil {
  239 +// o.Rollback()
  240 +// e := fmt.Errorf("")
  241 +// log.Error(e.Error())
  242 +// return protocol.NewErrWithMessage("1", e)
  243 +// }
  244 +// for _, v := range departSubset {
  245 +// if v.Member > 0 {
  246 +// o.Rollback()
  247 +// e := fmt.Errorf("Member > 0 ")
  248 +// log.Error(e.Error())
  249 +// return protocol.NewErrWithMessage("1", e)
  250 +// }
  251 +// }
  252 +// err = utils.ExecuteSQLWithOrmer(o, dataSql2, nowTime, relationLike)
  253 +// if err != nil {
  254 +// o.Rollback()
  255 +// e := fmt.Errorf("EXECUTE SQL err:%s", err)
  256 +// log.Error(e.Error())
  257 +// return protocol.NewErrWithMessage("1", e)
  258 +// }
  259 +// o.Commit()
  260 +// return nil
  261 +// }
  262 +
204 func DepartmentDelete(param protocol.RequestDepartmentDelete) error { 263 func DepartmentDelete(param protocol.RequestDepartmentDelete) error {
  264 + //检查是否可以被删除
205 var ( 265 var (
206 - departDelete *models.Department  
207 - err error 266 + //根据参数获取的职位
  267 + departDelete []*models.Department
  268 + //最终需要操作的职位
  269 + toDelete = make(map[int64]*models.Department)
208 ) 270 )
209 - departDelete, err = models.GetDepartmentById(param.ID)  
210 - if err != nil {  
211 - e := fmt.Errorf("GetDepartmentById err:%s", err)  
212 - log.Error(e.Error())  
213 - return protocol.NewErrWithMessage("1", e)  
214 - }  
215 - if departDelete.CompanyId != param.CompanyID {  
216 - e := fmt.Errorf("departDelete.CompanyId != param.CompanyID")  
217 - log.Error(e.Error())  
218 - return protocol.NewErrWithMessage("1", e)  
219 - }  
220 const ( 271 const (
221 - //获取部门子集 锁数据 select ... for update,  
222 - dataSql0 string = `SELECT id,relation,member FROM department WHERE relation LIKE ? AND delete_at = 0 FOR UPDATE` 272 + //获取部门子集,
  273 + dataSql0 string = `SELECT id,relation,member FROM department WHERE relation LIKE ? AND delete_at = 0 `
223 dataSql2 string = `update department set delete_at=? where relation LIKE ?` 274 dataSql2 string = `update department set delete_at=? where relation LIKE ?`
224 ) 275 )
225 - var (  
226 - departSubset []models.Department  
227 - relationLike string = departDelete.Relation + "%"  
228 - nowTime string = time.Now().String()  
229 - ) 276 + for _, id := range param.IDs {
  277 + var p *models.Department
  278 + p, err := models.GetDepartmentById(id)
  279 + if err != nil {
  280 + e := fmt.Errorf("GetDepartmentById(%d)", id)
  281 + log.Error(e.Error())
  282 + return protocol.NewErrWithMessage("1", e)
  283 + }
  284 + if p.CompanyId != param.CompanyID {
  285 + e := fmt.Errorf("CompanyID err")
  286 + log.Error(e.Error())
  287 + return protocol.NewErrWithMessage("1", e)
  288 + }
  289 + departDelete = append(departDelete, p)
  290 + toDelete[p.Id] = p
  291 + }
  292 + for _, pos := range departDelete {
  293 + var departSubset []models.Department
  294 + relationLike := pos.Relation + "%"
  295 + err := utils.ExecuteQueryAll(&departSubset, dataSql0, relationLike)
  296 + if err != nil {
  297 + e := fmt.Errorf("EXECUTE SQL err:%s", err)
  298 + log.Error(e.Error())
  299 + return protocol.NewErrWithMessage("1", e)
  300 + }
  301 + for _, subset := range departSubset {
  302 + if subset.Member > 0 {
  303 + e := fmt.Errorf("user in department,relation:%s", subset.Relation)
  304 + log.Error(e.Error())
  305 + return protocol.NewErrWithMessage("1", e)
  306 + }
  307 + if _, ok := toDelete[subset.Id]; ok {
  308 + delete(toDelete, subset.Id)
  309 + }
  310 + }
  311 + }
230 o := orm.NewOrm() 312 o := orm.NewOrm()
231 o.Begin() 313 o.Begin()
232 - err = utils.ExecuteQueryAllWithOrmer(o, &departSubset, dataSql0, relationLike)  
233 - if err != nil {  
234 - o.Rollback()  
235 - e := fmt.Errorf("")  
236 - log.Error(e.Error())  
237 - return protocol.NewErrWithMessage("1", e)  
238 - }  
239 - for _, v := range departSubset {  
240 - if v.Member > 0 { 314 + nowTime := time.Now()
  315 + for _, d := range toDelete {
  316 + relationlike := d.Relation + "%"
  317 + err := utils.ExecuteSQLWithOrmer(o, dataSql2, nowTime.String(), relationlike)
  318 + if err != nil {
241 o.Rollback() 319 o.Rollback()
242 - e := fmt.Errorf("Member > 0 ") 320 + e := fmt.Errorf("EXECUTE SQL err:%s", err)
243 log.Error(e.Error()) 321 log.Error(e.Error())
244 return protocol.NewErrWithMessage("1", e) 322 return protocol.NewErrWithMessage("1", e)
245 } 323 }
246 } 324 }
247 - err = utils.ExecuteSQLWithOrmer(o, dataSql2, nowTime, relationLike)  
248 - if err != nil {  
249 - o.Rollback()  
250 - e := fmt.Errorf("EXECUTE SQL err:%s", err)  
251 - log.Error(e.Error())  
252 - return protocol.NewErrWithMessage("1", e)  
253 - }  
254 o.Commit() 325 o.Commit()
255 return nil 326 return nil
256 } 327 }
@@ -5,37 +5,42 @@ import ( @@ -5,37 +5,42 @@ import (
5 "oppmg/common/log" 5 "oppmg/common/log"
6 "oppmg/models" 6 "oppmg/models"
7 "oppmg/protocol" 7 "oppmg/protocol"
  8 + "oppmg/utils"
  9 + "strings"
8 "time" 10 "time"
9 11
10 "github.com/astaxie/beego/orm" 12 "github.com/astaxie/beego/orm"
11 ) 13 )
12 14
13 //PositionAdd 添加职位 15 //PositionAdd 添加职位
14 -func PositionAdd(param protocol.RequestDepartmentAdd) error { 16 +func PositionAdd(param protocol.RequestPositionAdd) (*protocol.ResponsePositionInfo, error) {
15 var ( 17 var (
16 parentPosition *models.Position 18 parentPosition *models.Position
17 err error 19 err error
  20 + nowTime = time.Now()
  21 + positioninfo *protocol.ResponsePositionInfo
18 ) 22 )
  23 + positionAdd := &models.Position{
  24 + CompanyId: param.CompanyID,
  25 + Name: param.Name,
  26 + CreateAt: nowTime,
  27 + UpdateAt: nowTime,
  28 + DeleteAt: time.Unix(0, 0),
  29 + ParentId: param.ParentID,
  30 + }
19 if param.ParentID > 0 { 31 if param.ParentID > 0 {
20 parentPosition, err = models.GetPositionById(param.ParentID) 32 parentPosition, err = models.GetPositionById(param.ParentID)
21 if err != nil { 33 if err != nil {
22 - e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ParentID, err) 34 + e := fmt.Errorf("GetPositionById(%d) err:%s", param.ParentID, err)
23 log.Error(e.Error()) 35 log.Error(e.Error())
24 - return protocol.NewErrWithMessage("1", e) 36 + return nil, protocol.NewErrWithMessage("1", e)
25 } 37 }
26 if parentPosition.CompanyId != param.CompanyID { 38 if parentPosition.CompanyId != param.CompanyID {
27 - e := fmt.Errorf("parentDepart.CompanyId != param.CompanyID") 39 + e := fmt.Errorf("parentPosition.CompanyId != param.CompanyID")
28 log.Error(e.Error()) 40 log.Error(e.Error())
29 - return protocol.NewErrWithMessage("1", e) 41 + return nil, protocol.NewErrWithMessage("1", e)
30 } 42 }
31 } 43 }
32 - positionAdd := &models.Position{  
33 - CompanyId: param.CompanyID,  
34 - Name: param.Name,  
35 - CreateAt: time.Now(),  
36 - UpdateAt: time.Now(),  
37 - ParentId: param.ParentID,  
38 - }  
39 44
40 o := orm.NewOrm() 45 o := orm.NewOrm()
41 o.Begin() 46 o.Begin()
@@ -44,23 +49,224 @@ func PositionAdd(param protocol.RequestDepartmentAdd) error { @@ -44,23 +49,224 @@ func PositionAdd(param protocol.RequestDepartmentAdd) error {
44 o.Rollback() 49 o.Rollback()
45 e := fmt.Errorf("AddPosition err:%s", err) 50 e := fmt.Errorf("AddPosition err:%s", err)
46 log.Error(e.Error()) 51 log.Error(e.Error())
47 - return protocol.NewErrWithMessage("1", e) 52 + return nil, protocol.NewErrWithMessage("1", e)
48 } 53 }
49 err = positionAdd.SetRelation(parentPosition) 54 err = positionAdd.SetRelation(parentPosition)
50 if err != nil { 55 if err != nil {
51 o.Rollback() 56 o.Rollback()
52 e := fmt.Errorf("SetRelation err:%s", err) 57 e := fmt.Errorf("SetRelation err:%s", err)
53 log.Error(e.Error()) 58 log.Error(e.Error())
54 - return protocol.NewErrWithMessage("1", e) 59 + return nil, protocol.NewErrWithMessage("1", e)
55 } 60 }
56 err = models.UpdatePositionById(positionAdd, []string{"Relation"}, o) 61 err = models.UpdatePositionById(positionAdd, []string{"Relation"}, o)
57 if err != nil { 62 if err != nil {
58 o.Rollback() 63 o.Rollback()
59 e := fmt.Errorf("UpdatePositionById err:%s", err) 64 e := fmt.Errorf("UpdatePositionById err:%s", err)
60 log.Error(e.Error()) 65 log.Error(e.Error())
  66 + return nil, protocol.NewErrWithMessage("1", e)
  67 + }
  68 + o.Commit()
  69 + positioninfo = &protocol.ResponsePositionInfo{
  70 + ID: positionAdd.Id,
  71 + CompanyID: positionAdd.CompanyId,
  72 + Name: positionAdd.Name,
  73 + ParentID: positionAdd.ParentId,
  74 + ParemtName: "",
  75 + CreateTime: nowTime.Unix(),
  76 + }
  77 + if parentPosition != nil {
  78 + positioninfo.ParemtName = parentPosition.Name
  79 + }
  80 + return positioninfo, nil
  81 +}
  82 +
  83 +func PositionEdit(param protocol.RequestPositionEdit) (*protocol.ResponsePositionInfo, error) {
  84 + var (
  85 + positionUpdate *models.Position
  86 + err error
  87 + parentPosition *models.Position
  88 + positioninfo *protocol.ResponsePositionInfo
  89 + nowTime = time.Now()
  90 + )
  91 + positionUpdate, err = models.GetPositionById(param.ID)
  92 + if err != nil {
  93 + e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ID, err)
  94 + log.Error(e.Error())
  95 + return nil, protocol.NewErrWithMessage("1", e)
  96 + }
  97 + if positionUpdate.CompanyId != param.CompanyID {
  98 + e := fmt.Errorf("depart.CompanyId(%d) !=param.CompanyID(%d)", positionUpdate.CompanyId, param.CompanyID)
  99 + log.Error(e.Error())
  100 + return nil, protocol.NewErrWithMessage("1", e)
  101 + }
  102 + positionUpdate.Name = param.Name
  103 + positionUpdate.UpdateAt = nowTime
  104 + err = models.UpdatePositionById(positionUpdate, []string{"Name", "UpdateAt"})
  105 + if err != nil {
  106 + e := fmt.Errorf("UpdatePositionById err:%s", err)
  107 + log.Error(e.Error())
  108 + return nil, protocol.NewErrWithMessage("1", e)
  109 + }
  110 + //理部门上级发生变化的情况
  111 + if param.ParentID != 0 {
  112 + parentPosition, err = models.GetPositionById(param.ParentID)
  113 + if err != nil {
  114 + e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ParentID, err)
  115 + log.Error(e.Error())
  116 + return nil, protocol.NewErrWithMessage("1", e)
  117 + }
  118 + if parentPosition.CompanyId != positionUpdate.CompanyId {
  119 + e := fmt.Errorf("ParentID err")
  120 + log.Error(e.Error())
  121 + return nil, protocol.NewErrWithMessage("1")
  122 + }
  123 + }
  124 + //更新部门关系数据
  125 + err = positionRelationUpdate(positionUpdate, parentPosition)
  126 + if err != nil {
  127 + e := fmt.Errorf("positionRelationUpdate err:%s", err)
  128 + log.Error(e.Error())
  129 + return nil, protocol.NewErrWithMessage("1", e)
  130 + }
  131 + positioninfo = &protocol.ResponsePositionInfo{
  132 + ID: positionUpdate.Id,
  133 + CompanyID: positionUpdate.CompanyId,
  134 + Name: positionUpdate.Name,
  135 + CreateTime: positionUpdate.CreateAt.Unix(),
  136 + }
  137 + if param.ParentID != 0 {
  138 + positioninfo.ParentID = parentPosition.Id
  139 + positioninfo.ParemtName = parentPosition.Name
  140 + }
  141 +
  142 + return positioninfo, nil
  143 +}
  144 +
  145 +//positionRelationUpdate 处理部门上级发生变化的情况
  146 +func positionRelationUpdate(positionUpdate *models.Position, newparent *models.Position) error {
  147 + if newparent == nil {
  148 + return nil
  149 + }
  150 + const (
  151 + //获取某个部门的下级部门 select ... for update
  152 + dataSql0 string = `SELECT id,relation FROM position WHERE relation LIKE ? AND delete_at = 0 FOR UPDATE`
  153 + //更新关系树
  154 + dataSql2 string = `update position set relation=? where id=?`
  155 + //更新departUpdate的parent_id
  156 + dataSql3 string = `update position set parent_id=? where id=?`
  157 + )
  158 + var (
  159 + positionSubset []models.Position //子级部门
  160 + err error
  161 + oldRelation string = positionUpdate.Relation
  162 + relationLike string = oldRelation + "%"
  163 + newRelation string = fmt.Sprintf("%s/%d", newparent.Relation, positionUpdate.Id)
  164 + )
  165 + o := orm.NewOrm()
  166 + o.Begin()
  167 + //修改部门的parent_id
  168 + err = utils.ExecuteSQLWithOrmer(o, dataSql3, newparent.Id, positionUpdate.Id)
  169 + if err != nil {
  170 + o.Rollback()
  171 + e := fmt.Errorf("EXECUTE SQL err:%s", err)
  172 + log.Error(e.Error())
61 return protocol.NewErrWithMessage("1", e) 173 return protocol.NewErrWithMessage("1", e)
62 } 174 }
  175 + //获取部门及子级部门的relation和id
  176 + err = utils.ExecuteQueryAllWithOrmer(o, &positionSubset, dataSql0, relationLike)
  177 + if err != nil {
  178 + o.Rollback()
  179 + e := fmt.Errorf("EXECUTE SQL err:%s", err)
  180 + log.Error(e.Error())
  181 + return protocol.NewErrWithMessage("1", e)
  182 + }
  183 + //修改部门及子级部门的relation
  184 + for i := range positionSubset {
  185 + if positionSubset[i].Id == newparent.Id {
  186 + //确认新的父级id是否合法
  187 + o.Rollback()
  188 + e := fmt.Errorf("departSubset[i].Id == newparent.Id")
  189 + log.Error(e.Error())
  190 + return protocol.NewErrWithMessage("1", e)
  191 + }
  192 + //重建关系树
  193 + s := strings.TrimPrefix(positionSubset[i].Relation, oldRelation)
  194 + positionSubset[i].Relation = strings.TrimSpace(fmt.Sprintf("%s%s", newRelation, s))
  195 + err = utils.ExecuteSQLWithOrmer(o, dataSql2, positionSubset[i].Relation, positionSubset[i].Id)
  196 + if err != nil {
  197 + o.Rollback()
  198 + e := fmt.Errorf("EXECUTE SQL err:%s", err)
  199 + log.Error(e.Error())
  200 + return protocol.NewErrWithMessage("1", e)
  201 + }
  202 + }
  203 +
63 o.Commit() 204 o.Commit()
64 return nil 205 return nil
  206 +}
65 207
  208 +func PositionDelete(param protocol.RequestPositionDelete) error {
  209 + //检查是否可以被删除
  210 + var (
  211 + //根据参数获取的职位
  212 + positionDelete []*models.Position
  213 + //最终需要操作的职位
  214 + toDelete = make(map[int64]*models.Position)
  215 + )
  216 + const (
  217 + //获取部门子集,
  218 + dataSql0 string = `SELECT id,relation,member FROM position WHERE relation LIKE ? AND delete_at = 0 `
  219 + dataSql2 string = `update position set delete_at=? where relation LIKE ?`
  220 + )
  221 + for _, id := range param.IDs {
  222 + var p *models.Position
  223 + p, err := models.GetPositionById(id)
  224 + if err != nil {
  225 + e := fmt.Errorf("GetPositionById(%d)", id)
  226 + log.Error(e.Error())
  227 + return protocol.NewErrWithMessage("1", e)
  228 + }
  229 + if p.CompanyId != param.CompanyID {
  230 + e := fmt.Errorf("CompanyID err")
  231 + log.Error(e.Error())
  232 + return protocol.NewErrWithMessage("1", e)
  233 + }
  234 + positionDelete = append(positionDelete, p)
  235 + toDelete[p.Id] = p
  236 + }
  237 + for _, pos := range positionDelete {
  238 + var positionsubset []models.Position
  239 + relationLike := pos.Relation + "%"
  240 + err := utils.ExecuteQueryAll(&positionsubset, dataSql0, relationLike)
  241 + if err != nil {
  242 + e := fmt.Errorf("EXECUTE SQL err:%s", err)
  243 + log.Error(e.Error())
  244 + return protocol.NewErrWithMessage("1", e)
  245 + }
  246 + for _, subset := range positionsubset {
  247 + if subset.Member > 0 {
  248 + e := fmt.Errorf("user in position,relation:%s", subset.Relation)
  249 + log.Error(e.Error())
  250 + return protocol.NewErrWithMessage("1", e)
  251 + }
  252 + if _, ok := toDelete[subset.Id]; ok {
  253 + delete(toDelete, subset.Id)
  254 + }
  255 + }
  256 + }
  257 + o := orm.NewOrm()
  258 + o.Begin()
  259 + nowTime := time.Now()
  260 + for _, d := range toDelete {
  261 + relationlike := d.Relation + "%"
  262 + err := utils.ExecuteSQLWithOrmer(o, dataSql2, nowTime.String(), relationlike)
  263 + if err != nil {
  264 + o.Rollback()
  265 + e := fmt.Errorf("EXECUTE SQL err:%s", err)
  266 + log.Error(e.Error())
  267 + return protocol.NewErrWithMessage("1", e)
  268 + }
  269 + }
  270 + o.Commit()
  271 + return nil
66 } 272 }
  1 +package company
  2 +
  3 +func userAdd() error {
  4 + return nil
  5 +}
  6 +
  7 +func userEdit() error {
  8 + return nil
  9 +}
@@ -24,11 +24,11 @@ func RoleAdd(param protocol.RequestRoleAdd) (*protocol.ResponseRoleInfo, error) @@ -24,11 +24,11 @@ func RoleAdd(param protocol.RequestRoleAdd) (*protocol.ResponseRoleInfo, error)
24 return nil, protocol.NewErrWithMessage("1", e) 24 return nil, protocol.NewErrWithMessage("1", e)
25 } 25 }
26 var ( 26 var (
27 - parentRole *models.Role  
28 - err error  
29 - roleid int64 27 + //parentRole *models.Role
  28 + err error
  29 + roleid int64
30 ) 30 )
31 - parentRole, err = role.ValidatePid() 31 + _, err = role.ValidatePid()
32 if err != nil { 32 if err != nil {
33 e := fmt.Errorf("ValidatePid err:%s", err) 33 e := fmt.Errorf("ValidatePid err:%s", err)
34 log.Error(e.Error()) 34 log.Error(e.Error())
@@ -44,7 +44,6 @@ func RoleAdd(param protocol.RequestRoleAdd) (*protocol.ResponseRoleInfo, error) @@ -44,7 +44,6 @@ func RoleAdd(param protocol.RequestRoleAdd) (*protocol.ResponseRoleInfo, error)
44 Name: role.Name, 44 Name: role.Name,
45 Descript: role.Descript, 45 Descript: role.Descript,
46 Pid: role.Pid, 46 Pid: role.Pid,
47 - ParentName: parentRole.Name,  
48 Types: role.Types, 47 Types: role.Types,
49 CreateTime: role.CreateAt.Unix(), //Format("2006-01-02 15:04:05") 48 CreateTime: role.CreateAt.Unix(), //Format("2006-01-02 15:04:05")
50 } 49 }
@@ -81,9 +80,9 @@ func RoleDelete(param protocol.RequestRoleDelete) error { @@ -81,9 +80,9 @@ func RoleDelete(param protocol.RequestRoleDelete) error {
81 80
82 func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error) { 81 func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error) {
83 var ( 82 var (
84 - parentRole *models.Role  
85 - role *models.Role  
86 - err error 83 + //parentRole *models.Role
  84 + role *models.Role
  85 + err error
87 ) 86 )
88 role, err = models.GetRoleById(param.ID) 87 role, err = models.GetRoleById(param.ID)
89 if err != nil { 88 if err != nil {
@@ -99,7 +98,7 @@ func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error @@ -99,7 +98,7 @@ func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error
99 role.Pid = param.Pid 98 role.Pid = param.Pid
100 role.Descript = param.Descript 99 role.Descript = param.Descript
101 role.Name = param.Name 100 role.Name = param.Name
102 - if parentRole, err = role.ValidatePid(); err != nil { 101 + if _, err = role.ValidatePid(); err != nil {
103 e := fmt.Errorf("ValidatePid err:%s", err) 102 e := fmt.Errorf("ValidatePid err:%s", err)
104 log.Error(e.Error()) 103 log.Error(e.Error())
105 return nil, protocol.NewErrWithMessage("1", e) 104 return nil, protocol.NewErrWithMessage("1", e)
@@ -114,7 +113,6 @@ func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error @@ -114,7 +113,6 @@ func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error
114 Name: role.Name, 113 Name: role.Name,
115 Descript: role.Descript, 114 Descript: role.Descript,
116 Pid: role.Pid, 115 Pid: role.Pid,
117 - ParentName: parentRole.Name,  
118 Types: role.Types, 116 Types: role.Types,
119 CreateTime: role.CreateAt.Unix(), 117 CreateTime: role.CreateAt.Unix(),
120 } 118 }
@@ -166,3 +164,7 @@ func RoleHasPermission() error { @@ -166,3 +164,7 @@ func RoleHasPermission() error {
166 func PermissionHasRole() error { 164 func PermissionHasRole() error {
167 return nil 165 return nil
168 } 166 }
  167 +
  168 +func RolsHasUser() error {
  169 + return nil
  170 +}
  1 +package rbac