Files
fdesk/app/inquiry/service/InquiryLogService.php
T

187 lines
5.3 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;
}
/**
* 发布
*/
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);
}
/**
* 构建查询条件
*/
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']['country'][] = 'cn_addr';
$config['with']['country'][] = 'steering';
$config['with']['enter'][] = 'name';
$config['with']['recipient'][] = 'name';
$config['with']['channel'][] = 'name';
break;
case 'show':
case 'update':
case 'destroy':
case 'release':
case 'allocation':
$config['where']['id'] = $p['id'];
case 'store':
break;
}
return $config;
}
/**
* 添加前置处理
*/
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']);
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;
}
/**
* 发布后置处理
*/
protected function afterServiceRelease($res)
{
RedisQueueRedis::send('inquiry-to-crm', $res);
return $res;
}
/**
* 分配后置处理
*/
protected function afterServiceAllocation($res)
{
RedisQueueRedis::send('inquiry-to-crm', $res);
return $res;
}
}