| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- namespace addons\StarChain\common\models;
- use Yii;
- use yii\db\Expression;
- /**
- * This is the model class for table "{{%addons_star_chain_region}}".
- *
- * @property int $id
- * @property string|null $region_id
- * @property string|null $name
- * @property string|null $region_code
- * @property string|null $parent_code
- * @property string|null $parent_name
- * @property string|null $region_full_name
- * @property string|null $region_full_codes
- * @property string|null $longitude
- * @property string|null $latitude
- * @property string|null $initial
- * @property int|null $region_level
- */
- class Region extends BaseModel
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_star_chain_region}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['region_level'], 'integer'],
- [['region_id', 'name', 'region_code', 'parent_code', 'parent_name', 'region_full_name', 'region_full_codes', 'longitude', 'latitude'], 'string', 'max' => 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;
- }
- }
|