123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- namespace App\Repositories;
- use App\Models\CategoryDistrict;
- use Prettus\Repository\Eloquent\BaseRepository;
- use Prettus\Repository\Criteria\RequestCriteria;
- use Illuminate\Support\Facades\Cache;
- /**
- * Class MemberRepositoryEloquent.
- *
- * @package namespace App\Repositories;
- */
- class CategoryDistrictRepository extends BaseRepository
- {
- /**
- * Specify Model class name
- *
- * @return string
- */
- public function model()
- {
- return CategoryDistrict::class;
- }
- /**
- * Boot up the repository, pushing criteria
- */
- public function boot()
- {
- $this->pushCriteria(app(RequestCriteria::class));
- }
- /**
- * @param $id
- * @return mixed
- */
- public function getCategoryDistrict($id)
- {
- return $this->model->find($id);
- }
- public function getCategoryByParent($parent_id)
- {
- return $this->model->where('parent_id', $parent_id)->get();
- }
- public function getCategoryById($parent = 0)
- {
- return $this->model->where(['parent_id'=>$parent])->select(['id','name','parent_id','spell'])->get()->toArray();
- }
- public function getManydistrict($id)
- {
- return $this->model->whereIn('id', $id)->get();
- }
- //获取所有地区信息
- public function cityCateCache()
- {
- $res = $this->model->orderBy('parent_id', 'desc')->select(array('spell','id','parent_id','name'))->get()->toArray();
- $citySpell = array();
- foreach ($res as $key => $val) {
- $cityId[$val['id']] = $val;
- $citySpell[$val['spell']] = $val;
- }
- $city = array('spell'=>$citySpell,'id'=>$cityId);
- return $city;
- }
- //获取地区搜索信息
- public function citySearchCache()
- {
- $city = $city_list = array();
- $cityData = $this->model->select(array('id','parent_id'))->orderBy('parent_id', 'ASC')->get()->toArray();
- foreach ($cityData as $key => $val) {
- if (!$val['parent_id']) {
- $city_list[$val['id']] = $val['id'].'_0';
- $city[$val['id']] = array('tier'=>1,'spid'=>$val['id']);
- } else {
- //20200421增加顶级地区排序错误处理
- if(!array_key_exists($val['parent_id'],$city)){
- $city_list[$val['parent_id']] = $cityData[$val['parent_id']]['parent_id'].'_'.$val['parent_id'];
- $city[$val['parent_id']] = array('tier'=>2,'spid'=>$cityData[$val['parent_id']]['parent_id'].'_'.$val['parent_id']);
- }
- if ($city[$val['parent_id']]['tier'] == 1) {
- $city_list[$val['id']] = $city[$val['parent_id']]['spid'].'_'.$val['id'];
- $city[$val['id']] = array('tier'=>2,'spid'=>$city[$val['parent_id']]['spid'].'_'.$val['id']);
- } elseif ($city[$val['parent_id']]['tier'] == 2) {
- $city_list[$val['id']] = $city[$val['parent_id']]['spid'].'_'.$val['id'];
- }
- }
- }
- return $city_list;
- }
- //读取省市数据
- public function getDistrictCache($pid = 0)
- {
- if (null === $district = Cache::get('district')) {
- $district = array();
- $districtData = $this->model->select(['id','parent_id','name'])->orderBy('order', 'DESC')->get()->toArray();
- foreach ($districtData as $key => $val) {
- $district[$val['parent_id']][$val['id']] = $val['name'];
- }
- Cache::put('district', $district, '86400');
- }
- if ($pid === 'all') {
- return $district;
- }
- if (array_has($district, intval($pid))) {
- return $district[intval($pid)];
- } else {
- return array();
- }
- }
- public function customDistrictCache()
- {
- if (null === $district = Cache::get('district_custom_cate')) {
- $district = array();
- $districtData = $this->model->select(array('id','parent_id','name','spell'))->orderBy('order', 'desc')->get()->toArray();
- foreach ($districtData as $key => $val) {
- $district[$val['parent_id']][$val['id']] = $val;
- }
- Cache::put('district_custom_cate', $district, '86400');
- }
- return $district;
- }
- public function getAllDistricts()
- {
- if (null === $districts = Cache::get('all_districts')) {
- $districts = array();
- $districtData = $this->model->select(array('id','parent_id','name','spell'))->orderBy('order', 'desc')->get()->toArray();
- foreach ($districtData as $key => $val) {
- $districts[$val['id']] = $val;
- }
- Cache::put('all_districts', $districts, '86400');
- }
- return $districts;
- }
- }
|