TrafficStatistics.php
6.0 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
<?php
namespace App\Models\Bside\Statistics;
use App\Models\Base;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
/**
* App\Models\Bside\Statistics\TrafficStatistics
*
* @property int $id
* @property int|null $type 类型:
* 1 - 访问来源
* 2 - 地域分布
* 3 - 受访页面
* 4 - 访问终端
* 5 - 流量趋势
* @property int|null $year 年
* @property int|null $month 月
* @property string|null $top 访问来源|国家|页面TOP15 - 百分比 - json格式
* @property int|null $innum 询盘量
* @property float|null $conversion 询盘转化率
* @property int|null $pvnum 浏览量
* @property int|null $ipnum 访客量
* @property int|null $pcnum PC端访问量
* @property int|null $mbnum 移动端访问量
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @method static Builder|TrafficStatistics newModelQuery()
* @method static Builder|TrafficStatistics newQuery()
* @method static Builder|TrafficStatistics query()
* @method static Builder|TrafficStatistics whereConversion($value)
* @method static Builder|TrafficStatistics whereCreatedAt($value)
* @method static Builder|TrafficStatistics whereId($value)
* @method static Builder|TrafficStatistics whereInnum($value)
* @method static Builder|TrafficStatistics whereIpnum($value)
* @method static Builder|TrafficStatistics whereMbnum($value)
* @method static Builder|TrafficStatistics whereMonth($value)
* @method static Builder|TrafficStatistics wherePageview($value)
* @method static Builder|TrafficStatistics wherePcnum($value)
* @method static Builder|TrafficStatistics wherePvnum($value)
* @method static Builder|TrafficStatistics whereTop($value)
* @method static Builder|TrafficStatistics whereType($value)
* @method static Builder|TrafficStatistics whereUpdatedAt($value)
* @method static Builder|TrafficStatistics whereUserSessions($value)
* @method static Builder|TrafficStatistics whereYear($value)
* @mixin \Eloquent
*/
class TrafficStatistics extends Base
{
protected $table = 'gl_traffic_statistics';
/** @var int 访问来源 */
const TYPE_SOURCE = 1;
/** @var int 地域分布 */
const TYPE_DISTRIBUTION = 2;
/** @var int 受访页面 */
const TYPE_PAGE = 3;
/** @var int 访问终端 */
const TYPE_TERMINAL = 4;
/** @var int 流量趋势 */
const TYPE_TREND = 5;
/** @var string[] 类型对应字段 */
const FIELDS = [
self::TYPE_SOURCE => ['pvnum', 'ipnum', 'top'],
self::TYPE_DISTRIBUTION => ['pvnum', 'ipnum', 'top'],
self::TYPE_PAGE => ['pvnum', 'ipnum', 'top'],
self::TYPE_TERMINAL => ['pvnum', 'ipnum', 'top', 'pcnum', 'mbnum'],
self::TYPE_TREND => ['pvnum', 'ipnum', 'top', 'innum'],
];
/** @var string[] 数据库字段对应返回字段 */
const KEY_VALUE = [
'pvnum' => 'pv_count',
'ipnum' => 'ip_count',
'pcnum' => 'pc_count',
'mbnum' => 'mobile_count',
'innum' => 'in_count',
'top' => 'lists'
];
/**
* 根据类型获取字段
* @param int $type
* @return string[]
*/
public function selectFields(int $type = self::TYPE_SOURCE)
{
return self::FIELDS[$type] ?? [];
}
/**
* 根据类型获取月份列表数据
* @param int $type
* @param $date
* @return TrafficStatistics[]|Builder[]|Collection
*/
public function getMonthsLists(int $type = self::TYPE_SOURCE, $date = null)
{
$query = $this->query()->where('type', $type);
if ($date != null) {
if (is_array($date)) {
$years = $months = [];
foreach ($date as $value) {
$dd = explode('-', $value);
$year = $dd[0];
$month = $dd[1];
if (!in_array($year, $years)) {
$years[] = $year;
}
if (!in_array($month, $months)) {
$months[] = $month;
}
}
$query->whereIn('year', $years)
->whereIn('month', $months);
} else {
$dd = explode('-', $date);
$year = $dd[0];
$month = $dd[1];
$query->where('year', $year)
->where('month', $month);
}
} else {
$query->where('year', date('Y'))
->where('month', date('m'));
}
return $query->get();
}
/**
* 根据类型获取单条月份数据
* @param int $type
* @param $year
* @param $month
* @return TrafficStatistics|Builder|Model|object|null
*/
public function getMonth(int $type = self::TYPE_SOURCE, $year = null, $month = null)
{
$year = $year ?? date('Y');
$month = $month ?? date('m');
return $this->query()->where('type', $type)
->where('year', $year)
->where('month', $month)
->first();
}
/**
* 根据类型获取数据
* @param int $type
* @param null $year
* @param null $month
* @return TrafficStatistics|Builder|Model|object|null
*/
public function getData(int $type = self::TYPE_SOURCE, $year = null, $month = null)
{
$key_value = self::KEY_VALUE;
$field_arr = [];
foreach ($this->selectFields($type) as $field) {
$field_arr[] = "{$field} as {$key_value[$field]}";
}
$query = $this->query();
if ($field_arr) {
$query->selectRaw(implode(',', $field_arr));
}
return $query->where('type', $type)
->where('year', $year)
->where('month', $month)
->first();
}
/**
* 格式化数据
* @param $value
* @return mixed
*/
public function getListsAttribute($value)
{
return json_decode($value);
}
}