1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- declare (strict_types = 1);
- namespace App\Command;
- use App\Model\Ad;
- use App\Model\Order;
- use App\Model\OrderAd;
- use Hyperf\Command\Annotation\Command; // 假设Order模型在App\Model命名空间下
- use Hyperf\Command\Command as HyperfCommand; // 假设OrderAd模型在App\Model命名空间下
- use Hyperf\DbConnection\Db;
- use Psr\Log\LoggerInterface;
- // 假设Ad模型在App\Model命名空间下
- #[Command]
- class EndOrderAdminCommand extends HyperfCommand
- {
- protected ?string $name = 'end:order-admin';
- protected $logger;
- public function __construct(LoggerInterface $logger)
- {
- parent::__construct();
- $this->logger = $logger;
- }
- public function configure()
- {
- parent::configure();
- $this->setDescription('结束订单并处理相关记录');
- }
- public function handle()
- {
- // 获取需要处理的订单ID列表
- // $currentTime = time();
- $currentTime = date('Y-m-d H:i:s');
- $orderIds = Order::where('status', 6)
- ->where('edtime', '<=', $currentTime)
- ->pluck('id')
- ->toArray();
- $this->info('需要处理的订单ID列表:' . implode(', ', $orderIds));
- foreach ($orderIds as $orderId) {
- $data = ['id' => $orderId];
- $this->processOrder($data);
- }
- }
- private function processOrder(array $data)
- {
- $order = Order::where('id', $data['id'])->first();
- if (empty($order)) {
- $this->logger->warning("没有找到订单ID: {$data['id']}");
- return;
- }
- Db::beginTransaction();
- try {
- $order->status = 7;
- $order->ad_status = 7;
- $order->save();
- // 获取 order_ad 表中的记录
- OrderAd::where('order_id', $data['id'])
- ->update(['status' => 7]);
- // 在ad表中删除相同pid的数据
- Ad::where('order_id', $data['id'])->delete();
- Db::commit();
- } catch (\Exception $e) {
- Db::rollBack();
- $this->logger->error("处理订单ID: {$data['id']} 时发生错误: " . $e->getMessage());
- }
- }
- }
|