405 lines
14 KiB
PHP
405 lines
14 KiB
PHP
<?php
|
||
|
||
namespace app\chukebao\controller;
|
||
|
||
use app\chukebao\model\AutoGreetings;
|
||
use library\ResponseHelper;
|
||
use think\Db;
|
||
|
||
class AutoGreetingsController extends BaseController
|
||
{
|
||
|
||
public function getList(){
|
||
$page = $this->request->param('page', 1);
|
||
$limit = $this->request->param('limit', 10);
|
||
$keyword = $this->request->param('keyword', '');
|
||
$is_template = $this->request->param('is_template', 0);
|
||
$userId = $this->getUserInfo('id');
|
||
$companyId = $this->getUserInfo('companyId');
|
||
|
||
|
||
if($is_template == 1){
|
||
$where = [
|
||
['is_template','=',1],
|
||
['isDel' ,'=', 0],
|
||
];
|
||
}else{
|
||
$where = [
|
||
['companyId','=',$companyId],
|
||
['userId' ,'=', $userId],
|
||
['isDel' ,'=', 0],
|
||
];
|
||
}
|
||
|
||
|
||
|
||
|
||
if(!empty($keyword)){
|
||
$where[] = ['name','like','%'.$keyword.'%'];
|
||
}
|
||
|
||
$query = AutoGreetings::where($where);
|
||
$total = $query->count();
|
||
$list = $query->where($where)->page($page,$limit)->order('id desc')->select();
|
||
|
||
|
||
foreach ($list as &$item) {
|
||
$item['trigger'] = json_decode($item['trigger'],true);
|
||
}
|
||
unset($item);
|
||
|
||
return ResponseHelper::success(['list'=>$list,'total'=>$total]);
|
||
}
|
||
|
||
|
||
/**
|
||
* 添加
|
||
* @return \think\response\Json
|
||
* @throws \Exception
|
||
*/
|
||
public function create(){
|
||
$name = $this->request->param('name', '');
|
||
$trigger = $this->request->param('trigger', 0);
|
||
$condition = $this->request->param('condition', '');
|
||
$content = $this->request->param('content', '');
|
||
$level = $this->request->param('level', 0);
|
||
$status = $this->request->param('status', 1);
|
||
$userId = $this->getUserInfo('id');
|
||
$companyId = $this->getUserInfo('companyId');
|
||
|
||
if (empty($name) || empty($trigger) || empty($content)){
|
||
return ResponseHelper::error('参数缺失');
|
||
}
|
||
|
||
if (in_array($trigger,[2,3]) && empty($condition)){
|
||
return ResponseHelper::error('具体条件不能为空');
|
||
}
|
||
|
||
if ($trigger == 2){
|
||
$condition = !empty($condition) ? $condition : [];
|
||
}
|
||
|
||
if ($trigger == 3){
|
||
$condition = explode(',',$condition);
|
||
}
|
||
|
||
|
||
Db::startTrans();
|
||
try {
|
||
$AutoGreetings = new AutoGreetings();
|
||
$AutoGreetings->name = $name;
|
||
$AutoGreetings->trigger = $trigger;
|
||
$AutoGreetings->condition = json_encode($condition,256);
|
||
$AutoGreetings->content = $content;
|
||
$AutoGreetings->level = $level;
|
||
$AutoGreetings->status = $status;
|
||
$AutoGreetings->userId = $userId;
|
||
$AutoGreetings->companyId = $companyId;
|
||
$AutoGreetings->updateTime = time();
|
||
$AutoGreetings->createTime = time();
|
||
$AutoGreetings->save();
|
||
Db::commit();
|
||
return ResponseHelper::success(' ','创建成功');
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
return ResponseHelper::error('创建失败:'.$e->getMessage());
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
/**
|
||
* 详情
|
||
* @return \think\response\Json
|
||
*/
|
||
public function details()
|
||
{
|
||
$id = $this->request->param('id', '');
|
||
$userId = $this->getUserInfo('id');
|
||
$companyId = $this->getUserInfo('companyId');
|
||
if (empty($id)){
|
||
return ResponseHelper::error('参数缺失');
|
||
}
|
||
$data = AutoGreetings::where(['id'=>$id,'isDel' => 0,'userId' => $userId,'companyId' => $companyId])->find();
|
||
if (empty($data)){
|
||
return ResponseHelper::error('该内容已被删除或者不存在');
|
||
}
|
||
|
||
|
||
$data['condition'] = json_decode($data['condition'],true);
|
||
if ($data['trigger'] == 3){
|
||
$data['condition'] = implode(',',$data['condition']);
|
||
}
|
||
|
||
unset($data['createTime'],$data['updateTime'],$data['isDel'],$data['delTime']);
|
||
return ResponseHelper::success($data,'获取成功');
|
||
}
|
||
|
||
/**
|
||
* 删除
|
||
* @return \think\response\Json
|
||
*/
|
||
public function del()
|
||
{
|
||
$id = $this->request->param('id', '');
|
||
$userId = $this->getUserInfo('id');
|
||
$companyId = $this->getUserInfo('companyId');
|
||
if (empty($id)){
|
||
return ResponseHelper::error('参数缺失');
|
||
}
|
||
$data = AutoGreetings::where(['id'=>$id,'isDel' => 0,'userId' => $userId,'companyId' => $companyId])->find();
|
||
if (empty($data)){
|
||
return ResponseHelper::error('该已被删除或者不存在');
|
||
}
|
||
Db::startTrans();
|
||
try {
|
||
$data->isDel = 1;
|
||
$data->delTime = time();
|
||
$data->save();
|
||
Db::commit();
|
||
return ResponseHelper::success('','删除成功');
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
return ResponseHelper::error('删除失败:'.$e->getMessage());
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 更新
|
||
* @return \think\response\Json
|
||
* @throws \Exception
|
||
*/
|
||
public function update(){
|
||
$id = $this->request->param('id', '');
|
||
$name = $this->request->param('name', '');
|
||
$trigger = $this->request->param('trigger', 0);
|
||
$condition = $this->request->param('condition', []);
|
||
$content = $this->request->param('content', '');
|
||
$level = $this->request->param('level', 0);
|
||
$status = $this->request->param('status', 1);
|
||
$userId = $this->getUserInfo('id');
|
||
$companyId = $this->getUserInfo('companyId');
|
||
|
||
if (empty($id) || empty($name) || empty($trigger) || empty($content)){
|
||
return ResponseHelper::error('参数缺失');
|
||
}
|
||
|
||
if (in_array($trigger,[2,3]) && empty($condition)){
|
||
return ResponseHelper::error('具体条件不能为空');
|
||
}
|
||
|
||
if ($trigger == 2){
|
||
$condition = !empty($condition) ? $condition : [];
|
||
}
|
||
|
||
if ($trigger == 3){
|
||
$condition = explode(',',$condition);
|
||
}
|
||
|
||
|
||
$query = AutoGreetings::where(['id'=>$id,'isDel' => 0,'userId' => $userId,'companyId' => $companyId])->find();
|
||
if (empty($query)){
|
||
return ResponseHelper::error('该内容已被删除或者不存在');
|
||
}
|
||
Db::startTrans();
|
||
try {
|
||
$query->name = $name;
|
||
$query->trigger = $trigger;
|
||
$query->condition = !empty($condition) ? json_encode($condition,256) : json_encode([]);
|
||
$query->content = $content;
|
||
$query->level = $level;
|
||
$query->status = $status;
|
||
$query->userId = $userId;
|
||
$query->companyId = $companyId;
|
||
$query->updateTime = time();
|
||
$query->createTime = time();
|
||
$query->save();
|
||
Db::commit();
|
||
return ResponseHelper::success(' ','修改成功');
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
return ResponseHelper::error('修改失败:'.$e->getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 修改状态
|
||
* @return \think\response\Json
|
||
* @throws \Exception
|
||
*/
|
||
public function setStatus(){
|
||
$id = $this->request->param('id', '');
|
||
$userId = $this->getUserInfo('id');
|
||
$companyId = $this->getUserInfo('companyId');
|
||
|
||
if (empty($id)){
|
||
return ResponseHelper::error('参数缺失');
|
||
}
|
||
|
||
$query = AutoGreetings::where(['id'=>$id,'isDel' => 0,'userId' => $userId,'companyId' => $companyId])->find();
|
||
if (empty($query)){
|
||
return ResponseHelper::error('该内容已被删除或者不存在');
|
||
}
|
||
Db::startTrans();
|
||
try {
|
||
$query->status = !empty($query['status']) ? 0 : 1;
|
||
$query->updateTime = time();
|
||
$query->save();
|
||
Db::commit();
|
||
return ResponseHelper::success(' ','修改成功');
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
return ResponseHelper::error('修改失败:'.$e->getMessage());
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
/**
|
||
* 拷贝
|
||
* @return \think\response\Json
|
||
* @throws \Exception
|
||
*/
|
||
public function copy(){
|
||
$id = $this->request->param('id', '');
|
||
$userId = $this->getUserInfo('id');
|
||
$companyId = $this->getUserInfo('companyId');
|
||
|
||
if (empty($id) ){
|
||
return ResponseHelper::error('参数缺失');
|
||
}
|
||
|
||
$data = AutoGreetings::where(['id'=>$id,'isDel' => 0,'userId' => $userId,'companyId' => $companyId])->find();
|
||
if (empty($data)){
|
||
return ResponseHelper::error('该内容已被删除或者不存在');
|
||
}
|
||
Db::startTrans();
|
||
try {
|
||
$query = new AutoGreetings();
|
||
$query->name = $data['name'] . '_copy';
|
||
$query->trigger = $data['trigger'];
|
||
$query->condition = $data['condition'];
|
||
$query->content = $data['content'];
|
||
$query->level = $data['level'];
|
||
$query->status = $data['status'];
|
||
$query->userId = $userId;
|
||
$query->companyId = $companyId;
|
||
$query->updateTime = time();
|
||
$query->createTime = time();
|
||
$query->save();
|
||
Db::commit();
|
||
return ResponseHelper::success(' ','拷贝成功');
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
return ResponseHelper::error('拷贝失败:'.$e->getMessage());
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 统计概览
|
||
* - 总触发次数
|
||
* - 活跃规则(近一个月)
|
||
* - 发送成功率
|
||
* - 平均响应时间(秒)
|
||
* - 规则效果排行(按发送次数降序、平均响应时间升序)
|
||
* @return \think\response\Json
|
||
*/
|
||
public function stats()
|
||
{
|
||
$companyId = $this->getUserInfo('companyId');
|
||
$userId = $this->getUserInfo('id');
|
||
|
||
$start30d = time() - 30 * 24 * 3600;
|
||
|
||
try {
|
||
// 公司维度(用于除排行外的统计)
|
||
$companyWhere = [
|
||
['companyId', '=', $companyId],
|
||
];
|
||
// 排行维度(限定个人)
|
||
$rankingWhere = [
|
||
['companyId', '=', $companyId],
|
||
['userId', '=', $userId],
|
||
];
|
||
|
||
// 1) 总触发次数
|
||
$totalTriggers = Db::name('kf_auto_greetings_record')
|
||
->where($companyWhere)
|
||
->count();
|
||
|
||
// 2) 近30天活跃规则(仅返回数量,按公司维度,distinct autoId)
|
||
$activeRulesCount = Db::name('kf_auto_greetings_record')
|
||
->where($companyWhere)
|
||
->where('createTime', '>=', $start30d)
|
||
->distinct(true)
|
||
->count('autoId');
|
||
|
||
// 3) 发送成功率
|
||
$sendCount = Db::name('kf_auto_greetings_record')
|
||
->where($companyWhere)
|
||
->where('isSend', '=', 1)
|
||
->count();
|
||
// 成功率:百分比,保留两位小数
|
||
$sendRate = $totalTriggers > 0 ? round(($sendCount * 100) / $totalTriggers, 2) : 0.00;
|
||
|
||
// 4) 平均响应时间(receiveTime - sendTime,单位秒)
|
||
$avgResponse = Db::name('kf_auto_greetings_record')
|
||
->where($companyWhere)
|
||
->whereRaw('sendTime IS NOT NULL AND receiveTime IS NOT NULL AND receiveTime >= sendTime')
|
||
->avg(Db::raw('(receiveTime - sendTime)'));
|
||
$avgResponse = $avgResponse ? (int)round($avgResponse) : 0;
|
||
|
||
// 5) 规则效果排行(按发送次数降序、平均响应时间升序)
|
||
$ranking = Db::name('kf_auto_greetings_record')
|
||
->where($rankingWhere)
|
||
->field([
|
||
'autoId AS id',
|
||
'COUNT(*) AS totalCount',
|
||
'SUM(CASE WHEN isSend = 1 THEN 1 ELSE 0 END) AS sendCount',
|
||
'AVG(CASE WHEN sendTime IS NOT NULL AND receiveTime IS NOT NULL AND receiveTime >= sendTime THEN (receiveTime - sendTime) END) AS avgResp'
|
||
])
|
||
->group('autoId')
|
||
->orderRaw('sendCount DESC, avgResp ASC')
|
||
->limit(20)
|
||
->select();
|
||
|
||
// 附加规则名称(如存在)
|
||
$autoIds = array_values(array_unique(array_column($ranking, 'id')));
|
||
$autoIdToRule = [];
|
||
if (!empty($autoIds)) {
|
||
$rules = AutoGreetings::where([['id', 'in', $autoIds]])
|
||
->field('id,name,trigger')
|
||
->select();
|
||
foreach ($rules as $rule) {
|
||
$autoIdToRule[$rule['id']] = [
|
||
'name' => $rule['name'],
|
||
'trigger' => $rule['trigger'],
|
||
];
|
||
}
|
||
}
|
||
|
||
foreach ($ranking as &$row) {
|
||
$row['avgResp'] = isset($row['avgResp']) && $row['avgResp'] !== null ? (int)round($row['avgResp']) : 0;
|
||
// 百分比,两位小数
|
||
$row['sendRate'] = ($row['totalCount'] ?? 0) > 0 ? round((($row['sendCount'] ?? 0) * 100) / $row['totalCount'], 2) : 0.00;
|
||
$row['name'] = $autoIdToRule[$row['id']]['name'] ?? '';
|
||
$row['trigger'] = $autoIdToRule[$row['id']]['trigger'] ?? null;
|
||
}
|
||
unset($row);
|
||
|
||
return ResponseHelper::success([
|
||
'totalTriggers' => (int)$totalTriggers,
|
||
'activeRules' => (int)$activeRulesCount,
|
||
'sendSuccessRate' => $sendRate,
|
||
'avgResponseSeconds' => $avgResponse,
|
||
'ruleRanking' => $ranking,
|
||
], '统计成功');
|
||
} catch (\Exception $e) {
|
||
return ResponseHelper::error('统计失败:' . $e->getMessage());
|
||
}
|
||
}
|
||
} |