MonthCountLogic.php 8.1 KB
<?php

namespace App\Http\Logic\Bside\HomeCount;

use App\Helper\FormGlobalsoApi;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\HomeCount\Count;
use App\Models\HomeCount\MonthCount;
use App\Models\Inquiry\InquiryFormData;
use App\Models\Project\DeployOptimize;
use App\Services\ProjectServer;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class MonthCountLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new MonthCount();
        $this->param = $this->requestAll;
    }

    /**
     * @remark :获取数据
     * @name   :getCountLists
     * @author :lyh
     * @method :post
     * @time   :2023/7/3 9:39
     */
    public function getCountLists($map,$order = 'created_at',$filed = ['*']){
        $map['project_id'] = $this->user['project_id'];
        $lists = $this->model->list($map,$order,$filed);
        if(isset($this->project['is_record_china_visit']) && ($this->project['is_record_china_visit'] == 0)){
            foreach ($lists as $k => $v){
                if(empty($v['source_country'])){
                    continue;
                }
                $source_country = json_decode($v['source_country']);
                foreach ($source_country as $k1 => $v1){
                    $v1 = (array)$v1;
                    if($v1['country'] == '中国'){
                        unset($source_country[$k1]);
                    }
                }
                $v['source_country'] = json_encode(array_values($source_country));
                $lists[$k] = $v;
            }
        }
        $lists['new'] = $this->currentMonthCount();
        return $this->success($lists);
    }

    /**
     * @remark :获取当前月数据统计
     * @name   :currentMonth
     * @author :lyh
     * @method :post
     * @time   :2023/7/3 9:55
     */
    public function currentMonthCount(){
        $startTime = Carbon::now()->startOfMonth()->toDateString();
        $endTime = date('Y-m-d',time());
        $arr = [];
        ProjectServer::useProject($this->user['project_id']);
        $arr = $this->inquiryCount($arr,$startTime,$endTime,$this->user['domain']);
        $arr = $this->flowCount($arr,$startTime,$endTime,$this->user['project_id']);
        $arr = $this->sourceCount($arr,$startTime,$endTime,$this->user['domain']);
        $arr['month'] = date('Y-m',time());
        return $this->success($arr);
    }
    /**
     * @param $domain
     * @param $project_id
     * @remark :询盘按月统计
     * @name   :inquiryCount
     * @author :lyh
     * @method :post
     * @time   :2023/6/30 14:29
     */
    public function inquiryCount(&$arr,&$startTime,&$endTime,$domain){
        $inquiry_list = (new FormGlobalsoApi())->getInquiryList($domain,'',1,100000000);
        if(!empty($inquiry_list)){
            //总数
            $arr['total'] = $inquiry_list['data']['total'] ?? 0;
            //数据详情
            $data = $inquiry_list['data']['data'] ?? '';
            $arr['month_total'] = 0;
            $countryArr = [];
            if(isset($data) && !empty($data)){
                foreach ($data as $v){
                    if(($startTime.' 00:00:00' <= $v['submit_time']) && $v['submit_time'] <= $endTime.' 23:59:59'){
                        $arr['month_total']++;
                    }
                    if(isset($countryArr[$v['country']])){
                        $countryArr[$v['country']]++;
                    }else{
                        $countryArr[$v['country']] = 1;
                    }
                }
            }
        }
        //加上其他询盘
        $arr['total'] += InquiryFormData::getCount();
        $arr['month_total'] += InquiryFormData::getCount([$startTime, $endTime]);
        $countryData = InquiryFormData::getCountryCount([$startTime, $endTime]);
        foreach ($countryData as $v1){
            if(isset($countryArr[$v1['country']])){
                $countryArr[$v1['country']] += $v1['count'];
            }else{
                $countryArr[$v1['country']] = $v1['count'];
            }
        }
        arsort($countryArr);
        $top20 = array_slice($countryArr, 0, 15, true);
        $arr['country'] = $top20;
        return $arr;
    }

    /**
     * @remark :流量统计
     * @name   :flowCount
     * @author :lyh
     * @method :post
     * @time   :2023/6/30 14:31
     */
    public function flowCount(&$arr,&$startTime,&$endTime,$project_id){
        $pv_ip = DB::table('gl_count')
            ->where(['project_id'=>$project_id])
            ->whereBetween('date', [$startTime,$endTime])
            ->select(DB::raw('SUM(pv_num) as pv_num'), DB::raw('SUM(ip_num) as ip_num'))
            ->orderBy('id','desc')
            ->first();
        $arr['pv'] = $pv_ip->pv_num;
        $arr['ip'] = $pv_ip->ip_num;
        $arr['rate']  = 0;
        if($arr['ip'] != 0){
            $arr['rate'] = round(($arr['month_total'] / $arr['ip']) * 100,2);
        }
        return $arr;
    }

    /**
     * @remark :来源访问前8
     * @name   :sourceCount
     * @author :lyh
     * @method :post
     * @time   :2023/6/30 16:14
     */
    public function sourceCount(&$arr,$startTime,$endTime,$domain){
        //访问来源前10
        $source = DB::connection('custom_mysql')->table('gl_customer_visit')
            ->select('referrer_url', DB::raw('COUNT(*) as count'))
            ->groupBy('referrer_url')
            ->whereBetween('updated_date', [$startTime,$endTime])
            ->orderByDesc('count')->limit(10)->get()->toArray();
        $arr['source'] = $source;
        //访问国家前15
        $query = DB::connection('custom_mysql')->table('gl_customer_visit')
            ->select('country',DB::raw('COUNT(*) as ip'),DB::raw('SUM(depth) as pv'))
            ->groupBy('country');
        if(isset($this->project['is_record_china_visit']) && ($this->project['is_record_china_visit'] == 0)){
            $query->where('country','<>','中国');
        }
        $source_country = $query->whereBetween('updated_date', [$startTime,$endTime])
            ->orderBy('ip','desc')->limit(15)->get()->toArray();
        $arr['source_country'] = $source_country;
        //受访界面前15
        $referrer_url = DB::connection('custom_mysql')->table('gl_customer_visit')
            ->select('url',DB::raw('COUNT(*) as num'))
            ->orderBy('num','desc')
            ->whereBetween('updated_date', [$startTime,$endTime])
            ->groupBy('url')
            ->limit(15)->get()->toArray();
        $arr['referrer_url'] = $referrer_url;
        //访问断后
        $referrer_port = DB::connection('custom_mysql')->table('gl_customer_visit')
            ->select('device_port',DB::raw('COUNT(*) as num'))
            ->orderBy('num','desc')
            ->whereBetween('updated_date', [$startTime,$endTime])
            ->groupBy('device_port')
            ->limit(15)->get()->toArray();
        $arr['referrer_port'] = $referrer_port;
        return $arr;
    }

    /**
     * @remark :根据时间获取pv,ip
     * @name   :getIpPvCount
     * @author :lyh
     * @method :post
     * @time   :2023/7/3 10:30
     */
    public function getIpPvCount(){
        $count = new Count();
        $startTime = date("Y-m-d", strtotime("-9 months", mktime(0, 0, 0)));
        $ensTime = date('Y-m-d',time());
        $lists = $count->list(['date'=>['between',[$startTime,$ensTime]],'project_id'=>$this->user['project_id']],'id',['*'],'asc');
        $groupedData = [];
        foreach ($lists as $k=>$v){
            $month = date('Y-m', strtotime($v['date']));
            if(!isset($groupedData[$month])){
                $groupedData[$month] = [];
            }
            if(empty($v['country'])){
                $v['country'] = [];
            }
            $groupedData[$month][] = $v;
        }
        return $this->success($groupedData);
    }

    /**
     * @remark :获取关键字列表
     * @name   :getKeywordLists
     * @author :lyh
     * @method :post
     * @time   :2023/7/4 10:19
     */
    public function getKeywordLists(){
        $optimizeModel = new DeployOptimize();
        $info = $optimizeModel->read(['project_id'=>$this->user['project_id']],['main_keywords','customer_keywords']);
        return $this->success($info);
    }
}