<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/26/026
 * Time: 11:15
 */

namespace backend\controllers;

use yii\filters\HttpCache;
use yii\web\Controller;
use Yii;
use backend\server\User;
use backend\base\Help;
use yii\captcha\Captcha;
use yii\log\FileTarget;

class PublicController extends Controller
{

    public $enableCsrfValidation = false;
    public $layout = false;

    public function actionAa()
    {
        phpinfo();
    }


    /*
     * 对比楼盘接口
     */
    public function actionAllhouse()
    {
        $model = \common\models\House::find()->select(['name'])->andWhere(['del' => 1])->asArray()->all();
        if (!empty($model)) {
            return Help::JsonCode(Help::SUCCESS, 'ch', $model);
        }
    }

    public function actionCes()
    {

        $model = \common\models\House::find()->select(['name', 'id'])->andWhere(['del' => 1])->andWhere(['like', 'name', ' '])->asArray()->all();
        $m = new \common\models\House();
        foreach ($model as $val) {
            $_m = clone $m;
            $a = $_m::findOne($val['id']);
            if (!empty($a)) {
                $a->name = trim($val['name'], " ");
                $a->save(false);
            }
        }
    }

    public function actionHouse()
    {
//            ini_set('memory_limit','502M');
//            ini_set("max_execution_time", "0");
//           $name = json_decode(file_get_contents(Yii::$app->basePath . '/runtime/house.json'),true);
        $model = \common\models\House::find()->select(['pfg_house.*', 'pfg_house_detail.*'])->leftJoin('pfg_house_detail', 'pfg_house.id=pfg_house_detail.hid')->andWhere(['pfg_house.del' => 1])->asArray()->all();

        return Help::JsonCode(Help::SUCCESS, 'ch', $model);
    }

    public function actionSs()
    {
        $name = Yii::$app->request->post('name');
        $val = \common\models\House::find()->select(['pfg_house.*', 'pfg_house_detail.*'])->leftJoin('pfg_house_detail', 'pfg_house.id=pfg_house_detail.hid')->andWhere(['pfg_house.del' => 1])->andWhere(['pfg_house.name' => $name])->asArray()->one();

//
//            foreach ($model as &$val)
//            {
        if (!empty($val['characteristic'])) {
            $val['characteristic'] = array_column(\common\models\Characteristic::find()->select(['name'])->andWhere(['id' => json_decode($val['characteristic'], true)])->asArray()->all(), 'name');
        }

//                 //1
        if (!empty($val['type'])) {
            $val['type'] = array_column(\common\models\CategoryLabel::find()->select(['name'])->andWhere(['type' => 1])->andWhere(['id' => json_decode($val['type'], true)])->asArray()->all(), 'name');
        }
//                //2
        if (!empty($val['architecture_type'])) {
            $val['architecture_type'] = array_column(\common\models\CategoryLabel::find()->select(['name'])->andWhere(['type' => 2])->andWhere(['id' => json_decode($val['architecture_type'], true)])->asArray()->all(), 'name');

        }
//                //3
        if (!empty($val['property_tag'])) {
            $val['property_tag'] = array_column(\common\models\CategoryLabel::find()->select(['name'])->andWhere(['type' => 3])->andWhere(['id' => json_decode($val['property_tag'], true)])->asArray()->all(), 'name');

        }
        $val['city'] = \common\models\CategoryCity::find()->select(['city_name'])->andWhere(['del' => 1])->andWhere(['id' => $val['city']])->asArray()->one()['city_name'];
//
        $val['houstalbum'] = \common\models\HouseAlbum::find()->select(['pfg_house_album.title', 'pfg_house_album.img', 'pfg_category_album.name as album_name'])->leftJoin('pfg_category_album', 'pfg_house_album.album_id=pfg_category_album.id')->andWhere(['pfg_house_album.hid' => $val['id']])->andWhere(['pfg_house_album.del' => 1])->asArray()->all();
        $val['housetype'] = \common\models\HouseType::find()->select(['pfg_house_type.title', 'pfg_house_type.img', 'pfg_house_type.area', 'pfg_house_type.indoor_info', 'pfg_category_housetype.huxing_name as type_name'])->leftJoin('pfg_category_housetype', 'pfg_house_type.type_id=pfg_category_housetype.id')->andWhere(['pfg_house_type.hid' => $val['id']])->andWhere(['pfg_house_type.del' => 1])->asArray()->all();
        $val['housepeitao'] = \common\models\HouseFacilities::find()->select(['pfg_category_facilities.name as peitao_name', 'pfg_house_facilities.distance', 'pfg_house_facilities.latitude_longitude', 'pfg_house_facilities.name'])->leftJoin('pfg_category_facilities', 'pfg_house_facilities.fid=pfg_category_facilities.id')->andWhere(['pfg_house_facilities.hid' => $val['id']])->andWhere(['pfg_house_facilities.del' => 1])->asArray()->all();
        $val['housepermit'] = \common\models\HousePermit::find()->select(['permit', 'time', 'building_num'])->andWhere(['hid' => $val['id']])->andWhere(['del' => 1])->asArray()->all();
        $val['houseprice'] = \common\models\HousePriceRecord::find()->select(['price', 'create_time', 'min_price', 'explain'])->andWhere(['hid' => $val['id']])->andWhere(['del' => 1])->asArray()->all();
//            }
        return Help::JsonCode(Help::SUCCESS, 'ch', $val);
//
    }

    public function actionAaa()
    {
        $model = new \common\models\Demo();
        $model->data = $_POST['data'];
        $model->save();
//        $log = new FileTarget();
//        $log->logFile = Yii::getAlias('@common').'/log/sync'.date('Ymd').'.log';
//        $log->messages[] = [json_encode(),1,'application',microtime(true)];
//        $log->export();
    }
//         public function behaviors()
//         {
//             return [
//                 [
//                     'class' => 'yii\filters\HttpCache',
//                     'only' => ['login'],
// //                    'lastModified' => function ($action, $params) {
// //                        return 1521815690;
// //                    },
//                     'etagSeed'=>function($action,$params){
//                         return serialize(['ass']);
//                     }
//                 ],
//             ];
//         }

    //注册
    public function actionSignup()
    {
        $this->VerifySkip();
        return $this->render('signup');
    }

    //注册提交数据
    public function actionSignupfrom()
    {
        $model = $this->UserModel();
        $row = $model->Signup();
        if ($row === true) {
            Help::JsonCode(200, '注册成功');
        }
        Help::JsonCode(100, '注册失败', $row);
    }

    //登录
    public function actionLogin()
    {
        if (empty(Yii::$app->session['user_info'])) {
            if (Yii::$app->request->isPost) {
                $model = new \backend\server\LoginFrom();
                $row = $model->loginVerification();
                if ($row === true) {
                    return Help::JsonCode(Help::SUCCESS, '登录成功');
                }
                return Help::JsonCode(Help::ERROR, '登录失败', $row);
            }
            return $this->render('login');
        }
        $this->redirect(['/index/index']);
    }


    //登录提交数据
    public function actionLoginfrom()
    {
        $model = $this->UserModel();
        $result = $model->Login();
        if ($result === true) {
            Help::JsonCode(200, '登录成功');
        }
        Help::JsonCode(300, $result);
    }

    public function actionLogout()
    {
        unset(Yii::$app->session['user_info']);
        Yii::$app->session->close();
        $this->redirect(['/public/login']);
    }

    public function actionResourceclear()
    {

        $name = Yii::$app->cache;
        if ($name->set(Yii::$app->params['resourceCacheKey']['backend'], time())) {
            return Help::JsonCode(Help::SUCCESS, '缓存清理成功');
        }
        return Help::JsonCode(Help::ERROR, '缓存清理失败');
    }

    public function actions()
    {
        return [
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'maxLength' => 4,
                'minLength' => 4,
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
                'testLimit' => 999,
            ],
        ];
    }

    //同步楼盘接口
    public function actionSynchouse()
    {
        $input = Yii::$app->request->post('data');

        $model = new \backend\server\SyncHouse();
        $result = $model->HouseOpe(json_decode($input, true));

        if ($result === true) return Help::JsonCode(Help::SUCCESS, $result);
        return Help::JsonCode(Help::ERROR, $result);

    }

    protected function UserModel()
    {
        return new User();
    }

    protected function VerifySkip()
    {
        if (!empty(Yii::$app->session['user_info'])) $this->goHome();
    }


    /*
    * 返回当天报名去重复的数据
    */
    public function actionThensignup()
    {

        $input = Yii::$app->request->post();
        if (isset($input['start']) && isset($input['end']) && is_numeric($input['start']) && is_numeric($input['end'])) {
            $model = \common\models\Enroll::find();
            $model->andWhere(['del' => 1]);
            $model->andWhere(['>=', 'create_at', $input['start']]);
            $model->andWhere(['<=', 'create_at', $input['end']]);
            $data = $model->groupBy('mobile')->count();
            if (!empty($data)) {
                return Help::JsonCode(Help::SUCCESS, Yii::t('app', 'get_success'), ['count' => $data]);
            }
        }
        return Help::JsonCode(Help::ERROR, Yii::t('app', 'get_error'));
    }

    //接收淘房同步资讯
    public function actionTfsyncnews()
    {
        try {
            $input = Yii::$app->request->post('data');
            $model = new \backend\server\news\Tfsync();
            if ($model->getData($input) == true) {
                return Help::JsonCode(Help::SUCCESS, '成功');
            }

        } catch (\Exception $e) {
            return Help::JsonCode(Help::ERROR, $e->getMessage());
        }
        return Help::JsonCode(Help::ERROR, '添加失败');
    }

    /**
     * 直播同步接受api
     * */

    public function actionSynclive()
    {
        $input = Yii::$app->request->post();

        $model = new \backend\server\SyncLive();
        $result = $model->LiveOpe($input);
        if ($result === true) return Help::JsonCode(Help::SUCCESS, $result);
        return Help::JsonCode(Help::ERROR, $result);
    }

    /**
     * 敏感词库同步接收
     * */
    public function actionSyncword()
    {
        $input = Yii::$app->request->post();
        $model = new \backend\server\SyncWord();
        $result = $model->LiveOpe($input);
        if ($result === true) return Help::JsonCode(Help::SUCCESS, $result);
        return Help::JsonCode(Help::ERROR, $result);
    }

    /**
     * 资讯同步接收
     * */
    public function actionSynchronizationnews()
    {
        $input = Yii::$app->request->post();
        $model = new \backend\server\SyncNews();
        $result = $model->newsOpe($input);
        if ($result === true) return Help::JsonCode(Help::SUCCESS, $result);
        return Help::JsonCode(Help::ERROR, $result);
    }

    /**
     * 用户同步接收
     * */
    public function actionSyncuser()
    {
        $input = Yii::$app->request->post();
        $model = new \backend\server\SyncUser();
        $result = $model->userOpe($input);
        if ($result === true) return Help::JsonCode(Help::SUCCESS, $result);
        return Help::JsonCode(Help::ERROR, $result);
    }

    /**
     * VR同步接收
     * */
    public function actionVrsync()
    {
        $input = Yii::$app->request->post();
        $model = new \backend\server\SyncVr();
        $result = $model->userOpe($input);
        if ($result === true) return Help::JsonCode(Help::SUCCESS, $result);
        return Help::JsonCode(Help::ERROR, $result);
    }
}