CountLogic.php 9.3 KB
<?php

namespace App\Http\Logic\Bside\HomeCount;


use App\Helper\FormGlobalsoApi;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Geo\GeoCount;
use App\Models\RankData\RankDataBmseo;
use App\Models\Template\BCustomTemplate;
use App\Models\Visit\Visit;
use App\Models\Visit\VisitItem;
use App\Models\HomeCount\Count;
use App\Models\News\News;
use App\Models\Product\Product;
use App\Models\Project\Project;
use App\Models\RankData\RankData as RankDataModel;
use App\Models\Service\Service;
use App\Services\ProjectServer;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class CountLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new Count();
        if($this->user['project_id'] == 5172){
            $this->user['project_id'] = 3298;
        }
    }

    /**
     * @name   :昨日统计数据 yesterday_count
     * @author :lyh
     * @method :post
     * @time   :2023/5/23 17:30
     */
    public function yesterday_count(){
        $yesterday = Carbon::yesterday()->toDateString();

        $param = [
            'date' => $yesterday,
            'project_id' => $this->user['project_id']
        ];
        $info = $this->model->read($param,['pv_num','ip_num','inquiry_num','date','compliance_day','service_day','country']);
        if($info === false){
            $info = $this->model->read(['project_id' => $this->user['project_id']],
                ['pv_num','ip_num','inquiry_num','date','compliance_day','service_day','country']);
        }
        $inquiry_num = Cache::get('inquiry_num_'.$this->user['project_id']);
        if(empty($inquiry_num)){
            $domain = parse_url($this->user['domain'], PHP_URL_HOST); // 直接取域名部分
            $inquiry_list = (new FormGlobalsoApi())->getInquiryAll($domain,$this->user['is_upgrade']);
            if($inquiry_list !== false){
                if(isset($inquiry_list['status']) && $inquiry_list['status'] != 400){
                    $info['inquiry_num'] = $inquiry_list['data']['count'];
                    Cache::add('inquiry_num_'.$this->user['project_id'],$inquiry_list['data']['count'],3600);
                }
            }
        }
        //获取项目的剩余时长
        $projectModel = new Project();
        $projectInfo = $projectModel->read(['id'=>$this->user['project_id']],['remain_day','finish_remain_day']);
        $info['service_day'] = $projectInfo['remain_day'];
        $info['compliance_day'] = $projectInfo['finish_remain_day'];
        return $this->success($info);
    }

    /**
     * @name   :(方案信息)scheme_info
     * @author :lyh
     * @method :post
     * @time   :2023/5/24 11:48
     */
    public function scheme_info(){
        $data = [
            'company'=>$this->project['company'] ?? '',
            'scheme'=>!empty($this->project['deploy_build']['plan']) ? Project::planMap()[$this->project['deploy_build']['plan']] : '',
            'service_duration'=>$this->project['deploy_build']['service_duration'] ?? 0,
        ];
        return $this->success($data);
    }

    /**
     * @name   :(总访问量)total_count
     * @author :lyh
     * @method :post
     * @time   :2023/5/24 13:33
     */
    public function total_count($inquiry_num = ''){
        ProjectServer::useProject($this->user['project_id']);
        $pv = (new VisitItem())->count();
        $ip = (new Visit())->count();
        $data = [
            'total_pv'=>$pv,
            'total_ip'=>$ip,
            'conversion_rate' => (isset($inquiry_num) && !empty($inquiry_num) && ($ip != 0)) ? round(($inquiry_num / $ip) * 100,2) : 0,
        ];
        return $this->success($data);
    }

    /**
     * @name   :(前一天关键字排名统计)keyword_data
     * @author :lyh
     * @method :post
     * @time   :2023/5/24 14:03
     */
    public function keyword_data_count(){
        $version = $this->project['version'] ?? '';
        $yesterday = date('Y-m-d');
        if($this->user['project_seo_type'] == 1){
            $rankDataModel = new RankDataBmseo();
        }else{
            $rankDataModel = new RankDataModel();
        }
        $param = [
            'updated_date' => $yesterday,
            'project_id' => $this->user['project_id']
        ];
        $data = $rankDataModel->read($param,['first_num','first_page_num','first_three_pages_num','first_five_pages_num','first_ten_pages_num']);
        if($data === false){
            $param = [
                'project_id' => $this->user['project_id']
            ];
            $data = $rankDataModel->read($param,['first_num','first_page_num','first_three_pages_num','first_five_pages_num','first_ten_pages_num']);
            if($data === false){
                $data = [];
            }
        }
        if(!empty($version) && ($version == '7.5')){
            $geoCountModel = new GeoCount();
            $geoInfo = $geoCountModel->where('project_id',$this->user['project_id'])->where('date', '<=', date('Y-m-d'))->orderBy('date', 'desc')->first();
            $geoInfo = $geoInfo ? $geoInfo->toArray() : [];
            $data['geo_qualify_total'] = $geoInfo['qualify_total'] ?? 0;
            $data['total_count'] = ($this->project['deploy_build']['keyword_num'] ?? 0) + ($this->project['geo_qualify_num'] ?? 0);
        }
        return $this->success($data);
    }

    /**
     * @name   :(相关数据统计)with_data_count
     * @author :lyh
     * @method :post
     * @time   :2023/5/24 14:12
     */
    public function with_data_count(){
        $product_count = (new Product())->where(['project_id' => $this->user['project_id']])->count();
        $news_count = (new News())->where(['project_id' => $this->user['project_id']])->count();
        $page_count = (new BCustomTemplate())->where(['project_id' => $this->user['project_id'],'status'=>1])->count();
        $data = [
            'product_count' => $product_count,
            'news_count' => $news_count,
            'page_count' => $page_count,
        ];
        return $this->success($data);
    }

    /**
     * @name   :(30天数据统计)visit_data_count
     * @author :lyh
     * @method :post
     * @time   :2023/5/24 14:18
     */
    public function visit_data_count(){
        $param = [
            'date' => ['between',[now()->subDays(30)->startOfDay()->toDateString(),now()->startOfDay()->toDateString()]],
            'project_id' => $this->user['project_id']
        ];
        $data = $this->model->list($param,'date',['id','pv_num','ip_num','date']);
        return $this->success($data);
    }

    /**
     * @name   :(访问来源统计)referrer_count
     * @author :lyh
     * @method :post
     * @time   :2023/5/24 15:32
     */
    public function referrer_count(){
        $customerVisitModel = new Visit();
        $data = $customerVisitModel->select('referrer_url', DB::raw('COUNT(*) as count'))
            ->groupBy('referrer_url')->where(['domain'=>trim(str_replace('https://','',$this->user['domain']),'/')])
            ->orderByDesc('count')->limit(9)->get()->toArray();
        $total = $customerVisitModel->count();
        if(!empty($data)){
            foreach ($data as $k=>$v){
                if(empty($v['referrer_url'])){
                    unset($data[$k]);
                    continue;
                }
                $data[$k]['proportion'] = ($v['count']/$total) * 100;
            }
            $data = array_values($data);
        }
        return $this->success($data);
    }

    /**
     * @name   :(访问国家统计)access_country_count
     * @author :lyh
     * @method :post
     * @time   :2023/5/24 15:56
     */
    public function access_country_count(){

        $customerVisitModel = new Visit();
        $data = $customerVisitModel->select('country',DB::raw('COUNT(*) as ip'),DB::raw('SUM(depth) as pv'))
            ->groupBy('country')
            ->orderBy('ip','desc')->limit(11)->get()->toArray();
        $result =array();
        if(!empty($data)){
            foreach ($data as $k => $v){
                if(isset($v['country']) && isset($this->project['is_record_china_visit']) && ($this->project['is_record_china_visit'] != 1) && ($v['country'] == '中国')){
                    continue;
                }else{
                    $v['pv'] = (int)$v['pv'];
                    $result[] = $v;
                }
            }
        }
        return $this->success($result);
    }

    /**
     * @name   :(售后服务中心)enterprise_service
     * @author :lyh
     * @method :post
     * @time   :2023/5/25 10:22
     */
    public function enterprise_service(){
        $serviceModel = new Service();
        $lists = $serviceModel->list(['type'=>1],'created_at');
        foreach ($lists as $k => $v){
            switch ($v['key']){
                case 'images':
                    $arr = explode(',',$v['values']);
                    foreach ($arr as $k1 => $v1){
                        $v['images_link'][$k1] = getImageUrl($v1);
                    }
                    break;
                case 'android':
                    $v['android_link'] = getImageUrl($v['values']);
                    break;
                case 'official_account':
                    $v['official_account_link'] = getImageUrl($v['values']);
                    break;
                case 'ios':
                    $v['ios_link'] = getImageUrl($v['values']);
                    break;
            }
            $lists[$k] = $v;
        }
        return $this->success($lists);
    }

}