增加同步crm
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use support\Model;
|
||||
|
||||
class CrmCustomer extends Model
|
||||
{
|
||||
/**
|
||||
* The connection name for the model.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $connection = 'mysql';
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'crm_customer';
|
||||
|
||||
/**
|
||||
* The primary key associated with the table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = true;
|
||||
|
||||
|
||||
protected $dateFormat = 'U';
|
||||
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
*/
|
||||
protected $fillable = ['id', 'sn', 'corp_abbr', 'user_id', 'user_sn', 'type', 'grade', 'sex', 'channel_id', 'country_id', 'car_type_id', 'magnitude', 'email', 'phone', 'WhatsApp', 'contacts', 'describe', 'desc_collect', 'address', 'lat', 'lng', 'follow_last', 'follow_next', 'warn', 'follow_remark', 'created_at', 'updated_at', 'deleted_at'];
|
||||
|
||||
/**
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
}
|
||||
@@ -96,4 +96,4 @@ class User extends Model
|
||||
->orWhere('phone', $account)
|
||||
->orWhere('email', $account);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,9 +13,9 @@ use support\Cache;
|
||||
class IndexController
|
||||
{
|
||||
|
||||
protected $noNeedLogin = ['index', 'view', 'perm', 'createResource'];
|
||||
protected $noNeedLogin = ['index', 'view', 'perm', 'createResource', 'test'];
|
||||
|
||||
protected $noNeedHeaderData = ['index', 'view', 'perm', 'createResource'];
|
||||
protected $noNeedHeaderData = ['index', 'view', 'perm', 'createResource', 'test'];
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
@@ -76,7 +76,7 @@ class IndexController
|
||||
// ],
|
||||
'model' => [
|
||||
'module' => 'common',
|
||||
'name' => 'inquiry_channel'
|
||||
'name' => 'crm_customer'
|
||||
],
|
||||
// 'service' => [
|
||||
// 'module' => 'inquiry',
|
||||
@@ -90,10 +90,18 @@ class IndexController
|
||||
// 'module' => 'inquiry',
|
||||
// 'name' => 'country'
|
||||
// ],
|
||||
'table' => 'inquiry_channel'
|
||||
'table' => 'crm_customer'
|
||||
];
|
||||
|
||||
$result = (new Generate($options))->doGenerate();
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
public function test()
|
||||
{
|
||||
$res = ['id'=>25714];
|
||||
\Webman\RedisQueue\Redis::send('inquiry-to-crm', $res);
|
||||
return 'success';
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\InquiryLog;
|
||||
use support\exception\BusinessException;
|
||||
use support\Request;
|
||||
use Webman\RedisQueue\Redis as RedisQueueRedis;
|
||||
|
||||
class InquiryLogService
|
||||
{
|
||||
@@ -39,7 +40,7 @@ class InquiryLogService
|
||||
$data = $this->preProcess('Release', $data, $request);
|
||||
$config = $this->buildQueryConfig($request, 'release');
|
||||
$updated = $this->logicUpdateBy($config, $data);
|
||||
return $this->postProcess('Release', $updated);
|
||||
return $this->postProcess('Release', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,7 +51,7 @@ class InquiryLogService
|
||||
$data = $this->preProcess('Allocation', $data, $request);
|
||||
$config = $this->buildQueryConfig($request, 'allocation');
|
||||
$updated = $this->logicUpdateBy($config, $data);
|
||||
return $this->postProcess('Allocation', $updated);
|
||||
return $this->postProcess('Allocation', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,7 +140,7 @@ class InquiryLogService
|
||||
throw new BusinessException('数据不存在');
|
||||
}
|
||||
if ($info->status != 0) {
|
||||
throw new BusinessException('询单已发布');
|
||||
throw new BusinessException('询单已被发布');
|
||||
}
|
||||
$data['r_time'] = time();
|
||||
$data['status'] = 1;
|
||||
@@ -158,8 +159,29 @@ class InquiryLogService
|
||||
if ($info->status == 2) {
|
||||
throw new BusinessException('询单已被分配');
|
||||
}
|
||||
if ($info->status == 0) {
|
||||
$data['r_time'] = time();
|
||||
}
|
||||
$data['t_time'] = time();
|
||||
$data['status'] = 2;
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布后置处理
|
||||
*/
|
||||
protected function afterServiceRelease($res)
|
||||
{
|
||||
RedisQueueRedis::send('inquiry-to-crm', $res);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分配后置处理
|
||||
*/
|
||||
protected function afterServiceAllocation($res)
|
||||
{
|
||||
RedisQueueRedis::send('inquiry-to-crm', $res);
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace app\queue\redis;
|
||||
|
||||
use app\common\model\CrmCustomer;
|
||||
use app\common\model\InquiryLog;
|
||||
use Webman\RedisQueue\Consumer;
|
||||
use app\common\model\User;
|
||||
use support\exception\BusinessException;
|
||||
|
||||
class InquiryToCrm implements Consumer
|
||||
{
|
||||
// 要消费的队列名
|
||||
public $queue = 'inquiry-to-crm';
|
||||
|
||||
// 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
|
||||
public $connection = 'default';
|
||||
|
||||
// 消费
|
||||
public function consume($data)
|
||||
{
|
||||
try {
|
||||
// 获取询盘信息
|
||||
$info = InquiryLog::where('id', $data['id'])->where('sync_crm', 0)->first();
|
||||
if ($info) {
|
||||
// 获取用户信息
|
||||
$recipient = 0;
|
||||
$corpId = 0;
|
||||
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 - 1;
|
||||
$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;
|
||||
// 修改同步CRM状态
|
||||
$info->sync_crm = 1;
|
||||
// 保存
|
||||
if (!$crmCustomer->save() || !$info->save()) {
|
||||
throw new BusinessException('同步失败~');
|
||||
}
|
||||
}
|
||||
} catch (\Throwable $th) {
|
||||
throw $th;
|
||||
}
|
||||
}
|
||||
|
||||
// 消费失败回调
|
||||
/*
|
||||
$package = [
|
||||
'id' => 1357277951, // 消息ID
|
||||
'time' => 1709170510, // 消息时间
|
||||
'delay' => 0, // 延迟时间
|
||||
'attempts' => 2, // 消费次数
|
||||
'queue' => 'send-mail', // 队列名
|
||||
'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容
|
||||
'max_attempts' => 5, // 最大重试次数
|
||||
'error' => '错误信息' // 错误信息
|
||||
]
|
||||
*/
|
||||
public function onConsumeFailure(\Throwable $e, $package)
|
||||
{
|
||||
echo "consume failure\n";
|
||||
echo $e->getMessage() . "\n";
|
||||
// 无需反序列化
|
||||
var_export($package);
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ return [
|
||||
'count' => getenv('REDIS_QUEUE_PROCESS_COUNT'), // 可以设置多进程同时消费
|
||||
'constructor' => [
|
||||
// 消费者类目录
|
||||
// 'consumer_dir' => app_path() . '/queue/redis'
|
||||
'consumer_dir' => app_path() . '/queue/redis',
|
||||
'consumer_dir' => app_path() . getenv('REDIS_QUEUE_CONSUMER_DIR')
|
||||
]
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user