<?php

namespace App\Http\Controllers\Migration;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Models\Department;
use App\Models\Mongo;

class TbCaseController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function show($id){
        switch($id)
        {
            case "mysql2mongo" : $this->mysql2mongo();
        }
    }


    public function mysql2mongo()
    {
        $tb_case = DB::table("tb_case")->limit(50)->get();
        foreach($tb_case as $data){
            $val["case_id"]             = $data->case_id;
            $val["modality"]            = $data->modality;
            $val["studydate"]           = $data->studydate;
            $val["seriesdate"]          = $data->seriesdate;
            $val["studyuid"]            = $data->studyuid;
            $val["seriesuid"]           = $data->seriesuid;
            $val["dicomtag"]            = $data->dicomtag;
            $val["dicomsr"]             = $data->dicomsr;
            $val["caseuniq"]            = $data->caseuniq;
            $val["updatetime"]          = $data->updatetime;
            $val["comcreate"]           = $data->comcreate;
            $val["case_hn"]             = $data->case_hn;
            $val["case_dateregister"]   = $data->case_dateregister;
            $val["case_procedure"]      = $data->case_procedure;
            $val["case_procedurecode"]  = $data->case_procedurecode;
            $val["case_dateappointment"]= $data->case_dateappointment;
            $val["case_physicians01"]   = $data->case_physicians01;
            $val["case_status"]         = $data->case_status;
            $val["case_status_queue"]   = $data->case_status_queue;
            $val                        = $this->json2field($val,$data->case_json);
            $val["case_photo"]          = $data->case_photo;
            $val["case_pdfversion"]     = $data->case_pdfversion;
            $val["case_room"]           = $data->case_room;
            $val["case_roomsort"]       = $data->case_roomsort;
            $val["ready_status"]        = $data->ready_status;
            $val["ready_comment"]       = $data->ready_comment;
            $val["case_vip"]            = $data->case_vip;
            $val["case_semi"]           = $data->case_semi;
            $val["case_booking"]        = $data->case_booking;
            Mongo::table("tb_case")->insert($val);
        }

        dd($tb_case);
    }

    public function json2field($val,$data)
    {
        $json = jsonDecode($data);
        foreach($json as $k1 => $v1)
        {
            $val[$k1]=$v1;
        }
        return $val;
    }


}
