| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- <?php
- namespace services\common;
- use Yii;
- use common\models\common\Provinces;
- use common\components\Service;
- use common\helpers\ArrayHelper;
- /**
- * Class ProvincesService
- * @package services\common
- * @author jianyan74 <751393839@qq.com>
- */
- class ProvincesService extends Service
- {
- /**
- * 获取省市区禁用状态
- *
- * @param array $provinceIds
- * @param array $cityIds
- * @param array $areaIds
- * @return mixed
- */
- public function getAreaTree(array $provinceIds, array $cityIds, array $areaIds)
- {
- $address = $this->findAllInCache();
- $allIds = [];
- foreach ($address as &$item) {
- $allIds[$item['pid']][] = $item['id'];
- }
- // 计算选中状态
- foreach ($address as &$item) {
- $item['is_disabled'] = true;
- $data = $allIds[$item['id']] ?? [];
- if ($item['level'] == 1) {
- foreach ($data as $datum) {
- !in_array($datum, $cityIds) && $item['is_disabled'] = false;
- }
- }
- if ($item['level'] == 2) {
- foreach ($data as $datum) {
- !in_array($datum, $areaIds) && $item['is_disabled'] = false;
- }
- }
- if ($item['level'] == 3 && !in_array($item['id'], $areaIds)) {
- $item['is_disabled'] = false;
- }
- unset($data);
- }
- // 递归重组省市区
- $address = ArrayHelper::itemsMerge($address, 0);
- // 大区
- $regionalAll = $this->regionalAll();
- $regroupAddress = [];
- foreach ($address as $value) {
- foreach ($regionalAll as $key => $data) {
- foreach ($data as $datum) {
- $datum == $value['title'] && $regroupAddress[$key][] = $value;
- }
- }
- }
- unset($address, $regionalAll, $allIds);
- return $regroupAddress;
- }
- /**
- * 根据区ID 获取省市区ID
- *
- * @param $area_id
- * @return array
- */
- public function getParentIdsByAreaId($area_id)
- {
- $area = $this->findById((int) $area_id);
- list($zero, $province_id, $city_id) = explode('-', $area['tree']);
- return [trim($province_id), trim($city_id), $area_id];
- }
- /**
- * @param int $pid
- * @return int|string
- */
- public function getCountByPid($pid = 0)
- {
- return Provinces::find()
- ->select(['id'])
- ->where(['pid' => $pid])
- ->count();
- }
- /**
- * @param $ids
- * @return array|\yii\db\ActiveRecord[]
- *
- */
- public function findByIds($ids)
- {
- return Provinces::find()
- ->select(['id', 'title', 'pid', 'level'])
- ->orderBy('id asc')
- ->where(['in', 'id', $ids])
- ->asArray()
- ->all();
- }
- /**
- * @param $id
- * @return array|\yii\db\ActiveRecord|null
- */
- public function findById($id)
- {
- return Provinces::find()
- ->where(['id' => $id])
- ->asArray()
- ->one();
- }
- /**
- * @param int $pid
- * @param string $level
- * @return array
- */
- public function getCityMapByPid($pid = 0, $level = '')
- {
- return ArrayHelper::map($this->getCityByPid($pid, $level), 'id', 'title');
- }
- /**
- * 根据父级ID返回信息
- *
- * @param int $pid
- * @return array
- */
- public function getCityByPid($pid = 0, $level = '')
- {
- if ($pid === '') {
- return [];
- }
- return Provinces::find()
- ->where(['pid' => $pid])
- ->orderBy('id asc')
- ->select(['id', 'title', 'pid'])
- ->andFilterWhere(['level' => $level])
- ->cache(600)
- ->asArray()
- ->all();
- }
- /**
- * 根据id获取区域名称
- *
- * @param $id
- * @return mixed
- */
- public function getName($id)
- {
- if ($provinces = Provinces::findOne($id)) {
- return $provinces['title'] ?? '';
- }
- return false;
- }
- /**
- * 根据id数组获取区域名称
- *
- * @param $id
- * @return mixed
- */
- public function getCityListName(array $ids)
- {
- if ($provinces = Provinces::find()->where(['in', 'id', $ids])->orderBy('id asc')->all()) {
- $address = '';
- foreach ($provinces as $province) {
- $address .= $province['title'] . ' ';
- }
- return $address;
- }
- return false;
- }
- /**
- * @return array
- */
- public function getJsonData()
- {
- $data = $this->findAllInCache();
- $items = ArrayHelper::itemsMerge($data, 0, 'id', 'pid', 'child');
- return $items;
- }
- /**
- * @return array|mixed|\yii\db\ActiveRecord[]
- */
- public function findAllInCache()
- {
- $cacheKey = 'rf:provinces';
- // 获取缓存
- if (!($data = Yii::$app->cache->get($cacheKey))) {
- $data = Provinces::find()
- ->select(['id', 'title', 'pid', 'level'])
- ->where(['<=', 'level', 3])
- ->orderBy('id asc')
- ->asArray()
- ->all();
- Yii::$app->cache->set($cacheKey, $data, 60 * 60 * 24 * 24);
- }
- return $data;
- }
- /**
- * 获取大区
- *
- * @return array
- */
- public function regionalAll()
- {
- $region = [
- '华东' => [
- '江苏省',
- '上海市',
- '浙江省',
- '安徽省',
- '江西省',
- ],
- '华北' => [
- '天津市',
- '河北省',
- '山西省',
- '内蒙古自治区',
- '山东省',
- '北京市',
- ],
- '华南' => [
- '广东省',
- '广西壮族自治区',
- '海南省',
- '福建省',
- ],
- '华中' => [
- '湖南省',
- '河南省',
- '湖北省',
- ],
- '东北' => [
- '辽宁省',
- '吉林省',
- '黑龙江省',
- ],
- '西北' => [
- '陕西省',
- '甘肃省',
- '青海省',
- '宁夏回族自治区',
- '新疆维吾尔自治区',
- ],
- '西南' => [
- '重庆市',
- '四川省',
- '贵州省',
- '云南省',
- '西藏自治区',
- ],
- '港澳台' => [
- '香港特别行政区',
- '澳门特别行政区',
- '台湾省',
- ],
- ];
- return $region;
- }
- }
|