InquiryForwardDayCount.php 2.0 KB
<?php

namespace App\Console\Commands\Inquiry;

use App\Models\Inquiry\ForwardCount;
use App\Models\Inquiry\InquiryInfo;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '每日统计询盘转发数量';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $day_start = date('Y-m-d', strtotime('-1 day'));
        $day_arr = explode('-', $day_start);

        $year = $day_arr[0];
        $month = $day_arr[1];
        $day = $day_arr[2];

        $manage_ids = InquiryInfo::select('operator_id')->where('status', InquiryInfo::STATUS_FINISH)->where('operator_id', '>', 0)->orderBy('operator_id', 'asc')->distinct()->pluck('operator_id')->toArray();
        foreach ($manage_ids as $mid) {
            $day_count = ForwardCount::where('manage_id', $mid)->where('year', $year)->where('month', $month)->where('day', $day)->first();
            if (!$day_count) {
                $day_count = new ForwardCount();
                $day_count->manage_id = $mid;
                $day_count->year = $year;
                $day_count->month = $month;
                $day_count->day = $day;
            }
            $day_count->count = InquiryInfo::where('status', InquiryInfo::STATUS_FINISH)->where('operator_id', $mid)->where('updated_at', 'like', $day_start . '%')->count('id') ?? 0;
            $day_count->save();
        }

        Cache::forget('inquiry_manage_count');

        $this->output('success');
    }

    public function output($message)
    {
        echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
    }
}