作者 lyh

gx

1 -<?php  
2 -  
3 -namespace App\Models\CustomerVisit;  
4 -  
5 -use App\Models\Base;  
6 -use Illuminate\Database\Eloquent\Builder;  
7 -use Illuminate\Database\Eloquent\Collection;  
8 -use Illuminate\Database\Eloquent\Factories\HasFactory;  
9 -  
10 -/**  
11 - * App\Models\CustomerVisit\CustomerVisit  
12 - *  
13 - * @property int $id ID  
14 - * @property string $url 客户访问URL  
15 - * @property string $referrer_url 访问来源  
16 - * @property int $device_port 设备端口 1:PC端,2:移动端  
17 - * @property string $country 国家  
18 - * @property string $city 地区  
19 - * @property string $ip 访问ip  
20 - * @property int $depth 访问深度  
21 - * @property string $domain 域名  
22 - * @property \Illuminate\Support\Carbon $created_at 访问时间  
23 - * @property \Illuminate\Support\Carbon $updated_at 更新时间  
24 - * @property string $updated_date 统计日期  
25 - * @method static \Database\Factories\CustomerVisit\CustomerVisitFactory factory(...$parameters)  
26 - * @method static Builder|CustomerVisit newModelQuery()  
27 - * @method static Builder|CustomerVisit newQuery()  
28 - * @method static Builder|CustomerVisit query()  
29 - * @method static Builder|CustomerVisit whereCity($value)  
30 - * @method static Builder|CustomerVisit whereCountry($value)  
31 - * @method static Builder|CustomerVisit whereCreatedAt($value)  
32 - * @method static Builder|CustomerVisit whereDepth($value)  
33 - * @method static Builder|CustomerVisit whereDevicePort($value)  
34 - * @method static Builder|CustomerVisit whereDomain($value)  
35 - * @method static Builder|CustomerVisit whereId($value)  
36 - * @method static Builder|CustomerVisit whereIp($value)  
37 - * @method static Builder|CustomerVisit whereReferrerUrl($value)  
38 - * @method static Builder|CustomerVisit whereUpdatedAt($value)  
39 - * @method static Builder|CustomerVisit whereUpdatedDate($value)  
40 - * @method static Builder|CustomerVisit whereUrl($value)  
41 - * @mixin \Eloquent  
42 - */  
43 -class CustomerVisit extends Base  
44 -{  
45 - use HasFactory;  
46 - protected $table = 'gl_customer_visit';  
47 - const LIMIT = 15;  
48 -  
49 - /** @var int PC端 */  
50 - const TERMINAL_PC = 1;  
51 - /** @var int 移动端 */  
52 - const TERMINAL_MOBILE = 2;  
53 -  
54 - /**  
55 - * @param string $field 统计字段  
56 - * @param string $action 统计方法 count|sum  
57 - * @param bool $isDay 是否按天查询  
58 - * @param string|null $date 日期 格式:Y-m-d  
59 - * @return int  
60 - */  
61 - public function queryField(string $field, string $action = 'count', bool $isDay = false, string $date = null)  
62 - {  
63 - $query = $this->query()->selectRaw("{$action}({$field}) as count");  
64 - if ($date) {  
65 - $dd = explode('-', $date);  
66 - $year = $dd[0] ?? null;  
67 - $month = $dd[1] ?? null;  
68 - $day = $dd[2] ?? null;  
69 - if (!is_null($year)) {  
70 - $query->whereYear('updated_date', '=', $year);  
71 - }  
72 - if (!is_null($month)) {  
73 - $query->whereMonth('updated_date', '=', $month);  
74 - }  
75 - if (!is_null($day)) {  
76 - $query->whereDay('updated_date', '=', $day);  
77 - }  
78 - } else {  
79 - $query->whereYear('updated_date', '=', date("Y"))  
80 - ->whereMonth('updated_date', '=', date("m"));  
81 - if ($isDay) {  
82 - $query->whereDay('updated_date', '=', date("d"));  
83 - }  
84 - }  
85 -  
86 - return (int)$query->value('count') ?? 0;  
87 - }  
88 -  
89 -  
90 - /**  
91 - * @param string $field 统计字段  
92 - * @param int $action 终端类型  
93 - * @param string|null $date 日期 格式:Y-m  
94 - * @return int  
95 - */  
96 - public function queryTerminal(string $field, string $date = null, int $action = self::TERMINAL_PC)  
97 - {  
98 - $query = $this->query()  
99 - ->selectRaw("count(*) as count")  
100 - ->where($field, '=', $action);  
101 - $this->extracted($date, $query);  
102 - return (int)$query->value('count') ?? 0;  
103 - }  
104 -  
105 - /**  
106 - * @param string $field 查询字段  
107 - * @param string|null $date 日期 格式:Y-m-d  
108 - * @param int $limit 查询条数  
109 - * @return Builder[]|Collection|\Illuminate\Support\Collection  
110 - */  
111 - public function getRankingData(string $field, string $date = null, int $limit = self::LIMIT)  
112 - {  
113 - $query = $this->query()  
114 - ->selectRaw('count(*) as count, ' . $field . ' as request');  
115 - $this->extracted($date, $query);  
116 - return $query->groupBy($field)  
117 - ->orderBy('count', 'desc')  
118 - ->limit($limit)  
119 - ->get();  
120 - }  
121 -  
122 - /**  
123 - * 获取相加总数  
124 - * @param string $field  
125 - * @param string|null $date 日期 格式:Y-m-d  
126 - * @return int  
127 - */  
128 - public function getSum(string $field, string $date = null)  
129 - {  
130 - return $this->queryField($field, 'sum', false, $date);  
131 - }  
132 -  
133 - /**  
134 - * 获取总数  
135 - * @param string $field  
136 - * @param string|null $date 日期 格式:Y-m-d  
137 - * @return int  
138 - */  
139 - public function getCount(string $field, string $date = null)  
140 - {  
141 - return $this->queryField($field, 'count', false, $date);  
142 - }  
143 -  
144 - /**  
145 - * 获取列表和总数  
146 - * @param Collection $data  
147 - * @param int $limit  
148 - * @return array  
149 - */  
150 - public function returnLists(Collection $data, int $limit = self::LIMIT)  
151 - {  
152 - $lists = $data->toArray();  
153 - $lists = $lists ? array_slice($lists, 0, $limit) : [];  
154 - $count = $data->count();  
155 - return compact('lists', 'count');  
156 - }  
157 -  
158 - /**  
159 - * 查询当月流量的浏览量  
160 - * @param string|null $data 日期 格式:Y-m-d  
161 - * @return int  
162 - */  
163 - public function getMonthPVCount(string $data = null)  
164 - {  
165 - return $this->getSum('depth', $data);  
166 - }  
167 -  
168 - /**  
169 - * 查询当月流量的访客量  
170 - * @param string|null $data 日期 格式:Y-m-d  
171 - * @return int  
172 - */  
173 - public function getMonthIPCount(string $data = null)  
174 - {  
175 - return $this->getCount('ip', $data);  
176 - }  
177 -  
178 - /**  
179 - * 查询当月TOP15访问来源  
180 - * @param string|null $date 日期 格式:Y-m-d  
181 - * @param int $limit 查询条数  
182 - * @return array  
183 - */  
184 - public function getUrlCount(string $date = null, int $limit = self::LIMIT)  
185 - {  
186 - return $this->getRankingData('referrer_url', $date, $limit);  
187 - }  
188 -  
189 - /**  
190 - * 查询当月TOP15访问国家来源  
191 - * @param string|null $date 日期 格式:Y-m  
192 - * @param int $limit  
193 - * @return array  
194 - */  
195 - public function getCountryCount(string $date = null, int $limit = self::LIMIT)  
196 - {  
197 - return $this->getRankingData('country', $date, $limit);  
198 - }  
199 -  
200 - /**  
201 - * 查询当月TOP15受访页面  
202 - * @param string|null $date 日期 格式:Y-m  
203 - * @param int $limit  
204 - * @return array  
205 - */  
206 - public function getPageCount(string $date = null, int $limit = self::LIMIT)  
207 - {  
208 - return $this->getRankingData('url', $date, $limit);  
209 - }  
210 -  
211 - /**  
212 - * 查询当月TOP15访问终端  
213 - * @param string|null $date 日期 格式:Y-m-d  
214 - * @return int  
215 - */  
216 - public function getTerminalPcCount(string $date = null)  
217 - {  
218 - return $this->queryTerminal('device_port', $date);  
219 - }  
220 -  
221 - /**  
222 - * 查询当月TOP15访问终端  
223 - * @param string|null $date 日期 格式:Y-m-d  
224 - * @return int  
225 - */  
226 - public function getTerminalMobileCount(string $date = null)  
227 - {  
228 - return $this->queryTerminal('device_port', $date, self::TERMINAL_MOBILE);  
229 - }  
230 -  
231 - /**  
232 - * 查询当日流量的浏览量  
233 - * @param string|null $date 日期 格式:Y-m-d  
234 - * @return int  
235 - */  
236 - public function getDayPVCount(string $date = null)  
237 - {  
238 - return $this->queryField('depth', 'sum', true, $date);  
239 - }  
240 -  
241 - /**  
242 - * 查询当日流量的访客量  
243 - * @param string|null $date 日期 格式:Y-m-d  
244 - * @return int  
245 - */  
246 - public function getDayIPCount(string $date = null)  
247 - {  
248 - return $this->queryField('ip', 'count', true, $date);  
249 - }  
250 -  
251 - /**  
252 - * @param $date  
253 - * @param $query  
254 - * @return void  
255 - */  
256 - public function extracted($date, $query)  
257 - {  
258 - if (!is_null($date)) {  
259 - $dd = explode('-', $date);  
260 - $year = $dd[0] ?? null;  
261 - $month = $dd[1] ?? null;  
262 - if (!is_null($year)) {  
263 - $query->whereYear('updated_date', '=', $year);  
264 - }  
265 - if (!is_null($month)) {  
266 - $query->whereMonth('updated_date', '=', $month);  
267 - }  
268 - } else {  
269 - $query->whereYear('updated_date', '=', date("Y"))  
270 - ->whereMonth('updated_date', '=', date("m"));  
271 - }  
272 - }  
273 -}  
1 -<?php  
2 -  
3 -namespace App\Models\CustomerVisit;  
4 -  
5 -use App\Models\Base;  
6 -  
7 -class CustomerVisitItem extends Base  
8 -{  
9 - protected $table = 'gl_customer_visit_item';  
10 -}  
@@ -17,9 +17,6 @@ class Visit extends Base @@ -17,9 +17,6 @@ class Visit extends Base
17 //设置关联表名 17 //设置关联表名
18 protected $table = 'gl_customer_visit'; 18 protected $table = 'gl_customer_visit';
19 19
20 - //连接数据库  
21 - protected $connection = 'custom_mysql';  
22 -  
23 protected $appends = ['device_text']; 20 protected $appends = ['device_text'];
24 21
25 public static function deviceMap(){ 22 public static function deviceMap(){
@@ -12,11 +12,6 @@ use App\Models\Base; @@ -12,11 +12,6 @@ use App\Models\Base;
12 */ 12 */
13 class VisitItem extends Base 13 class VisitItem extends Base
14 { 14 {
15 -  
16 //设置关联表名 15 //设置关联表名
17 protected $table = 'gl_customer_visit_item'; 16 protected $table = 'gl_customer_visit_item';
18 -  
19 - //连接数据库  
20 - protected $connection = 'custom_mysql';  
21 -  
22 } 17 }