100 lines
3.8 KiB
PHP
100 lines
3.8 KiB
PHP
<?php
|
|
namespace app\command;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use support\Db;
|
|
use support\Log;
|
|
|
|
class FixInquiryLogOrderNo extends Command
|
|
{
|
|
protected static $defaultName = 'sn';
|
|
protected static $defaultDescription = 'Fix duplicate order_no values in inquiry_log table';
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
// Start transaction for safety
|
|
Db::beginTransaction();
|
|
|
|
try {
|
|
$output->writeln('Starting duplicate order_no fix process...');
|
|
|
|
// Find all duplicate order_no values using Laravel-style query
|
|
$duplicates = Db::table('inquiry_log')
|
|
->select('order_no', Db::raw('COUNT(*) as count'))
|
|
->groupBy('order_no')
|
|
->having('count', '>', 1)
|
|
->get();
|
|
|
|
if ($duplicates->isEmpty()) {
|
|
$output->writeln('No duplicate order_no values found.');
|
|
Db::commit();
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
$output->writeln(sprintf('Found %d order_no values with duplicates.', $duplicates->count()));
|
|
|
|
$totalFixed = 0;
|
|
|
|
foreach ($duplicates as $dup) {
|
|
$orderNo = $dup->order_no;
|
|
|
|
$output->writeln("Processing duplicates for order_no: {$orderNo}");
|
|
|
|
// Get all records with this order_no, ordered by created_at (oldest first)
|
|
$records = Db::table('inquiry_log')
|
|
->where('order_no', $orderNo)
|
|
->orderBy('created_at', 'asc')
|
|
->get();
|
|
|
|
// Keep the first record as is, modify the rest
|
|
$keepOriginal = true;
|
|
|
|
foreach ($records as $record) {
|
|
if ($keepOriginal) {
|
|
$output->writeln("Keeping original record ID {$record->id} with order_no {$record->order_no}");
|
|
$keepOriginal = false;
|
|
continue;
|
|
}
|
|
|
|
// Generate new order_no by appending suffix
|
|
$newOrderNo = orderNum();
|
|
$output->writeln("Updating record ID {$record->id} from {$record->order_no} to {$newOrderNo}");
|
|
|
|
// Update the record
|
|
Db::table('inquiry_log')
|
|
->where('id', $record->id)
|
|
->update(['order_no' => $newOrderNo]);
|
|
|
|
$totalFixed++;
|
|
|
|
sleep(1);
|
|
}
|
|
}
|
|
|
|
Db::commit();
|
|
$output->writeln("Successfully fixed {$totalFixed} duplicate records.");
|
|
|
|
// Verify no duplicates remain
|
|
$remainingDuplicates = Db::table('inquiry_log')
|
|
->select('order_no', Db::raw('COUNT(*) as count'))
|
|
->groupBy('order_no')
|
|
->having('count', '>', 1)
|
|
->get();
|
|
|
|
if ($remainingDuplicates->isEmpty()) {
|
|
$output->writeln('Verification: No duplicate order_no values remain.');
|
|
} else {
|
|
$output->writeln('Warning: Some duplicates still remain. Please run the command again.');
|
|
}
|
|
|
|
return self::SUCCESS;
|
|
} catch (\Throwable $e) {
|
|
Db::rollback();
|
|
$output->writeln("Error: " . $e->getMessage());
|
|
Log::error("FixInquiryLogOrderNo command failed: " . $e->getMessage());
|
|
return self::FAILURE;
|
|
}
|
|
}
|
|
} |