<?php

namespace App\Imports\Order;

use App\Admin\Actions\backstage\User\service;
use App\Models\Order;
use App\Models\OrderPack;
use App\Models\ServicePack;
use App\Models\User;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToCollection;

class serviceOrder implements ToCollection
{
    /**
    * @param Collection $collection
    */
    public function collection(Collection $collection)
    {
//        0 => "P20200605203723816"
//        1 => "云南超级宝妈健康管理服务包"
//        2 => "0.01"
//        3 => "0.00"
//        4 => "0.01"
//        5 => "18487339976"
//        6 => "已完成"
//        7 => "2020-06-05 20:37:23"

        foreach ($collection as $row){
            if($row[0] == '订单编号') continue;

//            dd($row);
            //用户信息
            $user_id = $this->getValue(new User(),['phone'=>$row[5]],'id');
            $servicePack = servicePack::where(['name'=>$row[1]])->first();

            if(empty($user_id)){
                echo $row[5].'患者缺失'.PHP_EOL;
                Log::info('订单没有用户信息： '.$row[0].' 电话：'.$row[1].' 时间：'.$row[3].PHP_EOL);
                continue;
            }
            if(empty($servicePack)){
                echo $row[1].'服务包缺失'.PHP_EOL;
                continue;
            }
            $status = Order::getStatus();
            //是否用优惠券
//            $row[3] >0 ? $is_discont = 1 : $is_discont=0;
            //订单状态
            $order_status = array_search($row[6],$status);
            $orderInfo = [
                'user_id'=>$user_id,
                'order_status'=>$order_status,
                'product_type'=>6,
                'created_at'=>$row[7],
//                'is_discount'=>$is_discont,
                'total_amount'=>intval($row[2])*100,
                'discount_amount'=>intval($row[3])*100,
                'payment_amount'=>intval($row[4])*100,
            ];
            $order = Order::create($orderInfo);
            $orderId = $order['id'];
            $order_sn = build_sn($orderId);
            Order::where('id', $orderId)->update(['order_sn' => $order_sn]);

            $serviceInfo = [
                'order_id'=>$orderId,
                'service_pack_id'=>$servicePack->id,
                'pack_name'=>$row[2],
                'team_id'=>json_encode($servicePack->team_id),
                'pack_intro'=>$servicePack->intro,
                'appoint_num'=>$servicePack->appoint_num,
                'chat_num'=>$servicePack->chat_num,
                'phone_minutes'=>$servicePack->id,
                'team_id'=>json_encode($servicePack->team_id),
                'vaccine_limit_amount'=>$servicePack->vaccine_limit_amount,
                'nurses_limit_amount'=>$servicePack->nurses_limit_amount,
                'effective_days'=>$servicePack->effective_days,
                'relationship_type'=>1,
                'start_time'=>time(),
                'end_time'=>(time() + 86400 * 3650),
            ];

            OrderPack::insert($serviceInfo);
        }

        return null;
    }

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