<?php

namespace App\Console\Commands;

use App\Facades\UploadService;
use App\Models\MedicalReport;
use App\Models\ServiceUser;
use App\Models\User;
use DB;
use Illuminate\Console\Command;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class ImportMedicalReport extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sync:medical_report {type}';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        $type =  $this->argument("type");
        if ($type == "import") {
            $this->importReport();
        }  else {
            $this->syncReport();
        }
    }

    public function syncReport() 
    {
        $user_num = 0;
        $other_num = 0;
        $name_arr = [];
        $name_arr2 = [];
        $logs = DB::table(table: "health_reports")->get();
        foreach($logs as $index=>$log) {
            $this->info("第{$index}位");
            $user = User::where("name", $log->name)->whereIn("source", User::APP_USER_SOURCE)->first();
            if ($user) {// 用户存在
                $user_num++;
                $report = MedicalReport::where("user_id", $user->id)->where("type", MedicalReport::TYPE_BEFORE)->first();
                if (empty($report)) {// 没有方案前报告
                    MedicalReport::create([
                        "user_id"=>$user->id,
                        "type"=>MedicalReport::TYPE_BEFORE,
                        "content"=>$log->content,
                        "medical_date"=>date("Y-m-d"),
                        "desc"=>"技术导入数据",
                        "anomaly_type"=>1
                    ]);
                }
                if ($report->desc != "技术导入数据") {
                    $name_arr2[] = $log->name;
                }

            
            } else {// 用户不存在
                $other_num++;
                $name_arr[] = $log->name;
            }
        }

        $this->info("用户数量：{$user_num}");
        $this->info("非用户数量：{$other_num}");
        $this->info("非用户名称：".json_encode($name_arr,JSON_UNESCAPED_UNICODE));
        $this->info("已填过用户名称：".json_encode($name_arr2,JSON_UNESCAPED_UNICODE));

    }

    public function importReport()
    {
        
        // $dir = storage_path("health");
        // $sub_dirs = glob($dir . '/*');
        $dir = "medical_reports/health/";
        $sub_dirs = UploadService::getOssDir($dir);
        foreach ($sub_dirs as $index => $sub_dir) {
            $this->info("第{$index}位");
            $arr = explode("/", $sub_dir);
            $name = $arr[count($arr)-2];
            if (empty($name)) dd($sub_dir);
            $log = DB::table("health_reports")->where("name", $name)->first();
            if ($log) continue;
            $files = $sub_dirs = UploadService::getOssFile($sub_dir);
            $urls = [];
            if (count($files) == 0) {
                $this->info($sub_dir);
                dd($index);
            }
            foreach ($files as $file) {
                $this->info($file);
                if (strpos($file, ".pdf")) {
                    dd($index);
                }
                $urls[] = "https://images.health.ufutx.com/".$file;
            }
            DB::table(table: "health_reports")->insert(["name"=>$name, "content"=>json_encode($urls)]);
        }
    }
}
