<?php

namespace App\Console\Commands;

use App\Facades\TaskService;
use App\Jobs\SendTemplateNotice;
use App\Models\Collaborator;
use App\Models\DmaProcess;
use App\Models\DmaProcessLog;
use App\Models\Group;
use App\Models\Guide;
use App\Models\Order;
use App\Models\Restaurant;
use App\Models\ServiceRole;
use App\Services\ChatService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class SendImMsg extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'send:im:msg {type}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '发送网易云消息';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Log::info('SendImMsg start');
        //接收参数
        $type = $this->argument('type');
        $date = date('Y-m-d');
        $time = time();
        //超过5点没发送,发送单对单消息给到副教练
        $chatService = new ChatService();
        if ($type == 1) {
            Log::info('SendImMsg start1');
            //查看当前日期设置的餐单,并查看当天有没有发送过消息通知(redis)
            $date = date("Y-m-d", strtotime("+1 day"));
            $guides = Guide::with("order")->where('phase_date', $date)->where("send_status", 0)->get();
            // $key = Guide::SEND_GUIDE_MSG;
            foreach ($guides as $guide) {
                //                $is_send = Redis::get($key.$guide->id);
//                if($is_send){
//                    continue;
//                }
                if ($guide->send_status) {
                    continue;
                }
                // 订单状态非开始中 跳过
                if ($guide->order && $guide->order->status != "STARTING") {
                    continue;
                }
                $group = Group::where('order_id', $guide->order_id)->first();
                if (!$group || !$group->im_chat_id) {
                    continue;
                }
                $params = [];
                $params['chat_id'] = $group->im_chat_id;
                $params['group_name'] = $group->name;
                $chatService->sendImMsgUserToUser($guide->order_id, '系统通知', ($group->order->name ?? "") . '今日发送餐单已超时,请尽快发送', 'https://image.fulllinkai.com/202403/29/dea3e0c27107cdf178635d2a41199e5e.png', 'yfheal://app/push/IMGroup', [ServiceRole::MAIN_COACH, ServiceRole::COACH, ServiceRole::CUSTOMER], $params);
                DmaProcessLog::addUserProcessLog($group->order->user_id ?? 0, $guide->order_id ?? 0, 1, "send_delay_date_guide", "系统发送餐单超时通知", 0, 5);

                $theme = "请发送「用户" . $guide->order->name . "」明天的餐单";
                $processKey = "send_date_guide";
                $start_date = date("Y-m-d");
                $end_date = date("Y-m-d", strtotime("+1 day"));
                $log = DmaProcessLog::where("order_id", $guide->order_id)->where("key_name", $processKey)->where("created_at", [$start_date, $end_date])->first();
                if (empty($log)) {
                    TaskService::AddTask(0, 0, $theme, [2], $guide->order_id, "", "", "", $processKey);
                }
            }
        } elseif ($type == 2) {
            Log::info('SendImMsg start2');
            $guide_counts = Guide::groupBy('order_id')->selectRaw('order_id, COUNT(*) as count')->get();
            foreach ($guide_counts as $item) {
                if ($item->count != 21) {
                    continue;
                }
                $order = Order::where('id', $item->order_id)->first();
                if ($order->status == "STARTING") {
                    $chatService->sendImMsgUserToUser($item->order_id, '系统通知', '请所有服务人员与用户' . $order->name . '进行第二次视频通话', 'https://image.fulllinkai.com/202403/29/dea3e0c27107cdf178635d2a41199e5e.png', '', [ServiceRole::MAIN_COACH, ServiceRole::COACH, ServiceRole::CUSTOMER]);

                    // 待办事项-主教练
                    $theme = "请邀请所有服务人员进行「用户" . $order->name . "」服务的第二次「视频通话」，并将相关视频链接转发给“友福服务”";
                    TaskService::AddTask(0, 0, $theme, [1], $order->id);
                }
            }
        } elseif ($type == 3) {
            Log::info('SendImMsg start3');
            //查看最近前24小时,近半小时完成的订单
            $end_time = $time - 86400;
            $start_time = $end_time - 1800;
            $end_date = date('Y-m-d H:i:s', $end_time);
            $start_date = date('Y-m-d H:i:s', $start_time);
            $orders = Order::where('updated_at', '>=', $start_date)->where('updated_at', '<', $end_date)->where('status', 'FINISHED')->get();
            foreach ($orders as $order) {
                $chatService->sendImMsgUserToUser($order->id, '系统通知', '请所有服务人员与' . ($order->name ?? "") . '用户进行第三次视频通话', 'https://image.fulllinkai.com/202403/29/dea3e0c27107cdf178635d2a41199e5e.png', '', [ServiceRole::MAIN_COACH, ServiceRole::COACH, ServiceRole::CUSTOMER]);

                // 待办事项-主教练
                $theme = "请邀请所有服务人员进行「用户" . $order->name . "」服务的第三次「视频通话」，并将相关视频链接转发给“友福服务”";
                TaskService::AddTask(0, 0, $theme, [1], $order->id);

            }
        } elseif ($type == 4) {
            Log::info('SendImMsg start4');

        }
        Log::info('SendImMsg end');
        return Command::SUCCESS;
    }
}
