You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

73 lines
1.9 KiB

<?php
declare(strict_types=1);
namespace App\Amqp\Consumer;
use App\Model\Order;
use App\Model\SpeakerDevic;
use App\Service\DeviceServiceInterface;
use Hyperf\Amqp\Result;
use Hyperf\Amqp\Annotation\Consumer;
use Hyperf\Amqp\Message\ConsumerMessage;
use Hyperf\DbConnection\Db;
use PhpAmqpLib\Message\AMQPMessage;
use Hyperf\Di\Annotation\Inject;
/**
* @Consumer(exchange="devicOrder", routingKey="devicOrder", queue="devicOrder", nums=4)
*/
class DevicOrderConsumer extends ConsumerMessage
{
/**
* @Inject
* @var DeviceServiceInterface
*/
protected $deviceService;
public function consumeMessage($data, AMQPMessage $message): string
{
try {
$orderMainId = $message->getBody();
$order = Order::query()
->select(['id', 'store_id', 'money'])
->where(['order_main_id' => $orderMainId, 'type' => 4, 'dm_state' => 2])
->first();
if (is_null($order)||!$order) {
return Result::ACK;
}
$deviceNames = SpeakerDevic::query()
->select(['device_name'])
->where(['store_id' => $order['store_id'], 'is_bind' => SpeakerDevic::IS_BIND_YES])
->get()
->toArray();
if (empty($deviceNames)||!$deviceNames) {
return Result::ACK;
}
$msg = "{\"msg\":\"到账".$order['money']."\"}";
$res = $this->deviceService->pubMsgToStoreByDevName($deviceNames, $msg);
if ($res == true) {
return Result::ACK;
} else {
return Result::REQUEUE;
}
} catch (\Exception $e) {
return Result::REQUEUE;
}
}
public function isEnable(): bool
{
if(env('APP_ENV') == 'local') {
return false;
}
return parent::isEnable();
}
}