OwnerBillReportService.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerBillReport;
  4. use App\Services\common\QueryService;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use App\Traits\ServiceAppAop;
  7. Class OwnerBillReportService
  8. {
  9. use ServiceAppAop;
  10. /**
  11. * @param Builder $builder
  12. * @param array $params
  13. * @return Builder
  14. */
  15. private function query(Builder $builder, array $params)
  16. {
  17. $columnQueryRules = [
  18. 'counting_month_start' => ['alias' => 'counting_month', 'startDate' => '-01'],
  19. 'counting_month_end' => ['alias' => 'counting_month', 'endDate' => '-31'],
  20. 'owner_id' => ['multi' => ','],
  21. ];
  22. if (($params["customer_id"] ?? false) || ($params["owner_group_id"] ?? false)){
  23. $builder->whereHas('owner',function ($query)use(&$params){
  24. /** @var Builder $query*/
  25. if ($params["customer_id"] ?? false){
  26. $query->where("customer_id",$params["customer_id"]);
  27. unset($params["customer_id"]);
  28. }
  29. if ($params["owner_group_id"] ?? false){
  30. $query->where("user_owner_group_id",$params["owner_group_id"]);
  31. unset($params["owner_group_id"]);
  32. }
  33. });
  34. }
  35. return app(QueryService::class)->query($params, $builder, $columnQueryRules);
  36. }
  37. public function paginate(array $params, array $withs = null)
  38. {
  39. $bills = OwnerBillReport::query()->orderByDesc('id');
  40. if ($withs)$bills->with($withs);
  41. return $this->query($bills,$params)->paginate($params["paginate"] ?? 50);
  42. }
  43. public function update(array $params, array $values)
  44. {
  45. return $this->query(OwnerBillReport::query(),$params)->update($values);
  46. }
  47. public function get(array $params, array $withs = null)
  48. {
  49. $bills = OwnerBillReport::query()->orderByDesc('id');
  50. if ($withs)$bills->with($withs);
  51. return $this->query($bills,$params)->get();
  52. }
  53. public function first(array $params)
  54. {
  55. $query = OwnerBillReport::query();
  56. foreach ($params as $column=>$param){
  57. $query->where($column,$param);
  58. }
  59. return $query->first();
  60. }
  61. }