命令行
This commit is contained in:
62
app/command/OrderTask.php
Normal file
62
app/command/OrderTask.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace app\command;
|
||||
|
||||
use app\common\library\DatabaseRoute;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use support\Log;
|
||||
|
||||
|
||||
class OrderTask extends Command
|
||||
{
|
||||
protected static $defaultName = 'OrderTask';
|
||||
protected static $defaultDescription = 'OrderTask';
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->addArgument('name', InputArgument::OPTIONAL, 'Name description');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param InputInterface $input
|
||||
* @param OutputInterface $output
|
||||
* @return int
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
try {
|
||||
Log::info("未支付订单删除开始");
|
||||
// 获取3小时前的时间
|
||||
$threeHoursAgo = date('Y-m-d H:i:s', strtotime('-3 hours'));
|
||||
// 开启事务确保数据一致性
|
||||
DatabaseRoute::transactionXa(function () use ($threeHoursAgo, $output) {
|
||||
$deletedOrders = DatabaseRoute::deleteAllDbDirect('orders', function ($query) use ($threeHoursAgo) {
|
||||
return $query->where('create_time', '<=', $threeHoursAgo)
|
||||
->where('status', '<>', 1);
|
||||
});
|
||||
Log::info("删除了 {$deletedOrders} 条未支付订单");
|
||||
$deletedPayments = DatabaseRoute::deleteAllDbDirect('pay_details', function ($query) use ($threeHoursAgo) {
|
||||
return $query->where('create_time', '<=', $threeHoursAgo)
|
||||
->where('state', '<>', 1);
|
||||
});
|
||||
Log::info("删除了 {$deletedPayments} 条未支付支付记录");
|
||||
$output->writeln("<info>清理完成: 删除 {$deletedOrders} 个订单, {$deletedPayments} 个支付记录</info>");
|
||||
});
|
||||
} catch (\Exception $e) {
|
||||
Log::error("未支付订单清理失败: " . $e->getMessage());
|
||||
$output->writeln("<error>清理失败: " . $e->getMessage() . "</error>");
|
||||
return 1; // 返回错误码
|
||||
}
|
||||
// 返回成功码
|
||||
$output->writeln(0);
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user