<?php

namespace app\model;

use app\model\Base;
/**
 * @property integer $ancestor_id 上级用户ID
 * @property integer $descendant_id 下级用户ID
 * @property integer $depth 层级深度（0表示自己）
 * @property integer $status 用户有效性状态，0表示无效，1表示有效
 */
class UserTeam extends Base
{
    public function user()
    {
        return $this->belongsTo(User::class, 'descendant_id', 'id');
    }
    /**
     * 根据用户ID向上查询团队成员
     * @param mixed $user_id
     * @param mixed $user_field
     * @return array
     */
    static function getTeamByChild($user_id = 0,$user_field=''){
        $list = self::alias('ut')
            ->join('user u', 'ut.ancestor_id = u.id')
            ->where('ut.descendant_id', $user_id)
            //->where('ut.ancestor_id','<>', $data['user_id'])
            //->where('ut.depth', '<=', 3) // 限制三级内
            ->field('u.id as user_id,u.group, ut.depth')
            ->order('ut.depth ASC')->select();
        if(!is_array($list)){
            $list = $list->toArray();
        }
        return $list;
    }
    /**
     * 根据用户ID向下查询团队
     * @param mixed $user_id
     * @param mixed $user_field
     * @return array
     */
    static function getTeamByParent($user_id = 0,$user_field=''){
        $list = self::alias('ut')
            ->join('user u', 'ut.ancestor_id = u.id')
            //->where('ut.descendant_id', $user_id)
            ->where('ut.ancestor_id','<>', $user_id)
            //->where('ut.depth', '<=', 3) // 限制三级内
            ->field('u.id as user_id,u.group, ut.depth')
            ->order('ut.depth ASC')->select();
        if(!is_array($list)){
            $list = $list->toArray();
        }
        return $list;
    }
}
