<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/3/4
 * Time: 上午9:50
 */

namespace common\models;

class Video extends Common
{
    public function rules()
    {
        return [
            ['hid', 'unique', 'targetClass' => 'common\models\Video', 'on' => ['add'], 'message' => '该楼盘已存在', 'filter' => function ($query) {
                return $query->andWhere(['del' => $this->setDel]);
            }],
            [['title', 'hid'], 'required', 'message' => '不能为空'],
            ['sort', 'default', 'value' => 0],
            [['thumb', 'source', 'key_words'], 'string', 'max' => 50],
            ['allow', 'in', 'range' => [1, 2]],
            ['video_url', 'string', 'max' => 150],
            [['video_category', 'city', 'hid', 'sort'], 'number'],
            [['description', 'content'], 'string'],
            ['uid', 'number']
        ];
    }

    public function attributeLabels()
    {
        return [
            'title' => '视频标题',
            'thumb' => '封面图',
            'video_url' => '视频路径',
            'source' => '视频来源',
            'key_words' => '关键词',
            'city' => '所属城市',
            'content' => '内容介绍',
            'description' => '描述',
            'sort' => '排序',
            'allow' => '是否推荐',
            'video_category' => '所属分类',
        ];
    }

    public function Authenticator($input)
    {
        $this->load($input, '');
        if (!$this->validate()) return $this->errors;
        return $this;
    }

    public function FindById($id)
    {
        return self::findOne($id);
    }

    //查看楼盘存在数据
    public function HidList()
    {
        $query = self::find();
        $query->andWhere(['del' => $this->setDel]);
        $query->andWhere(['hid' => $this->hid]);
        $query->select(['hid', 'video_url']);
        return $query->asArray()->all();
    }

    public function getList($input)
    {
        $query = $this->QueryFind();
        $query->select(['pfg_house.name as house_name', 'pfg_house.id as house_id', 'pfg_video.*', 'pfg_category_city.city_name', 'pfg_category_video.type_name']);
        $query = $this->LeftJoinWhere($query);
        $query = $this->LeftJoinCategoryCity($query);
        $query = $this->LeftJoinHouse($query);
        $query = $this->LeftJoinCategoryVideo($query);
        if (!empty($input)) {
            $query->offset = ($input['page'] - 1) * $input['limit'];
            $query->limit = $input['limit'];
        }
        return $query->orderBy(['pfg_video.create_at' => SORT_DESC])->asArray()->all();
    }

    public function Total()
    {
        $query = $this->QueryFind();
        $query = $this->LeftJoinWhere($query);
        $query = $this->LeftJoinCategoryCity($query);
        $query = $this->LeftJoinHouse($query);
        $query = $this->LeftJoinCategoryVideo($query);
        return $query->count();
    }

    /*
     * 区域分组
     * */
    public function GroupCity()
    {
        $query = self::find();
        $query->andWhere(['pfg_video.state' => 1]);
        $query->andWhere(['pfg_video.del' => 1]);
        $query->select(['pfg_category_city.city_name', 'pfg_video.city']);
        $query = $this->LeftJoinCategoryCity($query);
        $query->groupBy('pfg_video.city');
        return $query->asArray()->all();
    }

    /*
     * 前端视频首页
     * */
    public function homeList($input)
    {
        $query = self::find();
        $query->andWhere(['pfg_video.state' => 1]);
        $query->andWhere(['pfg_video.del' => 1]);
        $query->andFilterWhere(['pfg_video.city' => $this->city]);
        $query->andFilterWhere(['pfg_video.allow' => $this->allow]);
        $query->select(['pfg_house.name', 'pfg_house.sale_price', 'pfg_video.city', 'pfg_video.plays', 'pfg_video.thumbs_up', 'pfg_video.description', 'pfg_video.id', 'pfg_video.hid', 'pfg_house_detail.price_unit', 'pfg_video.thumb', 'pfg_video.video_url']);
        $query = $this->LeftJoinHouse($query);
        $query->leftJoin('pfg_house_detail', 'pfg_house.id = pfg_house_detail.hid');
        if (!empty($input)) {
            $query->offset = ($input['page'] - 1) * $input['limit'];
            $query->limit = $input['limit'];
        }
        return $query->asArray()->all();
    }


    //移动端搜索接口
    public function getText($post)
    {
        $query = self::find();
        $query->andWhere(['pfg_video.state' => 1]);
        $query->andWhere(['pfg_video.del' => 1]);
        $query->select(['pfg_house.name', 'pfg_house.sale_price', 'pfg_video.city', 'pfg_video.plays',
            'pfg_video.thumbs_up', 'pfg_video.description', 'pfg_video.id', 'pfg_video.hid',
            'pfg_video.thumb', 'pfg_video.video_url']);
        if (!empty($post['text'])) {
            $query->andWhere(['like', 'pfg_house.name', $post['text']]);
        }
        $query->leftJoin('pfg_house', 'pfg_video.hid = pfg_house.id');
        $query->leftJoin('pfg_house_detail', 'pfg_house.id = pfg_house_detail.hid');
        $query->orderBy(['pfg_video.create_at' => SORT_DESC]);
        return $query->asArray()->all();
    }


    /**
     *
     */
    public function homeListTotal()
    {
        $query = self::find();
        $query->andWhere(['pfg_video.state' => 1]);
        $query->andWhere(['pfg_video.del' => 1]);
        $query->andFilterWhere(['pfg_video.city' => $this->city]);
        $query = $this->LeftJoinHouse($query);
        $query->leftJoin('pfg_house_detail', 'pfg_house.id = pfg_house_detail.hid');
        return $query->count();
    }

    /**
     * 随机显示
     */
    public function RandList($limit = null, $is_push = null)
    {
        $query = self::find();
        $query->andWhere(['pfg_video.state' => 1]);
        $query->andWhere(['pfg_video.del' => 1]);
        $query->select(['pfg_house.name', 'pfg_category_city.city_name', 'pfg_house.sale_price', 'pfg_video.city', 'pfg_video.id', 'pfg_video.hid', 'pfg_house_detail.price_unit', 'pfg_video.thumb', 'pfg_house.thumb as img']);
        if (!empty($is_push)) {
            $query->andWhere(['pfg_house.is_push' => 2]);
        }
        $query = $this->LeftJoinHouse($query);
        $query->leftJoin('pfg_house_detail', 'pfg_house.id = pfg_house_detail.hid');
        $query->leftJoin('pfg_category_city', 'pfg_house.city = pfg_category_city.id');
        $query->limit = $limit;
        return $query->orderBy('rand()')->asArray()->all();
    }

    /**
     * @param $query
     * @return mixed
     */
    public function HomeFind()
    {
        $query = self::find();
        $query->andWhere(['pfg_video.state' => 1]);
        $query->andWhere(['pfg_video.del' => 1]);
        $query->andWhere(['pfg_video.id' => $this->id]);
        $query->select(['pfg_house.name', 'pfg_house.sale_price',
            'pfg_video.city', 'pfg_video.id', 'pfg_video.hid',
            'pfg_house_detail.price_unit', 'pfg_video.thumb',
            'pfg_video.video_url', 'pfg_video.source', 'pfg_video.create_at',
            'pfg_house_detail.open_time', 'pfg_house_detail.residential_type',
            'pfg_house_detail.launch_time', 'pfg_house_detail.address', 'pfg_house_detail.info', 'pfg_house_detail.hid']);
        $query = $this->LeftJoinHouse($query);
        $query->leftJoin('pfg_house_detail', 'pfg_house.id = pfg_house_detail.hid');
        return $query->asArray()->one();
    }

    private function LeftJoinWhere($query)
    {
        $query->andFilterWhere(['pfg_video.del' => $this->setDel]);
        $query->andFilterWhere(['pfg_video.city' => $this->city]);
        $query->andFilterWhere(['pfg_video.video_category' => $this->video_category]);
        $query->andFilterWhere(['like', 'pfg_video.title', $this->title]);
        $query->andFilterWhere(['like', 'pfg_house.name', $this->hid]);
        return $query;
    }

    private function LeftJoinHouse($query)
    {
        return $query->leftJoin('pfg_house', 'pfg_video.hid = pfg_house.id');
    }

    private function LeftJoinCategoryVideo($query)
    {
        return $query->leftJoin('pfg_category_video', 'pfg_video.video_category = pfg_category_video.id');
    }

    private function LeftJoinCategoryCity($query)
    {
        return $query->leftJoin('pfg_category_city', 'pfg_video.city = pfg_category_city.id');
    }

    private function QueryFind()
    {
        return self::find();
    }
}