<?php

namespace App\Imports\Order;

use App\Models\Docter;
use App\Models\Order;
use App\Models\OrderPatient;
use App\Models\OrderVaccine;
use App\Models\Organization;
use App\Models\Patient;
use App\Models\Vaccine;
use App\User;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Illuminate\Support\Facades\Log;

class vaccineSheet implements ToCollection
{
    /**
    * @param Collection $collection
    */
    public function collection(Collection $collection)
    {
        // todo 缺少患者信息导致患者信息无法确定，支付方式,无订单编号,无法去重,没机构，没用户如何处理
        $i = 1;
        foreach ($collection as $row) {
            if($row[0] == '就诊人姓名') continue;
            $user_id = $this->getValue(new User(),['phone'=>$row[1]],'id');
            if(empty($user_id)){
                echo $row[1].'用户缺失'.PHP_EOL;
                continue;
            }

            $org_id = $this->getValue(new Organization(),['name'=>$row[2]],'id');
            if(empty($org_id)){
                echo $row[2].'机构缺失'.PHP_EOL;
                //todo 没有机构直接跳过
                continue;
            }

            $vaccine_id = $this->getValue(new Vaccine(),['name'=>$row[3]],'id');
            //疫苗为空直接创建疫苗
            if(empty($vaccine_id)){
                $vaccine = ['name'=>$row[3],'org_id'=>$org_id,'type'=>1,'states'=>1];
                $vaccine_id = Vaccine::insertGetId($vaccine);
            }

            //患者信息
            $patientInfo = Patient::where(['name'=>$row[0]])->first();
            if(empty($patientInfo)){
                echo $row[0].'患者缺失'.PHP_EOL;
                continue;
            }

            if(empty($user_id)){
                echo $row[1].'用户不存在'.PHP_EOL;
                Log::info('订单没有用户信息： '.$row[0].' 电话：'.$row[1].' 时间：'.$row[4].PHP_EOL);
               continue;
            }

            $status = Order::getStatus();
            //订单状态
            $order_status = array_search($row[5],$status);
//            $order_sn = build_sn($i);
            //todo 写入计免订单表，订单患者表
            $time = explode(' ',$row[4]);
            $appion_time = explode('-',$time[1]);
            $start =strtotime($time[0].$appion_time[0]);
            //预约时间大于今天
            ($start- 86400) > strtotime('today') ?$createTimer = strtotime('-1 days') : $createTimer = ($start- 86400) > strtotime('today');
            $created_at = date('Y-m-d H:i:s',$createTimer);
            $end = strtotime($time[0].$appion_time[1]);
            $orderInfo = [
                'user_id'=>$user_id,
                'patient_id'=>$patientInfo->id,
                'docter_id'=>0,
                'order_status'=>$order_status,
                'organization_id'=>$org_id,
                'product_type'=>4,
                'created_at'=>$created_at
            ];
            $order = Order::create($orderInfo);
            $orderId = $order['id'];
            $order_sn = build_sn($order['id']);
            Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
            //没有患者名称不存数据
            if(empty($patientInfo)) continue;

            $orderPatient = [
                'patient_id'=>$patientInfo->id,
                'name'=>$row[0],
                'order_id'=>$orderId,
                'patient_id'=>$patientInfo->id,
                'address'=>$patientInfo->address,
                'sex'=>$patientInfo->sex,
                'appoint_start_time'=>$start,
                'appoint_end_time'=>$end,
                'organization_id'=>$org_id,
            ];


            $orderPid = OrderPatient::insertGetId($orderPatient);

            $vaccineOrder = [
                'vaccine_id'=>$vaccine_id,
                'order_id'=>$orderId,
                'order_patient_id'=>$orderPid,
                'vaccine_name'=>$row[3],
            ];
            OrderVaccine::insert($vaccineOrder);
        }
        return null;
    }

    public function getValue($model,$where,$field)
    {
        return $model->where($where)->value($field);
    }
}
