<?php

namespace App\Console\Commands;

use App\Jobs\SendTemplateNotice;
use App\Models\Collaborator;
use App\Models\Restaurant;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class SendEvaluateRestaurantMsg extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'send:evaluate:restaurant:msg {diet_type}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '发送评价通知';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Log::info('SendEvaluateRestaurantMsg start');
        //接收参数
        $diet_type = $this->argument('diet_type');

        $date = date('Y-m-d');
        //查看当天已定餐人员，不重复发送通知
        $today_user = Restaurant::where('date', $date)
            ->where('status', Restaurant::STATUS_TAKE)
            ->where('diet_type', $diet_type)
            ->groupBy('user_id')
            ->pluck('user_id')
            ->toArray();

        foreach ($today_user as $user_id) {
            $open_id = Collaborator::where('user_id', $user_id)->value('openid');

            Log::info('SendEvaluateRestaurantMsg:user_id:' . $user_id . "|open_id:" . $open_id);
            //            $open_id = 'oHGap6F1HR-5q-JdRKrgb6DwztWQ';
            if (!$open_id) {
                continue;
            }
            //发送模板消息通知 url = /api/h5/wechat/user/auth?target_path=cateringApplyFor
            $params = [
                'touser' => $open_id,
                'template_id' => config('wechat.tpls.evaluate_restaurant'),
                'url' => config('app.url') . '/order/#/cateringApplyFor',
                'data' => [
                    'thing2' => ['value' => '订餐评价'],
                    'time6' => ['value' => $date],
                ]
            ];
            SendTemplateNotice::dispatch($params)->onQueue('health');
        }
        Log::info('SendEvaluateRestaurantMsg end');
        return Command::SUCCESS;
    }
}
