<?php
/**
 * 用户要求开通VIP关系表 Table<ai_allwin_user_level>
 */
namespace app\allwin\model;
use think\Model;
use think\facade\Validate;

class Levels extends Model{

    protected $pk = 'id';
    protected $table = 'ai_allwin_user_level';

    public function user(){
        return $this->hasOne('app\common\model\SystemUser','id','user_id');
    } 

    /**
     * 创建用户关系
     * @param integer $user_id   当前用户ID
     * @param integer $parent_id 上级用户ID
     * @return void
     */
    public static function addLevel(int $user_id,int $parent_id){
        if($parent_id){
            $data[0]['level']     = 1;
            $data[0]['parent_id'] = $parent_id;
            $data[0]['user_id']   = $user_id;
            $levellist = self::where('user_id',$parent_id)->select();
            foreach ($levellist as $value) {
                $level  = $value['level'] + 1;
                $data[] = ['level' => $level,'parent_id' => $value['parent_id'],'user_id' => $user_id];
            }
            return self::insertAll($data);
        }
        return true;
    }

    /**
     * 查询子集用户
     * @param [type] $uid
     * @param array $level  [1,2,3,4]层级
     * @return void
     */
    public static function levelUser($uid,array $level){
        $lists = self::where(['parent_id' => $uid,'level' => $level])->order('id desc')->paginate(20,false);
        if(empty($lists)){
            return;
        }
        $data = [];
        foreach ($lists as $key => $value) {
            $data[$key]['face']        = $value->user->face;
            $data[$key]['nickname']    = $value->user['nickname'];
            $data[$key]['level']       = $value->level;
            $data[$key]['parent_id']   = $value->parent_id;
            $data[$key]['user_id']     = $value->user['id'];
            $data[$key]['invite_code'] = $value->user['invite_code'];
            $data[$key]['create_time'] = date('m-d H:i',$value->user['create_time']);
        }
        return $data;
    }

    /**
     * 查询某个条件下用户列表
     * @param array $agent 社群主级别
     * @param int $user_id 用户ID
     * @param int $level 层级
     * @return void
     */
    public function children_user(int $uid){
        //读取所有邀请关联用户
        $level = self::where(['user_id' => $uid])->field('parent_id,level,user_id')->order('id desc')->select()->toArray();
        if(empty($level)){
            return [];
        }
        foreach ($level as $key => $value) {
            $uida[$value['parent_id']]['level']   = $value['level'];
            $uida[$value['parent_id']]['user_id'] = $value['user_id'];
        }
        //读取所有用户信息
        $uid = array_column($level,'parent_id');
        $info = model('AllwinUser')->with(['user','store'])->whereIn('uid',$uid)->order('id desc')->select()->toArray();
        if(empty($info)){
            return [];
        }
        $level_user = [];
        foreach ($info as $key => $value) {
            $level                             = $uida[$value['uid']]['level'];
            $level_user[$level]['user_id']     = $uida[$value['uid']]['user_id'];
            $level_user[$level]['invite_code'] = $value['user']['invite_code'];
            $level_user[$level]['nickname']    = $value['user']['nickname'];
            $level_user[$level]['face']        = $value['user']['face'];
            $level_user[$level]['phone_uid']   = $value['user']['phone_uid'];
            $level_user[$level]['parent_id']   = $value['user']['id'];
            $level_user[$level]['level']       = $level;
            $level_user[$level]['store_name']  = empty($value['store']) ? '[无店铺]平台会员' : $value['store']['name'];    
        }
        krsort($level_user);
        return $level_user;
    }
}