Files
cunkebao_v3/Server/application/cunkebao/controller/StatsController.php
2025-07-17 17:15:14 +08:00

199 lines
6.7 KiB
PHP

<?php
namespace app\cunkebao\controller;
use think\Db;
use think\Controller;
class StatsController extends Controller
{
const WEEK = [
0 => '周日',
1 => '周一',
2 => '周二',
3 => '周三',
4 => '周四',
5 => '周五',
6 => '周六',
];
/**
* 基础信息
* @return \think\response\Json
*/
public function baseInfoStats()
{
$deviceNum = Db::name('device')->where(['companyId' => $this->request->userInfo['companyId'], 'deleteTime' => 0])->count();
$wechatNum = Db::name('wechat_customer')->where(['companyId' => $this->request->userInfo['companyId']])->count();
$aliveWechatNum = Db::name('wechat_customer')->alias('wc')
->join('device_wechat_login dwl', 'wc.wechatId = dwl.wechatId')
->where(['wc.companyId' => $this->request->userInfo['companyId'], 'dwl.alive' => 1])
->group('wc.wechatId')
->count();
$data = [
'deviceNum' => $deviceNum,
'wechatNum' => $wechatNum,
'aliveWechatNum' => $aliveWechatNum,
];
return successJson($data, '获取成功');
}
/**
* 场景获客统计
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function planStats()
{
$num = $this->request->param('num', 4);
$planScene = Db::name('plan_scene')
->field('id,name,image')
->where(['status' => 1])
->order('sort DESC')
->page(1, $num)
->select();
foreach ($planScene as &$v) {
$allNum = Db::name('customer_acquisition_task')->alias('ac')
->join('task_customer tc', 'tc.task_id = ac.id')
->where(['ac.sceneId' => $v['id'], 'ac.companyId' => $this->request->userInfo['companyId'], 'ac.deleteTime' => 0])
->count();
$addNum = Db::name('customer_acquisition_task')->alias('ac')
->join('task_customer tc', 'tc.task_id = ac.id')
->where(['ac.sceneId' => $v['id'], 'ac.companyId' => $this->request->userInfo['companyId'], 'ac.deleteTime' => 0])
->whereIn('tc.status', [1, 2, 3, 4])
->count();
$passNum = Db::name('customer_acquisition_task')->alias('ac')
->join('task_customer tc', 'tc.task_id = ac.id')
->where(['ac.sceneId' => $v['id'], 'ac.companyId' => $this->request->userInfo['companyId'], 'ac.deleteTime' => 0])
->whereIn('tc.status', [4])
->count();
$v['allNum'] = $allNum;
$v['addNum'] = $addNum;
$v['passNum'] = $passNum;
}
unset($v);
return successJson($planScene, '获取成功');
}
public function todayStats()
{
$date = date('Y-m-d',time());
$start = strtotime($date . ' 00:00:00');
$end = strtotime($date . ' 23:59:59');
$companyId = $this->request->userInfo['companyId'];
$momentsNum = Db::name('workbench')->alias('w')
->join('workbench_moments_sync_item wi', 'w.id = wi.workbenchId')
->where(['w.companyId' => $companyId])
->where('wi.createTime', 'between', [$start, $end])
->count();
$groupPushNum = Db::name('workbench')->alias('w')
->join('workbench_group_push_item wi', 'w.id = wi.workbenchId')
->where(['w.companyId' => $companyId])
->where('wi.createTime', 'between', [$start, $end])
->count();
$addNum = Db::name('customer_acquisition_task')->alias('ac')
->join('task_customer tc', 'tc.task_id = ac.id')
->where(['ac.companyId' => $companyId, 'ac.deleteTime' => 0])
->where('tc.updateTime', 'between', [$start, $end])
->whereIn('tc.status', [1, 2, 3, 4])
->count();
// 通过量
$passNum = Db::name('customer_acquisition_task')->alias('ac')
->join('task_customer tc', 'tc.task_id = ac.id')
->where(['ac.companyId' => $companyId, 'ac.deleteTime' => 0])
->where('tc.updateTime', 'between', [$start, $end])
->whereIn('tc.status', [4])
->count();
if (!empty($passNum)){
$passRate = number_format(($addNum / $passNum) * 100,2) ;
}else{
$passRate = '0%';
}
$sysActive = '90%';
$data = [
'momentsNum' => $momentsNum,
'groupPushNum' => $groupPushNum,
'addNum' => $addNum,
'passNum' => $passNum,
'passRate' => $passRate,
'sysActive' => $sysActive,
];
return successJson($data, '获取成功');
}
/**
* 近7天获客统计
* @return \think\response\Json
*/
public function customerAcquisitionStats7Days()
{
$companyId = $this->request->userInfo['companyId'];
$days = 7;
$dates = [];
$allNum = [];
$addNum = [];
$passNum = [];
for ($i = $days - 1; $i >= 0; $i--) {
$date = date('Y-m-d', strtotime("-$i day"));
$start = strtotime($date . ' 00:00:00');
$end = strtotime($date . ' 23:59:59');
// 获客总量
$allNum[] = Db::name('customer_acquisition_task')->alias('ac')
->join('task_customer tc', 'tc.task_id = ac.id')
->where(['ac.companyId' => $companyId, 'ac.deleteTime' => 0])
->where('tc.createTime', 'between', [$start, $end])
->count();
// 添加量
$addNum[] = Db::name('customer_acquisition_task')->alias('ac')
->join('task_customer tc', 'tc.task_id = ac.id')
->where(['ac.companyId' => $companyId, 'ac.deleteTime' => 0])
->where('tc.updateTime', 'between', [$start, $end])
->whereIn('tc.status', [1, 2, 3, 4])
->count();
// 通过量
$passNum[] = Db::name('customer_acquisition_task')->alias('ac')
->join('task_customer tc', 'tc.task_id = ac.id')
->where(['ac.companyId' => $companyId, 'ac.deleteTime' => 0])
->where('tc.updateTime', 'between', [$start, $end])
->whereIn('tc.status', [4])
->count();
$week = date("w", strtotime($date));
$dates[] = self::WEEK[$week];
}
$data = [
'date' => $dates,
'allNum' => $allNum,
'addNum' => $addNum,
'passNum' => $passNum,
];
return successJson($data, '获取成功');
}
}