MonthCount.php 8.1 KB
<?php
/**
 * @remark :
 * @name   :CountProject.php
 * @author :lyh
 * @method :post
 * @time   :2024/1/8 9:03
 */

namespace App\Console\Commands\MonthlyCount;

use App\Helper\FormGlobalsoApi;
use App\Models\Com\UpdateOldInfo;
use App\Models\Domain\DomainInfo;
use App\Models\Inquiry\InquiryFormData;
use App\Models\Project\Project;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Models\HomeCount\MonthCount AS MonthCountModel;

class MonthCount extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'month_counts';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '每天生成月统计记录';

    public function handle(){
        $list = DB::table('gl_project')->where('gl_project.extend_type','=',0)
            ->whereIn('gl_project.type',[1,2,3,4,6])
            ->leftJoin('gl_project_deploy_build', 'gl_project.id', '=', 'gl_project_deploy_build.project_id')
            ->leftJoin('gl_project_deploy_optimize', 'gl_project.id', '=', 'gl_project_deploy_optimize.project_id')
            ->select($this->selectParam())->get()->toArray();
        foreach ($list as $v) {
            $v = (array)$v;
            if($v['type'] == Project::TYPE_ZERO){
                continue;
            }
            if($v['is_upgrade'] == 1){
                $oldModel = new UpdateOldInfo();
                $info = $oldModel->read(['project_id' => $v['id']]);
                if ($info !== false) {
                    $url = $info['old_domain_online'];
                }else{
                    continue;
                }
            }else{
                $domainInfo = new DomainInfo();
                if(!empty($v['domain'])){
                    $info = $domainInfo->read(['id'=>$v['domain']]);
                    if($info !== false){
                        $url = $info['domain'];
                    }
                }else{
                    $url = $v['test_domain'];
                }
            }
            ProjectServer::useProject($v['id']);
            echo date('Y-m-d H:i:s') . '项目id:'.$v['id'] . PHP_EOL;
            $this->count($v['id'], $url ,$v['is_upgrade'] ?? 0);
            DB::disconnect('custom_mysql');
        }
    }

    /**
     * @name   :(查询参数设置)selectParam
     * @author :lyh
     * @method :post
     * @time   :2023/6/14 15:00
     */
    public function selectParam(){
        $select = [
            'gl_project.id AS id',
            'gl_project.type AS type',
            'gl_project.extend_type AS extend_type',
            'gl_project_deploy_build.test_domain AS test_domain',
            'gl_project.is_upgrade AS is_upgrade',
            'gl_project_deploy_optimize.domain AS domain',
            'gl_project_deploy_build.project_id AS project_id',
            'gl_project.cooperate_date AS cooperate_date',
            'gl_project_deploy_build.service_duration AS service_duration',
        ];
        return $select;
    }
    /**
     * @remark :日统计记录
     * @name   :count
     * @author :lyh
     * @method :post
     * @time   :2024/1/8 9:05
     */
    public function count($project_id,$url,$is_upgrade = 0){
        $arr = [];
        $v = ['month'=>date('Y-m')];
        $monthCountModel = new MonthCountModel();
        $info = $monthCountModel->read(['month'=>$v['month'],'project_id'=>$project_id]);
        // 获取当月开始时间
        $start = date('Y-m-01', strtotime($v['month']));
        // 获取当月结束时间
        $end = date('Y-m-t', strtotime($v['month']));
        $arr['project_id'] = $project_id;
        $res = (new FormGlobalsoApi())->getMonthInquiry($url,$v['month'],$is_upgrade ?? 0);
        $arr['total'] = $arr['month_total'] = 0;
        if(isset($res['data']['count'])){
            echo date('Y-m-d H:i:s') . '数据:'.$res['data']['count'] . PHP_EOL;
            $arr['month_total'] = $res['data']['count'] + InquiryFormData::getCount([$start.' 00:00:00',$end.' 00:00:00']);
        }
        //获取上一个的count
        $previousMonth = date('Y-m', strtotime($v['month'] . ' -1 month'));
        $previousInfo = $monthCountModel->read(['month'=>$previousMonth,'project_id'=>$project_id]);
        if($previousInfo === false){
            $arr['total'] = $arr['month_total'];
        }else{
            $arr['total'] = $arr['month_total'] + ($previousInfo['total'] ?? 0);
        }
        $country = [];
        if(isset($res['data']['data'])){
            $country = $res['data']['data'];
        }
        $countryData = InquiryFormData::getCountryCount([$start.' 00:00:00',$end.' 00:00:00']);
        foreach ($countryData as $v1){
            if(isset($country[$v1['country']])){
                $country[$v1['country']] += $v1['count'];
            }else{
                $country[$v1['country']] = $v1['count'];
            }
        }
        $arr['country'] = json_encode($country);
        $arr['month'] = $v['month'];
        $arr = $this->pv_ip($arr,$start,$end,$project_id);
        $arr = $this->sourceCount($arr,$start,$end);
        if($info === false){
            $selectedDate = $start;
            $firstDayOfNextMonth = date('Y-m-01 01:00:00', strtotime("$selectedDate +1 month"));
            $arr['created_at'] = $firstDayOfNextMonth;
            $arr['updated_at'] = $firstDayOfNextMonth;
            $monthCountModel->insert($arr);
        }else{
            $monthCountModel->edit($arr,['id'=>$info['id']]);
        }
        echo date('Y-m-d H:i:s') . 'end' . PHP_EOL;
    }

    /**
     * @remark :本月询盘总量
     * @name   :month_total
     * @author :lyh
     * @method :post
     * @time   :2024/1/8 11:02
     */
    public function pv_ip(&$arr,$start,$end,$project_id){
        $pv_ip = DB::table('gl_count')
            ->where(['project_id'=>$project_id])
            ->where('date','>=',$start.' 00:00:00')
            ->where('date','<=',$end.' 23:59:59')
            ->select(DB::raw('SUM(pv_num) as pv_num'), DB::raw('SUM(ip_num) as ip_num'),DB::raw('SUM(inquiry_num) as inquiry_num'))
            ->first();
        $arr['pv'] = $pv_ip->pv_num;
        $arr['ip'] = $pv_ip->ip_num;
        if($arr['ip'] != 0){
            $arr['rate'] = round((($arr['month_total'] ?? 0) / $arr['ip']) * 10,2);
        }
        return $arr;
    }
    /**
     * @remark :来源访问前8
     * @name   :sourceCount
     * @author :lyh
     * @method :post
     * @time   :2023/6/30 16:14
     */
    public function sourceCount(&$arr,$startTime,$endTime){
        //访问来源前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'] = json_encode($source);
        //访问国家前15
        $source_country = DB::connection('custom_mysql')->table('gl_customer_visit')
            ->select('country',DB::raw('COUNT(*) as ip'),DB::raw('SUM(depth) as pv'))
            ->groupBy('country')
            ->whereBetween('updated_date', [$startTime,$endTime])
            ->orderBy('ip','desc')->limit(15)->get()->toArray();
        $arr['source_country'] = json_encode($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'] = json_encode($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'] = json_encode($referrer_port);
        return $arr;
    }

}