OwnerBillReportService.php 2.3 KB

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