Area.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Modes;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Cache;
  5. /**
  6. * App\Modes\Area
  7. *
  8. * @property int $id
  9. * @property string $name 区域名称
  10. * @property string $short_name 区域简称
  11. * @property int $code 区域代码
  12. * @property int $parent_code 上级城市代码
  13. * @property int $level 区域层级:1-省,2-市,3-区/县,4-乡镇/街道
  14. * @property int $status 状态:1-正常,2-失效
  15. * @property \Illuminate\Support\Carbon $created_at
  16. * @property \Illuminate\Support\Carbon $updated_at
  17. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Modes\UserAddress[] $UserAddress
  18. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Area newModelQuery()
  19. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Area newQuery()
  20. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Area query()
  21. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Area whereCode($value)
  22. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Area whereCreatedAt($value)
  23. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Area whereId($value)
  24. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Area whereLevel($value)
  25. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Area whereName($value)
  26. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Area whereParentCode($value)
  27. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Area whereShortName($value)
  28. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Area whereStatus($value)
  29. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Area whereUpdatedAt($value)
  30. * @mixin \Eloquent
  31. * @property int $pid 上级id
  32. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Area wherePid($value)
  33. * @property int $area_id 区域ID
  34. * @property int $sort 排序
  35. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Area whereAreaId($value)
  36. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Area whereSort($value)
  37. */
  38. class Area extends Model
  39. {
  40. protected $table = 'area';
  41. /**
  42. * 获取区域名称
  43. * @author fatty
  44. * @date 2018/12/21
  45. * @param $id
  46. * @description
  47. */
  48. public static function getName($id)
  49. {
  50. if (empty($id)) {
  51. return '';
  52. }
  53. if (!Cache::has('JC_AreaNameById_' . $id)) {
  54. $name = Area::whereId($id)->select('name')->first();
  55. $name = isset($name) ? $name->name : '';
  56. Cache::forever('JC_AreaNameById_' . $id, $name);
  57. }
  58. return Cache::get('JC_AreaNameById_' . $id);
  59. }
  60. /**
  61. *
  62. * @author fatty
  63. * @date 2019/1/7
  64. * @description
  65. */
  66. public static function getIterationArea($code = 0, $temp = [])
  67. {
  68. if (!Cache::has('JC_Area')) {
  69. $area = Area::whereParentCode($code)->select(['name', 'code'])->get();
  70. if ($area->isNotEmpty()) {
  71. foreach ($area as $item) {
  72. $temp[] = [
  73. 'name' => $item->name,
  74. 'value' => $item->code
  75. ];
  76. if ($code != 0) {
  77. $temp = array_merge($temp, ['parent' => $code]);
  78. }
  79. // $temp = self::getIterationArea($item['code'], $temp);
  80. }
  81. }
  82. }
  83. return $temp;
  84. }
  85. /**
  86. * 检查省市区的从属关系
  87. * @author lyh
  88. * @date 2019/3/21
  89. * @description
  90. */
  91. public static function isRealation($province, $city, $district)
  92. {
  93. if (Area::wherePid($city)->whereId($district)->exists() == false) {
  94. return false;
  95. }
  96. if (Area::wherePid($province)->whereId($city)->exists() == false) {
  97. return false;
  98. }
  99. return true;
  100. }
  101. /**
  102. * 获取地区信息
  103. * @author lyh
  104. * @date 2019/4/15
  105. * @param int $province 省id
  106. * @param int $city 地级市ID
  107. * @param int $district 县区ID
  108. * @return string
  109. * @description
  110. */
  111. public static function getArea($province, $city, $district)
  112. {
  113. return trim(self::getName($province) . ' ' . self::getName($city) . ' ' . self::getName($district));
  114. }
  115. public function UserAddress()
  116. {
  117. return $this->hasMany('App\Modes\UserAddress', 'id');
  118. }
  119. }