department.go 1.7 KB
package protocol

import (
	"time"
)

const (
	DepartmentUser = iota //用户所有部门
	DepartmentAll         //公司所有部门
)

/*Departments */
type DepartmentsRequest struct {
	//Type int `json:"type" valid:"Required"` //1:公司所有部门  2:用户所在部门
	Type int `json:"type"` //0:用户所在部门 1:公司所有部门
}
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"`
	PId           int           `orm:"column(parent_id)" json:"-"`
	ManagerString string        `orm:"column(managers)" json:"-"`
	Relation      string        `orm:"column(relation)"`
	CreateTime    time.Time     `orm:"column(create_time)"`
	Managers      []int         `json:"-"`
	Departments   []*Department `json:"departments,omitempty"`
}