OwnerBillReportService.php 2.3 KB

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