'{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(); } }