CategoryJobsRepository.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\CategoryJobs;
  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 CategoryJobsRepository extends BaseRepository
  13. {
  14. /**
  15. * Specify Model class name
  16. *
  17. * @return string
  18. */
  19. public function model()
  20. {
  21. return CategoryJobs::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 getCategoryJobs($id)
  35. {
  36. return $this->model->find($id);
  37. }
  38. public function getCategoryById($parent = 0)
  39. {
  40. return $this->model->where(['parent_id'=>$parent])->select(['id','name','parent_id'])->get()->toArray();
  41. }
  42. public function getManyJobs($id)
  43. {
  44. return $this->model->whereIn('id', $id)->get();
  45. }
  46. public function jobsCategoryCache()
  47. {
  48. $lists = array();
  49. $jobsData = $this->model->select(array('id','parent_id','name','spell'))->orderBy('order', 'desc')->get()->toArray();
  50. foreach ($jobsData as $key => $val) {
  51. $lists[$val['parent_id']][$val['id']] = $val;
  52. }
  53. return $lists;
  54. }
  55. public function jobsCateInfoCache()
  56. {
  57. $res = $this->model
  58. ->select(array('spell','id','parent_id','name'))
  59. ->orderBy('parent_id', 'desc')
  60. ->get()
  61. ->toArray();
  62. $cateSpell = array();
  63. $cateId =array();
  64. foreach ($res as $key => $val) {
  65. $cateId[$val['id']] = $val;
  66. $cateSpell[$val['spell']] = $val;
  67. }
  68. $lists = array('spell'=>$cateSpell,'id'=>$cateId);
  69. return $lists;
  70. }
  71. public function getJobCategories()
  72. {
  73. $job_cates = Cache::get('jobs_custom_cate');
  74. if (null === $job_cates) {
  75. $job_cates = $this->jobsCategoryCache();
  76. Cache::put('jobs_custom_cate', $job_cates, '86400');
  77. }
  78. return $job_cates;
  79. }
  80. public function getAllJobsCategories($parent_id = 0){
  81. $result = [];
  82. $result = $this->model->where(['parent_id'=>$parent_id])->select(['id','name','parent_id'])->get()->toArray();
  83. if($result){
  84. foreach ($result as $k => $v){
  85. $child = $this->getAllJobsCategories($v['id']);
  86. if($child && is_array($child) && count($child) > 0){
  87. $result[$k]['child'] = $child;
  88. }
  89. }
  90. return $result;
  91. }
  92. }
  93. }