This commit is contained in:
2025-08-25 09:21:28 +08:00
parent 6730f11cca
commit 118c8d99f2
2 changed files with 123 additions and 61 deletions
+15 -2
View File
@@ -126,9 +126,22 @@ class IndexController
// \Webman\RedisQueue\Redis::send('notice', $data); // \Webman\RedisQueue\Redis::send('notice', $data);
$info = InquiryLog::with(['channelInfo', 'countryInfo', 'enter'])->where('id', 25998)->where('sync_crm', 0)->first(); $info = InquiryLog::with(['channelInfo', 'countryInfo', 'enter'])->where('id', 1)->where('sync_crm', 0)->first();
var_dump($info); var_dump(empty($info->recipient));
if ($info) {
var_dump(11);
} else {
var_dump(22);
}
if ($info !== NULL) {
var_dump(33);
} else {
var_dump(44);
}
return json(['code' => 0, 'msg' => 'success']); return json(['code' => 0, 'msg' => 'success']);
} }
+108 -59
View File
@@ -10,8 +10,9 @@ use app\common\model\User;
use app\common\utils\FeishuMessage; use app\common\utils\FeishuMessage;
use support\exception\BusinessException; use support\exception\BusinessException;
use support\Log; use support\Log;
use support\Model;
class InquiryToCrm implements Consumer class InquiryToCrmNew implements Consumer
{ {
// 要消费的队列名 // 要消费的队列名
public $queue = 'inquiry-to-crm-new'; public $queue = 'inquiry-to-crm-new';
@@ -22,73 +23,121 @@ class InquiryToCrm implements Consumer
// 消费 // 消费
public function consume($data) public function consume($data)
{ {
/*
询单 与 CRM 之间的同步场景描述
添加(询单)
添加 不发 不分 (不同步)1
添加 发了 不分 (要同步)2
添加 不发 分了 (要同步)3
添加 不发 直分 (要同步)4
更新(询单)
没发 没分 更新 (不同步)5
没发 分过 更新 (要同步)6
发过 没分 更新 (要同步)7
发过 分过 更新 (要同步)8
*/
try { try {
// 获取询盘信息 // 获取询盘信息
$info = InquiryLog::with(['channelInfo', 'countryInfo', 'enter'])->where('id', $data['id'])->where('sync_crm', 0)->first(); $info = InquiryLog::with(['channelInfo', 'countryInfo', 'enter'])->where('id', $data['id'])->first();
// 无论添加 或 更新,询单必然要存在
if (!$info) {
if ($info) { throw new BusinessException('询盘不存在:' . $data['id']);
// 获取用户信息
$recipient = 0;
$corpId = 0;
$uinfo = null;
if ($info->recipient) {
$recipient = $info->recipient;
$uinfo = User::where('id', $info->recipient)->first();
if ($uinfo) {
$corpId = $uinfo->corp_id;
}
}
// 同步客户数据
$crmCustomer = new CrmCustomer();
$crmCustomer->sn = $info->order_no;
$crmCustomer->corp_abbr = $corpId;
$crmCustomer->user_id = $recipient;
$crmCustomer->type = 1;
$crmCustomer->channel_id = $info->channel;
$crmCustomer->country_id = $info->country;
$crmCustomer->magnitude = $info->quality;
$crmCustomer->email = $info->email;
$crmCustomer->phone = $info->tel;
$crmCustomer->WhatsApp = $info->whatsapp;
$crmCustomer->contacts = $info->contacts;
$crmCustomer->desc_collect = $info->content;
$crmCustomer->enter_user_id = $info->enter_user_id;
$crmCustomer->obtain = time();
// 修改同步CRM状态
$info->sync_crm = 1;
// 保存
if (!$crmCustomer->save() || !$info->save()) {
throw new BusinessException('同步失败~');
}
// // 发送邮件消息
// if ($uinfo && $uinfo->email) {
// $options = [
// 'id' => $info->id,
// 'email' => $uinfo->email
// ];
// // Log::info('发送邮件消息:' . json_encode($options));
// \Webman\RedisQueue\Redis::send('email-message', $options);
// }
$wsdata = [
'sender_id' => 0,
'sender_type' => 'system',
'title' => '有新的客户线索,' . $info->order_no . ',请及时处理',
'content' => '有新的客户线索,' . $info->order_no . ',请及时处理',
'scene' => 1,
'recipient' => [$info->recipient]
];
// 异步推送
\Webman\RedisQueue\Redis::send('notice', $wsdata);
} }
if ($info->status == 0 && $info->recipient == 0) {
// 如果询单未发、未分,则不与crm同步,不做任何处理
return true;
}
// 更改库信息
$this->syncCustomer($info);
$wsdata = [
'sender_id' => 0,
'sender_type' => 'system',
'title' => '有新的客户线索,' . $info->order_no . ',请及时处理',
'content' => '有新的客户线索,' . $info->order_no . ',请及时处理',
'scene' => 1,
'recipient' => [$info->recipient]
];
// 异步推送
\Webman\RedisQueue\Redis::send('notice', $wsdata);
} catch (\Throwable $th) { } catch (\Throwable $th) {
throw $th; throw $th;
} }
} }
public function syncCustomer(Model $info)
{
$uInfo = $this->recipientInfo($info);
$crmCustomerInfo = CrmCustomer::where('sn', $info->order_no)->first();
if ($crmCustomerInfo) {
$udata = [
'corp_abbr' => $uInfo['corpId'],
'user_id' => $info->recipient,
'channel_id' => $info->channel,
'country_id' => $info->country,
'magnitude' => $info->quality,
'email' => $info->email,
'phone' => $info->tel,
'WhatsApp' => $info->whatsapp,
'contacts' => $info->contacts,
'desc_collect' => $info->content,
];
if (!$crmCustomerInfo->update($udata)) {
throw new BusinessException('同步失败,更新客户线索失败');
}
} else {
$crmCustomer = new CrmCustomer();
$crmCustomer->sn = $info->order_no;
$crmCustomer->corp_abbr = $uInfo['corpId'];
$crmCustomer->user_id = $uInfo['recipient'];
$crmCustomer->type = 1;
$crmCustomer->channel_id = $info->channel;
$crmCustomer->country_id = $info->country;
$crmCustomer->magnitude = $info->quality;
$crmCustomer->email = $info->email;
$crmCustomer->phone = $info->tel;
$crmCustomer->WhatsApp = $info->whatsapp;
$crmCustomer->contacts = $info->contacts;
$crmCustomer->desc_collect = $info->content;
$crmCustomer->enter_user_id = $info->enter_user_id;
$crmCustomer->obtain = time();
// 修改同步CRM状态
$info->sync_crm = 1;
// 保存
if (!$crmCustomer->save() || !$info->save()) {
throw new BusinessException('同步失败,创建客户线索失败');
}
}
return true;
}
public function recipientInfo(Model $info)
{
$recipient = 0;
$corpId = 0;
$uinfo = null;
if (!empty($info->recipient)) {
$recipient = $info->recipient;
$uinfo = User::where('id', $info->recipient)->first();
if ($uinfo) {
$corpId = $uinfo->corp_id;
}
}
return [
'recipient' => $recipient,
'corpId' => $corpId,
'uinfo' => $uinfo
];
}
// 消费失败回调 // 消费失败回调
/* /*
$package = [ $package = [