377 lines
11 KiB
PHP
377 lines
11 KiB
PHP
<?php
|
|
|
|
namespace app\inquiry\service;
|
|
|
|
use app\common\validate\InquiryLogValidate;
|
|
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
|
|
{
|
|
|
|
use CrudServiceTrait;
|
|
|
|
/**
|
|
* 指定当前要操作的模型
|
|
*/
|
|
protected string $modelClass = InquiryLog::class;
|
|
|
|
/**
|
|
* 验证器
|
|
*/
|
|
protected $validator = InquiryLogValidate::class;
|
|
|
|
/**
|
|
* 获取验证器
|
|
*/
|
|
public function getValidator()
|
|
{
|
|
return new $this->validator;
|
|
}
|
|
|
|
|
|
/**
|
|
* 构建查询条件
|
|
*/
|
|
protected function buildQueryConfig(Request $request, string $scene = 'index'): array
|
|
{
|
|
$p = $request->all();
|
|
|
|
$config = [];
|
|
$config['model'] = $this->modelClass;
|
|
switch ($scene) {
|
|
case 'index':
|
|
// ====== ====== 排序 ====== ======
|
|
$config['order']['id'] = 'desc';
|
|
|
|
// ====== ====== 筛选 ====== ======
|
|
// 渠道
|
|
if (isset($p['channel']) && $p['channel']) {
|
|
$config['where']['channel'] = $p['channel'];
|
|
}
|
|
// 国家
|
|
if (isset($p['country']) && $p['country']) {
|
|
$config['where']['country'] = $p['country'];
|
|
}
|
|
// 时间
|
|
if (isset($p['time']) && $p['time']) {
|
|
$time = explode(' - ', $p['time']);
|
|
$config['where']['created_at'] = [
|
|
'between', [strtotime($time[0]), strtotime($time[1])]
|
|
];
|
|
}
|
|
// 状态
|
|
if (isset($p['status']) && in_array($p['status'], [0,1,2])) {
|
|
$config['where']['status'] = $p['status'];
|
|
}
|
|
// 关键字
|
|
if (isset($p['keyword']) && $p['keyword']) {
|
|
$k = '%' . $p['keyword'] . '%';
|
|
foreach (['order_no','email','tel','whatsapp','contacts'] as $col) {
|
|
$config['orWhere'][] = [$col, 'like', $k];
|
|
}
|
|
}
|
|
|
|
// ====== ====== 关联 ====== ======
|
|
$config['with']['countryInfo'][] = 'cn_addr';
|
|
$config['with']['countryInfo'][] = 'steering';
|
|
$config['with']['enter'][] = 'name';
|
|
$config['with']['recipientInfo'][] = 'name';
|
|
$config['with']['channelInfo'][] = 'name';
|
|
break;
|
|
case 'show':
|
|
case 'update':
|
|
case 'destroy':
|
|
case 'release':
|
|
case 'allocation':
|
|
case 'reAllocation':
|
|
$config['where']['id'] = $p['id'];
|
|
case 'store':
|
|
break;
|
|
}
|
|
|
|
return $config;
|
|
}
|
|
|
|
/**
|
|
* 发布
|
|
*/
|
|
public function serviceRelease(Request $request, array $data = [])
|
|
{
|
|
$data = $this->preProcess('Release', $data, $request);
|
|
$config = $this->buildQueryConfig($request, 'release');
|
|
$updated = $this->logicUpdateBy($config, $data);
|
|
return $this->postProcess('Release', $data);
|
|
}
|
|
|
|
/**
|
|
* 分配
|
|
*/
|
|
public function serviceAllocation(Request $request, array $data = [])
|
|
{
|
|
$data = $this->preProcess('Allocation', $data, $request);
|
|
$config = $this->buildQueryConfig($request, 'allocation');
|
|
$updated = $this->logicUpdateBy($config, $data);
|
|
return $this->postProcess('Allocation', $data);
|
|
}
|
|
|
|
/**
|
|
* 重新分配
|
|
*/
|
|
public function serviceReAllocation(Request $request, array $data = [])
|
|
{
|
|
$data = $this->preProcess('ReAllocation', $data, $request);
|
|
$config = $this->buildQueryConfig($request, 'reAllocation');
|
|
$updated = $this->logicUpdateBy($config, $data);
|
|
return $this->postProcess('ReAllocation', $data);
|
|
}
|
|
|
|
// ====== 前置处理 ======
|
|
/**
|
|
* 添加前置处理
|
|
*/
|
|
public function beforeServiceStore($data, Request $request)
|
|
{
|
|
$data['order_no'] = orderNum();
|
|
$data['enter_user_id'] = $request->user->uid;
|
|
// $data['recipient'] = 0;
|
|
$data['email'] = trim($data["email"]);
|
|
$data['tel'] = trim($data['tel']);
|
|
$data['whatsapp'] = trim($data['whatsapp']);
|
|
|
|
// 将OSS直连地址替换为代理地址
|
|
$ossDomain = 'https://vehicle-album.oss-cn-beijing.aliyuncs.com/';
|
|
$proxyPath = getenv('MAIN_DOMAIN').'/proxy/ossurl?file=';
|
|
|
|
$data['content'] = str_replace(
|
|
'?Expires=',
|
|
'&Expires=',
|
|
$data['content']
|
|
);
|
|
$data['content'] = str_replace(
|
|
$ossDomain,
|
|
$proxyPath,
|
|
$data['content']
|
|
);
|
|
|
|
// 如果添加时填写了接单人
|
|
if (isset($data['recipient']) && $data['recipient']) {
|
|
$data['r_time'] = time();
|
|
$data['t_time'] = time();
|
|
$data['status'] = 2;
|
|
} else {
|
|
$data['recipient'] = 0;
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 更新前置处理
|
|
*/
|
|
public function beforeServiceUpdate($data, Request $request)
|
|
{
|
|
$info = $this->findByPk($request->post('id'));
|
|
if (!$info) {
|
|
throw new BusinessException('数据不存在');
|
|
}
|
|
|
|
// 处理富文本
|
|
if (md5(normalizeRichText($data['content'])) !== md5(normalizeRichText($info->content))) {
|
|
$ossDomain = 'https://vehicle-album.oss-cn-beijing.aliyuncs.com/';
|
|
$proxyPath = getenv('MAIN_DOMAIN').'/proxy/ossurl?file=';
|
|
|
|
$data['content'] = str_replace(
|
|
'?Expires=',
|
|
'&Expires=',
|
|
$data['content']
|
|
);
|
|
$data['content'] = str_replace(
|
|
$ossDomain,
|
|
$proxyPath,
|
|
$data['content']
|
|
);
|
|
}
|
|
|
|
// 如果更新时操作了跟单人
|
|
if ($info->recipient) {
|
|
$info->recipient == $data['recipient'] ?: $data['t_time'] = time();
|
|
} else {
|
|
$data['r_time'] = time();
|
|
$data['t_time'] = time();
|
|
$data['status'] = 2;
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 发布前置处理
|
|
*/
|
|
public function beforeServiceRelease($data, Request $request)
|
|
{
|
|
$info = $this->findByPk($request->post('id'));
|
|
if (!$info) {
|
|
throw new BusinessException('数据不存在');
|
|
}
|
|
if ($info->status != 0) {
|
|
throw new BusinessException('询单已被发布');
|
|
}
|
|
$data['r_time'] = time();
|
|
$data['status'] = 1;
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 分配前置处理
|
|
*/
|
|
public function beforeServiceAllocation($data, Request $request)
|
|
{
|
|
$info = $this->findByPk($request->post('id'));
|
|
if (!$info) {
|
|
throw new BusinessException('数据不存在');
|
|
}
|
|
if ($info->status == 2) {
|
|
throw new BusinessException('询单已被分配');
|
|
}
|
|
if ($info->status == 0) {
|
|
$data['r_time'] = time();
|
|
}
|
|
$data['t_time'] = time();
|
|
$data['status'] = 2;
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 重新分配前置处理
|
|
*/
|
|
public function beforeServiceReAllocation($data, Request $request)
|
|
{
|
|
$info = $this->findByPk($request->post('id'));
|
|
if (!$info) {
|
|
throw new BusinessException('数据不存在');
|
|
}
|
|
$data['t_time'] = time();
|
|
return $data;
|
|
}
|
|
|
|
// ====== 后置处理 ======
|
|
/**
|
|
* 发布后置处理
|
|
*/
|
|
protected function afterServiceRelease($res)
|
|
{
|
|
RedisQueueRedis::send('inquiry-to-crm-new', $res);
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 分配后置处理
|
|
*/
|
|
protected function afterServiceAllocation($res)
|
|
{
|
|
RedisQueueRedis::send('inquiry-to-crm-new', $res);
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 添加后置处理
|
|
*/
|
|
protected function afterServiceStore($res)
|
|
{
|
|
RedisQueueRedis::send('inquiry-to-crm-new', $res);
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 更新后置处理
|
|
*/
|
|
protected function afterServiceUpdate($res)
|
|
{
|
|
if ($res['recipient']) {
|
|
RedisQueueRedis::send('inquiry-to-crm-new', $res);
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 重新分配后置处理
|
|
*/
|
|
protected function afterServiceReAllocation($res)
|
|
{
|
|
RedisQueueRedis::send('inquiry-to-crm-new', $res);
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 获取询单ById
|
|
* @param int|string $id
|
|
* @param string $orderSn
|
|
* @return Model|null
|
|
*/
|
|
public function getInquiryLogBy(int|string $id)
|
|
{
|
|
$config = [];
|
|
$config['model'] = $this->modelClass;
|
|
$config['with']['countryInfo'][] = 'cn_addr';
|
|
$config['with']['countryInfo'][] = 'steering';
|
|
$config['with']['enter'][] = 'name';
|
|
$config['with']['recipientInfo'][] = 'name';
|
|
$config['with']['channelInfo'][] = 'name';
|
|
$config['orWhere'][] = ['id', '=', $id];
|
|
$config['orWhere'][] = ['order_no', '=', $id];
|
|
return $this->logicShow($config);
|
|
}
|
|
|
|
/**
|
|
* 获取等级名称
|
|
* @param $id
|
|
* @return string
|
|
*/
|
|
public function qualityName($id)
|
|
{
|
|
if (!$id) {
|
|
return '🤍 银牌商机';
|
|
}
|
|
$quality = [
|
|
1 => '💛金牌商机',
|
|
2 => '🤍银牌商机',
|
|
3 => '🧡铜牌商机'
|
|
];
|
|
return $quality[$id];
|
|
}
|
|
|
|
/**
|
|
* 获取内容(发送邮箱用)
|
|
* @param int $id
|
|
* @return array
|
|
*/
|
|
public function getContent(int $id)
|
|
{
|
|
$relation = ['enter', 'recipientInfo', 'channelInfo', 'countryInfo'];
|
|
$info = InquiryLog::with($relation)->find($id);
|
|
|
|
$enterName = $info->enter ? $info->enter->name : ' - ';
|
|
$recipientName = $info->recipientInfo ? $info->recipientInfo->name : ' - ';
|
|
$channelName = $info->channelInfo ? $info->channelInfo->name : ' - ';
|
|
$countryName = $info->countryInfo ? $info->countryInfo->cn_addr : ' - ';
|
|
|
|
$data = [
|
|
'order_no' => $info->order_no && $info->order_no != '' ? $info->order_no : ' - ',
|
|
'enter_user_id' => $enterName,
|
|
'recipient' => $recipientName,
|
|
'quality' => (new \app\inquiry\service\InquiryLogService())->qualityName($info->quality),
|
|
'channel' => $channelName,
|
|
'email' => $info->email && $info->email != '' ? $info->email : ' - ',
|
|
'tel' => $info->tel && $info->tel != '' ? $info->tel : ' - ',
|
|
'whatsapp' => $info->whatsapp && $info->whatsapp != '' ? $info->whatsapp : ' - ',
|
|
'country' => $countryName,
|
|
'contacts' => $info->contacts && $info->contacts != '' ? $info->contacts : ' - ',
|
|
'content' => $info->content && $info->content != '' ? $info->content : ' - ',
|
|
'r_time' => $info->r_time ? date('Y-m-d H:i:s', $info->r_time) : ' - ',
|
|
't_time' => $info->t_time ? date('Y-m-d H:i:s', $info->t_time) : ' - '
|
|
];
|
|
return $data;
|
|
}
|
|
} |