Region.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace addons\StarChain\common\models;
  3. use Yii;
  4. use yii\db\Expression;
  5. /**
  6. * This is the model class for table "{{%addons_star_chain_region}}".
  7. *
  8. * @property int $id
  9. * @property string|null $region_id
  10. * @property string|null $name
  11. * @property string|null $region_code
  12. * @property string|null $parent_code
  13. * @property string|null $parent_name
  14. * @property string|null $region_full_name
  15. * @property string|null $region_full_codes
  16. * @property string|null $longitude
  17. * @property string|null $latitude
  18. * @property string|null $initial
  19. * @property int|null $region_level
  20. */
  21. class Region extends BaseModel
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_star_chain_region}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['region_level'], 'integer'],
  37. [['region_id', 'name', 'region_code', 'parent_code', 'parent_name', 'region_full_name', 'region_full_codes', 'longitude', 'latitude'], 'string', 'max' => 64],
  38. [['initial'], 'string', 'max' => 10],
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => 'ID',
  48. 'region_id' => 'Region ID',
  49. 'name' => 'Name',
  50. 'region_code' => 'Region Code',
  51. 'parent_code' => 'Parent Code',
  52. 'parent_name' => 'Parent Name',
  53. 'region_full_name' => 'Region Full Name',
  54. 'region_full_codes' => 'Region Full Codes',
  55. 'longitude' => 'Longitude',
  56. 'latitude' => 'Latitude',
  57. 'initial' => 'Initial',
  58. 'region_level' => 'Region Level',
  59. ];
  60. }
  61. /**
  62. * 获取供应链城市地址编码
  63. *
  64. * @param string $city
  65. * @return Region
  66. */
  67. public static function getCityID($city)
  68. {
  69. // 特殊处理
  70. $city = static::getReplaceName($city);
  71. return self::find()->where(['name' => $city])->one();
  72. }
  73. /**
  74. * 获取地区地址编码
  75. *
  76. * @param integer $code 城市编码
  77. * @param string $district 地区地址
  78. * @return static|null
  79. */
  80. public static function getDistrictByDistrict(int $code, string $district)
  81. {
  82. // 特殊处理
  83. $district = static::getReplaceName($district);
  84. return self::find()->where(['parent_code' => $code, 'name' => $district])->one();
  85. }
  86. /**
  87. * 获取地区地址code
  88. *
  89. * @param array $region
  90. * @return string|boolean
  91. */
  92. public static function getAreaCode($region)
  93. {
  94. if (isset($region[2]) && is_string($region[2])) {
  95. $region[2] = static::getReplaceName($region[2]);
  96. }
  97. $area = $region[2] . ',' . $region[1] . ',' . $region[0];
  98. $region_full_codes = self::find()->where(['region_full_name' => $area])->select('region_full_codes')->scalar();
  99. if (empty($region_full_codes)) {
  100. $area = $region[2] . '%' . $region[0];
  101. $region_full_codes = self::find()->andWhere(new Expression("region_full_name like '{$area}'"))->select('region_full_codes')->scalar();
  102. }
  103. if (empty($region_full_codes)) {
  104. return false;
  105. }
  106. return $region_full_codes;
  107. }
  108. /**
  109. * 通过省市区获取地区编码
  110. *
  111. * @param string $province
  112. * @param string $city
  113. * @param string $district
  114. * @return static|null
  115. */
  116. public static function getDistrictByRegion(string $province, string $city, string $district, $town = null)
  117. {
  118. $city = static::getReplaceName($city);
  119. $district = static::getReplaceName($district);
  120. $area = $district . ',' . $city . ',' . $province;
  121. $district_info = static::find()->where(['region_full_name' => $area])->one();
  122. if (empty($district_info)) {
  123. $area = $district . '%' . $province;
  124. $town && $area = $town . '%' . $area;
  125. $district_info = static::find()->andWhere(new Expression("region_full_name like '{$area}'"))->one();
  126. }
  127. return $district_info;
  128. }
  129. /**
  130. * @param string $name
  131. * @return string
  132. */
  133. protected static function getReplaceName(string $name)
  134. {
  135. $list = [
  136. [
  137. '即墨市',
  138. '即墨区',
  139. ],
  140. [
  141. '荔浦县',
  142. '荔浦市',
  143. ],
  144. [
  145. '监利县',
  146. '监利市'
  147. ],
  148. [
  149. '蓬莱市',
  150. '蓬莱区',
  151. ],
  152. ];
  153. foreach ($list as $item) {
  154. if (stripos($name, $item[0]) !== false) {
  155. return $item[1];
  156. }
  157. }
  158. return $name;
  159. }
  160. /**
  161. * @param array $address
  162. * @return array
  163. */
  164. public static function getAddress(array $address): array
  165. {
  166. if (empty($address)) return $address;
  167. $provinceList = ['新疆维吾尔自治区'];
  168. $cityList = [
  169. '可克达拉市',
  170. '北屯市',
  171. '阿拉尔市',
  172. '昆玉市',
  173. '石河子市',
  174. '双河市',
  175. '铁门关市',
  176. '图木舒克市',
  177. '五家渠市',
  178. ];
  179. if (in_array($address[0], $provinceList) && in_array($address[2], $cityList)) {
  180. return array_values(array_unique($address));
  181. }
  182. return $address;
  183. }
  184. }