<?php

namespace App\Console\Commands;

use App\Models\DmaProcessLog;
use App\Models\MedicalReport;
use App\Models\Order;
use App\Facades\CommonService;
use App\Models\OtherHealthFile;
use App\Models\ServiceRole;
use App\Services\ChatService;
use App\Facades\TaskService;
use cccdl\yunxin_sdk\Im\Msg;
use Illuminate\Console\Command;
use Log;

class MedicalReportNotice extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'order:finished:medical_report';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Log::info("复检报告提醒提醒");
        $nowDate = date("Y-m-d");
        $orders = Order::whereDoesntHave("afterMedicalReport")->where("service_status", "FINISHED")->get();
        foreach ($orders as $order) {
            $log_at = DmaProcessLog::where("order_id", $order->id)->where("key_name", "mark_order_finish")->orderBy("id", "desc")->value("created_at");
            if (empty($log_at))
                continue;
            $log_at = $log_at->toDateString();
            $day = CommonService::diffDays($nowDate, $log_at);
            Log::info("订单id:" . $order->id . " day=" . $day);
            if ($day == 35) {
                //                 $imMsg = new Msg(config('chat.im.app_id'), config('chat.im.app_secret'));
//                 $imMsg->sendMsg(make_wangyiyun_accid($order->group->owner_id), 1, $order->group->im_chat_id, 0, json_encode([
//                     'msg' => "尊敬的【{$order->name}】：

                // 您好！您的健康方案已结束一段时间，我们始终关注您的身体状况。为了确保您的健康持续得到有效管理，建议您近期安排一次身体复查，并将报告提交至本群。我们期待了解您的最新健康状况，以便为您提供更精准的建议和支持。

                // 感谢您的理解与配合！

                // 祝您健康愉快！"
//                 ]));



            } elseif ($day == 23 || $day == 26 || $day == 29) {
                //                 $theme = "用户【" . $order->name . "】的群，复检倒计时7天！
// 请跟进【" . $order->name . "】上传【方案后复检报告】，请所有服务人员即刻跟进，务必确保本周内100%完成";

                $file = OtherHealthFile::where("user_id", $order->user_id)->where("type", 1)->first();
                $report = MedicalReport::where("user_id", $order->user_id)->where("order_id", $order->id)->where("type", 1)->first();
                if (empty($file) && empty($report)) {
                    $theme = "请提醒用户【" . $order->name . "】上传复检报告";
                    TaskService::AddTask(0, 0, $theme, [1, 2, 3], $order->id);

                    $chatService = new ChatService();
                    $chatService->sendImMsgUserToUser($order->id, '系统通知', $theme, '', '', [ServiceRole::MAIN_COACH, ServiceRole::COACH, ServiceRole::CUSTOMER]);

                }
            }
        }
        return Command::SUCCESS;
    }
}


