CustomerVisit.php
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace App\Models\CustomerVisit;
use App\Models\Base;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class CustomerVisit extends Base
{
use HasFactory;
protected $table = 'gl_customer_visit';
const LIMIT = 15;
/** @var int PC端 */
const TERMINAL_PC = 1;
/** @var int 移动端 */
const TERMINAL_MOBILE = 2;
public function queryField($field, $action = 'count', $whereDay = false)
{
$query = $this->query()
->selectRaw("{$action}({$field}) as count")
->whereYear('updated_date', '=', date("Y"))
->whereMonth('updated_date', '=', date("m"));
if ($whereDay) {
$query->whereDay('updated_date', '=', date("d"));
}
return (int)$query->value('count') ?? 0;
}
public function queryTerminal($field, $action = self::TERMINAL_PC)
{
return (int)$this->query()
->selectRaw("count(*) as count")
->where($field, '=', $action)
->whereYear('updated_date', '=', date("Y"))
->whereMonth('updated_date', '=', date("m"))
->value('count') ?? 0;
}
public function getRankingData($field, $limit = self::LIMIT)
{
return $this->query()
->selectRaw('count(*) as count, ' . $field . ' as request')
->whereYear('updated_date', '=', date("Y"))
->whereMonth('updated_date', '=', date("m"))
->groupBy($field)
->orderBy('count', 'desc')
->limit($limit)
->get();
}
public function getSum($field)
{
return $this->queryField($field, 'sum');
}
public function getCount($field)
{
return $this->queryField($field);
}
public function returnLists(Collection $data, int $limit = self::LIMIT)
{
$lists = $data->toArray();
$lists = $lists ? array_slice($lists, 0, $limit) : [];
$count = $data->count();
return compact('lists', 'count');
}
/**
* 查询当月流量的浏览量
* @return int
*/
public function getMonthPVCount()
{
return $this->getSum('depth');
}
/**
* 查询当月流量的访客量
* @return int
*/
public function getMonthIPCount()
{
return $this->getCount('ip');
}
/**
* 查询当月TOP15访问来源
* @param int $limit
* @return array
*/
public function getUrlCount(int $limit = self::LIMIT)
{
return $this->getRankingData('referrer_url', $limit);
}
/**
* 查询当月TOP15访问国家来源
* @param int $limit
* @return array
*/
public function getCountryCount(int $limit = self::LIMIT)
{
return $this->getRankingData('country', $limit);
}
/**
* 查询当月TOP15受访页面
* @param int $limit
* @return array
*/
public function getPageCount(int $limit = self::LIMIT)
{
return $this->getRankingData('url', $limit);
}
/**
* 查询当月TOP15访问终端
* @return int
*/
public function getTerminalPcCount()
{
return $this->queryTerminal('device_port');
}
/**
* 查询当月TOP15访问终端
* @return int
*/
public function getTerminalMobileCount()
{
return $this->queryTerminal('device_port', self::TERMINAL_MOBILE);
}
/**
* 查询当日流量的浏览量
* @return int
*/
public function getDayPVCount()
{
return $this->queryField('depth', 'sum', true);
}
/**
* 查询当日流量的访客量
* @return int
*/
public function getDayIPCount()
{
return $this->queryField('ip', 'count', true);
}
}