<?php

namespace Modules\Camera\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Modules\Camera\Entities\CameraList;
use Modules\Camera\Enum\CameraEnum;
use Modules\Camera\Services\CameraServices;
use Modules\Mine\Entities\MineList;

class CameraListExcel implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $exceData;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(array $data)
    {
        $this->exceData = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $this->fieldsMap($this->exceData);
    }

    protected function fieldsMap($rows)
    {
        //矿区信息
        $mine_name = trim($rows[0][0]);
        $area_name = isset($rows[0][1]) ? trim($rows[0][1]) : '';

        $mine_parent_id = MineList::where('title', $mine_name)->value('id');
        if (!$mine_parent_id) {
            return false;
        }

        if (!$area_name) {
            $mine_id = $mine_parent_id;
        } else {
            $mine_list = MineList::where(
                [
                    'title'     => $area_name,
                ]
            )->select(
                [
                    'id',
                    'degree'
                ]
            )->first();

            if (!$mine_list) {
                return false;
            }

            if ($mine_parent_id != explode('|', $mine_list->degree)[0]) {
                return false;
            }

            $mine_id = $mine_list->id;
        }

        if (!$mine_id) {
            return false;
        }

        $data = [];
        foreach ($rows as $index => $item) {
            if ($index == 0 || $index == 1) {
                continue;
            }

            $params = [
                'user_name'      => trim($item[1]),
                'password'       => trim($item[2]),
                'ip'             => trim($item[3]),
                'port'           => trim($item[4]),
                'com_number'     => trim($item[5]),
                'video_recorder' => trim($item[10]),
            ];

            $camera_source = $item[8] ? trim($item[8]) : CameraEnum::CAMERA_SOURCE_1;

            if ($camera_source == CameraEnum::CAMERA_SOURCE_3) {
                $camera_url = trim($item[9]);
            } else {
                $camera_url = CameraServices::makeCameraUrl($params);
            }

            //通过ip+端口+通道号作为唯一标识判断数据是否存在
            $query = CameraList::where(
                [
                    'ip'         => $params['ip'],
                    'port'       => $params['port'],
                    'com_number' => $params['com_number'],
                ]
            )->first();

            $sort = $item[6] ? trim($item[6]) : CameraEnum::DEFAULT_SORT;

            if ($query && $params['ip'] && $params['port'] && $params['com_number']) {
                $res = CameraList::where('id', $query->id)->update(
                    [
                        'mine_id'        => $mine_id,
                        'camera_name'    => trim($item[0]),
                        'user_name'      => trim($item[1]),
                        'password'       => trim($item[2]),
                        'ip'             => trim($item[3]),
                        'port'           => trim($item[4]),
                        'com_number'     => trim($item[5]),
                        'sort'           => $sort,
                        'camera_type'    => trim($item[7]),
                        'camera_source'  => $camera_source,
                        'camera_url'     => $camera_url,
                        'video_recorder' => trim($item[10]),
                        'updated_at'     => date('Y-m-d H:i:s'),
                    ]
                );
                continue;
            }

            $data[] = [
                'mine_id'        => $mine_id,
                'camera_name'    => trim($item[0]),
                'user_name'      => trim($item[1]),
                'password'       => trim($item[2]),
                'ip'             => trim($item[3]),
                'port'           => trim($item[4]),
                'com_number'     => trim($item[5]),
                'sort'           => $sort,
                'camera_type'    => trim($item[7]),
                'revert_id'      => CameraEnum::CAMERA_DEFAULT_REVERT_ID,
                'camera_source'  => $camera_source,
                'camera_url'     => $camera_url,
                'video_recorder' => trim($item[10]),
                'created_at'     => date('Y-m-d H:i:s'),
                'updated_at'     => date('Y-m-d H:i:s'),
            ];
        }

        $result = CameraList::insert($data);

        if (!$result) {
            return false;
        }

        return true;
    }
}
