|
...
|
...
|
@@ -4,9 +4,12 @@ namespace App\Http\Logic\Bside\Statistics; |
|
|
|
|
|
|
|
use App\Http\Logic\Bside\BaseLogic;
|
|
|
|
use App\Models\Bside\Statistics\TrafficStatistics;
|
|
|
|
use App\Models\Bside\Statistics\TrafficTrends;
|
|
|
|
use App\Models\CustomerVisit\CustomerVisit;
|
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
|
|
use Illuminate\Contracts\Cache\Repository;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
...
|
...
|
@@ -30,11 +33,11 @@ class StatisticsLogic extends BaseLogic |
|
|
|
try {
|
|
|
|
$this->date = request()->get('date') ?? null;
|
|
|
|
// 判断传入日期是否大于当月
|
|
|
|
if ($this->checkIsGreaterMonth($this->date)) {
|
|
|
|
if (checkIsGreaterMonth($this->date)) {
|
|
|
|
$this->date = null;
|
|
|
|
}
|
|
|
|
// 判断传入日期是否是当月
|
|
|
|
if ($this->checkIsMonth($this->date)) {
|
|
|
|
if (checkIsMonth($this->date)) {
|
|
|
|
$this->date = null;
|
|
|
|
}
|
|
|
|
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
|
|
...
|
...
|
@@ -83,6 +86,57 @@ class StatisticsLogic extends BaseLogic |
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取当月的每天的流量趋势
|
|
|
|
* @return Builder[]|Collection
|
|
|
|
*/
|
|
|
|
public function getDaysTrend()
|
|
|
|
{
|
|
|
|
$trafficTrends = new TrafficTrends();
|
|
|
|
$data = $trafficTrends->getMonthsLists($this->date);
|
|
|
|
$original_date = $data->pluck('date')->all();
|
|
|
|
// 获取当月的每天的日期
|
|
|
|
$month_date = getDateDays($this->date);
|
|
|
|
// 判断是否存在缺失的日期
|
|
|
|
if (count($month_date) > 1) {
|
|
|
|
// 去除当天的日期
|
|
|
|
array_pop($month_date);
|
|
|
|
// 获取缺失的日期
|
|
|
|
$diff = array_diff($month_date, $original_date);
|
|
|
|
if ($diff) {
|
|
|
|
// 获取缺失的数据并保存
|
|
|
|
$data = array_merge($data->toArray(), $this->getTrendsLists($diff));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$date = date('Y-m-d');
|
|
|
|
$ip_count = $this->customerVisit->getDayIPCount();
|
|
|
|
$pv_count = $this->customerVisit->getDayPVCount();
|
|
|
|
$data[] = compact('date', 'ip_count', 'pv_count');
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取缺失的数据并保存
|
|
|
|
* @param array $dates 日期,格式:['y-m-d', 'y-m-d']
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getTrendsLists(array $dates)
|
|
|
|
{
|
|
|
|
$data = [];
|
|
|
|
$trafficTrends = new TrafficTrends();
|
|
|
|
foreach ($dates as $item) {
|
|
|
|
$date = $item;
|
|
|
|
$ip_count = $this->customerVisit->getDayIPCount($item);
|
|
|
|
$pv_count = $this->customerVisit->getDayPVCount($item);
|
|
|
|
$trafficTrends->day = $item;
|
|
|
|
$trafficTrends->ipnum = $ip_count;
|
|
|
|
$trafficTrends->pvnum = $pv_count;
|
|
|
|
$trafficTrends->save();
|
|
|
|
$data[] = compact('date', 'ip_count', 'pv_count');
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $response
|
|
|
|
* @return bool
|
|
|
|
* @throws Throwable
|
|
...
|
...
|
@@ -240,37 +294,4 @@ class StatisticsLogic extends BaseLogic |
|
|
|
// 判断传入日期是否大于当前日期
|
|
|
|
return $timestamp > $now;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 判断传入日期是否大于当月
|
|
|
|
* @param $date
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function checkIsGreaterMonth($date)
|
|
|
|
{
|
|
|
|
// 传入日期的时间戳
|
|
|
|
$timestamp = strtotime($date);
|
|
|
|
// 当前月份的时间戳
|
|
|
|
$nowMonth = strtotime(date('Y-m'));
|
|
|
|
// 判断传入日期是否大于当前月份
|
|
|
|
return $timestamp > $nowMonth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 判断传入日期是否是当月
|
|
|
|
* @param $date
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function checkIsMonth($date)
|
|
|
|
{
|
|
|
|
// 获取当前时间戳
|
|
|
|
$now = time();
|
|
|
|
// 获取当月的起始时间戳和结束时间戳
|
|
|
|
$firstDay = strtotime(date('Y-m-01', $now));
|
|
|
|
$lastDay = strtotime(date('Y-m-t', $now));
|
|
|
|
// 传入日期的时间戳
|
|
|
|
$timestamp = strtotime($date);
|
|
|
|
// 判断传入日期是否在当月范围内
|
|
|
|
return $timestamp >= $firstDay && $timestamp <= $lastDay;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|