代码优化
This commit is contained in:
@@ -25,13 +25,18 @@ class StatsController extends Controller
|
||||
*/
|
||||
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();
|
||||
|
||||
$where = [
|
||||
['departmentId','=',$this->request->userInfo['companyId']]
|
||||
];
|
||||
if (!empty($this->request->userInfo['isAdmin'])){
|
||||
$where[] = ['id','=',$this->request->userInfo['s2_accountId']];
|
||||
}
|
||||
$accounts = Db::table('s2_company_account')->where($where)->column('id');
|
||||
|
||||
$deviceNum = Db::table('s2_device')->whereIn('currentAccountId',$accounts)->where(['isDeleted' => 0])->count();
|
||||
$wechatNum = Db::table('s2_wechat_account')->whereIn('deviceAccountId',$accounts)->count();
|
||||
$aliveWechatNum = Db::table('s2_wechat_account')->whereIn('deviceAccountId',$accounts)->where(['wechatAlive' => 1])->count();
|
||||
$data = [
|
||||
'deviceNum' => $deviceNum,
|
||||
'wechatNum' => $wechatNum,
|
||||
@@ -58,29 +63,50 @@ class StatsController extends Controller
|
||||
->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;
|
||||
if (empty($planScene)) {
|
||||
return successJson([], '获取成功');
|
||||
}
|
||||
unset($v);
|
||||
|
||||
$sceneIds = array_column($planScene, 'id');
|
||||
$companyId = $this->request->userInfo['companyId'];
|
||||
|
||||
$stats = Db::name('customer_acquisition_task')->alias('ac')
|
||||
->join('task_customer tc', 'tc.task_id = ac.id')
|
||||
->where([
|
||||
['ac.companyId', '=', $companyId],
|
||||
['ac.deleteTime', '=', 0],
|
||||
['ac.sceneId', 'in', $sceneIds],
|
||||
])
|
||||
->field([
|
||||
'ac.sceneId',
|
||||
Db::raw('COUNT(1) as allNum'),
|
||||
Db::raw("SUM(CASE WHEN tc.status IN (1,2,3,4) THEN 1 ELSE 0 END) as addNum"),
|
||||
Db::raw("SUM(CASE WHEN tc.status = 4 THEN 1 ELSE 0 END) as passNum"),
|
||||
])
|
||||
->group('ac.sceneId')
|
||||
->select();
|
||||
|
||||
$statsMap = [];
|
||||
foreach ($stats as $row) {
|
||||
$sceneId = is_array($row) ? ($row['sceneId'] ?? 0) : ($row->sceneId ?? 0);
|
||||
if (!$sceneId) {
|
||||
continue;
|
||||
}
|
||||
$statsMap[$sceneId] = [
|
||||
'allNum' => (int)(is_array($row) ? ($row['allNum'] ?? 0) : ($row->allNum ?? 0)),
|
||||
'addNum' => (int)(is_array($row) ? ($row['addNum'] ?? 0) : ($row->addNum ?? 0)),
|
||||
'passNum' => (int)(is_array($row) ? ($row['passNum'] ?? 0) : ($row->passNum ?? 0)),
|
||||
];
|
||||
}
|
||||
|
||||
foreach ($planScene as &$item) {
|
||||
$sceneStats = $statsMap[$item['id']] ?? ['allNum' => 0, 'addNum' => 0, 'passNum' => 0];
|
||||
$item['allNum'] = $sceneStats['allNum'];
|
||||
$item['addNum'] = $sceneStats['addNum'];
|
||||
$item['passNum'] = $sceneStats['passNum'];
|
||||
}
|
||||
unset($item);
|
||||
|
||||
return successJson($planScene, '获取成功');
|
||||
}
|
||||
|
||||
@@ -150,44 +176,62 @@ class StatsController extends Controller
|
||||
$companyId = $this->request->userInfo['companyId'];
|
||||
$days = 7;
|
||||
|
||||
$dates = [];
|
||||
$endTime = strtotime(date('Y-m-d 23:59:59'));
|
||||
$startTime = strtotime(date('Y-m-d 00:00:00', strtotime('-' . ($days - 1) . ' day')));
|
||||
|
||||
$dateMap = [];
|
||||
$dateLabels = [];
|
||||
for ($i = 0; $i < $days; $i++) {
|
||||
$currentDate = date('Y-m-d', strtotime("-" . ($days - 1 - $i) . " day"));
|
||||
$weekIndex = date("w", strtotime($currentDate));
|
||||
$dateMap[$currentDate] = self::WEEK[$weekIndex];
|
||||
$dateLabels[] = self::WEEK[$weekIndex];
|
||||
}
|
||||
|
||||
$baseWhere = [
|
||||
['ac.companyId', '=', $companyId],
|
||||
['ac.deleteTime', '=', 0],
|
||||
];
|
||||
|
||||
$fetchCounts = function (string $timeField, array $status = []) use ($baseWhere, $startTime, $endTime) {
|
||||
$query = Db::name('customer_acquisition_task')->alias('ac')
|
||||
->join('task_customer tc', 'tc.task_id = ac.id')
|
||||
->where($baseWhere)
|
||||
->whereBetween('tc.' . $timeField, [$startTime, $endTime]);
|
||||
if (!empty($status)) {
|
||||
$query->whereIn('tc.status', $status);
|
||||
}
|
||||
$rows = $query->field([
|
||||
"FROM_UNIXTIME(tc.{$timeField}, '%Y-%m-%d')" => 'day',
|
||||
'COUNT(1)' => 'total'
|
||||
])->group('day')->select();
|
||||
|
||||
$result = [];
|
||||
foreach ($rows as $row) {
|
||||
$day = is_array($row) ? ($row['day'] ?? '') : ($row->day ?? '');
|
||||
$total = (int)(is_array($row) ? ($row['total'] ?? 0) : ($row->total ?? 0));
|
||||
if ($day) {
|
||||
$result[$day] = $total;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
};
|
||||
|
||||
$allNumDict = $fetchCounts('createTime');
|
||||
$addNumDict = $fetchCounts('updateTime', [1, 2, 3, 4]);
|
||||
$passNumDict = $fetchCounts('updateTime', [4]);
|
||||
|
||||
$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];
|
||||
foreach (array_keys($dateMap) as $dateKey) {
|
||||
$allNum[] = $allNumDict[$dateKey] ?? 0;
|
||||
$addNum[] = $addNumDict[$dateKey] ?? 0;
|
||||
$passNum[] = $passNumDict[$dateKey] ?? 0;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'date' => $dates,
|
||||
'date' => $dateLabels,
|
||||
'allNum' => $allNum,
|
||||
'addNum' => $addNum,
|
||||
'passNum' => $passNum,
|
||||
|
||||
Reference in New Issue
Block a user