<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/2/24
 * Time: 下午12:48
 */

namespace frontendApi\modules\v1\controllers;

use common\helpers\Cache;
use common\helpers\user\Info;
use common\models\Period;
use common\models\UserNetwork;
use Yii;
use common\models\User;

class AtlasController extends BaseController {
    public $modelClass = User::class;

    public function behaviors() {
        $behaviors = parent::behaviors();
        return $behaviors;
    }

    /**
     * 主点位信息
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws \yii\web\HttpException
     */
    public function actionMainUserInfo() {
        $userName = Yii::$app->request->get('userName', '');
        $periodNum = Yii::$app->request->get('periodNum', null);

        if ($userName !== '') {
            if (!$userId = Info::getUserIdByUserName($userName)) {
                return static::notice('会员不存在', 400);
            }
            $topUid = \Yii::$app->user->id;
            $userNetInfo = UserNetwork::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $userId], 'PARENT_UIDS');
            $parentUidsArr = explode(',', $userNetInfo['PARENT_UIDS']);
            if(!in_array($topUid,$parentUidsArr)){
                return static::notice('会员与当前用户不再同一安置网络内', 400);
            }

        } else {
            $userId = \Yii::$app->user->id;
        }
        $baseInfo = Info::baseInfo($userId, $periodNum);
        $decLevelConfig = Cache::getDecLevelConfig();
        $empLevelConfig = Cache::getEmpLevelConfig();
        return static::notice([
            [
                'TOP_RELATION_DEEP' => $baseInfo['RELATION_DEEP'],
                'TOP_NETWORK_DEEP' => $baseInfo['NETWORK_DEEP'],
                'USER_ID' => $userId,
                'USER_NAME' => $baseInfo['USER_NAME'],
                'REAL_NAME' => $baseInfo['REAL_NAME'],
                'PERIOD_AT' => $baseInfo['PERIOD_AT'],
                'DEC_LV_NAME' => $decLevelConfig[$baseInfo['DEC_LV']]['LEVEL_NAME'],
                'EMP_LV_NAME' => $empLevelConfig[$baseInfo['EMP_LV']]['LEVEL_NAME'],
                'leaf' => false,
                'icon' => 'el-icon-user-solid',
                'children' => null,
                'isExpanded' => false,
                'displayNone' => 'display-none',
                'className' => 'first-node',
                'listPeriodNum' => $periodNum
            ],
        ]);
    }

    /**
     * 安置网络图
     * @return mixed
     * @throws \yii\web\HttpException
     */
    public function actionNetwork() {
        $userId = Yii::$app->request->get('id', \Yii::$app->user->id);
        $periodNum = Yii::$app->request->get('periodNum', null);
        $deep = Yii::$app->request->get('deep', 5);
        if ($deep > 20) {
            return static::notice('最多查看会员的前20层子会员', 400);
        }
        $allData = UserNetwork::getChildrenWithDeepAndLayer($userId, $deep, 1, $periodNum);
        return static::notice(['allData' => $allData, 'periodNum' => $periodNum]);
    }

    /**
     * 安置网络列表
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws \yii\web\HttpException
     */
    public function actionNetworkList() {
        $userName = Yii::$app->request->get('userName');
        $deep = Yii::$app->request->get('deep', 5);
        $periodNum = Yii::$app->request->get('periodNum', null);
        if (!$userName) {
            $userId = \Yii::$app->user->id;
        } else {
            if (!$userId = Info::getUserIdByUserName($userName)) {
                return static::notice('会员不存在', 400);
            }
            $topUid = \Yii::$app->user->id;
            $userNetInfo = UserNetwork::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $userId], 'PARENT_UIDS');
            $parentUidsArr = explode(',', $userNetInfo['PARENT_UIDS']);
            if(!in_array($topUid,$parentUidsArr)){
                return static::notice('会员与当前用户不再同一安置网络内', 400);
            }
        }
        $period = Period::instance();
        $mainUserInfo = Info::baseInfo($userId, $periodNum);
        $decLevelConfig = Cache::getDecLevelConfig();
        $empLevelConfig = Cache::getEmpLevelConfig();
        $data = UserNetwork::getChildrenInDeepFromPeriodWithPage($userId, $deep, $periodNum, 1 ? ['pageSize' => 10] : []);
        foreach($data['list'] as $key=>$value){
            $userBaseInfo = Info::baseInfo($value['USER_ID'], $periodNum);
            $data['list'][$key]['LIST_PERIOD_NUM'] = $periodNum?$periodNum:$period->getNowPeriodNum();
            $data['list'][$key]['COUNT_DEEP'] = (int)($userBaseInfo['NETWORK_DEEP']);
            $data['list'][$key]['USER_NAME'] = $userBaseInfo['USER_NAME'];
            $data['list'][$key]['SEE_USER_NAME'] = $userBaseInfo['USER_NAME'];
            $data['list'][$key]['REAL_NAME'] = $userBaseInfo['REAL_NAME'];
            $data['list'][$key]['PERIOD_AT'] = $userBaseInfo['PERIOD_AT'];
            $data['list'][$key]['DEC_LV_NAME'] = $decLevelConfig[$userBaseInfo['DEC_LV']]['LEVEL_NAME'];
            $data['list'][$key]['EMP_LV_NAME'] = $empLevelConfig[$userBaseInfo['EMP_LV']]['LEVEL_NAME'];
        }
        $data['periodNum'] = $periodNum;
        $data['listTopDeep'] = $mainUserInfo['NETWORK_DEEP'];

        return static::notice($data);
    }

    /**
     * 获取期数
     * @return mixed
     * @throws \yii\web\HttpException
     */
    public function getPeriod(){
        $period = Period::instance();
        $periodNum = $period->getNowPeriodNum();
        return static::notice([
            'periodNum' => $periodNum,
        ]);
    }

}