<?php namespace App\Http\Services; //后台用户相关信息服务 use App\Model\SupplierChannelModel; use App\Model\SupplierContactModel; use App\Model\SupplierSyncModel; use Illuminate\Support\Facades\DB; use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; class SyncSupplierService { //同步供应商到erp public function syncSupplierToErp($supplierId) { //先去获取供应商的信息 $supplierModel = new SupplierChannelModel(); $supplier = $supplierModel->where('supplier_id', $supplierId)->first(); if (empty($supplier)) { return false; } //只推送禁用和启用状态的 if (!in_array($supplier['status'], [2, -2, -3])) { return true; } //先去sync表查询记录是否存在,不存在新增,存在修改 $supplierSyncModel = new SupplierSyncModel(); $exist = $supplierSyncModel->where('supplier_id', $supplierId)->count(); if (!empty($exist)) { $supplierSyncModel->where('supplier_id', $supplierId)->update([ 'erp_sync_status' => 0, 'update_time' => time(), 'last_update_source' => 2, ]); } else { $supplierSyncModel->insert([ 'erp_sync_status' => 0, 'supplier_id' => $supplierId, 'add_time' => time(), 'last_update_source' => 2, ]); } $contactModel = new SupplierContactModel(); $contact = $contactModel->where('supplier_id', $supplier['supplier_id'])->orderBy('contact_id', 'asc')->first(); $purchases = $this->getPurchases($supplier['channel_uid']); $message = [ 'name' => $supplier['supplier_name'], 'PTID' => $supplier['supplier_code'], 'STATUS' => $supplier['status'] == 2 ? 1 : 2, ]; if (!empty($supplier['supplier_group'])) { $message['groupNumber'] = array_get(config('sync.SyncSupplierType'), $supplier['supplier_group'], ''); } if (!empty($supplier['tax_number'])) { $message['taxRegisterNo'] = $supplier['tax_number']; } if (!empty($purchases)) { $message['personName'] = $purchases; } if (!empty($supplier['supplier_address'])) { $message['address'] = $supplier['supplier_address']; } if (!empty(array_get($contact, 'supplier_consignee'))) { $message['contactPerson'] = array_get($contact, 'supplier_consignee'); } if (!empty(array_get($contact, 'supplier_telephone'))) { $message['phone'] = array_get($contact, 'supplier_telephone'); } if (!empty($supplier['currency'])) { $message['currencyNumber'] = array_get(config('sync.SyncCurrencyType'), $supplier['currency']); } $conn = new AMQPStreamConnection(config('database.connections.rabbitmq.host'), config('database.connections.rabbitmq.port'), config('database.connections.rabbitmq.login'), config('database.connections.rabbitmq.password')); $channel = $conn->channel(); $channel->queue_declare('supplier_sync', false, true, false, false); $msg = new AMQPMessage(json_encode($message), array('content_type' => 'text/plain')); $result = $channel->basic_publish($msg, '', 'supplier_sync'); // $result = $channel->basic_publish($msg); } //获取采购人员列表 private function getPurchases($purchaseUid) { $purchaseUid = explode(',', $purchaseUid); if (empty($purchaseUid)) { return []; } $adminIds = DB::connection()->table('lie_intracode')->whereIn('code_id', $purchaseUid)->pluck('admin_id'); return DB::connection()->table('user_info')->whereIn('userId', $adminIds)->pluck('name'); } }