<?php

namespace App\Imports\Order;

use AlibabaCloud\Gpdb\V20160503\DeleteDatabase;
use App\Models\Docter;
use App\Models\Order;
use App\Models\OrderPatient;
use App\Models\Organization;
use App\Models\Patient;
use App\User;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithProgressBar;

class chatOrder implements ToCollection,WithBatchInserts,WithChunkReading
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function collection(Collection $collection)
    {

        foreach ($collection as $row) {
            if ($row[0] == '订单编号') continue;
            //检查是否有该订单
            $isHave = $this->getValue(new Order(), ['order_sn' => $row[0]], 'id');
            if ($isHave) continue;

            $user_id = $this->getValue(new User(), ['phone' => $row[3]], 'id');
            if (empty($user_id)) {
                echo $row[3] . '用户缺失' . PHP_EOL;
                continue;
            }
            //患者信息
            $patientInfo = Patient::where(['name' => $row[2]])->first();
            if (empty($patientInfo)) {
//                $pp = Patient::where('user_id',$user_id)->get()->toArray();
                echo $row[2] . '患者缺失' . PHP_EOL;
                continue;
            }
            $org_name = substr($row[6],18);
            $org_id = $this->getValue(new Organization(), ['name' => $org_name], 'id');
            if (empty($org_id)) {
                echo $row[6] . '机构缺失' . PHP_EOL;
                continue;
            }

            //如果没有用户不导入该订单
            if (empty($user_id)) {
                Log::info('订单没有用户信息,订单编号： ' . $row[0] . PHP_EOL);
                continue;
            }

            //订单状态
            $status = Order::getStatus();
            //订单状态
            $paystatus = Order::getPayStatus();
            $pay_status = array_search($row[18], $paystatus);
            $order_status = array_search($row[9], $status);
            if($row[9] == '已过期') $order_status = 6;
            if($row[18] == '未付款' || empty($row[18])) $pay_status = 1;


            $docter_id = $this->getValue(new Docter(), ['name' => $row[4]], 'id');
            $evaluate = $row[10] == '已评价' ? 1 : 0;
            $pay_types = [1 => '微信支付', 2 => '余额支付', 3 => '服务包支付'];
            //支付方式
            $payType = array_search($row[16], $pay_types) + 1;
            if ($payType == 1) $payType = 2;

            $orderInfo = [
                'order_sn' => $row[0],
                'patient_id' => $patientInfo->id,
                'user_id' => $user_id,
                'docter_id' => $docter_id,
                'order_status' => $order_status,
                'organization_id' => $org_id,
                'is_evaluate' => $evaluate,
                'is_discount' => $row[11],
                'total_amount' => $row[12] * 100,
                'discount_amount' => $row[13] * 100,
                'payment_amount' => $row[15] * 100,
                'payment_type' => $payType,
                'payment_status' => $pay_status,
                'product_type' => 2,
                'created_at' => $row[19],
            ];
            $order = Order::create($orderInfo);
            $orderId = $order['id'];

            $orderPatient = [
                'patient_id' => $patientInfo->id,
                'name' => $row[0],
                'order_id' => $orderId,
                'patient_id' => $patientInfo->id,
                'address' => $patientInfo->address,
                'sex' => $patientInfo->sex,
                'organization_id' => $org_id,
            ];
            OrderPatient::insert($orderPatient);
        }
        return null;
    }

    public function startRow(): int
    {
        return 2;
    }

    public function batchSize(): int
    {
        return 1000;
    }

    public function chunkSize(): int
    {
        return 1000;
    }

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


}
