OwnerAreaReportService.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerAreaReport;
  4. use App\Services\common\QueryService;
  5. use Illuminate\Database\Eloquent\Builder;
  6. Class OwnerAreaReportService
  7. {
  8. /**
  9. * @param Builder $builder
  10. * @param array $params
  11. * @return Builder
  12. */
  13. private function query(Builder $builder, array $params)
  14. {
  15. $columnQueryRules = [
  16. 'counting_month_start' => ['alias' => 'counting_month', 'startDate' => '-01'],
  17. 'counting_month_end' => ['alias' => 'counting_month', 'endDate' => '-31'],
  18. 'owner_id' => ['multi' => ','],
  19. ];
  20. if ($params["customer_id"] ?? false){
  21. $builder->whereHas('owner',function ($query)use(&$params){
  22. /** @var Builder $query*/
  23. $query->where("customer_id",$params["customer_id"]);
  24. unset($params["customer_id"]);
  25. });
  26. }
  27. return app(QueryService::class)->query($params, $builder, $columnQueryRules);
  28. }
  29. public function paginate(array $params, array $withs = null)
  30. {
  31. $areas = OwnerAreaReport::query()->orderByDesc('id');
  32. if ($withs)$areas->with($withs);
  33. return $this->query($areas,$params)->paginate($params["paginate"] ?? 50);
  34. }
  35. public function update(array $params, array $values)
  36. {
  37. return $this->query(OwnerAreaReport::query(),$params)->update($values);
  38. }
  39. public function get(array $params, array $withs = null)
  40. {
  41. $query = OwnerAreaReport::query()->orderByDesc('id');
  42. if ($withs)$query->with($withs);
  43. return $this->query($query,$params)->get();
  44. }
  45. }