|
|
|
<?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];
|
|
|
|
}
|
|
|
|
} |