| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- namespace App\Services;
- use App\Logistic;
- use App\OwnerFeeDetail;
- use App\Traits\ServiceAppAop;
- use App\OwnerLogisticFeeDetail;
- use Carbon\Carbon;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Pagination\LengthAwarePaginator;
- use Illuminate\Support\Collection;
- class OwnerLogisticFeeDetailService
- {
- const TYPE = '快递费-明细';
- use ServiceAppAop;
- /**
- * @var $modelClass OwnerLogisticFeeDetail
- */
- protected $modelClass = OwnerLogisticFeeDetail::class;
- //插入数据 ServiceAppAop的 insert 方法 支持批量
- /**
- * 根据货主查询 和时间段查询
- * @param string $owner_id
- * @param string $start
- * @param string $end
- * @param $paginateParams
- * @return LengthAwarePaginator
- */
- public function getDetails(string $owner_id, string $start, string $end, $paginateParams): LengthAwarePaginator
- {
- $ownerLogisticFeeDetails = $this->getSql($owner_id, $start, $end)
- ->paginate($paginateParams['paginate'] ?? 50);
- $items = $this->buildDetails($ownerLogisticFeeDetails);
- return new LengthAwarePaginator(
- $items,
- $ownerLogisticFeeDetails->total(),
- $ownerLogisticFeeDetails->perPage(),
- $ownerLogisticFeeDetails->currentPage(),
- ["path" => $ownerLogisticFeeDetails->path(), "pageName" => "page"]
- );
- }
- /**
- * @param Builder $logistic_ids
- * @param string $owner_id
- * @param string $start
- * @param string $end
- * @return Builder
- */
- private function getOwnerFeeDetailQuery(Builder $logistic_ids, string $owner_id, string $start, string $end): Builder
- {
- return OwnerFeeDetail::query()->selectRaw('id')
- ->where('type', '发货')
- ->where('outer_table_name', 'orders')
- ->whereIn('logistic_id', $logistic_ids)
- ->where('owner_id', $owner_id)
- ->where('worked_at', '>=', Carbon::parse($start)->startOfDay())
- ->where('worked_at', '<=', Carbon::parse($end)->endOfDay());
- }
- /**
- * @param string $owner_id
- * @param string $start
- * @param string $end
- * @return Builder
- */
- public function getSql(string $owner_id, string $start, string $end): Builder
- {
- return OwnerLogisticFeeDetail::query()->with([
- 'ownerFeeDetail:id,province,weight,logistic_fee',
- 'ownerFeeDetailLogistic:id,weight,logistic_fee,logistic_bill',
- 'logistic:id,name'
- ])
- ->whereIn('owner_fee_detail_id', $this->getOwnerFeeDetailQuery(Logistic::query()->selectRaw('id')->where('type', '快递'), $owner_id, $start, $end))
- ->orderBy('logistic_id');
- }
- /**
- * @param $ownerLogisticFeeDetails
- * @return array
- */
- public function buildDetails($ownerLogisticFeeDetails): array
- {
- $items = [];
- foreach ($ownerLogisticFeeDetails as $ownerLogisticFeeDetail) {
- $items[] = [
- 'id' => $ownerLogisticFeeDetail->id,
- 'logistic_name' => $ownerLogisticFeeDetail->logistic->name ?? '',//快递公司
- 'province' => $ownerLogisticFeeDetail->province,//省份
- 'logistic_bill' => $ownerLogisticFeeDetail->logistic_bill ?? '',//快递单号
- 'weight' => $ownerLogisticFeeDetail->ownerFeeDetailLogistic->weight ?? $ownerLogisticFeeDetail->ownerFeeDetail->weight ?? '0.00',//重量
- 'initial_weight_price' => $ownerLogisticFeeDetail->initial_weight_price ?? '',//首重价格
- 'additional_price' => $ownerLogisticFeeDetail->additional_price ?? '',//续重价格
- 'logistic_fee' => $ownerLogisticFeeDetail->ownerFeeDetailLogistic->logistic_fee ?? $ownerLogisticFeeDetail->ownerFeeDetail->logistic_fee ?? '0.00',//快递费
- ];
- }
- return $items;
- }
- }
|