<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
use think\facade\Cache;

function getDepartmentChild($pid = 0){
	$department = Cache::get('department');
	if(!$department){
		$department = \app\model\auth\Departments::where('status', '=', 1)->column('id,parent_id,title', 'id');
		Cache::set('department', $department);
	}
	$res = getChilds($department, $pid, 'id', 'parent_id');
	$res[] = (int) $pid;   //把自己包含在内
	return $res;
}

/**
 * 获得所有的子
 * @param  [type] $id [description]
 * @return [type]     [description]
 */
function getChilds($data, $id = 0, $pk = 'id', $pid = 'parent_id') {
	$array = [];
	foreach ($data as $k => $v) {
		if ($v[$pid] == $id) {
			$array[] = (int) $v[$pk];
			array_merge($array, getChilds($data, $v[$pk]));
		}
	}
	return $array;
}