<?php namespace App\Http\Services; //后台用户相关信息服务 use App\Model\RedisModel; use App\Model\SupplierAttachmentsModel; use App\Model\SupplierChannelModel; use App\Model\SupplierContactModel; use App\Model\SupplierSyncModel; use Illuminate\Support\Arr; 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'); } /** * 推送参数: * { * "system_id": "1",//来源系统id 1猎芯科技,2猎芯供应链,3crm * "source_sn": "1", //源编码 各个系统标识公司的标识号 * "company_type": "1",//公司类型,1客户,2供应商 * "company_name": "测试公司", //公司名称 * "company_tax_no": "1234567",//公司税号 * "init_nature": "1234567",//来源公司初鉴性质 * "region": "1",//地区,1大陆,2海外 * "pi_file_url": "dasdsa" //pi附件地址 * } */ //同步供应商信息到一体化中心 public function syncSupplierToUnited($supplierId) { $supplier = SupplierChannelModel::where('supplier_id', $supplierId)->first()->toArray(); $fileUrl = SupplierAttachmentsModel::where('supplier_id', $supplierId) ->where('field_name', 'registration_certificate') ->value('file_url'); //构建需要同步的数据 $syncData = [ 'system_id' => 1, 'source_sn' => (string)$supplier['supplier_id'], 'company_type' => 2, 'company_name' => $supplier['supplier_name'], 'company_tax_no' => $supplier['tax_number'], 'init_nature' => Arr::get(config('fixed.SupplierGroup'), $supplier['supplier_group']), 'region' => $supplier['region'] == SupplierChannelModel::REGION_CN ? 1 : 2, 'pi_file_url' => $fileUrl ?: '', ]; (new QueueDeliveryService())->push(QueueDeliveryService::PUSH_TYPE_SYNC_HTTP, '/sync/Company/syncCompany', $syncData); } /** * 推送参数为: * { * "group_code":"K0000013", //集团编码 * "source_sn":"1",//来源id,其实就是供应商id,因为新增的时候丢过去的就是供应商id * "company_name":"\u6d4b\u8bd5\u516c\u53f8",//公司名 * "init_nature":"1234567",//初鉴性质 * "company_nature":"1234567"//公司真实性质 * "company_category":"普通供应商"//公司类别,只要不是普"通供应商",就是要拉黑的 * } **/ //接收一体化系统处理好的供应商数据,可能是第一次新增返回的数据,也可能是一体化那边修改的数据需要同步到供应商 public function syncSupplierToUnitedResult($syncResult) { $groupCode = array_get($syncResult, 'group_code'); $sourceSn = array_get($syncResult, 'source_sn'); $companyCategory = array_get($syncResult, 'company_category', ''); $isEntity = array_get($syncResult, 'is_entity', 0); $supplier = SupplierChannelModel::where('supplier_id', $sourceSn)->orWhere('group_code', $groupCode)->first(); $supplier = !empty($supplier) ? $supplier->toArray() : []; $supplierId = $supplier['supplier_id']; if ($companyCategory != '') { //实体名单和黑名单都要拉黑,如果不属于黑名单,那么就要将状态改成审核中 if ($companyCategory != '普通供应商') { $data['status'] = SupplierChannelModel::STATUS_BLOCK; $data['block_reason'] = '一体化系统黑名单供应商'; } elseif ($isEntity) { $data['status'] = SupplierChannelModel::STATUS_BLOCK; $data['block_reason'] = '一体化系统黑名单供应商'; } else { //判断原来是拉黑状态,才变成审核中,因为有可能不是修改公司类型,只是修改公司性质 if ($supplier['status'] == SupplierChannelModel::STATUS_BLOCK) { $data['status'] = SupplierChannelModel::STATUS_IN_REVIEW; } } } $data['sync_united_status'] = SupplierChannelModel::SYNC_UNITED_STATUS_OK; $data['group_code'] = $groupCode; $data['company_nature'] = $syncResult['company_nature']; $data['company_category'] = $companyCategory; //$sourceSn不为空的话,就是代表这是第一次新增的供应商,所以会回传供应商id过来 if ($sourceSn) { $data['supplier_name'] = $syncResult['company_name']; $result = SupplierChannelModel::where('supplier_id', $supplierId) ->update($data); if ($result) { //再次同步供应商到金蝶 $this->syncSupplierToErp($supplierId); } return $result; } else { return SupplierChannelModel::where('group_code', $groupCode) ->update($data); } } }