OwnerBillReportService.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerBillReport;
  4. use App\Services\common\QueryService;
  5. use Illuminate\Database\Eloquent\Builder;
  6. Class OwnerBillReportService
  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) || ($params["owner_group_id"] ?? false)){
  21. $builder->whereHas('owner',function ($query)use(&$params){
  22. /** @var Builder $query*/
  23. if ($params["customer_id"] ?? false){
  24. $query->where("customer_id",$params["customer_id"]);
  25. unset($params["customer_id"]);
  26. }
  27. if ($params["owner_group_id"] ?? false){
  28. $query->where("user_owner_group_id",$params["owner_group_id"]);
  29. unset($params["owner_group_id"]);
  30. }
  31. });
  32. }
  33. return app(QueryService::class)->query($params, $builder, $columnQueryRules);
  34. }
  35. public function paginate(array $params, array $withs = null)
  36. {
  37. $bills = OwnerBillReport::query()->orderByDesc('id');
  38. if ($withs)$bills->with($withs);
  39. return $this->query($bills,$params)->paginate($params["paginate"] ?? 50);
  40. }
  41. public function update(array $params, array $values)
  42. {
  43. return $this->query(OwnerBillReport::query(),$params)->update($values);
  44. }
  45. public function get(array $params, array $withs = null)
  46. {
  47. $bills = OwnerBillReport::query()->orderByDesc('id');
  48. if ($withs)$bills->with($withs);
  49. return $this->query($bills,$params)->get();
  50. }
  51. public function first(array $params)
  52. {
  53. $query = OwnerBillReport::query();
  54. foreach ($params as $column=>$param){
  55. $query->where($column,$param);
  56. }
  57. return $query->first();
  58. }
  59. }