<?php

namespace App\Imports\Docter;

use App\Models\Organization;
use Maatwebsite\Excel\Concerns\ToModel;
use phpDocumentor\Reflection\Types\Nullable;

class OrganizationInfo implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        //跳过首行
        if (($row[0] == "名称")) {
            return null;
        }
        //地址默认值
        if ($row[5]== null)
        {
            $address = '';
        }else{
            $address = $row[5];
        }
        //经度默认值
        if ($row[6] == null)
        {
            $latitude = 0;
        }else{
            $latitude = $row[6];
        }
        //纬度默认值
        if ($row[7] == null)
        {
            $longitude = 0;
        }else{
            $longitude = $row[7];
        }



        //判断机构等级
        switch ($row[1]){
            case "未定级" :
                $level = 0;
                break;
            case "一级" :
                $level = 1;
                break;
            case "二级" :
                $level = 2;
                break;
            case "三级" :
                $level = 3;
                break;
            default :
                $level = 0;
                break;
        }


        $num = Organization::where('name',$row[0])->count();
        if ($num > 0)
        {
            \Log::info('重复的机构'.$row[0]);
            return null ;
        }
        return new Organization([
            //
            'type' => 1,
            'name' => $row[0],
            'level' => $level,
            'introduce' => $row[2],
            'phone' => $row[3],
            'address' => $address,
            'latitude' => $latitude,
            'longitude' => $longitude,
        ]);
    }
    public function uniqueBy()
    {
        return 'name';
    }
}
