available()) { $this->error('统一表 store_violation_records 尚未建立。'); return self::FAILURE; } $connection = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror')); $chunk = max(min((int) $this->option('chunk'), 2000), 100); $platform = trim((string) $this->option('platform')); $total = 0; $query = $connection->table(StoreViolationRecordRepository::TABLE) ->select(['id', 'platform', 'violation_no', 'shop_name', 'violation_type', 'violation_reason', 'violation_date', 'appeal_deadline']) ->orderBy('id'); if ($platform !== '') { $query->where('platform', $platform); } $query->chunk($chunk, function ($rows) use ($calculator, $connection, &$total): void { $updates = []; foreach ($rows as $row) { $matchType = trim((string) ($row->violation_type ?? '')) !== '' ? (string) $row->violation_type : ($row->violation_reason ?? null); // upsert 的插入侧会校验非空列,带上 platform / violation_no 以通过严格模式; // 命中已有记录时只会更新派生字段(update 列表)。 $updates[] = [ 'id' => $row->id, 'platform' => (string) $row->platform, 'violation_no' => (string) ($row->violation_no ?? ''), ] + $calculator->calculate( (string) $row->platform, $row->shop_name, $matchType, $row->violation_date ? (string) $row->violation_date : null, $row->appeal_deadline ? (string) $row->appeal_deadline : null, ); } if ($updates !== []) { $connection->table(StoreViolationRecordRepository::TABLE)->upsert( $updates, ['id'], StoreViolationRecordRepository::ALERT_DERIVED_COLUMNS, ); $total += count($updates); } }); $this->info(sprintf('预警重算完成:%d 行。', $total)); return self::SUCCESS; } }