option('from')); $to = trim((string) $this->option('to')); $to = $to !== '' ? $to : Carbon::now('Asia/Shanghai')->subDay()->toDateString(); $companyId = (int) $this->option('company'); $months = array_values(array_filter( array_map('strval', (array) $this->option('month')), static fn (string $month): bool => trim($month) !== '', )); try { $result = $synchronizer->sync( $from, $to, $companyId, $months, (bool) $this->option('dry-run'), ); } catch (Throwable $exception) { $this->outputResult([ 'status' => 'failed', 'from' => $from, 'to' => $to, 'error' => $exception->getMessage(), ]); return self::FAILURE; } if ((bool) $this->option('dry-run')) { $this->outputResult($result); return self::SUCCESS; } $results = []; foreach ($result['months'] as $month) { $exitCode = $this->call('store-receipt:order-match:sync', [ 'month' => $month, '--company' => $companyId, '--planting-only' => true, ]); $results[$month] = ['exit_code' => $exitCode]; if ($exitCode !== self::SUCCESS) { $this->outputResult([ 'status' => 'failed', 'from' => $from, 'to' => $to, 'months' => $result['months'], 'results' => $results, 'error' => $month . ' 种菜金额重算失败。', ]); return self::FAILURE; } } $result['status'] = 'ok'; $result['results'] = $results; $this->outputResult($result); return self::SUCCESS; } private function outputResult(array $result): void { if ((bool) $this->option('json')) { $this->line(json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); return; } if (($result['status'] ?? '') === 'failed') { $this->error((string) ($result['error'] ?? '种菜金额同步失败。')); return; } $this->info(sprintf( '业绩明细种菜金额已刷新:核查 %s ~ %s,月份 %s。', $result['from'], $result['to'], implode('、', $result['months']), )); foreach ($result['planting_sync'] ?? [] as $month => $sync) { $this->line(sprintf( ' %s 种菜报销明细:%d 单,写入 %d 行。', $month, (int) ($sync['orders'] ?? 0), (int) ($sync['inserted'] ?? 0), )); } } }