作者 yangfu

获取子部门列表

package protocol
import "time"
import (
"time"
)
const (
DepartmentUser = iota //用户所有部门
... ... @@ -16,6 +18,37 @@ type DepartmentsResponse struct {
Departments []*Department `json:"departments,omitempty"`
}
//获取一级部门列表
func (this DepartmentsResponse) GetRootDepartment() []*Department {
if len(this.Departments) == 0 {
return []*Department{}
}
return this.Departments[0].Departments
}
//获得部门底下子部门的编号列表(包含本身)
func (this DepartmentsResponse) GetChildDepartmentIds(d *Department, isContainSelf bool) []int {
idList := make([]int, 0)
if isContainSelf {
idList = append(idList, d.DepartmentId)
}
idList = append(idList, walkDeparment(d)...)
return idList
}
//遍历部门
func walkDeparment(d *Department) []int {
idList := make([]int, 0)
for i := range d.Departments {
d := d.Departments[i]
idList = append(idList, d.DepartmentId)
if len(d.Departments) > 0 {
idList = append(idList, walkDeparment(d)...)
}
}
return idList
}
type Department struct {
DepartmentId int `orm:"column(department_id)" json:"id"`
Name string `orm:"column(name)" json:"name"`
... ...
... ... @@ -147,3 +147,5 @@ func AchievementDetail(header *protocol.RequestHeader, request *protocol.Achieve
return
}
//func GetSourceChance(header )
... ...
package department
import (
"opp/protocol"
"testing"
)
func TestGetRootDepartment(t *testing.T) {
sub := []*protocol.Department{
{DepartmentId: 100, Name: "100"},
{DepartmentId: 200, Name: "200"},
}
Departments := []*protocol.Department{
{DepartmentId: 1, Name: "1", Departments: sub},
{DepartmentId: 2, Name: "2"},
{DepartmentId: 3, Name: "3", Departments: []*protocol.Department{{DepartmentId: 31, Name: "3-1", Departments: sub}, {DepartmentId: 32, Name: "3-2"}}},
{DepartmentId: 4, Name: "4", Departments: []*protocol.Department{{DepartmentId: 41, Name: "4-1"}, {DepartmentId: 42, Name: "4-2", Departments: sub}}},
}
var deps protocol.DepartmentsResponse = protocol.DepartmentsResponse{
Departments: []*protocol.Department{
{DepartmentId: 0, Name: "0", Departments: Departments},
},
}
depList := deps.GetRootDepartment()
for i := range depList {
d := depList[i]
t.Log("部门:", d.DepartmentId, deps.GetChildDepartmentIds(d, true))
}
}
... ...