作者 yangfu

获取子部门列表

1 package protocol 1 package protocol
2 2
3 -import "time" 3 +import (
  4 + "time"
  5 +)
4 6
5 const ( 7 const (
6 DepartmentUser = iota //用户所有部门 8 DepartmentUser = iota //用户所有部门
@@ -16,6 +18,37 @@ type DepartmentsResponse struct { @@ -16,6 +18,37 @@ type DepartmentsResponse struct {
16 Departments []*Department `json:"departments,omitempty"` 18 Departments []*Department `json:"departments,omitempty"`
17 } 19 }
18 20
  21 +//获取一级部门列表
  22 +func (this DepartmentsResponse) GetRootDepartment() []*Department {
  23 + if len(this.Departments) == 0 {
  24 + return []*Department{}
  25 + }
  26 + return this.Departments[0].Departments
  27 +}
  28 +
  29 +//获得部门底下子部门的编号列表(包含本身)
  30 +func (this DepartmentsResponse) GetChildDepartmentIds(d *Department, isContainSelf bool) []int {
  31 + idList := make([]int, 0)
  32 + if isContainSelf {
  33 + idList = append(idList, d.DepartmentId)
  34 + }
  35 + idList = append(idList, walkDeparment(d)...)
  36 + return idList
  37 +}
  38 +
  39 +//遍历部门
  40 +func walkDeparment(d *Department) []int {
  41 + idList := make([]int, 0)
  42 + for i := range d.Departments {
  43 + d := d.Departments[i]
  44 + idList = append(idList, d.DepartmentId)
  45 + if len(d.Departments) > 0 {
  46 + idList = append(idList, walkDeparment(d)...)
  47 + }
  48 + }
  49 + return idList
  50 +}
  51 +
19 type Department struct { 52 type Department struct {
20 DepartmentId int `orm:"column(department_id)" json:"id"` 53 DepartmentId int `orm:"column(department_id)" json:"id"`
21 Name string `orm:"column(name)" json:"name"` 54 Name string `orm:"column(name)" json:"name"`
@@ -147,3 +147,5 @@ func AchievementDetail(header *protocol.RequestHeader, request *protocol.Achieve @@ -147,3 +147,5 @@ func AchievementDetail(header *protocol.RequestHeader, request *protocol.Achieve
147 147
148 return 148 return
149 } 149 }
  150 +
  151 +//func GetSourceChance(header )
  1 +package department
  2 +
  3 +import (
  4 + "opp/protocol"
  5 + "testing"
  6 +)
  7 +
  8 +func TestGetRootDepartment(t *testing.T) {
  9 + sub := []*protocol.Department{
  10 + {DepartmentId: 100, Name: "100"},
  11 + {DepartmentId: 200, Name: "200"},
  12 + }
  13 + Departments := []*protocol.Department{
  14 + {DepartmentId: 1, Name: "1", Departments: sub},
  15 + {DepartmentId: 2, Name: "2"},
  16 + {DepartmentId: 3, Name: "3", Departments: []*protocol.Department{{DepartmentId: 31, Name: "3-1", Departments: sub}, {DepartmentId: 32, Name: "3-2"}}},
  17 + {DepartmentId: 4, Name: "4", Departments: []*protocol.Department{{DepartmentId: 41, Name: "4-1"}, {DepartmentId: 42, Name: "4-2", Departments: sub}}},
  18 + }
  19 +
  20 + var deps protocol.DepartmentsResponse = protocol.DepartmentsResponse{
  21 + Departments: []*protocol.Department{
  22 + {DepartmentId: 0, Name: "0", Departments: Departments},
  23 + },
  24 + }
  25 + depList := deps.GetRootDepartment()
  26 + for i := range depList {
  27 + d := depList[i]
  28 + t.Log("部门:", d.DepartmentId, deps.GetChildDepartmentIds(d, true))
  29 + }
  30 +}