<?php

namespace App\Console\Commands;

use App\Jobs\SendTemplateNotice;
use App\Models\Guide;
use Illuminate\Console\Command;

class DailyGuide extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'guide:notice';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '每日餐单提醒';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        //当天餐单
        $now_date = date("Y-m-d");
        Guide::with('officialWechat', 'order')->where('phase_date', $now_date)->chunk(100, function ($guides) {
            foreach ($guides as $guide) {
                $param = [
                    'touser' => $guide->officialWechat?$guide->officialWechat->openid:null,
                    'template_id' => config('wechat.tpls.daily_guide_notice'),
                    'url' => '',
                    'miniprogram' => [
                        'appid' => config('wechat.mini_program.app_id'),
                        'pagepath' => 'pages/tabBar/serve',
                    ],
                    'data' => [
                        'first' => ['value' => '您的餐单还未完成，继续加油！'],
                        'keyword1' => ['value' => $guide->order->trade_no],
                        "keyword2" => ['value' => $guide->order->desc],
                        "keyword3" => ['value' => $guide->phase_title],
                        "keyword4" => ['value' => "当天内"],
                        'remark' => ['value' => "点击进入查看详情"]
                    ]
                ];
                SendTemplateNotice::dispatch($param)->onQueue('health');
            }
        });
        //注释
        return Command::SUCCESS;
    }
}
