CategoryCity.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/4
  6. * Time: 上午9:28
  7. */
  8. namespace common\models;
  9. class CategoryCity extends Common
  10. {
  11. public function rules()
  12. {
  13. return [
  14. [['city_name','pinyin','abroad','ll'],'required','message'=>'{attribute}不能为空'],
  15. ['sort','number','message'=>'请输入数字'],
  16. ['city_name','string','min'=>2,'max'=>10,'message'=>'请输入正确的城市名称'],
  17. ['city_name', 'unique', 'targetClass' => 'common\models\CategoryCity','on'=>['add'],'message'=>'该城市名称已经存在','filter'=>function($query){
  18. $query->andWhere(['pid'=>$this->pid]);
  19. return $query->andWhere(['del'=>$this->setDel]);
  20. }],
  21. ['city_name','trim'],
  22. [['ll','a','pinyin'],'string','max'=>100],
  23. [['state','del','abroad'],'number'],
  24. [['state','del'],'default','value'=>1],
  25. [['state','del','abroad'],'in','range'=>[1,2]],
  26. ['pid','default','value'=>0],
  27. ['path','default','value'=>'0,'],
  28. ['level','in','range'=>[1,2,3]],
  29. ['level','default','value'=>1,'on'=>['add']],
  30. ['dialogue','string']
  31. ];
  32. }
  33. public function attributeLabels()
  34. {
  35. return [
  36. 'city_name'=>'区域名称',
  37. 'pinyin'=>'区域拼音',
  38. 'abroad'=>'所属国籍',
  39. 'a'=>'A记录',
  40. 'll'=>'经纬度',
  41. 'dialogue'=>'商务通代码',
  42. ];
  43. }
  44. /*
  45. * 调用里面的验证,错误返回数组,正确返回对象
  46. * */
  47. public function Authenticator($input)
  48. {
  49. $this->load($input,'');
  50. if(!$this->validate()) return $this->errors;
  51. return $this;
  52. }
  53. /**
  54. * @param 修改
  55. */
  56. public function Edit($input)
  57. {
  58. $row = self::findOne($input['id']);
  59. if($row->load($input,'') && $row->save()) return true;
  60. return $row->errors;
  61. }
  62. //获取父类信息
  63. public function Getfather($id)
  64. {
  65. // return self::fin
  66. }
  67. public function FindById($id)
  68. {
  69. return self::findOne($id);
  70. }
  71. public function getList($page,$arr = [])
  72. {
  73. $query = self::find();
  74. $query->select($arr);
  75. // $query = $this->WhereColumn($query);
  76. $query->andFilterWhere(['like','city_name',$this->city_name]);
  77. $query->andFilterWhere(['pid'=>$this->pid]);
  78. $query->andFilterWhere(['id'=>$this->id]);
  79. $query->andFilterWhere(['level'=>$this->level]);
  80. $query->andFilterWhere(['abroad'=>$this->abroad]);
  81. if(!empty($this->state))
  82. {
  83. $query->andFilterWhere(['state'=>$this->state]);
  84. }
  85. else
  86. {
  87. $query->andWhere(['state'=>1]);
  88. }
  89. $query->andWhere(['del'=>$this->setDel]);
  90. if(!empty($page['page']))
  91. {
  92. $query->offset = ($page['page'] - 1) * $page['limit'];
  93. $query->limit = $page['limit'];
  94. }
  95. return $query->orderBy(['sort'=>SORT_DESC])->asArray()->all();
  96. }
  97. /*
  98. * 排序
  99. * */
  100. public function SortgetList($page)
  101. {
  102. $query = self::find();
  103. $query->select(['id','city_name','pid',"CONCAT(path,id,',') as paths",'pinyin']);
  104. $query = $this->WhereColumn($query);
  105. if(!empty($page['page']))
  106. {
  107. $query->offset = ($page['page'] - 1) * $page['limit'];
  108. $query->limit = $page['limit'];
  109. }
  110. return $query->orderBy(['paths'=>SORT_ASC])->asArray()->all();
  111. }
  112. private function WhereColumn($query)
  113. {
  114. $query->andFilterWhere(['like','city_name',$this->city_name]);
  115. $query->andFilterWhere(['pid'=>$this->pid]);
  116. $query->andFilterWhere(['id'=>$this->id]);
  117. $query->andFilterWhere(['abroad'=>$this->abroad]);
  118. if(!empty($this->state))
  119. {
  120. $query->andFilterWhere(['state'=>$this->state]);
  121. }
  122. else
  123. {
  124. $query->andWhere(['state'=>1]);
  125. }
  126. $query->andWhere(['del'=>$this->setDel]);
  127. return $query;
  128. }
  129. public function Total()
  130. {
  131. $query = self::find();
  132. $query = $this->WhereColumn($query);
  133. return $query->count();
  134. }
  135. //传递父类pid 获取所有子类
  136. public function GetAllSon($id,$sele = null,$depth = 1)
  137. {
  138. $query = self::find();
  139. $query->select($sele);
  140. $query->andWhere(['del'=>$this->setDel]);
  141. $query->andWhere(['pid'=>$id]);
  142. $query->andWhere(['state'=>1]);
  143. $query->orderBy(['sort'=>SORT_DESC]);
  144. $rows = $query->asArray()->all();
  145. $data = $rows;
  146. if($depth == 1){
  147. if($rows != null)
  148. {
  149. $id = array_column($rows,'id');
  150. $q = self::find();
  151. $q->select($sele);
  152. $q->andWhere(['del'=>$this->setDel]);
  153. $q->andWhere(['pid'=>$id]);
  154. $r = $q->asArray()->all();
  155. if($r != null)
  156. {
  157. $data = array_merge($rows,$r);
  158. }
  159. }
  160. }
  161. return $data;
  162. }
  163. /**
  164. * 传递父类pid获取豪宅子类
  165. * */
  166. public function GetHaoSon($id)
  167. {
  168. $son_city = PushmMansion::find()->select(['pfg_category_city.id','pfg_category_city.city_name','pfg_category_city.pinyin'])
  169. ->andWhere(['pfg_pushm_mansion.del'=>1])
  170. ->andWhere(['pfg_category_city.pid'=>$id])
  171. ->leftJoin('pfg_house','pfg_house.id = pfg_pushm_mansion.hid')
  172. ->innerJoin('pfg_category_city','pfg_category_city.id= pfg_house.city')
  173. ->asArray()->all();
  174. return $son_city;
  175. }
  176. /*
  177. * 获取所有子类
  178. * */
  179. public function Sonlist()
  180. {
  181. $query = self::find();
  182. $query->select(['id','city_name']);
  183. $query->andWhere(['<>','pid',0]);
  184. $query->andWhere(['del'=>$this->setDel]);
  185. return $query->asArray()->all();
  186. }
  187. /**
  188. * 传递区域ID
  189. */
  190. public function FatherSon($id)
  191. {
  192. $arr = [];
  193. $query = self::find();
  194. $query->andWhere(['del'=>$this->setDel]);
  195. $query->andWhere(['id'=>$id]);
  196. $result = $query->one();
  197. if(!empty($result) && $result['pid'] != 0)
  198. {
  199. $arr['city'] = $result['city_name'];
  200. $queryModel = self::find();
  201. $queryModel->andWhere(['del'=>$this->setDel]);
  202. $queryModel->andWhere(['id'=>$result['pid']]);
  203. $row = $queryModel->one();
  204. if($row != null)
  205. {
  206. $arr['province'] = $row['city_name'];
  207. }
  208. }
  209. return $arr;
  210. }
  211. //查询是否是父类
  212. public function TestingId($id)
  213. {
  214. $row = static::findOne($id);
  215. if(!empty($row))
  216. {
  217. if($row['pid'] == 0)
  218. {
  219. return static::find()->select(['id'])->andWhere(['del'=>$this->setDel])->andWhere(['pid'=>$row['id']])->column();
  220. }
  221. else
  222. {
  223. return [$row['id']];
  224. }
  225. }
  226. }
  227. //优选新房地区 2019-03-25
  228. public function getSonCity($id,$sele = null,$limts=5)
  229. {
  230. $query = self::find();
  231. $query->select($sele);
  232. $query->andWhere(['del'=>$this->setDel]);
  233. $query->andWhere(['pid'=>$id]);
  234. if(!empty($this->state))
  235. {
  236. $query->andFilterWhere(['state'=>$this->state]);
  237. }
  238. else
  239. {
  240. $query->andFilterWhere(['state'=>1]);
  241. }
  242. $query->orderBy(['sort'=>SORT_DESC]);
  243. $query->limit=$limts;
  244. $rows = $query->asArray()->all();
  245. $data = $rows;
  246. if($rows != null)
  247. {
  248. $id = array_column($rows,'id');
  249. $q = self::find();
  250. $q->select($sele);
  251. $q->andWhere(['del'=>$this->setDel]);
  252. $q->andWhere(['pid'=>$id]);
  253. $r = $q->asArray()->all();
  254. if($r != null)
  255. {
  256. $data = array_merge($rows,$r);
  257. }
  258. }
  259. return $data;
  260. }
  261. //区域电话管理
  262. public function telList($input)
  263. {
  264. $query = $this->telWhere();
  265. $rows = $query->orderBy(['pfg_category_city.abroad'=>SORT_ASC,'pfg_category_city.id'=>SORT_ASC])->asArray()->all();
  266. if (!empty($input['id'])){
  267. $lowerlevel = $this->telWhere()->andWhere(['pfg_category_city.id'=>$input['id']])->asArray()->all();
  268. $rows = $lowerlevel;
  269. if($lowerlevel[0]['pid'] != 0){
  270. $upperlevel = $this->telWhere()->andWhere(['pfg_category_city.id'=>$lowerlevel[0]['pid']])->asArray()->all();
  271. $rows = array_merge($upperlevel,$lowerlevel);
  272. }
  273. }
  274. if (!empty($input['name'])){
  275. if ($input['name'] == 2){
  276. $name = $this->telWhere()->andWhere(['pfg_category_tel.name'=>null])->asArray()->all();
  277. $pid = array_column($name,'pid');
  278. if($pid != 0){
  279. $upperlevel = $this->telWhere()->andWhere(['IN','pfg_category_city.id',$pid])->asArray()->all();
  280. $arr = array_merge($upperlevel,$name);
  281. $rows = $this->assoc_unique($arr,'id');
  282. }
  283. } elseif ($input['name'] == 1){
  284. $name = $this->telWhere()->andWhere(['not',['pfg_category_tel.name'=>null]])->asArray()->all();
  285. $pid = array_column($name,'pid');
  286. if($pid != 0){
  287. $upperlevel = $this->telWhere()->andWhere(['IN','pfg_category_city.id',$pid])->asArray()->all();
  288. $arr = array_merge($upperlevel,$name);
  289. $rows = $this->assoc_unique($arr,'id');
  290. }
  291. }
  292. }
  293. return $rows;
  294. }
  295. //区域电话管理
  296. public function telTotal(){
  297. $query = $this->telWhere()->count();
  298. return $query;
  299. }
  300. //区域电话管理
  301. private function telWhere(){
  302. $query = self::find();
  303. $query->select(['pfg_category_city.city_name','pfg_category_city.id','pfg_category_tel.name','pfg_category_city.pid'])
  304. ->leftJoin('pfg_category_tel_city','pfg_category_city.id = pfg_category_tel_city.cid')
  305. ->leftJoin('pfg_category_tel','pfg_category_tel_city.tid = pfg_category_tel.id and pfg_category_tel.del = 1')
  306. ->andWhere(['pfg_category_city.del'=> 1]);
  307. return $query;
  308. }
  309. private function assoc_unique($arr, $key) {
  310. $tmp_arr = array();
  311. foreach ($arr as $k => $v) {
  312. if (in_array($v[$key], $tmp_arr)) {//搜索$v[$key]是否在$tmp_arr数组中存在,若存在返回true
  313. unset($arr[$k]);
  314. } else {
  315. $tmp_arr[] = $v[$key];
  316. }
  317. }
  318. sort($arr); //sort函数对数组进行排序
  319. return $arr;
  320. }
  321. //父类城市,按国内外排序
  322. public function GetUpCity()
  323. {
  324. $query = self::find();
  325. $query->select(['id','pid','city_name','pinyin']);
  326. $query->andWhere(['del'=>$this->setDel]);
  327. $query->andWhere(['state'=>1]);
  328. $query->andWhere(['pid'=>0]);
  329. $query->orderBy(['abroad'=>SORT_ASC,'sort'=>SORT_DESC]);
  330. return $query->asArray()->all();
  331. }
  332. public function getCityNameTurnId($cityname)
  333. {
  334. $query = self::find();
  335. $query->select(['id','pid','city_name']);
  336. $query->andFilterWhere(['city_name'=>$cityname['city']]);
  337. return $query->asArray()->one();
  338. }
  339. public function getParentCity()
  340. {
  341. return $this->hasOne(CategoryCity::className(), ['id' => 'pid'])->from(CategoryCity::tableName().' fp')->select('fp.city_name');
  342. }
  343. public function getTongSonList($city){
  344. $data = self::find()->where(['del'=>1,'state'=>1,'pid'=>$city])->asArray()->all();
  345. $list = [];
  346. if(!empty($data)){
  347. $list = array_column($data,'id');
  348. }
  349. $list[] = $city;
  350. return $list;
  351. }
  352. /*
  353. * 国内 国外
  354. */
  355. public function TypeAbroad($sele = [])
  356. {
  357. $query = static::find();
  358. $query->andWhere(['state' => 1]);
  359. $query->andWhere(['del' => 1]);
  360. $query->andWhere(['abroad' => $this->abroad]);
  361. $query->select($sele);
  362. return $query->orderBy(['sort' => SORT_DESC])->asArray()->all();
  363. }
  364. }