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

namespace common\models;

class HouseComment extends Common
{
    public $xuqiu = [1 => '预热观看', 2 => '有打算', 3 => '准备下手'];

    public $house_name; //搜索楼盘名字
    public $reply_status; //回复与未回复

    public function rules()
    {
        return [
            ['mobile', 'match', 'pattern' => '/^1[23456789]\d{9}$/', 'message' => '请输入正确的手机号码'],
            [['mobile', 'hid', 'content', 'ip'], 'required', 'message' => '{attribute}不能为空'],
            ['content', 'string', 'max' => 200],
            ['ip', 'ip'],

            ['diduan', 'compare', 'compareValue' => 5, 'operator' => '<='],
            ['jiaotong', 'compare', 'compareValue' => 5, 'operator' => '<='],
            ['peitao', 'compare', 'compareValue' => 5, 'operator' => '<='],
            ['huanjing', 'compare', 'compareValue' => 5, 'operator' => '<='],
            ['xingjiabi', 'compare', 'compareValue' => 5, 'operator' => '<='],

            [['del', 'is_show', 'initiator'], 'in', 'range' => [1, 2], 'message' => '请输入正确的信息'],
            [['del', 'is_show', 'initiator'], 'default', 'value' => 1],
//            [['jiaotong','peitao','huanjing','xingjiabi','diduan'],'integer','max'=>1],
            ['demand', 'in', 'range' => array_flip($this->xuqiu), 'message' => '购房需求错误'],

            //搜索字段
            ['house_name', 'string', 'max' => 30],
            ['reply_status', 'in', 'range' => [1, 2]],
            ['read', 'in', 'range' => [1, 2]],

            ['author', 'string', 'max' => 255],
        ];
    }

    public function attributeLabels()
    {
        return [
            'hid' => '楼盘名称',
            'mobile' => '手机号码',
            'ip' => '用户IP地址',
            'content' => '评测内容',
            'author' => '添加人',
        ];
    }

    public function getList($page)
    {
        $query = self::find();
        $query->select(['pfg_house.name', 'pfg_house_comment.*', 'count(pfg_house_comment.hid) as total']);
        $query->andWhere(['pfg_house_comment.del' => 1]);
        if (!empty($page['content_status'])) {
            $query->andWhere(['pfg_house_comment.read' => $page['content_status']]);
        }
        $query->leftJoin('pfg_house', 'pfg_house_comment.hid=pfg_house.id');
        if (!empty($page['house_text'])) {
            $query->andWhere(['like', 'pfg_house.name', trim($page['house_text'], " ")]);
        }
        if (!empty($page['page'])) {
            $query->offset = ($page['page'] - 1) * $page['limit'];
            $query->limit = $page['limit'];
        }
        $query->groupBy('pfg_house_comment.hid')->orderBy(['pfg_house_comment.create_at' => SORT_DESC])->asArray();
        return $query->all();
    }

    public function Total()
    {

        return self::find()->andWhere(['del' => 1])->andFilterWhere(['hid' => $this->hid])->count();
    }

    public function MoregetList($page)
    {
        $query = self::find();
        $select = ['pfg_house.name', 'pfg_house_comment.*'];
//        $query->select(['pfg_house.name','pfg_house_comment.*','(case when pfg_house_commentreply.reply_content is null then 1 when pfg_house_commentreply.reply_content is not null then 2 else null end) reply']);


        $query->innerJoin('pfg_house', 'pfg_house_comment.hid=pfg_house.id');
        if ($this->reply_status == 2) {
            array_push($select, 'pfg_house_commentreply.id as reply');
            $query->innerJoin("pfg_house_commentreply", "pfg_house_comment.id = pfg_house_commentreply.cid");
        } else if ($this->reply_status == 1) {
            $cid = HouseCommentreply::find()->select('cid')->column();
            $query->andFilterWhere(['not in', 'pfg_house_comment.id', $cid]);
        } else {
            array_push($select, 'pfg_house_commentreply.id as reply');
            $query->leftJoin("pfg_house_commentreply", " pfg_house_comment.id = pfg_house_commentreply.cid");
        }
        $query->select($select);
//        if(!empty($page['reply_status'])) {
//            if ($page['reply_status'] == 1) {
//                $query->andWhere("pfg_house_commentreply.reply_content is null");
//            }
//            if ($page['reply_status'] == 2) {
//                $query->andWhere("pfg_house_commentreply.reply_content is not null");
//            }
//        }
        $query->andWhere(['pfg_house.del' => 1]);
        $query->andWhere(['pfg_house_comment.del' => 1]);
        $query->andFilterWhere(['pfg_house_comment.hid' => $this->hid]);
        $query->andFilterWhere(['pfg_house_comment.read' => $this->read]);
        $query->andFilterWhere(['pfg_house_comment.initiator' => $this->initiator]);
        $query->andFilterWhere(['like', 'pfg_house.name', $this->house_name]);

        if (!empty($page['page'])) {
            $query->offset = ($page['page'] - 1) * $page['limit'];
            $query->limit = $page['limit'];
        }
        return $query->orderBy(['pfg_house_comment.create_at' => SORT_DESC])->asArray()->all();
//        $query = self::find();
//        $query->select(['pfg_house_comment.*','(case when pfg_house_commentreply.reply_content is null then 1 when pfg_house_commentreply.reply_content is not null then 2 else null end) reply']);
//        $query->leftJoin("pfg_house_commentreply","pfg_house_comment.id = pfg_house_commentreply.cid");
//        if(!empty($page['reply_status'])) {
//            if ($page['reply_status'] == 1) {
//                $query->andWhere("pfg_house_commentreply.reply_content is null");
//            }
//            if ($page['reply_status'] == 2) {
//                $query->andWhere("pfg_house_commentreply.reply_content is not null");
//            }
//        }
//        $query->andWhere(['pfg_house_comment.del'=>1]);
//        $query->andWhere(['pfg_house_comment.hid'=>$this->hid]);
//        if(!empty($page['content_status'])){
//            $query->andWhere(['pfg_house_comment.read' => $page['content_status']]);
//        }
//        if(!empty($page['page']))
//        {
//            $query->offset = ($page['page'] - 1) * $page['limit'];
//            $query->limit = $page['limit'];
//        }
//        return $query->orderBy(['pfg_house_comment.create_at'=>SORT_DESC])->asArray()->all();
    }

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

    //Ip 限制
    public function Iplimit($input)
    {
        $total = self::find()->andWhere(['del' => 1, 'hid' => $input['hid'], 'ip' => $input['ip']])->count();
        if ($total >= 2) {
            return false;
        }
        return true;
    }

    public function getHouseList($page)
    {

        $query = self::find();
        $query->select(['pfg_house.name', 'pfg_house_comment.*']);
        $query->andWhere(['pfg_house_comment.del' => 1]);
        $query->andWhere(['pfg_house_comment.is_show' => 1]);
        $query->andWhere(['pfg_house_comment.hid' => $this->hid]);
        $query->leftJoin('pfg_house', 'pfg_house_comment.hid=pfg_house.id');
        if (is_array($page) && !empty($page)) {
            $query->offset = ($page['page'] - 1) * $page['limit'];
            $query->limit = $page['limit'];
        } else {
            $query->limit = $page;
        }
        return $query->orderBy(['pfg_house_comment.create_at' => SORT_DESC])->asArray()->all();
    }

    public function getHouseListTotal()
    {
        $query = self::find();
        $query->andWhere(['del' => 1]);
        $query->andWhere(['is_show' => 1]);
        $query->andWhere(['hid' => $this->hid]);
        return $query->count();
    }

    public function initSql($query)
    {
        $query->select(['pfg_house_comment.*', '(case when pfg_house_commentreply.reply_content is null then 1 when pfg_house_commentreply.reply_content is not null then 2 else null end) reply']);
        $query->leftJoin("pfg_house_commentreply", "pfg_house_comment.id = pfg_house_commentreply.cid");
        if (!empty($page['reply_status'])) {
            if ($page['reply_status'] == 1) {
                $query->andWhere("pfg_house_commentreply.reply_content is null");
            }
            if ($page['reply_status'] == 2) {
                $query->andWhere("pfg_house_commentreply.reply_content is not null");
            }
        }
        $query->andWhere(['pfg_house_comment.del' => 1]);
        $query->andWhere(['pfg_house_comment.hid' => $this->hid]);
        if (!empty($page['content_status'])) {
            $query->andWhere(['pfg_house_comment.read' => $page['content_status']]);
        }
        return $query;
    }

    public function moreTotal($page)
    {
        $query = self::find();

        $query->innerJoin('pfg_house', 'pfg_house_comment.hid=pfg_house.id');
        if ($this->reply_status == 2) {
            $query->innerJoin("pfg_house_commentreply", "pfg_house_comment.id = pfg_house_commentreply.cid");
        } else if ($this->reply_status == 1) {
            $cid = HouseCommentreply::find()->select('cid')->column();
            $query->andFilterWhere(['not in', 'pfg_house_comment.id', $cid]);
//            $query->innerJoin("pfg_house_commentreply", "pfg_house_commentreply.cid != pfg_house_comment.id ");
//            $query->andFilterWhere(['<>','pfg_house_commentreply.cid','null']);

        }
        $query->andWhere(['pfg_house_comment.del' => 1]);
        $query->andFilterWhere(['pfg_house_comment.hid' => $this->hid]);
        $query->andFilterWhere(['pfg_house_comment.initiator' => $this->initiator]);
        $query->andFilterWhere(['like', 'pfg_house.name', $this->house_name]);
        $query->andFilterWhere(['pfg_house_comment.read' => $this->read]);

        return $query->count();
//        $query = self::find();
//        $query->select(['pfg_house_comment.*','(case when pfg_house_commentreply.reply_content is null then 1 when pfg_house_commentreply.reply_content is not null then 2 else null end) reply']);
//        $query->leftJoin("pfg_house_commentreply","pfg_house_comment.id = pfg_house_commentreply.cid");
//        if(!empty($page['reply_status'])) {
//            if ($page['reply_status'] == 1) {
//                $query->andWhere("pfg_house_commentreply.reply_content is null");
//            }
//            if ($page['reply_status'] == 2) {
//                $query->andWhere("pfg_house_commentreply.reply_content is not null");
//            }
//        }
//        $query->andWhere(['pfg_house_comment.del'=>1]);
//        $query->andWhere(['pfg_house_comment.hid'=>$this->hid]);
//        if(!empty($page['content_status'])){
//            $query->andWhere(['pfg_house_comment.read' => $page['content_status']]);
//        }
//        return $query->count();
    }


    //PC资讯列表页热门评论
    public function GetComment($limit)
    {
        $query = self::find();
        $query->andWhere(['pfg_house.is_push' => 2]);
        $query->andWhere(['pfg_house.is_view' => 1]);
        $query->andWhere(['pfg_house.del' => 1]);
        $query->andWhere(['pfg_house_comment.is_show' => 1, 'pfg_house_comment.del' => 1]);
        $query->select(['pfg_house.id', 'pfg_house.name', 'pfg_house_comment.mobile', 'pfg_house_comment.content', 'pfg_house_comment.create_at']);
        $query->leftJoin('pfg_house', 'pfg_house_comment.hid = pfg_house.id');
        $query->groupBy('pfg_house_comment.hid');
        $query->limit = $limit;
        return $query->orderBy('RAND()')->asArray()->all();
    }


}