OwnerFeeDetailService.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerFeeDetail;
  4. use App\Services\common\QueryService;
  5. use Illuminate\Database\Eloquent\Builder;
  6. Class OwnerFeeDetailService
  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. 'worked_at_start' => ['alias' => 'worked_at', 'startDate' => ''],
  17. 'worked_at_end' => ['alias' => 'worked_at', 'endDate' => ''],
  18. 'owner_id' => ['multi' => ','],
  19. 'id' => ['multi' => ','],
  20. 'operation_bill' => ['like' => ''],
  21. 'logistic_bill' => ['like' => ''],
  22. ];
  23. if ($params["customer_id"] ?? false){
  24. $builder->whereHas('owner',function ($query)use(&$params){
  25. /** @var Builder $query*/
  26. $query->where("customer_id",$params["customer_id"]);
  27. unset($params["customer_id"]);
  28. });
  29. }
  30. return app(QueryService::class)->query($params, $builder, $columnQueryRules, 'owner_fee_details');
  31. }
  32. public function paginate(array $params, array $withs = null)
  33. {
  34. $areas = OwnerFeeDetail::query()->orderByDesc('id');
  35. if ($withs)$areas->with($withs);
  36. return $this->query($areas,$params)->paginate($params["paginate"] ?? 50);
  37. }
  38. public function getSql($params)
  39. {
  40. $query = $this->query(OwnerFeeDetail::query()->orderByDesc('id'),$params);
  41. $query->selectRaw("owner_fee_details.*,(work_fee+logistic_fee) total");
  42. $query->leftJoin("owners","owner_fee_details.owner_id","owners.id")
  43. ->selectRaw("owners.name owner_name")
  44. ->leftJoin("customers","owners.customer_id","customers.id")
  45. ->selectRaw("customers.name customer_name")
  46. ->leftJoin("shops","owner_fee_details.shop_id","shops.id")
  47. ->selectRaw("shops.name shop_name")
  48. ->leftJoin("logistics","owner_fee_details.logistic_id","logistics.id")
  49. ->selectRaw("logistics.name logistic_name");
  50. return $query->sql();
  51. }
  52. public function create(array $params)
  53. {
  54. return OwnerFeeDetail::query()->create($params);
  55. }
  56. }