<?php

namespace App\Console\Commands;

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

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

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

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

        // 获取当前日期的星期几
        $day_of_week = date('w');

        if ($day_of_week == 0) {
            return Command::SUCCESS;
        }

        //获取所有订餐人员
        $list = Restaurant::whereIn('status', [Restaurant::STATUS_PAY, Restaurant::STATUS_WAIT, Restaurant::STATUS_FINISHED, Restaurant::STATUS_EVALUATE])
            ->groupBy('user_id')
            ->pluck('user_id')
            ->toArray();

        //查看用户是否设置开关
        $redis_switch_key = Restaurant::REDIS_SEND_SWITCH;
        $switch_uids = Redis::hgetall($redis_switch_key);
        //查看哪些用户关闭了订餐提醒
        $uids = [];
        foreach ($switch_uids as $key => $value) {
            if ($value == 1) {
                continue;
            }
            $uids[] = $key;
        }
        //将所有历史订餐人员和设置关闭提醒的人取差集,如果已定餐人员不设置关闭，默认发送通知
        $diff = array_diff($list, $uids);

        $date = date('Y-m-d');
        //查看当天已定餐人员，不重复发送通知
        $today_user = Restaurant::where('date', $date)
            ->whereIn('status', [Restaurant::STATUS_PAY, Restaurant::STATUS_WAIT, Restaurant::STATUS_FINISHED, Restaurant::STATUS_EVALUATE])
            ->where('diet_type', $diet_type)
            ->groupBy('user_id')
            ->pluck('user_id')
            ->toArray();
        $user_ids = array_diff($diff, $today_user);

        foreach ($user_ids as $user_id) {
            $open_id = Wechat::where('user_id', $user_id)->value('openid');
            Log::info('SendReserveRestaurantMsg:user_id:' . $user_id . "|open_id:" . $open_id);
            //            $open_id = 'oHGap6F1HR-5q-JdRKrgb6DwztWQ';
            if (!$open_id) {
                continue;
            }
            $diet = '午餐订餐';
            $reserve_time = '09:00~' . Restaurant::LUNCH_RESERVE_TIME;
            if ($diet_type == Restaurant::DIET_TYPE_DINNER) {
                $diet = '晚餐订餐';
                $reserve_time = '14:00~' . Restaurant::DINNER_RESERVE_TIME;
            }
            //发送模板消息通知 url = /api/h5/wechat/user/auth?target_path=cateringApplyFor
            $params = [
                'touser' => $open_id,
                'template_id' => config('wechat.tpls.reserve_restaurant'),
                'url' => config('app.url') . '/order/#/cateringApplyFor',
                'data' => [
                    'thing8' => ['value' => '友福健康'],
                    'thing12' => ['value' => '订餐提醒'],
                    'thing14' => ['value' => $diet],
                    'time13' => ['value' => $reserve_time],
                ]
            ];
            SendTemplateNotice::dispatch($params)->onQueue('health');
        }
        Log::info('SendReserveRestaurantMsg end');
        return Command::SUCCESS;
    }
}
