Logic.php
2.4 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
<?php
namespace App\Console\Commands\Bside\Statistics;
use App\Models\Bside\Statistics\TrafficStatistics;
use App\Models\CustomerVisit\CustomerVisit;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Throwable;
class Logic
{
/**
* @param int $type 统计类型
* @return array
*/
public function getMonths(int $type = TrafficStatistics::TYPE_SOURCE)
{
$date = getPreviousMonthsDate();
$TrafficStatistics = new TrafficStatistics();
$lists = $TrafficStatistics->getMonthsLists($type, $date);
if (!empty($lists)) {
$dd = [];
$lists->map(function ($item) use (&$dd) {
$dd[] = $item->year . '-' . str_pad($item->month, 2, '0', STR_PAD_LEFT);
});
$date = array_diff($date, $dd);
}
return $date;
}
/**
* @param array $column 统计数据
* @param int $type 统计类型
* @param string|null $date 日期 格式:Y-m
* @param string $message
* @return array
* @throws Throwable
*/
public function save(array $column, int $type = TrafficStatistics::TYPE_SOURCE, string $date = null, $message = '统计当月访问来源数据')
{
DB::beginTransaction();
// 获取当天的IP|PV数量
$customerVisit = new CustomerVisit();
$ip_count = $customerVisit->getMonthIPCount($date);
$pv_count = $customerVisit->getMonthPVCount($date);
$column['pvnum'] = $pv_count;
$column['ipnum'] = $ip_count;
$date = $date ?? date('Y-m');
// 统计数据并插入到数据库
$dd = explode('-', $date);
$year = $dd[0];
$month = $dd[1];
$column['year'] = $year;
$column['month'] = $month;
$trafficStatistics = new TrafficStatistics();
$isRes = $trafficStatistics->getMonth($type, $year, $month);
if ($isRes) {
$trafficStatistics = $isRes;
}
foreach ($column as $key => $value) {
$trafficStatistics->$key = $value;
}
$res = $trafficStatistics->save();
if ($res) {
DB::commit();
$meg = "{$date} - {$message}成功!";
} else {
DB::rollBack();
$meg = "{$date} - {$message}失败!";
}
return ['status' => $res, 'msg' => $meg];
}
}