CategoryDistrictRepository.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\CategoryDistrict;
  4. use Prettus\Repository\Eloquent\BaseRepository;
  5. use Prettus\Repository\Criteria\RequestCriteria;
  6. use Illuminate\Support\Facades\Cache;
  7. /**
  8. * Class MemberRepositoryEloquent.
  9. *
  10. * @package namespace App\Repositories;
  11. */
  12. class CategoryDistrictRepository extends BaseRepository
  13. {
  14. /**
  15. * Specify Model class name
  16. *
  17. * @return string
  18. */
  19. public function model()
  20. {
  21. return CategoryDistrict::class;
  22. }
  23. /**
  24. * Boot up the repository, pushing criteria
  25. */
  26. public function boot()
  27. {
  28. $this->pushCriteria(app(RequestCriteria::class));
  29. }
  30. /**
  31. * @param $id
  32. * @return mixed
  33. */
  34. public function getCategoryDistrict($id)
  35. {
  36. return $this->model->find($id);
  37. }
  38. public function getCategoryByParent($parent_id)
  39. {
  40. return $this->model->where('parent_id', $parent_id)->get();
  41. }
  42. public function getCategoryById($parent = 0)
  43. {
  44. return $this->model->where(['parent_id'=>$parent])->select(['id','name','parent_id','spell'])->get()->toArray();
  45. }
  46. public function getManydistrict($id)
  47. {
  48. return $this->model->whereIn('id', $id)->get();
  49. }
  50. //获取所有地区信息
  51. public function cityCateCache()
  52. {
  53. $res = $this->model->orderBy('parent_id', 'desc')->select(array('spell','id','parent_id','name'))->get()->toArray();
  54. $citySpell = array();
  55. foreach ($res as $key => $val) {
  56. $cityId[$val['id']] = $val;
  57. $citySpell[$val['spell']] = $val;
  58. }
  59. $city = array('spell'=>$citySpell,'id'=>$cityId);
  60. return $city;
  61. }
  62. //获取地区搜索信息
  63. public function citySearchCache()
  64. {
  65. $city = $city_list = array();
  66. $cityData = $this->model->select(array('id','parent_id'))->orderBy('parent_id', 'ASC')->get()->toArray();
  67. foreach ($cityData as $key => $val) {
  68. if (!$val['parent_id']) {
  69. $city_list[$val['id']] = $val['id'].'_0';
  70. $city[$val['id']] = array('tier'=>1,'spid'=>$val['id']);
  71. } else {
  72. //20200421增加顶级地区排序错误处理
  73. if(!array_key_exists($val['parent_id'],$city)){
  74. $city_list[$val['parent_id']] = $cityData[$val['parent_id']]['parent_id'].'_'.$val['parent_id'];
  75. $city[$val['parent_id']] = array('tier'=>2,'spid'=>$cityData[$val['parent_id']]['parent_id'].'_'.$val['parent_id']);
  76. }
  77. if ($city[$val['parent_id']]['tier'] == 1) {
  78. $city_list[$val['id']] = $city[$val['parent_id']]['spid'].'_'.$val['id'];
  79. $city[$val['id']] = array('tier'=>2,'spid'=>$city[$val['parent_id']]['spid'].'_'.$val['id']);
  80. } elseif ($city[$val['parent_id']]['tier'] == 2) {
  81. $city_list[$val['id']] = $city[$val['parent_id']]['spid'].'_'.$val['id'];
  82. }
  83. }
  84. }
  85. return $city_list;
  86. }
  87. //读取省市数据
  88. public function getDistrictCache($pid = 0)
  89. {
  90. if (null === $district = Cache::get('district')) {
  91. $district = array();
  92. $districtData = $this->model->select(['id','parent_id','name'])->orderBy('order', 'DESC')->get()->toArray();
  93. foreach ($districtData as $key => $val) {
  94. $district[$val['parent_id']][$val['id']] = $val['name'];
  95. }
  96. Cache::put('district', $district, '86400');
  97. }
  98. if ($pid === 'all') {
  99. return $district;
  100. }
  101. if (array_has($district, intval($pid))) {
  102. return $district[intval($pid)];
  103. } else {
  104. return array();
  105. }
  106. }
  107. public function customDistrictCache()
  108. {
  109. if (null === $district = Cache::get('district_custom_cate')) {
  110. $district = array();
  111. $districtData = $this->model->select(array('id','parent_id','name','spell'))->orderBy('order', 'desc')->get()->toArray();
  112. foreach ($districtData as $key => $val) {
  113. $district[$val['parent_id']][$val['id']] = $val;
  114. }
  115. Cache::put('district_custom_cate', $district, '86400');
  116. }
  117. return $district;
  118. }
  119. public function getAllDistricts()
  120. {
  121. if (null === $districts = Cache::get('all_districts')) {
  122. $districts = array();
  123. $districtData = $this->model->select(array('id','parent_id','name','spell'))->orderBy('order', 'desc')->get()->toArray();
  124. foreach ($districtData as $key => $val) {
  125. $districts[$val['id']] = $val;
  126. }
  127. Cache::put('all_districts', $districts, '86400');
  128. }
  129. return $districts;
  130. }
  131. }