argument('start_date'); $end = $this->argument('end_date'); try { $startDate = Carbon::createFromFormat('Y-m-d', $start)->startOfDay(); $endDate = Carbon::createFromFormat('Y-m-d', $end)->endOfDay(); } catch (\Exception $exception) { $this->error('日期格式错误,请使用 YYYY-MM-DD,例如 2025-02-01'); return 1; } if ($startDate->gt($endDate)) { $this->error('开始日期不能大于结束日期'); return 1; } $projectId = $this->argument('project_id') ?: $this->option('project_id'); $limit = (int) $this->option('limit'); $limit = $limit > 0 ? $limit : 50; $this->info(sprintf( '检测区间:%s ~ %s%s', $startDate->toDateString(), $endDate->toDateString(), $projectId ? ",项目 ID:{$projectId}" : '' )); $query = OrderItems::query() ->whereBetween('service_date', [$startDate->toDateString(), $endDate->toDateString()]) ->with(['order.project', 'product']); if ($projectId) { $query->whereHas('order', function ($q) use ($projectId) { $q->where('project_id', $projectId); }); } $holidayRatios = Holiday::query() ->whereBetween('date', [$startDate->toDateString(), $endDate->toDateString()]) ->when($projectId, function ($q) use ($projectId) { $q->where('project_id', $projectId); }) ->get(['project_id', 'date', 'price_ratio']) ->keyBy(function ($holiday) { return $holiday->project_id . ':' . $holiday->date; }); $total = 0; $rowsForTable = []; $query->chunkById(200, function ($items) use (&$total, &$rowsForTable, $limit, $holidayRatios) { foreach ($items as $item) { /** @var \App\Models\OrderItems $item */ $total++; $storedFee = (float) $item->fee; $projectId = optional($item->order)->project_id; $holidayKey = $projectId . ':' . $item->service_date; $holidayRatio = optional($holidayRatios->get($holidayKey))->price_ratio; $calcItem = clone $item; if ($item->relationLoaded('order')) { $calcItem->setRelation('order', $item->getRelation('order')); } if ($item->relationLoaded('product')) { $calcItem->setRelation('product', $item->getRelation('product')); } $calcItem->calculateFee(); $calculatedFee = (float) $calcItem->fee; if (count($rowsForTable) < $limit) { $rowsForTable[] = [ 'ID' => $item->id, '订单ID' => $item->order_id, '项目名称' => optional(optional($item->order)->project)->name ?? '', '日期' => $item->service_date, '节假日倍率' => $holidayRatio ?? '', '总价' => $item->total, '原fee' => $storedFee, '算法fee' => $calculatedFee, '是否扣款' => $item->paid_at ? '是' : '否', ]; } } }); if (empty($rowsForTable)) { $this->info('该时间段无子订单记录'); } else { $this->table( ['ID', '订单ID', '项目名称', '日期', '节假日倍率', '总价', '原fee', '算法fee', '是否扣款'], $rowsForTable ); } $this->info(sprintf('共检测 %d 条子订单。', $total)); return 0; } }