<?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;
    }

    public function stats()
    {
        $userId = get_my_id();
        $goodsType = 3;
        $sm = Model('common/SysSummary');
        $sysData= $sm->getInfo('toPaySlow');
        $helpSaleNum = Db::name('orders')->where(compact('userId','goodsType'))->where('payable', '>',0)->sum('payable');
        $zgTimes = Db::name('orders')->where(compact('userId','goodsType'))->count();
        $zgTimes += 1;
        $helpSaleNum += 1;
        $helpSaleNum = number_format($helpSaleNum, 2, '.', '');
        return [
            'zgTimes'=>$zgTimes,
            'myZgA'=>$helpSaleNum,
            'zgAmounts'=>$sysData['toPaySlow'],
        ];
    }

    public function stats2()
    {
        $userId = get_my_id();
        $goodsType = 3;
        return [
            'zgAmounts'=>Db::name('orders')->where(compact('userId','goodsType'))->sum('helpGetCount'),
        ];
    }

    /**
     * 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;
        $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 (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--;}
        } while (($newLevel <= $max) && ($newLevel <= (int)dataConf("helpSaleMaxLevel")));
//        pd($treeArr);
        return $treeArr;
    }
}