CommunityService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace addons\Community\common\service;
  3. use addons\Community\common\enums\CommunityEnum;
  4. use addons\Community\common\models\CommunityHead;
  5. use common\enums\StatusEnum;
  6. use common\models\common\Region;
  7. use services\common\BaseService;
  8. use Yii;
  9. /**
  10. * 社区服务类
  11. * CommunityService class
  12. */
  13. class CommunityService extends BaseService
  14. {
  15. /**
  16. * 获取社区团购区域列表
  17. *
  18. * @param integer $level
  19. * @return array
  20. */
  21. public function getRegionList(int $level)
  22. {
  23. $list = Yii::$app->cache->get(CommunityEnum::REGION_TREE . $level);
  24. if (empty($list)) {
  25. $where = [
  26. ['<=', 'level', $level],
  27. ['>', 'level', 0],
  28. ];
  29. $regions = Region::lists(['where' => $where, 'select' => 'id,name,parent_id,level', 'limit' => 10000]);
  30. $list = Region::getTreeRegion($regions);
  31. Yii::$app->cache->set(CommunityEnum::REGION_TREE . $level, $list);
  32. }
  33. $setting_service = new SettingService();
  34. $area = $setting_service->getFieldSetting($this->mall_id, 'area', true);
  35. $area = $area ? json_decode($area, true) : $area;
  36. $list = $this->filterRegions($area, $list);
  37. return $this->handleCommunityData($this->mall_id, $list);
  38. }
  39. /**
  40. * @Description: 将自提点数据放入地区数据
  41. * @Author: zsan
  42. * @DateTime 2023-02-09 14:50:16
  43. * @param integer $mall_id
  44. * @param array $regions
  45. * @return mixed
  46. */
  47. protected function handleCommunityData($mall_id, $regions)
  48. {
  49. $params = [
  50. 'where' => [
  51. ['mall_id' => $mall_id],
  52. ['check_status' => CommunityEnum::CHECK_ADOPT],
  53. ['status' => [StatusEnum::ENABLED]]
  54. ],
  55. 'select' => 'id, name, province_id, city_id, district_id',
  56. 'index' => 'id'
  57. ];
  58. $community_head_list = CommunityHead::lists($params, true);
  59. $district_arr = [];
  60. foreach ($community_head_list as $v) {
  61. if (!isset($district_arr[$v['district_id']])) $district_arr[$v['district_id']] = [];
  62. $district_arr[$v['district_id']][] = [
  63. 'id' => $v['id'],
  64. 'name' => $v['name'],
  65. 'level' => 4
  66. ];
  67. }
  68. foreach ($regions as &$region) {
  69. if (!isset($region['children'])) continue;
  70. if (count($district_arr) <= 0) break;
  71. foreach ($region['children'] as &$city) {
  72. if (!isset($city['children'])) continue;
  73. if (count($district_arr) <= 0) break 2;
  74. foreach ($city['children'] as &$district) {
  75. if (!array_key_exists($district['id'], $district_arr)) continue;
  76. if (count($district_arr) <= 0) break 3;
  77. $district['children'] = $district_arr[$district['id']];
  78. unset($district_arr[$district['id']]);
  79. }
  80. unset($district);
  81. }
  82. unset($city);
  83. }
  84. unset($region);
  85. return $regions;
  86. }
  87. protected function filterRegions($area, $regions)
  88. {
  89. foreach ($regions as $k => $v) {
  90. // 过滤省份 - 再没有全选下级的情况下那么省份ID不会被存储
  91. // if (!in_array($v['id'], $area)) {
  92. // unset($regions[$k]);
  93. // continue;
  94. // }
  95. $is_unset_province = true;
  96. if (!isset($v['children'])) continue;
  97. foreach ($v['children'] as $m => $n) {
  98. // 过滤城市 - 在没有全选的情况下 是不会保存城市的
  99. // if (!in_array($n['id'], $area)) {
  100. // unset($v['children'][$m]);
  101. // continue;
  102. // }
  103. $is_unset_city = true;
  104. if (!isset($n['children'])) continue;
  105. foreach ($n['children'] as $a => $b) {
  106. // 过滤区
  107. if (!in_array($b['id'], $area)) {
  108. unset($regions[$k]['children'][$m]['children'][$a]);
  109. } else {
  110. $is_unset_city = false;
  111. $is_unset_province = false;
  112. }
  113. }
  114. if ($is_unset_city) {
  115. unset($regions[$k]['children'][$m]);
  116. } else {
  117. $regions[$k]['children'][$m]['children'] = array_values($regions[$k]['children'][$m]['children']);
  118. }
  119. }
  120. if ($is_unset_province) {
  121. unset($regions[$k]);
  122. } else {
  123. $regions[$k]['children'] = array_values($regions[$k]['children']);
  124. }
  125. }
  126. return array_values($regions);
  127. }
  128. }