64], [['initial'], 'string', 'max' => 10], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'region_id' => 'Region ID', 'name' => 'Name', 'region_code' => 'Region Code', 'parent_code' => 'Parent Code', 'parent_name' => 'Parent Name', 'region_full_name' => 'Region Full Name', 'region_full_codes' => 'Region Full Codes', 'longitude' => 'Longitude', 'latitude' => 'Latitude', 'initial' => 'Initial', 'region_level' => 'Region Level', ]; } /** * 获取供应链城市地址编码 * * @param string $city * @return Region */ public static function getCityID($city) { // 特殊处理 $city = static::getReplaceName($city); return self::find()->where(['name' => $city])->one(); } /** * 获取地区地址编码 * * @param integer $code 城市编码 * @param string $district 地区地址 * @return static|null */ public static function getDistrictByDistrict(int $code, string $district) { // 特殊处理 $district = static::getReplaceName($district); return self::find()->where(['parent_code' => $code, 'name' => $district])->one(); } /** * 获取地区地址code * * @param array $region * @return string|boolean */ public static function getAreaCode($region) { if (isset($region[2]) && is_string($region[2])) { $region[2] = static::getReplaceName($region[2]); } $area = $region[2] . ',' . $region[1] . ',' . $region[0]; $region_full_codes = self::find()->where(['region_full_name' => $area])->select('region_full_codes')->scalar(); if (empty($region_full_codes)) { $area = $region[2] . '%' . $region[0]; $region_full_codes = self::find()->andWhere(new Expression("region_full_name like '{$area}'"))->select('region_full_codes')->scalar(); } if (empty($region_full_codes)) { return false; } return $region_full_codes; } /** * 通过省市区获取地区编码 * * @param string $province * @param string $city * @param string $district * @return static|null */ public static function getDistrictByRegion(string $province, string $city, string $district, $town = null) { $city = static::getReplaceName($city); $district = static::getReplaceName($district); $area = $district . ',' . $city . ',' . $province; $district_info = static::find()->where(['region_full_name' => $area])->one(); if (empty($district_info)) { $area = $district . '%' . $province; $town && $area = $town . '%' . $area; $district_info = static::find()->andWhere(new Expression("region_full_name like '{$area}'"))->one(); } return $district_info; } /** * @param string $name * @return string */ protected static function getReplaceName(string $name) { $list = [ [ '即墨市', '即墨区', ], [ '荔浦县', '荔浦市', ], [ '监利县', '监利市' ], [ '蓬莱市', '蓬莱区', ], ]; foreach ($list as $item) { if (stripos($name, $item[0]) !== false) { return $item[1]; } } return $name; } /** * @param array $address * @return array */ public static function getAddress(array $address): array { if (empty($address)) return $address; $provinceList = ['新疆维吾尔自治区']; $cityList = [ '可克达拉市', '北屯市', '阿拉尔市', '昆玉市', '石河子市', '双河市', '铁门关市', '图木舒克市', '五家渠市', ]; if (in_array($address[0], $provinceList) && in_array($address[2], $cityList)) { return array_values(array_unique($address)); } return $address; } }