InquiryMonthlyCount.php 3.1 KB
<?php

namespace App\Console\Commands\MonthlyCount;

use App\Helper\FormGlobalsoApi;
use App\Models\Project\DeployBuild;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class InquiryMonthlyCount extends Command
{
    const STATUS_ERROR = 400;
    public $error = 0;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'yesterday_count';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计昨日数据';

    /**
     * @remark :询盘月报告
     * @name   :handle
     * @author :lyh
     * @method :post
     * @time   :2023/6/30 9:32
     */
    public function handle(){
        $deployModel = new DeployBuild();
        $list = $deployModel->list();
        // 获取上个月的开始时间
        $startTime = Carbon::now()->subMonth()->startOfMonth();
        // 获取上个月的结束时间
        $endTime = Carbon::now()->subMonth()->endOfMonth();
        foreach ($list as  $value){
            //按月统计询盘记录
            $this->inquiryCount($startTime,$endTime,$value['test_domain'],$value['project_id']);
        }
    }

    /**
     * @param $domain
     * @param $project_id
     * @remark :询盘按月统计
     * @name   :inquiryCount
     * @author :lyh
     * @method :post
     * @time   :2023/6/30 14:29
     */
    public function inquiryCount($startTime,$endTime,$domain,$project_id){
        //TODO::上线后注释
        $domain = 'https://demomark.globalso.com/';
        $arr = [];
        $inquiry_list = (new FormGlobalsoApi())->getInquiryList($domain,'',1,100000000);
        //总数
        $arr['total'] = $inquiry_list['data']['total'];
        //数据详情
        $data = $inquiry_list['data']['data'];
        $arr['month_total'] = 0;
        $countryArr = [];
        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']] = 0;
            }
        }
        // 获取当前日期时间
        $arr['month'] = Carbon::now()->subMonth()->format('Y-m');
        arsort($countryArr);
        $top20 = array_slice($countryArr, 0, 15, true);
        $arr['country'] = json_encode($top20);
        $arr['created_at'] = date('Y-m-d H:i:s');
        $arr['updated_at'] = date('Y-m-d H:i:s');
        $arr['project_id'] = $project_id;
        DB::table('gl_inquiry_month_count')->insert($arr);
    }

    /**
     * @remark :流量统计
     * @name   :flowCount
     * @author :lyh
     * @method :post
     * @time   :2023/6/30 14:31
     */
    public function flowCount($startTime,$endTime,$project_id){
        DB::table('gl_count')
            ->where(['project_id'=>$project_id])
            ->where('date','>=',$startTime.' 00:00:00')
            ->where('date','<=',$endTime.' 23:59:59')
            ->sum('pv_num');
    }
}