<?php
declare(strict_types=1);
namespace App\Amqp\Consumer;
use App\Model\Customer; use App\Model\CustomerAddress; use Hyperf\Amqp\Result; use Hyperf\Amqp\Annotation\Consumer; use Hyperf\Amqp\Message\ConsumerMessage; use Hyperf\DbConnection\Db; use PhpAmqpLib\Message\AMQPMessage; use Hyperf\Logger\LoggerFactory; use Ramsey\Uuid\Uuid;
/**
@Consumer(exchange="customer", routingKey="customer", queue="customer", name="CustomerConsumer", nums=1)
/ class CustomerConsumer extends ConsumerMessage { private $logger;
public function __construct(LoggerFactory $loggerFactory) {
$this->logger = $loggerFactory->get('rabbitmq');
}
public function consumeMessage($data, AMQPMessage $message): string {
Db::beginTransaction(); try { if(!Customer::query()->where('ios_guid',$data['ios_guid'])->exists()){ $customer=new Customer(); $customer->Guid=Uuid::uuid4()->getHex(); $customer->ios_guid=$data['ios_guid']; $customer->Name=$data['name']; $customer->Email=$data['email']; $customer->Phone=$data['phone']; $customer->company_name=$data['company_name']; $customer->equipment_no=$data['equipment_no']; $customer->tax_number=$data['tax_number']; $customer->selling_price=$data['selling_price']; $customer->remark=$data['remark']; $customer->is_ipad=$data['is_ipad']??1; $customer->save(); }else{ Customer::query() ->where('ios_guid',$data['ios_guid']) ->update([ 'ios_guid'=>$data['ios_guid'], 'Name'=>$data['name'], 'Email'=>$data['email'], 'Phone'=>$data['phone'], 'company_name'=>$data['company_name'], 'equipment_no'=>$data['equipment_no'], 'tax_number'=>$data['tax_number'], 'selling_price'=>$data['selling_price'], 'remark'=>$data['remark'], 'is_ipad'=>$data['is_ipad']??1, ]); } if($customer){ if(!CustomerAddress::query() ->where('CustomerGuid',$customer->Guid) ->exists()){ $customeraddress=new CustomerAddress(); $customeraddress->Guid=Uuid::uuid4()->getHex(); $customeraddress->CustomerGuid=$customer->Guid; $customeraddress->Address1=$data['address']; $customeraddress->City=$data['city']; $customeraddress->Province=$data['province']; $customeraddress->CreateTime=date('Y-m-d H:i:s',time()); $customeraddress->save(); }else{ CustomerAddress::query() ->where('CustomerGuid',$customer->Guid) ->update([ 'CustomerGuid'=>$customer->Guid, 'Address1'=>$data['address'], 'City'=>$data['city'], 'Province'=>$data['province'], 'CreateTime'=>date('Y-m-d H:i:s',time()) ]); } } Db::commit(); $this->logger->info('Received and processed message:成功'); return Result::ACK; }catch (\Exception $exception){ Db::rollBack(); $this->logger->info('Received and processed message:'.$exception->getMessage()); return Result::DROP; }
} } @kolin 这样还是不行
推荐文章: