<?php

namespace App\Imports;

use App\OracleDOCOrderHeader;
use App\Order;
use App\OrderIssue;
use App\OrderIssueProcessLog;
use App\OrderIssueType;
use Doctrine\DBAL\Connection;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Imports\HeadingRowFormatter;

HeadingRowFormatter::default('none');

class OrderIssueImport implements ToCollection, WithHeadingRow,WithMultipleSheets
{

    public function Collection(Collection $collection)
    {
        $endIS = false;
        if (!isset($collection->toArray()[0]['原始运单号']) || !isset($collection->toArray()[0]['情况说明']) || !isset($collection->toArray()[0]['问题类别']))
            Cache::put('error', '请检查您第一行标题是否存在原始运单号，情况说明，问题类别');
        else $endIS = true;
        $exception = [];
        $sum = 2;
        $orderService = app('orderService');
        $orderIssueService = app('orderIssueService');
        $orderIssues = [];
        if ($endIS) {
            foreach ($collection as $row) {

                if (!$row['原始运单号']) {
                    array_push($exception, ['第' . $sum . '行数据运单号为空!']);
                    $sum++;
                    continue;
                }
                if (!$row['情况说明']) {
                    array_push($exception, ['第' . $sum . '行问题说明为空!']);
                    $sum++;
                    continue;
                }
                if (!$row['问题类别']) {
                    array_push($exception, ['第' . $sum . '行问题类别为空!']);
                    $sum++;
                    continue;
                }
                if(OracleDOCOrderHeader::where('orderNo',$row['原始运单号'])->count() == 0){
                    array_push($exception, ['订单' . $row['原始运单号'] . '运单号不存在']);
                    continue;
                }
                $order = Order::with('issue')->where('code', $row['原始运单号'])->first();

                $orderHeader =OracleDOCOrderHeader::where('orderNo',$row['原始运单号'])->first();
                if (!$row['问题类别']) {
                    array_push($exception, ['订单' . $row['原始运单号'] . '没有对应的问题类别']);
                } else if (!$orderHeader) {
                    array_push($exception, ['订单' . $row['原始运单号'] . '对应的WMS运单不存在']);
                } else if (isset($order->issue) ) {
                    array_push($exception, ['订单' . $row['原始运单号'] . '对应的问题件已存在']);
                } else {
                    $type = OrderIssueType::where('name',$row['问题类别'])->first();
                    if ($type == null) {
                        array_push($exception, ['订单' . $row['原始运单号'] . '没有对应的问题类别']);
                        continue;
                    }
                    $order = $orderService->createOrderByWMSOrderNo($row['原始运单号']);
                    if (!$order) {

                        $orderIssue = $orderIssueService->createOrFind($row['原始运单号']);
                        $orderIssue->order_issue_type_id = $type->id;
                        $orderIssue->save();

                        if (!$orderIssue) {
                            array_push($exception, ['订单' . $row['原始运单号'] . '问题件创建失败']);
                        }
                        OrderIssueProcessLog::create(['order_issue_id'=>$orderIssue['id'],'user_id'=>Auth::user()['id'],'content'=>'创建订单问题件','type'=>'创建']);
                        array_push($orderIssues, $orderIssue);
                    }
                }
                $sum++;
            }
        }
        Cache::put('exception', $exception, 86400);
    }

    /**
     * 该方法是实现上传文件只选中 第一个表
     * ExcelImprot 默认是有多少个分表就执行多少次的分表
     * @return OrderIssueImport[]|array
     */
    public function sheets():array{
        return [0=>new OrderIssueImport()];
    }
}
