107 lines
4.0 KiB
PHP
107 lines
4.0 KiB
PHP
<?php
|
||
|
||
namespace app\command;
|
||
|
||
use app\api\model\Orders;
|
||
use app\common\library\DatabaseRoute;
|
||
use app\utils\WuYouPayUtils;
|
||
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 TempOrderTask extends Command
|
||
{
|
||
protected static $defaultName = 'TempOrderTask';
|
||
protected static $defaultDescription = 'TempOrderTask';
|
||
|
||
/**
|
||
* @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('订单表数据处理开始');
|
||
|
||
// 查询待处理的支付明细(state=0,create_time 早于 15 分钟前)
|
||
$payDetailsList = DatabaseRoute::getAllDbData('pay_details', function ($query) {
|
||
return $query
|
||
->where('state', 0)
|
||
->where('create_time', '<', date('Y-m-d H:i:s', strtotime('-15 minutes')))
|
||
->order('create_time', 'asc')
|
||
->limit(1800);
|
||
})->select()->toArray();
|
||
|
||
if (empty($payDetailsList)) {
|
||
return 0;
|
||
}
|
||
|
||
Log::info('待处理数据' . count($payDetailsList) . '条');
|
||
|
||
foreach ($payDetailsList as $details) {
|
||
try {
|
||
usleep(100 * 1000); // sleep 100ms
|
||
|
||
// 根据 orderId 查询 Orders 表
|
||
$order = DatabaseRoute::getAllDbData('orders', function ($query) use ($details) {
|
||
return $query->where('orders_no', $details['order_id'])
|
||
->where('user_id', $details['user_id']);
|
||
})->find();
|
||
|
||
// 调用支付平台查询订单状态
|
||
$baseResp = WuYouPayUtils::queryOrder(
|
||
$details['trade_no'],
|
||
$details['user_id'],
|
||
(string)$details['money'],
|
||
'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X)...'
|
||
);
|
||
|
||
Log::info('baseResp: ' . json_encode($baseResp));
|
||
|
||
if (empty($baseResp['code']) || $baseResp['code'] != 200) {
|
||
Log::info('code 错误跳过');
|
||
continue;
|
||
}
|
||
|
||
if (($baseResp['payStatus'] ?? '') === 'SUCCESS' || ($baseResp['payStatus2'] ?? '') === 'SUCCESS') {
|
||
Log::info('payDetails: ' . json_encode($details));
|
||
Log::info('order: ' . json_encode($order));
|
||
Orders::updateOrderStatus($details, $order, $order['user_id']);
|
||
} else {
|
||
Log::info('订单未支付,修改状态: ' . $details['trade_no']);
|
||
DatabaseRoute::getDb('orders', $order['user_id'], true, true)->where([
|
||
'orders_id' => $order['orders_id']
|
||
])->update([
|
||
'status' => $order ? 3 : 2
|
||
]);
|
||
}
|
||
} catch (\Throwable $e) {
|
||
Log::error('订单数据处理异常:' . $e->getMessage());
|
||
}
|
||
}
|
||
|
||
Log::info('订单表数据处理完毕');
|
||
} catch (\Exception $e) {
|
||
Log::error("订单表数据处理失败: " . $e->getMessage());
|
||
Log::info($e->getTraceAsString());
|
||
$output->writeln("<error>订单表数据处理失败: " . $e->getMessage() . "</error>");
|
||
return 1; // 返回错误码
|
||
}
|
||
return self::SUCCESS;
|
||
}
|
||
|
||
}
|