DepartmentService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Services\Base;
  3. use App\Contracts\Repositories\Base\DepartmentRepository;
  4. use App\Repositories\Criteria\Base\DepartmentCriteria;
  5. use App\Repositories\Eloquent\Base\DepartmentRepositoryEloquent;
  6. use App\Repositories\Models\Base\Department;
  7. use App\Repositories\Presenters\Base\DepartmentPresenter;
  8. use Illuminate\Http\Request;
  9. class DepartmentService
  10. {
  11. /**
  12. * @var DepartmentRepositoryEloquent
  13. */
  14. private $departmentRepository;
  15. /**
  16. * DepartmentService constructor.
  17. *
  18. * @param DepartmentRepositoryEloquent $departmentRepositoryEloquent
  19. */
  20. public function __construct(DepartmentRepositoryEloquent $departmentRepositoryEloquent)
  21. {
  22. $this->departmentRepository = $departmentRepositoryEloquent;
  23. }
  24. /**
  25. * @param Request $request
  26. *
  27. * @return mixed
  28. * @throws \Prettus\Repository\Exceptions\RepositoryException
  29. */
  30. public function handleList(Request $request)
  31. {
  32. $this->departmentRepository->pushCriteria(new DepartmentCriteria($request));
  33. $this->departmentRepository->setPresenter(DepartmentPresenter::class);
  34. return $this->departmentRepository->searchDepartments();
  35. }
  36. /**
  37. * @param $id
  38. *
  39. * @return \Illuminate\Database\Eloquent\Model
  40. */
  41. public function handleProfile($id)
  42. {
  43. $this->departmentRepository->setPresenter(DepartmentPresenter::class);
  44. return $this->departmentRepository->searchDepartmentBy($id);
  45. }
  46. /**
  47. * @param array $data
  48. *
  49. * @return mixed
  50. * @throws \Prettus\Validator\Exceptions\ValidatorException
  51. */
  52. public function handleStore($data)
  53. {
  54. $data['company_id'] = 0;
  55. if ($data['parent_id']) {
  56. $data['company_id'] = Department::byIdGetCompanyId($data['company_id']);
  57. } else {
  58. $admin = login_admin();
  59. if ($admin && $admin['company_id']) $data['company_id'] = $admin['company_id'];
  60. }
  61. $department = $this->departmentRepository->create($data);
  62. return $department;
  63. }
  64. /**
  65. * @param array $data
  66. *
  67. * @return mixed
  68. * @throws \Prettus\Validator\Exceptions\ValidatorException
  69. */
  70. public function handleUpdate($data)
  71. {
  72. $department = $this->departmentRepository->update($data, $data['id']);
  73. return $department;
  74. }
  75. /**
  76. * @param Request $request
  77. *
  78. * @return mixed
  79. * @throws \Prettus\Validator\Exceptions\ValidatorException
  80. */
  81. public function handleDelete($id)
  82. {
  83. return $this->departmentRepository->delete($id);
  84. }
  85. /**
  86. * 选项数据
  87. * @param Request $request
  88. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
  89. * @throws \Prettus\Repository\Exceptions\RepositoryException
  90. */
  91. public function handleSelectOptions(Request $request)
  92. {
  93. $this->departmentRepository->pushCriteria(new DepartmentCriteria($request));
  94. return $this->departmentRepository->all([
  95. 'id',
  96. 'name',
  97. 'parent_id',
  98. 'sub_count',
  99. 'sort',
  100. ]);
  101. }
  102. }