<?php

namespace wstmart\app\controller;

use think\Db;
use wstmart\app\model\Users;
use wstmart\common\model\UserLevel as UL;
use wstmart\common\model\UserTrees as UT;

use think\Collection;

/**
 * ============================================================================
 * 用户控制器
 */
class UserLevel extends Base
{

    protected $beforeActionList = [
        'checkAuth' =>  ['except'=>'']// 访问这些except下的方法不需要执行前置操作
    ];

    /**
     * index
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author 倪苍华 <canghua.cc@gmail.com>
     * Date 2019/9/10 10:37
     */
    public function index()
    {
        $userId = get_my_id();
        $User   = UT::realGetMyChildren($userId, [$userId]);
        return $User;
    }

    /**
     * TreeList
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author 倪苍华 <canghua.cc@gmail.com>
     * Date 2019/9/10 11:18
     */
    public function TreeList()
    {
        $userId   = get_my_id();
        $treeArr  = [];
        $goodsType = 3;
        $min = Db::name('orders')->where(compact('userId','goodsType'))->min('helpUserLevel');
        if(0 == $min){$min = 1;}
        $max = Db::name('orders')->where(compact('userId','goodsType'))->max('helpUserLevel');
        $newLevel = 0;
        do {
            $newLevel++;
            $userId = UT::getUpperId($userId);// 找到我的层级
            if ($userId == null) $userId = 1;// 上级uid
            if (($newLevel >= $min) && UT::checkUserCanDisplay($userId)) {// 如果有,则使用上级信息
                if($newLevel>10){
                    $treeArr[$newLevel]['level'] = "第".$newLevel."层";
                }else{
                    $treeArr[$newLevel]['level'] = UT::$level[$newLevel];
                }
                $treeArr[$newLevel]['level_id'] = $newLevel;
                $treeArr[$newLevel]['userId']   = $userId;
                $treeArr[$newLevel]['userName'] = '';
            }else{$newLevel--;}
            if ($userId <= 1)break;
        } while (($newLevel <= $max) && ($newLevel <= (int)dataConf("helpSaleMaxLevel")));
//        pd($treeArr);
        return $treeArr;
    }
}