<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/5/21
 * Time: 下午5:18
 */

namespace common\models;


class PushmHouse  extends Common
{

        public function rules()
        {
            return [
                ['hid', 'unique', 'targetClass' => 'common\models\PushmHouse','on'=>['add'],'message'=>'该楼盘已存在','filter'=>function($query){
                            $query->andWhere(['del'=>$this->setDel]);
                    return  $query->andWhere(['type'=>$this->type]);
                }],
                ['hid','number','message'=>'请选择楼盘'],
                ['developers_id','number','message'=>'开发商'],
                ['sort','number','message'=>'排序只能是数字'],
                ['sort','number','max'=>10000],
                ['type','number'],
            ];
        }

        public function attributeLabels()
        {
            return [
                'hid'=>'楼盘名称',
                'type'=>'类型' //6=开发商调用
            ];
        }


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

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

        //hai
        public function getCitygroupby($input)
        {
            $query = self::find();
            $query->select(['pfg_category_city.pid','pfg_category_city.abroad','pfg_pushm_house.is_show']);
            $query->andWhere(['pfg_pushm_house.del'=>$this->setDel]);
            if(!empty($input['house_name']))
            {
                $query->andWhere(['like','pfg_house.name',$input['house_name']]);
            }
            $query->andWhere(['pfg_pushm_house.type'=>4]);//$input['type']
            $query->leftJoin('pfg_house','pfg_pushm_house.hid=pfg_house.id');
            $query->leftJoin('pfg_category_city','pfg_house.city = pfg_category_city.id');
            if(!empty($input['page']))
            {
                $query->limit = $input['limit'];
                $query->offset = ( $input['page'] - 1 )* $input['limit'];
            }
//            return $query->orderBy(['pfg_pushm_house.create_at'=>SORT_DESC])->asArray()->all();
            return $query->orderBy(['pfg_pushm_house.create_at'=>SORT_DESC])->groupBy('pfg_category_city.pid')->asArray()->all();
        }
        //

        public function getList($input)
        {
            $query = self::find();
            $query->select(['pfg_category_city.city_name','pfg_house.name','pfg_pushm_house.id','pfg_pushm_house.hid','pfg_pushm_house.create_at','pfg_pushm_house.is_show','pfg_pushm_house.sort','pfg_category_city.pid']);
            $query->andWhere(['pfg_pushm_house.del'=>$this->setDel]);
            if(!empty($input['house_name']))
            {
                $query->andWhere(['like','pfg_house.name',$input['house_name']]);
            }
            if (!empty($input['city_id']))
            {
                $query->andWhere(['pfg_house.city'=>$input['city_id']]);
            }
            $query->andWhere(['pfg_pushm_house.type'=>$input['type']]);
            $query->leftJoin('pfg_house','pfg_pushm_house.hid=pfg_house.id');
            $query->leftJoin('pfg_category_city','pfg_house.city = pfg_category_city.id');
            if(!empty($input['page']))
            {
                $query->limit = $input['limit'];
                $query->offset = ( $input['page'] - 1 )* $input['limit'];
            }
            return $query->orderBy(['pfg_pushm_house.create_at'=>SORT_DESC])->asArray()->all();
//            return $query->orderBy(['pfg_pushm_house.create_at'=>SORT_DESC])->groupBy('pfg_category_city.pid')->asArray()->all();
        }

        public function getList_v1($input)
        {
            $query = self::find();
            $query->select(['pfg_category_city.city_name','pfg_house.name','pfg_pushm_house.id','pfg_pushm_house.hid','pfg_pushm_house.create_at','pfg_pushm_house.is_show','pfg_pushm_house.sort']);
            if(!empty($input['house_name']))
            {
                $query->andWhere(['like','pfg_house.name',$input['house_name']]);
            }
            if(!empty($input['city']))
            {
                $query->andWhere(['pfg_house.city'=>$input['city']]);
            }
            if(!empty($input['developers_id']))
            {
                $query->andWhere(['pfg_pushm_house.developers_id'=>$input['developers_id']]);
            }
            $query->andWhere(['pfg_pushm_house.type'=>$input['type']]);

            $query->leftJoin('pfg_house','pfg_pushm_house.hid=pfg_house.id');
            $query->leftJoin('pfg_category_city','pfg_house.city = pfg_category_city.id');
            if(!empty($input['page']))
            {
                $count = clone $query;
                $arr['total'] = $count->count();
                $query->limit = $input['limit'];
                $query->offset = ( $input['page'] - 1 )* $input['limit'];
            }
                $arr['data'] = $query->orderBy(['pfg_pushm_house.sort'=>SORT_DESC,'pfg_pushm_house.create_at'=>SORT_DESC])->asArray()->all();
                return $arr;
        }

        public function Total()
        {
            $query = self::find();
            $query->andWhere(['pfg_pushm_house.del'=>$this->setDel]);
            $query->leftJoin('pfg_house','pfg_pushm_house.hid=pfg_house.id');
            return $query->count();
        }
    
        //重写
        public function Totalhotsalefeng($input)
        {
            $query = self::find();
            $query->andWhere(['pfg_pushm_house.del'=>$this->setDel]);
            if (!empty($input['city_id']))
            {
                $query->andWhere(['pfg_house.city'=>$input['city_id']]);
            }
            if(!empty($input['house_name']))
            {
                $query->andWhere(['like','pfg_house.name',$input['house_name']]);
            }
            $query->andWhere(['pfg_pushm_house.type'=>$input['type']]);
            $query->leftJoin('pfg_house','pfg_pushm_house.hid=pfg_house.id');
            return $query->count();
        }
        
    
        //废弃
        public function Totalhotsale()
        {
            $query = self::find();
            $query->andWhere(['pfg_pushm_house.del'=>$this->setDel]);
            $query->andWhere(['pfg_pushm_house.type'=>4]);
            $query->leftJoin('pfg_house','pfg_pushm_house.hid=pfg_house.id');
            return $query->count();
        }


        public function Homegetlist($limit,$rand = null)
        {
            $query = self::find();
            $query->select(['pfg_category_city.id as cid','pfg_category_city.city_name','pfg_house.name','pfg_house.sale_price','pfg_house_detail.price_unit','pfg_house_detail.preferential','pfg_house.characteristic','pfg_house.id','pfg_house.thumb','pfg_house.click_num','pfg_house.state']);
            $query->andWhere(['pfg_pushm_house.del'=>$this->setDel]);
            $query->andWhere(['pfg_pushm_house.is_show'=>1]);
            $query->andWhere(['pfg_pushm_house.type'=>$this->type]);
            $query->leftJoin('pfg_house','pfg_pushm_house.hid=pfg_house.id');
            $query->leftJoin('pfg_category_city','pfg_house.city=pfg_category_city.id');
            $query->leftJoin('pfg_house_detail','pfg_house.id=pfg_house_detail.hid');
            if($rand != null)
            {
                $query->orderBy('RAND()');
            }
            else
            {
                $query->orderBy(['pfg_pushm_house.sort'=>SORT_ASC]);
            }
            $query->limit = $limit;
            return $query->asArray()->all();
        }

        public function CityGetHome($input)
        {
            $query = self::find();
            $query->select(['pfg_category_city.id as cid','pfg_category_city.city_name','pfg_house.name','pfg_house.sale_price','pfg_house_detail.price_unit','pfg_house_detail.preferential','pfg_house.characteristic','pfg_house.id','pfg_house.thumb','pfg_house.click_num','pfg_house.state']);
            $query->andWhere(['pfg_pushm_house.del'=>$this->setDel]);
            $query->andWhere(['pfg_pushm_house.is_show'=>1]);
            $query->andWhere(['pfg_pushm_house.type'=>$this->type]);
            $query->andWhere(['IN','pfg_house.city',$input]);
            $query->leftJoin('pfg_house','pfg_pushm_house.hid=pfg_house.id');
            $query->leftJoin('pfg_category_city','pfg_house.city=pfg_category_city.id');
            $query->leftJoin('pfg_house_detail','pfg_house.id=pfg_house_detail.hid');
            $query->orderBy(['pfg_pushm_house.sort'=>SORT_ASC]);
            $query->limit = 5;
            return $query->asArray()->all();
        }


        //分组统计 目前用于 开发商-楼盘

        public function getListGroup()
        {
            $query = self::find();
            $query->select(['count(*) as total','developers_id']);
            $query->andFilterWhere(['developers_id'=>$this->developers_id]);
            return $query->groupBy(['developers_id'])->asArray()->all();
        }

    //分组统计 目前用于 开发商-楼盘
//
//    public function getListGroup()
//    {
//        $query = self::find();
//        $query->select(['count(*) as total','developers_id']);
//        $query->andFilterWhere(['developers_id'=>$this->developers_id]);
//        return $query->groupBy(['developers_id'])->asArray()->all();
//    }
        //移动端品牌馆
        public function GetHouse()
        {
            $query = self::find();
            $query->select(['pfg_house.id','pfg_house.name','pfg_house.sale_price','pfg_house.city','pfg_house.characteristic','pfg_house.thumb','pfg_house.is_tel',
                'pfg_house_detail.price_unit','pfg_category_city.city_name','pfg_house.state']);
            $query->andWhere(['pfg_pushm_house.type'=>$this->type]);
            $query->andWhere(['pfg_pushm_house.developers_id'=>$this->developers_id]);
            $query->andWhere(['pfg_house.is_view'=>1]);
            $query->andWhere(['pfg_house.del'=>1]);
            $query->andWhere(['pfg_category_city.state'=>1]); //陈晓锋添加
//            $query->andFilterWhere(['pfg_house.is_push'=>1]); //陈晓锋注释--为什么要加这个条件
            $query->leftJoin('pfg_developers','pfg_pushm_house.developers_id = pfg_developers.id');
            $query->leftJoin('pfg_house','pfg_pushm_house.hid = pfg_house.id');
            $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->orderBy(['pfg_pushm_house.sort'=>SORT_DESC,'pfg_pushm_house.id'=>SORT_DESC]);
            return $query->asArray()->all();

        }



}