CustomerVisit.php 3.9 KB
<?php

namespace App\Models\CustomerVisit;

use App\Models\Base;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class CustomerVisit extends Base
{
    use HasFactory;

    protected $table = 'gl_customer_visit';

    const LIMIT = 15;

    /** @var int PC端 */
    const TERMINAL_PC = 1;
    /** @var int 移动端 */
    const TERMINAL_MOBILE = 2;

    public function queryField($field, $action = 'count', $whereDay = false)
    {
        $query = $this->query()
                      ->selectRaw("{$action}({$field}) as count")
                      ->whereYear('updated_date', '=', date("Y"))
                      ->whereMonth('updated_date', '=', date("m"));
        if ($whereDay) {
            $query->whereDay('updated_date', '=', date("d"));
        }
        return (int)$query->value('count') ?? 0;
    }


    public function queryTerminal($field, $action = self::TERMINAL_PC)
    {
        return (int)$this->query()
                         ->selectRaw("count(*) as count")
                         ->where($field, '=', $action)
                         ->whereYear('updated_date', '=', date("Y"))
                         ->whereMonth('updated_date', '=', date("m"))
                         ->value('count') ?? 0;
    }

    public function getRankingData($field, $limit = self::LIMIT)
    {
        return $this->query()
                    ->selectRaw('count(*) as count, ' . $field . ' as request')
                    ->whereYear('updated_date', '=', date("Y"))
                    ->whereMonth('updated_date', '=', date("m"))
                    ->groupBy($field)
                    ->orderBy('count', 'desc')
                    ->limit($limit)
                    ->get();
    }

    public function getSum($field)
    {
        return $this->queryField($field, 'sum');
    }

    public function getCount($field)
    {
        return $this->queryField($field);
    }

    public function returnLists(Collection $data, int $limit = self::LIMIT)
    {
        $lists = $data->toArray();
        $lists = $lists ? array_slice($lists, 0, $limit) : [];
        $count = $data->count();
        return compact('lists', 'count');
    }

    /**
     * 查询当月流量的浏览量
     * @return int
     */
    public function getMonthPVCount()
    {
        return $this->getSum('depth');
    }

    /**
     * 查询当月流量的访客量
     * @return int
     */
    public function getMonthIPCount()
    {
        return $this->getCount('ip');
    }

    /**
     * 查询当月TOP15访问来源
     * @param int $limit
     * @return array
     */
    public function getUrlCount(int $limit = self::LIMIT)
    {
        return $this->getRankingData('referrer_url', $limit);
    }

    /**
     * 查询当月TOP15访问国家来源
     * @param int $limit
     * @return array
     */
    public function getCountryCount(int $limit = self::LIMIT)
    {
        return $this->getRankingData('country', $limit);
    }

    /**
     * 查询当月TOP15受访页面
     * @param int $limit
     * @return array
     */
    public function getPageCount(int $limit = self::LIMIT)
    {
        return $this->getRankingData('url', $limit);
    }

    /**
     * 查询当月TOP15访问终端
     * @return int
     */
    public function getTerminalPcCount()
    {
        return $this->queryTerminal('device_port');
    }

    /**
     * 查询当月TOP15访问终端
     * @return int
     */
    public function getTerminalMobileCount()
    {
        return $this->queryTerminal('device_port', self::TERMINAL_MOBILE);
    }

    /**
     * 查询当日流量的浏览量
     * @return int
     */
    public function getDayPVCount()
    {
        return $this->queryField('depth', 'sum', true);
    }

    /**
     * 查询当日流量的访客量
     * @return int
     */
    public function getDayIPCount()
    {
        return $this->queryField('ip', 'count', true);
    }
}