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

namespace backendApi\modules\v1\controllers;

use backendApi\modules\v1\models\exportForms\AtlasExportForm;
use backendApi\modules\v1\models\lists\atlas\EmpUserList;
use backendApi\modules\v1\models\lists\atlas\NetworkLineList;
use backendApi\modules\v1\models\lists\atlas\NetworkList;
use backendApi\modules\v1\models\lists\atlas\RelationList;
use common\helpers\Cache;
use common\helpers\Form;
use common\helpers\Level;
use common\helpers\user\Perf;
use common\helpers\user\Info;
use common\models\CalcBonus;
//use common\models\exportForms\AtlasRelationListExport;
use common\models\PerfMonth;
use common\models\Period;
use common\models\DeclarationLevel;
use common\models\EmployLevel;
use common\models\UserInfo;
use common\models\UserNetwork;
use common\models\UserRelation;
use Yii;
use common\models\User;

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

    public function behaviors() {
        $behaviors = parent::behaviors();
        //$behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }

    /**
     * 开拓网络图
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws \yii\web\HttpException
     */
    public function actionRelation() {
        $userId = Yii::$app->request->get('id', Yii::$app->params['mainUserId']);
        $deep = Yii::$app->request->get('deep', 2);
        $periodNum = Yii::$app->request->get('periodNum', null);
        if ($deep > 23) {
            return static::notice('最多查看会员的前23层子会员', 400);
        }
        $allData = UserRelation::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 actionMainUserInfo() {
        $userName = Yii::$app->request->get('userName', '');
        $periodNum = Yii::$app->request->get('periodNum', null);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);
        if ($userName !== '') {
            //$oneUser = Info::getUserByUserNameFromUserInfoTable($userName);
            if (!$userId = Info::getUserIdByUserName($userName)) {
                return static::notice('会员不存在', 400);
            }
        } else {
            $userId = Yii::$app->params['mainUserId'];
        }
        $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' => isset($empLevelConfig[$baseInfo['EMP_LV']])?$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\base\Exception
     * @throws \yii\web\HttpException
     */
    public function actionRelationList() {
        $userName = Yii::$app->request->get('userName');
        $deep = Yii::$app->request->get('deep', 2);
        $periodNum = Yii::$app->request->get('periodNum', null);
        if (!$userName) {
            $userId = Yii::$app->params['mainUserId'];
        } else {
            if (!$userId = Info::getUserIdByUserName($userName)) {
                return static::notice('会员不存在', 400);
            }
        }
        $listObj = new RelationList();
        $data = $listObj->getList(['condition' => '', 'params' => [], 'others' => ['userId' => $userId, 'deep' => $deep, 'periodNum' => $periodNum]]);
        return static::notice($data);
    }

    /**
     * 开拓网络列表导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws \yii\web\HttpException
     */
    public function actionRelationListExport() {
        $filter = $this->filterCondition([]);
        $userName = Yii::$app->request->get('userName');
        $deep = Yii::$app->request->get('deep', 2);
        $periodNum = Yii::$app->request->get('periodNum', null);
        if (!$userName) {
            $userId = Yii::$app->params['mainUserId'];
        } else {
            if (!$userId = Info::getUserIdByUserName($userName)) {
                return static::notice('会员不存在', 400);
            }
        }
        $form = new AtlasExportForm();
        $result = $form->run(array_merge($filter, ['others' => ['userId' => $userId, 'deep' => $deep, 'periodNum' => $periodNum]]), '开拓网络列表');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice('导出开始，请到文件管理-导出文件查看');
    }

    /**
     * 安置网络图
     * @return mixed
     * @throws \yii\web\HttpException
     */
    public function actionNetwork() {
        $userId = Yii::$app->request->get('id', Yii::$app->params['mainUserId']);
        $periodNum = Yii::$app->request->get('periodNum', null);
        $deep = Yii::$app->request->get('deep', 2);
        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', 2);
        $periodNum = Yii::$app->request->get('periodNum', null);
        if (!$userName) {
            $userId = Yii::$app->params['mainUserId'];
        } else {
            if (!$userId = Info::getUserIdByUserName($userName)) {
                return static::notice('会员不存在', 400);
            }
        }
        $listObj = new NetworkList();
        $data = $listObj->getList(['condition' => '', 'params' => [], 'others' => ['userId' => $userId, 'deep' => $deep, 'periodNum' => $periodNum]]);
        return static::notice($data);
    }

    /**
     * 安置网络列表导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws \yii\web\HttpException
     */
    public function actionNetworkListExport() {
        $filter = $this->filterCondition([]);
        $userName = Yii::$app->request->get('userName');
        $deep = Yii::$app->request->get('deep', 2);
        $periodNum = Yii::$app->request->get('periodNum', null);
        if (!$userName) {
            $userId = Yii::$app->params['mainUserId'];
        } else {
            if (!$userId = Info::getUserIdByUserName($userName)) {
                return static::notice('会员不存在', 400);
            }
        }
        $form = new AtlasExportForm();
        $result = $form->run(array_merge($filter, ['others' => ['userId' => $userId, 'deep' => $deep, 'periodNum' => $periodNum]]), '安置网络列表');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice('导出开始，请到文件管理-导出文件查看');
    }

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

}