CustomerVisit.php
8.3 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
namespace App\Models\CustomerVisit;
use App\Models\Base;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
/**
* App\Models\CustomerVisit\CustomerVisit
*
* @property int $id ID
* @property string $url 客户访问URL
* @property string $referrer_url 访问来源
* @property int $device_port 设备端口 1:PC端,2:移动端
* @property string $country 国家
* @property string $city 地区
* @property string $ip 访问ip
* @property int $depth 访问深度
* @property string $domain 域名
* @property \Illuminate\Support\Carbon $created_at 访问时间
* @property \Illuminate\Support\Carbon $updated_at 更新时间
* @property string $updated_date 统计日期
* @method static \Database\Factories\CustomerVisit\CustomerVisitFactory factory(...$parameters)
* @method static Builder|CustomerVisit newModelQuery()
* @method static Builder|CustomerVisit newQuery()
* @method static Builder|CustomerVisit query()
* @method static Builder|CustomerVisit whereCity($value)
* @method static Builder|CustomerVisit whereCountry($value)
* @method static Builder|CustomerVisit whereCreatedAt($value)
* @method static Builder|CustomerVisit whereDepth($value)
* @method static Builder|CustomerVisit whereDevicePort($value)
* @method static Builder|CustomerVisit whereDomain($value)
* @method static Builder|CustomerVisit whereId($value)
* @method static Builder|CustomerVisit whereIp($value)
* @method static Builder|CustomerVisit whereReferrerUrl($value)
* @method static Builder|CustomerVisit whereUpdatedAt($value)
* @method static Builder|CustomerVisit whereUpdatedDate($value)
* @method static Builder|CustomerVisit whereUrl($value)
* @mixin \Eloquent
*/
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;
/**
* @param string $field 统计字段
* @param string $action 统计方法 count|sum
* @param bool $isDay 是否按天查询
* @param string|null $date 日期 格式:Y-m-d
* @return int
*/
public function queryField(string $field, string $action = 'count', bool $isDay = false, string $date = null)
{
$query = $this->query()->selectRaw("{$action}({$field}) as count");
if ($date) {
$dd = explode('-', $date);
$year = $dd[0] ?? null;
$month = $dd[1] ?? null;
$day = $dd[2] ?? null;
if (!is_null($year)) {
$query->whereYear('updated_date', '=', $year);
}
if (!is_null($month)) {
$query->whereMonth('updated_date', '=', $month);
}
if (!is_null($day)) {
$query->whereDay('updated_date', '=', $day);
}
} else {
$query->whereYear('updated_date', '=', date("Y"))
->whereMonth('updated_date', '=', date("m"));
if ($isDay) {
$query->whereDay('updated_date', '=', date("d"));
}
}
return (int)$query->value('count') ?? 0;
}
/**
* @param string $field 统计字段
* @param int $action 终端类型
* @param string|null $date 日期 格式:Y-m
* @return int
*/
public function queryTerminal(string $field, string $date = null, int $action = self::TERMINAL_PC)
{
$query = $this->query()
->selectRaw("count(*) as count")
->where($field, '=', $action);
$this->extracted($date, $query);
return (int)$query->value('count') ?? 0;
}
/**
* @param string $field 查询字段
* @param string|null $date 日期 格式:Y-m-d
* @param int $limit 查询条数
* @return Builder[]|Collection|\Illuminate\Support\Collection
*/
public function getRankingData(string $field, string $date = null, int $limit = self::LIMIT)
{
$query = $this->query()
->selectRaw('count(*) as count, ' . $field . ' as request');
$this->extracted($date, $query);
return $query->groupBy($field)
->orderBy('count', 'desc')
->limit($limit)
->get();
}
/**
* 获取相加总数
* @param string $field
* @param string|null $date 日期 格式:Y-m-d
* @return int
*/
public function getSum(string $field, string $date = null)
{
return $this->queryField($field, 'sum', false, $date);
}
/**
* 获取总数
* @param string $field
* @param string|null $date 日期 格式:Y-m-d
* @return int
*/
public function getCount(string $field, string $date = null)
{
return $this->queryField($field, 'count', false, $date);
}
/**
* 获取列表和总数
* @param Collection $data
* @param int $limit
* @return array
*/
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');
}
/**
* 查询当月流量的浏览量
* @param string|null $data 日期 格式:Y-m-d
* @return int
*/
public function getMonthPVCount(string $data = null)
{
return $this->getSum('depth', $data);
}
/**
* 查询当月流量的访客量
* @param string|null $data 日期 格式:Y-m-d
* @return int
*/
public function getMonthIPCount(string $data = null)
{
return $this->getCount('ip', $data);
}
/**
* 查询当月TOP15访问来源
* @param string|null $date 日期 格式:Y-m-d
* @param int $limit 查询条数
* @return array
*/
public function getUrlCount(string $date = null, int $limit = self::LIMIT)
{
return $this->getRankingData('referrer_url', $date, $limit);
}
/**
* 查询当月TOP15访问国家来源
* @param string|null $date 日期 格式:Y-m
* @param int $limit
* @return array
*/
public function getCountryCount(string $date = null, int $limit = self::LIMIT)
{
return $this->getRankingData('country', $date, $limit);
}
/**
* 查询当月TOP15受访页面
* @param string|null $date 日期 格式:Y-m
* @param int $limit
* @return array
*/
public function getPageCount(string $date = null, int $limit = self::LIMIT)
{
return $this->getRankingData('url', $date, $limit);
}
/**
* 查询当月TOP15访问终端
* @param string|null $date 日期 格式:Y-m-d
* @return int
*/
public function getTerminalPcCount(string $date = null)
{
return $this->queryTerminal('device_port', $date);
}
/**
* 查询当月TOP15访问终端
* @param string|null $date 日期 格式:Y-m-d
* @return int
*/
public function getTerminalMobileCount(string $date = null)
{
return $this->queryTerminal('device_port', $date, self::TERMINAL_MOBILE);
}
/**
* 查询当日流量的浏览量
* @param string|null $date 日期 格式:Y-m-d
* @return int
*/
public function getDayPVCount(string $date = null)
{
return $this->queryField('depth', 'sum', true, $date);
}
/**
* 查询当日流量的访客量
* @param string|null $date 日期 格式:Y-m-d
* @return int
*/
public function getDayIPCount(string $date = null)
{
return $this->queryField('ip', 'count', true, $date);
}
/**
* @param $date
* @param $query
* @return void
*/
public function extracted($date, $query)
{
if (!is_null($date)) {
$dd = explode('-', $date);
$year = $dd[0] ?? null;
$month = $dd[1] ?? null;
if (!is_null($year)) {
$query->whereYear('updated_date', '=', $year);
}
if (!is_null($month)) {
$query->whereMonth('updated_date', '=', $month);
}
} else {
$query->whereYear('updated_date', '=', date("Y"))
->whereMonth('updated_date', '=', date("m"));
}
}
}