<?php

namespace common\models;

use common\helpers\Cache;
use common\helpers\user\Info;
use Yii;

/**
 * This is the model class for table "{{%USER_TEAM}}".
 *
 * @property string $ID
 * @property string $TEAM_NAME 团队名称
 * @property string $LEADER_UID 领导人ID
 * @property int $PERIOD_NUM 加入时的期数
 * @property int $UPDATE_PERIOD_NUM 修改期数
 * @property string $CREATE_ADMIN 创建人
 * @property string $UPDATE_ADMIN 修改人
 * @property string $CREATE_REMARK 备注
 * @property string $UPDATE_REMARK 备注
 * @property int $IS_DEL 是否删除
 * @property int $CREATED_AT 创建时间
 * @property int $UPDATED_AT 更新时间
 */
class UserTeam extends \common\components\ActiveRecord {
    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%USER_TEAM}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['TEAM_NAME', 'LEADER_UID', 'PERIOD_NUM', 'CREATE_ADMIN', 'CREATED_AT'], 'required'],
            [['PERIOD_NUM', 'UPDATE_PERIOD_NUM', 'IS_DEL', 'CREATED_AT', 'UPDATED_AT'], 'integer'],
            [['ID', 'TEAM_NAME', 'LEADER_UID', 'CREATE_ADMIN', 'UPDATE_ADMIN'], 'string', 'max' => 32],
            [['CREATE_REMARK', 'UPDATE_REMARK'], 'string', 'max' => 4000],
            [['LEADER_UID'], 'unique'],
            [['TEAM_NAME'], 'unique'],
            [['ID'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'ID' => 'ID',
            'TEAM_NAME' => '团队名称',
            'LEADER_UID' => '领导人ID',
            'PERIOD_NUM' => '加入时的期数',
            'UPDATE_PERIOD_NUM' => '修改期数',
            'CREATE_ADMIN' => '创建人',
            'UPDATE_ADMIN' => '修改人',
            'CREATE_REMARK' => '备注',
            'UPDATE_REMARK' => '备注',
            'IS_DEL' => '是否删除',
            'CREATED_AT' => '创建时间',
            'UPDATED_AT' => '更新时间',
        ];
    }

    /**
     * 获取一个团队
     * @param $id
     * @return array|null|\yii\db\ActiveRecord
     */
    public static function getTeam($id) {
        return static::findUseSlaves()->where('ID=:ID', [':ID' => $id])->asArray()->one();
    }

    /**
     * 通过会员ID查找团队
     * @param $userId
     * @return array|null
     */
    public static function getTeamByUserId($userId) {
        $userInfo = UserInfo::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $userId]);
        return static::findOneAsArray('ID=:ID', [':ID' => $userInfo['TEAM_ID']]);
    }

    /**
     * 团队下的所有会员
     * @param $id
     * @return array|null
     */
    public static function getAllChildren($id) {
        return UserInfo::findAllAsArray('TEAM_ID=:TEAM_ID', [':TEAM_ID' => $id]);
    }

    /**
     * 通过一个团队查找到上级团队领导人
     * @param $id
     * @return array|null|\yii\db\ActiveRecord
     */
    public static function getParentTeamFromTeam($id) {
        $oneTeam = static::find()->select('LEADER_UID')->where('ID=:ID', [':ID' => $id])->asArray()->one();
        return $oneTeam ? self::getPatentTeamLeader($oneTeam['LEADER_UID']) : null;
    }


    /**
     * 通过一个团队的领导会员获取上级团队
     * @param $userId
     * @return array|null
     */
    public static function getParentTeam($userId){
        // 联表查询安置网络的上级UID，并且是领导的会员
        $isTeamLeaderUser = UserNetwork::find()->select('UN.*,PUI.NETWORK_DEEP,PUI.IS_TEAM_LEADER,PUI.TEAM_ID')->where('UN.USER_ID=:USER_ID AND PUI.IS_TEAM_LEADER=1', [':USER_ID'=>$userId])->from(UserNetwork::tableName().' AS UN')->join('LEFT JOIN', UserInfo::tableName().' AS PUI', 'UN.PARENT_UID=PUI.USER_ID')->orderBy('PUI.NETWORK_DEEP DESC')->limit(1)->asArray()->one();
        // 拿到团队的ID以后，再查询团队
        return UserTeam::findOneAsArray('ID=:ID', [':ID'=>$isTeamLeaderUser['TEAM_ID']]);
    }

    /**
     * 通过一个团队的领导会员获取上级团队领导UID
     * @param $userId
     * @return mixed|null
     */
    public static function getPatentTeamLeader($userId) {
        $team = self::getParentTeam($userId);
        return $team ? $team['LEADER_UID'] : null;
    }

    /**
     * 全部团队
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getTeamData(){
        return self::find()->select('ID,TEAM_NAME,LEADER_UID')->where('IS_DEL=0')->orderBy('CREATED_AT ASC')->asArray()->all();
    }
}
