<?php

namespace App\Imports\Order;

use App\Models\Order;
use App\Models\OrderPatient;
use App\Models\Organization;
use App\Models\Patient;
use App\Models\Vaccine;
use App\User;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToCollection;
use SebastianBergmann\CodeCoverage\Report\PHP;

class nurseSheet implements ToCollection
{
    /**
     * @param Collection $collection
     */
    public function collection(Collection $collection)
    {
        // todo 缺少患者信息导致患者信息无法确定，支付方式,无订单编号,无法去重,没机构，没用户如何处理
        foreach ($collection as $row) {

            if($row[0] == '就诊人姓名') continue;

//            dd($row);
            //用户信息
            $user_id = $this->getValue(new User(),['phone'=>$row[1]],'id');
            if(empty($user_id)){
                Log::info('订单没有用户信息： '.$row[0].' 电话：'.$row[1].' 时间：'.$row[3].PHP_EOL);
                continue;
            }

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

            //下单时间
            $time = explode(' ',$row[3]);
            $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]);

            $org_id = $this->getValue(new Organization(),['name'=>$row[2]],'id');
            if(empty($org_id)){
                $org_id = 0;
                echo $row[2].'机构缺失'.PHP_EOL;
                continue;
            }


//            $status = Order::getStatus();
            //订单状态
            $status = [2=>'待就诊',4=>'已完成',5=>'用户取消',6=>'已过期',7=>'机构取消'];
            //状态监测
//            $cacheStatus = Cache::get('nurse_status');
//            if(empty($cacheStatus)){
//                $cacheStatus[0] = 'begin';
//                Cache::set('nurse_status',$cacheStatus);
//            } else {
//                dump($cacheStatus);
//            }
//            if(!in_array($row[4],$cacheStatus)){
//                 $cacheStatus[] = $row[4];
//                Cache::set('nurse_status',$cacheStatus);
//            }
//            continue;

            $order_status = array_search($row[4],$status);
            $notes = '';
            if($order_status == 5){
                $notes = '用户取消';
            }
            if($order_status == 6){
                $order_status = 5;
                $notes = '机构取消';
            }
            if(!in_array($row[4],$status)){
                echo $row[4].PHP_EOL;

            }

            //todo 写入计免订单表，订单患者表
            $orderInfo= [
                'user_id'=>$user_id,
                'docter_id'=>0,
                'order_status'=>$order_status,
                'organization_id'=>$org_id,
                'product_type'=>5,
                'patient_id'=>$patientInfo->id,
                'order_notes'=>$notes,
                'created_at'=>$created_at,
            ];

            $order = Order::create($orderInfo);
            $orderId = $order['id'];
            $order_sn = build_sn($orderId);
            Order::where('id', $orderId)->update(['order_sn' => $order_sn]);

            //没有患者名称不存数据
            if(empty($patientInfo)) continue;

            $orderPatient = [
                '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,
            ];

            OrderPatient::insertGetId($orderPatient);
        }
        return null;
    }

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