123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- <?php
- /**
- * Created by PhpStorm.
- * User: xiaofeng
- * Date: 2018/3/4
- * Time: 上午9:28
- */
- namespace common\models;
- class CategoryCity extends Common
- {
- public function rules()
- {
- return [
- [['city_name','pinyin','abroad','ll'],'required','message'=>'{attribute}不能为空'],
- ['sort','number','message'=>'请输入数字'],
- ['city_name','string','min'=>2,'max'=>10,'message'=>'请输入正确的城市名称'],
- ['city_name', 'unique', 'targetClass' => 'common\models\CategoryCity','on'=>['add'],'message'=>'该城市名称已经存在','filter'=>function($query){
- $query->andWhere(['pid'=>$this->pid]);
- return $query->andWhere(['del'=>$this->setDel]);
- }],
- ['city_name','trim'],
- [['ll','a','pinyin'],'string','max'=>100],
- [['state','del','abroad'],'number'],
- [['state','del'],'default','value'=>1],
- [['state','del','abroad'],'in','range'=>[1,2]],
- ['pid','default','value'=>0],
- ['path','default','value'=>'0,'],
- ['level','in','range'=>[1,2,3]],
- ['level','default','value'=>1,'on'=>['add']],
- ['dialogue','string']
- ];
- }
- public function attributeLabels()
- {
- return [
- 'city_name'=>'区域名称',
- 'pinyin'=>'区域拼音',
- 'abroad'=>'所属国籍',
- 'a'=>'A记录',
- 'll'=>'经纬度',
- 'dialogue'=>'商务通代码',
- ];
- }
- /*
- * 调用里面的验证,错误返回数组,正确返回对象
- * */
- public function Authenticator($input)
- {
- $this->load($input,'');
- if(!$this->validate()) return $this->errors;
- return $this;
- }
- /**
- * @param 修改
- */
- public function Edit($input)
- {
- $row = self::findOne($input['id']);
- if($row->load($input,'') && $row->save()) return true;
- return $row->errors;
- }
- //获取父类信息
- public function Getfather($id)
- {
- // return self::fin
- }
- public function FindById($id)
- {
- return self::findOne($id);
- }
- public function getList($page,$arr = [])
- {
- $query = self::find();
- $query->select($arr);
- // $query = $this->WhereColumn($query);
- $query->andFilterWhere(['like','city_name',$this->city_name]);
- $query->andFilterWhere(['pid'=>$this->pid]);
- $query->andFilterWhere(['id'=>$this->id]);
- $query->andFilterWhere(['level'=>$this->level]);
- $query->andFilterWhere(['abroad'=>$this->abroad]);
- if(!empty($this->state))
- {
- $query->andFilterWhere(['state'=>$this->state]);
- }
- else
- {
- $query->andWhere(['state'=>1]);
- }
- $query->andWhere(['del'=>$this->setDel]);
- if(!empty($page['page']))
- {
- $query->offset = ($page['page'] - 1) * $page['limit'];
- $query->limit = $page['limit'];
- }
- return $query->orderBy(['sort'=>SORT_DESC])->asArray()->all();
- }
- /*
- * 排序
- * */
- public function SortgetList($page)
- {
- $query = self::find();
- $query->select(['id','city_name','pid',"CONCAT(path,id,',') as paths",'pinyin']);
- $query = $this->WhereColumn($query);
- if(!empty($page['page']))
- {
- $query->offset = ($page['page'] - 1) * $page['limit'];
- $query->limit = $page['limit'];
- }
- return $query->orderBy(['paths'=>SORT_ASC])->asArray()->all();
- }
- private function WhereColumn($query)
- {
- $query->andFilterWhere(['like','city_name',$this->city_name]);
- $query->andFilterWhere(['pid'=>$this->pid]);
- $query->andFilterWhere(['id'=>$this->id]);
- $query->andFilterWhere(['abroad'=>$this->abroad]);
- if(!empty($this->state))
- {
- $query->andFilterWhere(['state'=>$this->state]);
- }
- else
- {
- $query->andWhere(['state'=>1]);
- }
- $query->andWhere(['del'=>$this->setDel]);
- return $query;
- }
- public function Total()
- {
- $query = self::find();
- $query = $this->WhereColumn($query);
- return $query->count();
- }
- //传递父类pid 获取所有子类
- public function GetAllSon($id,$sele = null,$depth = 1)
- {
- $query = self::find();
- $query->select($sele);
- $query->andWhere(['del'=>$this->setDel]);
- $query->andWhere(['pid'=>$id]);
- $query->andWhere(['state'=>1]);
- $query->orderBy(['sort'=>SORT_DESC]);
- $rows = $query->asArray()->all();
- $data = $rows;
- if($depth == 1){
- if($rows != null)
- {
- $id = array_column($rows,'id');
- $q = self::find();
- $q->select($sele);
- $q->andWhere(['del'=>$this->setDel]);
- $q->andWhere(['pid'=>$id]);
- $r = $q->asArray()->all();
- if($r != null)
- {
- $data = array_merge($rows,$r);
- }
- }
- }
- return $data;
- }
- /**
- * 传递父类pid获取豪宅子类
- * */
- public function GetHaoSon($id)
- {
- $son_city = PushmMansion::find()->select(['pfg_category_city.id','pfg_category_city.city_name','pfg_category_city.pinyin'])
- ->andWhere(['pfg_pushm_mansion.del'=>1])
- ->andWhere(['pfg_category_city.pid'=>$id])
- ->leftJoin('pfg_house','pfg_house.id = pfg_pushm_mansion.hid')
- ->innerJoin('pfg_category_city','pfg_category_city.id= pfg_house.city')
- ->asArray()->all();
- return $son_city;
- }
- /*
- * 获取所有子类
- * */
- public function Sonlist()
- {
- $query = self::find();
- $query->select(['id','city_name']);
- $query->andWhere(['<>','pid',0]);
- $query->andWhere(['del'=>$this->setDel]);
- return $query->asArray()->all();
- }
- /**
- * 传递区域ID
- */
- public function FatherSon($id)
- {
- $arr = [];
- $query = self::find();
- $query->andWhere(['del'=>$this->setDel]);
- $query->andWhere(['id'=>$id]);
- $result = $query->one();
- if(!empty($result) && $result['pid'] != 0)
- {
- $arr['city'] = $result['city_name'];
- $queryModel = self::find();
- $queryModel->andWhere(['del'=>$this->setDel]);
- $queryModel->andWhere(['id'=>$result['pid']]);
- $row = $queryModel->one();
- if($row != null)
- {
- $arr['province'] = $row['city_name'];
- }
- }
- return $arr;
- }
- //查询是否是父类
- public function TestingId($id)
- {
- $row = static::findOne($id);
- if(!empty($row))
- {
- if($row['pid'] == 0)
- {
- return static::find()->select(['id'])->andWhere(['del'=>$this->setDel])->andWhere(['pid'=>$row['id']])->column();
- }
- else
- {
- return [$row['id']];
- }
- }
- }
- //优选新房地区 2019-03-25
- public function getSonCity($id,$sele = null,$limts=5)
- {
- $query = self::find();
- $query->select($sele);
- $query->andWhere(['del'=>$this->setDel]);
- $query->andWhere(['pid'=>$id]);
- if(!empty($this->state))
- {
- $query->andFilterWhere(['state'=>$this->state]);
- }
- else
- {
- $query->andFilterWhere(['state'=>1]);
- }
- $query->orderBy(['sort'=>SORT_DESC]);
- $query->limit=$limts;
- $rows = $query->asArray()->all();
- $data = $rows;
- if($rows != null)
- {
- $id = array_column($rows,'id');
- $q = self::find();
- $q->select($sele);
- $q->andWhere(['del'=>$this->setDel]);
- $q->andWhere(['pid'=>$id]);
- $r = $q->asArray()->all();
- if($r != null)
- {
- $data = array_merge($rows,$r);
- }
- }
- return $data;
- }
- //区域电话管理
- public function telList($input)
- {
- $query = $this->telWhere();
- $rows = $query->orderBy(['pfg_category_city.abroad'=>SORT_ASC,'pfg_category_city.id'=>SORT_ASC])->asArray()->all();
- if (!empty($input['id'])){
- $lowerlevel = $this->telWhere()->andWhere(['pfg_category_city.id'=>$input['id']])->asArray()->all();
- $rows = $lowerlevel;
- if($lowerlevel[0]['pid'] != 0){
- $upperlevel = $this->telWhere()->andWhere(['pfg_category_city.id'=>$lowerlevel[0]['pid']])->asArray()->all();
- $rows = array_merge($upperlevel,$lowerlevel);
- }
- }
- if (!empty($input['name'])){
- if ($input['name'] == 2){
- $name = $this->telWhere()->andWhere(['pfg_category_tel.name'=>null])->asArray()->all();
- $pid = array_column($name,'pid');
- if($pid != 0){
- $upperlevel = $this->telWhere()->andWhere(['IN','pfg_category_city.id',$pid])->asArray()->all();
- $arr = array_merge($upperlevel,$name);
- $rows = $this->assoc_unique($arr,'id');
- }
- } elseif ($input['name'] == 1){
- $name = $this->telWhere()->andWhere(['not',['pfg_category_tel.name'=>null]])->asArray()->all();
- $pid = array_column($name,'pid');
- if($pid != 0){
- $upperlevel = $this->telWhere()->andWhere(['IN','pfg_category_city.id',$pid])->asArray()->all();
- $arr = array_merge($upperlevel,$name);
- $rows = $this->assoc_unique($arr,'id');
- }
- }
- }
- return $rows;
- }
- //区域电话管理
- public function telTotal(){
- $query = $this->telWhere()->count();
- return $query;
- }
- //区域电话管理
- private function telWhere(){
- $query = self::find();
- $query->select(['pfg_category_city.city_name','pfg_category_city.id','pfg_category_tel.name','pfg_category_city.pid'])
- ->leftJoin('pfg_category_tel_city','pfg_category_city.id = pfg_category_tel_city.cid')
- ->leftJoin('pfg_category_tel','pfg_category_tel_city.tid = pfg_category_tel.id and pfg_category_tel.del = 1')
- ->andWhere(['pfg_category_city.del'=> 1]);
- return $query;
- }
- private function assoc_unique($arr, $key) {
- $tmp_arr = array();
- foreach ($arr as $k => $v) {
- if (in_array($v[$key], $tmp_arr)) {//搜索$v[$key]是否在$tmp_arr数组中存在,若存在返回true
- unset($arr[$k]);
- } else {
- $tmp_arr[] = $v[$key];
- }
- }
- sort($arr); //sort函数对数组进行排序
- return $arr;
- }
- //父类城市,按国内外排序
- public function GetUpCity()
- {
- $query = self::find();
- $query->select(['id','pid','city_name','pinyin']);
- $query->andWhere(['del'=>$this->setDel]);
- $query->andWhere(['state'=>1]);
- $query->andWhere(['pid'=>0]);
- $query->orderBy(['abroad'=>SORT_ASC,'sort'=>SORT_DESC]);
- return $query->asArray()->all();
- }
- public function getCityNameTurnId($cityname)
- {
- $query = self::find();
- $query->select(['id','pid','city_name']);
- $query->andFilterWhere(['city_name'=>$cityname['city']]);
- return $query->asArray()->one();
- }
- public function getParentCity()
- {
- return $this->hasOne(CategoryCity::className(), ['id' => 'pid'])->from(CategoryCity::tableName().' fp')->select('fp.city_name');
- }
-
- public function getTongSonList($city){
- $data = self::find()->where(['del'=>1,'state'=>1,'pid'=>$city])->asArray()->all();
- $list = [];
- if(!empty($data)){
- $list = array_column($data,'id');
- }
- $list[] = $city;
- return $list;
- }
-
- /*
- * 国内 国外
- */
- public function TypeAbroad($sele = [])
- {
- $query = static::find();
- $query->andWhere(['state' => 1]);
- $query->andWhere(['del' => 1]);
- $query->andWhere(['abroad' => $this->abroad]);
- $query->select($sele);
- return $query->orderBy(['sort' => SORT_DESC])->asArray()->all();
- }
- }
|