ProvincesService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. namespace services\common;
  3. use Yii;
  4. use common\models\common\Provinces;
  5. use common\components\Service;
  6. use common\helpers\ArrayHelper;
  7. /**
  8. * Class ProvincesService
  9. * @package services\common
  10. * @author jianyan74 <751393839@qq.com>
  11. */
  12. class ProvincesService extends Service
  13. {
  14. /**
  15. * 获取省市区禁用状态
  16. *
  17. * @param array $provinceIds
  18. * @param array $cityIds
  19. * @param array $areaIds
  20. * @return mixed
  21. */
  22. public function getAreaTree(array $provinceIds, array $cityIds, array $areaIds)
  23. {
  24. $address = $this->findAllInCache();
  25. $allIds = [];
  26. foreach ($address as &$item) {
  27. $allIds[$item['pid']][] = $item['id'];
  28. }
  29. // 计算选中状态
  30. foreach ($address as &$item) {
  31. $item['is_disabled'] = true;
  32. $data = $allIds[$item['id']] ?? [];
  33. if ($item['level'] == 1) {
  34. foreach ($data as $datum) {
  35. !in_array($datum, $cityIds) && $item['is_disabled'] = false;
  36. }
  37. }
  38. if ($item['level'] == 2) {
  39. foreach ($data as $datum) {
  40. !in_array($datum, $areaIds) && $item['is_disabled'] = false;
  41. }
  42. }
  43. if ($item['level'] == 3 && !in_array($item['id'], $areaIds)) {
  44. $item['is_disabled'] = false;
  45. }
  46. unset($data);
  47. }
  48. // 递归重组省市区
  49. $address = ArrayHelper::itemsMerge($address, 0);
  50. // 大区
  51. $regionalAll = $this->regionalAll();
  52. $regroupAddress = [];
  53. foreach ($address as $value) {
  54. foreach ($regionalAll as $key => $data) {
  55. foreach ($data as $datum) {
  56. $datum == $value['title'] && $regroupAddress[$key][] = $value;
  57. }
  58. }
  59. }
  60. unset($address, $regionalAll, $allIds);
  61. return $regroupAddress;
  62. }
  63. /**
  64. * 根据区ID 获取省市区ID
  65. *
  66. * @param $area_id
  67. * @return array
  68. */
  69. public function getParentIdsByAreaId($area_id)
  70. {
  71. $area = $this->findById((int) $area_id);
  72. list($zero, $province_id, $city_id) = explode('-', $area['tree']);
  73. return [trim($province_id), trim($city_id), $area_id];
  74. }
  75. /**
  76. * @param int $pid
  77. * @return int|string
  78. */
  79. public function getCountByPid($pid = 0)
  80. {
  81. return Provinces::find()
  82. ->select(['id'])
  83. ->where(['pid' => $pid])
  84. ->count();
  85. }
  86. /**
  87. * @param $ids
  88. * @return array|\yii\db\ActiveRecord[]
  89. *
  90. */
  91. public function findByIds($ids)
  92. {
  93. return Provinces::find()
  94. ->select(['id', 'title', 'pid', 'level'])
  95. ->orderBy('id asc')
  96. ->where(['in', 'id', $ids])
  97. ->asArray()
  98. ->all();
  99. }
  100. /**
  101. * @param $id
  102. * @return array|\yii\db\ActiveRecord|null
  103. */
  104. public function findById($id)
  105. {
  106. return Provinces::find()
  107. ->where(['id' => $id])
  108. ->asArray()
  109. ->one();
  110. }
  111. /**
  112. * @param int $pid
  113. * @param string $level
  114. * @return array
  115. */
  116. public function getCityMapByPid($pid = 0, $level = '')
  117. {
  118. return ArrayHelper::map($this->getCityByPid($pid, $level), 'id', 'title');
  119. }
  120. /**
  121. * 根据父级ID返回信息
  122. *
  123. * @param int $pid
  124. * @return array
  125. */
  126. public function getCityByPid($pid = 0, $level = '')
  127. {
  128. if ($pid === '') {
  129. return [];
  130. }
  131. return Provinces::find()
  132. ->where(['pid' => $pid])
  133. ->orderBy('id asc')
  134. ->select(['id', 'title', 'pid'])
  135. ->andFilterWhere(['level' => $level])
  136. ->cache(600)
  137. ->asArray()
  138. ->all();
  139. }
  140. /**
  141. * 根据id获取区域名称
  142. *
  143. * @param $id
  144. * @return mixed
  145. */
  146. public function getName($id)
  147. {
  148. if ($provinces = Provinces::findOne($id)) {
  149. return $provinces['title'] ?? '';
  150. }
  151. return false;
  152. }
  153. /**
  154. * 根据id数组获取区域名称
  155. *
  156. * @param $id
  157. * @return mixed
  158. */
  159. public function getCityListName(array $ids)
  160. {
  161. if ($provinces = Provinces::find()->where(['in', 'id', $ids])->orderBy('id asc')->all()) {
  162. $address = '';
  163. foreach ($provinces as $province) {
  164. $address .= $province['title'] . ' ';
  165. }
  166. return $address;
  167. }
  168. return false;
  169. }
  170. /**
  171. * @return array
  172. */
  173. public function getJsonData()
  174. {
  175. $data = $this->findAllInCache();
  176. $items = ArrayHelper::itemsMerge($data, 0, 'id', 'pid', 'child');
  177. return $items;
  178. }
  179. /**
  180. * @return array|mixed|\yii\db\ActiveRecord[]
  181. */
  182. public function findAllInCache()
  183. {
  184. $cacheKey = 'rf:provinces';
  185. // 获取缓存
  186. if (!($data = Yii::$app->cache->get($cacheKey))) {
  187. $data = Provinces::find()
  188. ->select(['id', 'title', 'pid', 'level'])
  189. ->where(['<=', 'level', 3])
  190. ->orderBy('id asc')
  191. ->asArray()
  192. ->all();
  193. Yii::$app->cache->set($cacheKey, $data, 60 * 60 * 24 * 24);
  194. }
  195. return $data;
  196. }
  197. /**
  198. * 获取大区
  199. *
  200. * @return array
  201. */
  202. public function regionalAll()
  203. {
  204. $region = [
  205. '华东' => [
  206. '江苏省',
  207. '上海市',
  208. '浙江省',
  209. '安徽省',
  210. '江西省',
  211. ],
  212. '华北' => [
  213. '天津市',
  214. '河北省',
  215. '山西省',
  216. '内蒙古自治区',
  217. '山东省',
  218. '北京市',
  219. ],
  220. '华南' => [
  221. '广东省',
  222. '广西壮族自治区',
  223. '海南省',
  224. '福建省',
  225. ],
  226. '华中' => [
  227. '湖南省',
  228. '河南省',
  229. '湖北省',
  230. ],
  231. '东北' => [
  232. '辽宁省',
  233. '吉林省',
  234. '黑龙江省',
  235. ],
  236. '西北' => [
  237. '陕西省',
  238. '甘肃省',
  239. '青海省',
  240. '宁夏回族自治区',
  241. '新疆维吾尔自治区',
  242. ],
  243. '西南' => [
  244. '重庆市',
  245. '四川省',
  246. '贵州省',
  247. '云南省',
  248. '西藏自治区',
  249. ],
  250. '港澳台' => [
  251. '香港特别行政区',
  252. '澳门特别行政区',
  253. '台湾省',
  254. ],
  255. ];
  256. return $region;
  257. }
  258. }
粤ICP备19079148号