area_check.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/3/10
  6. * Time: 下午2:33
  7. */
  8. namespace area;
  9. require_once(BASE_ROOT_PATH . '/helper/util_helper.php');
  10. class area_check
  11. {
  12. protected $mItems;
  13. protected $mAreaIds;
  14. protected $mNameToProvid;
  15. protected $mod_area;
  16. private $mCache;
  17. public function __construct($table = 'area')
  18. {
  19. $this->mod_area = Model($table);
  20. $this->mItems = [];
  21. $this->mAreaIds = [];
  22. $this->mNameToProvid = [];
  23. $this->mCache = $this->load_cache();
  24. $this->init();
  25. }
  26. protected function sub_areas($parent_id)
  27. {
  28. if (isset($this->mCache[$parent_id])) {
  29. return $this->mCache[$parent_id];
  30. }
  31. return [];
  32. }
  33. protected function load_cache()
  34. {
  35. $data = rkcache('area_parents');
  36. if (!empty($data) && is_array($data)) {
  37. return $data;
  38. }
  39. $items = [];
  40. $mod = Model('area');
  41. $parents = $mod->table('area')
  42. ->field('area_parent_id')
  43. ->group('area_parent_id')
  44. ->limit(false)
  45. ->select();
  46. foreach ($parents as $parent)
  47. {
  48. $parent_id = intval($parent['area_parent_id']);
  49. $item = $mod->table('area')
  50. ->where(array('area_parent_id' => $parent_id,'enable' => 1))
  51. ->field('area_id,area_parent_id,area_deep,area_name')
  52. ->order('area_parent_id ASC,area_id ASC,area_name ASC')
  53. ->limit(false)
  54. ->select();
  55. $items[$parent_id] = $item;
  56. }
  57. wkcache('area_parents',$items);
  58. return $items;
  59. }
  60. protected function init()
  61. {
  62. $items = $this->sub_areas(0);
  63. foreach ($items as $item)
  64. {
  65. $area_id = intval($item['area_id']);
  66. $area_name = trim($item['area_name']);
  67. if($area_id >= 32) continue; //海外地区不处理了。
  68. $this->mItems[$area_id] = $item;
  69. $this->mAreaIds[$area_id] = [];
  70. $this->mNameToProvid[$area_name] = $area_id;
  71. $this->load($area_id);
  72. }
  73. }
  74. protected function load($parent_id)
  75. {
  76. $items = $this->sub_areas($parent_id);
  77. foreach ($items as $item)
  78. {
  79. $area_id = intval($item['area_id']);
  80. $this->mItems[$area_id] = $item;
  81. $this->mAreaIds[$area_id] = [];
  82. $this->mAreaIds[$parent_id][] = $area_id;
  83. $this->load($area_id);
  84. }
  85. }
  86. protected function next($ids)
  87. {
  88. $result = [];
  89. foreach ($ids as $id) {
  90. $subs = $this->mAreaIds[$id];
  91. $result = array_merge($result,$subs);
  92. }
  93. return $result;
  94. }
  95. public function export()
  96. {
  97. $items = $this->sub_areas(0);
  98. $provins = [];
  99. foreach ($items as $item) {
  100. $area_id = intval($item['area_id']);
  101. if($area_id >= 32) continue; //海外地区不处理了。
  102. $provins[] = $area_id;
  103. }
  104. $areas = $provins;
  105. $citys = $this->next($provins);
  106. $areas = array_merge($areas,$citys);
  107. $contrys = $this->next($citys);
  108. $areas = array_merge($areas,$contrys);
  109. $result = [];
  110. foreach ($areas as $id)
  111. {
  112. $item = [];
  113. $item['aid'] = $this->mItems[$id]['area_id'];
  114. $item['pid'] = $this->mItems[$id]['area_parent_id'];
  115. $item['n'] = $this->mItems[$id]['area_name'];
  116. $item['d'] = $this->mItems[$id]['area_deep'];
  117. $result[] = $item;
  118. }
  119. return $result;
  120. }
  121. }