<?php

namespace Modules\Camera\Http\Controllers;

use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Modules\Admin\Auxiliary\View\FromAuxiliary;
use Modules\Admin\Auxiliary\View\TableAuxiliary;
use Modules\Admin\Auxiliary\View\TreeAuxiliary;
use Modules\Admin\Http\Controllers\BaseController;
use Modules\Camera\Entities\CameraFieldList;
use Modules\Camera\Entities\CameraList;
use Modules\Camera\Enum\CameraEnum;
use Modules\Camera\Jobs\CameraListExcel;
use Modules\Camera\Services\CameraServices;
use Modules\Mine\Entities\MineList;
use Modules\Mine\Entities\WorkingSurface;
use Modules\Mine\Entities\WorkingSurfaceCamera;
use Modules\Mine\Services\MineServices;
use Modules\Mine\Services\SurfaceServices;
use DB;

class CameraController extends BaseController
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index()
    {
        return view('camera::index');
    }

    //摄像头列表
    public function cameraList(Request $request)
    {
        $this->menusActive[0] = 'adminCamera';
        $this->menusActive[1] = 'adminCameraList';

        //查询当前登录用户权限内的区域
        $user = DB::table('users')->where('staff_num',$request->user()->staff_num)->first();
        $mine_array = explode(';',$user->mine_role);//权限内的矿区
        $mine_all = DB::table('mine_list')->where('deleted_at',null)->get();//所有区域
        $mine_use = [];//权限内矿区下所有区域
        foreach($mine_all as $k=>$v){
            if(in_array(explode('|',$v->degree)[0],$mine_array)){
                $mine_use[] = $v->id;
            }
        }

        $camera_list = CameraList::join(
            'mine_list', 'camera_list.mine_id', '=', 'mine_list.id'
        )->whereIn('mine_list.id',$mine_use)->orderBy('camera_list.sort', 'desc');

        if ($request->has('mine_name') && $request->input('mine_name')) {
            $mine_id = MineList::where('title', 'like', '%' . $request->input('mine_name') . '%')->where('parent_id', 0)->get();
            if(sizeof($mine_id) == 0){
                $camera_list = $camera_list->where('mine_list.title', 'like', '%' . $request->input('mine_name') . '%')->where('parent_id', 0);
            }
            $camera_list = $camera_list->where(function ($query) use ($mine_id) {
                foreach($mine_id as $k=>$v){
                    $query->orWhere('mine_list.degree',  $v->id );
                    $query->orWhere('mine_list.degree', 'like', $v->id . '|' . '%');
                }
            });

//            foreach($mine_id as $k=>$v){
//                $camera_list = $camera_list->orWhere('mine_list.degree',  $v->id );
//                $camera_list = $camera_list->orWhere('mine_list.degree', 'like', $v->id . '|' . '%');
//            }


//              $camera_list = $camera_list->orWhere('mine_list.degree', 'like', 1 . '|' . '%');
//              $camera_list = $camera_list->orWhere('mine_list.degree', 'like', 2 . '|' . '%');
//            $camera_list = $camera_list->where('mine_list.title', 'like', '%' . $request->input('mine_name')  . '%')->where('parent_id', 0);
        }
        if ($request->has('area_name') && $request->input('area_name')) {
            $area_id = MineList::where('title', 'like', '%' . $request->input('area_name') . '%')->get();
            if(sizeof($area_id) == 0){
                $camera_list = $camera_list->where('mine_list.title', 'like', '%' . $request->input('area_name') . '%')->where('parent_id','!=', 0);
            }
            $camera_list = $camera_list->where(function ($query) use ($area_id) {
                foreach($area_id as $k=>$v){
                    $query->orWhere('mine_list.degree', $v->id)->where('mine_list.parent_id',0);
                    $query->orWhere('mine_list.degree', 'like', '%' . '|' .$v->id);
                }
            });

//            foreach($area_id as $k=>$v){
////                $camera_list = $camera_list->orWhere('mine_list.degree',  $v->id );
//                $camera_list = $camera_list->orWhere('mine_list.degree', $v->id)->where('mine_list.parent_id',0);
//                $camera_list = $camera_list->orWhere('mine_list.degree', 'like', '%' . '|' .$v->id);
//            }


//            $camera_list = $camera_list->where('title', 'like', '%' . $request->input('area_name') . '%');
//            dd($camera_list);exit;
//            $camera_list = $camera_list->where('mine_list.degree', 'like', '%' . '|' . $area_id);
//            $camera_list = $camera_list->where('mine_list.title', 'like', '%' . $request->input('mine_name')  . '%')->where('parent_id','!=', 0);
        }
        $data['mine_default'] = "";
        if ($request->has('mine_id') && $request->input('mine_id')) {
            $camera_list = $camera_list->where('mine_list.id',$request->input('mine_id'));
            $data['mine_default'] = $request->input('mine_id');
        }

        if ($request->has('camera_name') && $request->input('camera_name')) {
            $camera_list = $camera_list->where('camera_list.camera_name', 'like', '%' . $request->input('camera_name') . '%');
        }

        if ($request->has('camera_type') && $request->input('camera_type')) {
            $camera_list = $camera_list->where('camera_list.camera_type', $request->input('camera_type'));
        }

        if ($request->has('camera_source') && $request->input('camera_source')) {
            $camera_list = $camera_list->where('camera_list.camera_source', $request->input('camera_source'));
        }

        $camera_list = $camera_list->select(
            'camera_list.id',
            'camera_list.camera_url',
            'camera_list.camera_name',
            'camera_list.revert_id',
            'camera_list.sort',
            'camera_list.camera_type',
            'camera_list.camera_source',
            'camera_list.is_show',
            'mine_list.title',
            'mine_list.degree'
        )->paginate(CameraEnum::PAGE_SIZE);

        foreach ($camera_list as $key => $val) {
            $camera_list[$key]->parent_title = MineList::where('id', explode('|', $val->degree)[0])->value('title');
        }

        $tableObj             = new TableAuxiliary('camera/list', $camera_list);

//        $tableObj->search('input', 'mine_name', '矿区名称');
//        $tableObj->search('input', 'area_name', '区域名称');
        $tableObj->search('input', 'camera_name', '摄像头名称');
        $tableObj->search('select', 'camera_type', '摄像头类型', CameraEnum::CAMERA_TYPE_EXCHANGE);
        $tableObj->search('select', 'camera_source', '摄像头来源', CameraEnum::CAMERA_SOURCE_EXCHANGE);
        $tableObj->topActions = ['add', 'import', 'importTemplate'];
        $tableObj->column('parent_title', '矿区名称');
        $tableObj->column('title', '区域名称');
        $tableObj->column('camera_name', '摄像头名称');
//        $tableObj->column('camera_type', '摄像头类型', function ($camera_type) {
//            return CameraEnum::CAMERA_TYPE_EXCHANGE[$camera_type];
//        });
        $tableObj->column('camera_url', '链接');
//        $tableObj->column('camera_source', '摄像头来源', function ($camera_source) {
//            return CameraEnum::CAMERA_SOURCE_EXCHANGE[$camera_source];
//        });
//        $tableObj->column('revert_id', '流媒体id');

        $tableObj->column('is_show', '是否显示', function ($is_show, $item) {
            return '<span class="layui-form"><input type="checkbox" name="is_show" data-href="camera/list/edit?type=show&id=' . $item['id'] . '" lay-skin="switch" lay-text="是|否" ' . ($is_show == CameraEnum::IS_SHOW_YES ? 'checked' : '') . '></span>';
        });
        $tableObj->column('sort', '排序');
        $data['pagesize']           = ceil($camera_list->total()/CameraEnum::PAGE_SIZE);
        $data['path']           = $tableObj->path;
        $data['items']          = $tableObj->items;
        $data['columns']        = $tableObj->columns;
        $data['searchs']        = $tableObj->searchColumns;
        $data['primaryKey']     = $tableObj->primaryKey;
        $data['displayActions'] = $tableObj->displayActions;
        if (is_string($data['displayActions']['value'])) {
            $data['displayActions']['value'] = [$data['displayActions']['value']];
        }
        $data['actionBtns']          = $tableObj->actionBtns;
        $data['actionBtnsAttribute'] = $tableObj->actionBtnsAttribute;
        $data['topActions']          = $tableObj->topActions;
        $data['displayActionOthers'] = $tableObj->displayActionOthers;
        $data['topActionOthers']     = $tableObj->topActionOthers;
        $data                        += \Illuminate\Support\Facades\Request::except('_token');

        //区域列表
        $mine_list = DB::table('mine_list')->whereIn('id',$mine_array)->where('parent_id',0)->where('deleted_at',null)->get();
        $tree_data = [];
        foreach($mine_list as $k=>$v){
            $tree_data[$v->id]['id'] = $v->id;
            $tree_data[$v->id]['name'] = $v->title;
            $child_list = DB::table('mine_list')->where('parent_id',$v->id)->where('deleted_at',null)->orderBy('sort','desc')->get();
            if(count($child_list)>0){
                $tree_data[$v->id]['type'] = 'folder';
                $tree_data[$v->id]['additionalParameters']['children'] = $this->mineTree($child_list,$v->id);
            }else{
                $tree_data[$v->id]['type'] = 'item';
            }
        }
        $data['tree_data'] = $tree_data;
        return $this->view('camera::camera_list', $data);
    }

    //摄像头管理区域列表ajax
    public function mineList(Request $request){
        //查询当前登录用户权限内的区域
        $user = DB::table('users')->where('staff_num',$request->user()->staff_num)->first();
        $mine_array = explode(';',$user->mine_role);//权限内的矿区
        $mine_all = DB::table('mine_list')->where('deleted_at',null)->get();//所有区域
        $mine_use = [];//权限内矿区下所有区域
        foreach($mine_all as $k=>$v){
            if(in_array(explode('|',$v->degree)[0],$mine_array)){
                $mine_use[] = $v->id;
            }
        }

        //区域列表
        $mine_list = DB::table('mine_list')->whereIn('id',$mine_use)->where('parent_id',0)->where('deleted_at',null)->get();

        $tree_data = [];
        foreach($mine_list as $k=>$v){
            $tree_data[$v->id]['id'] = $v->id;
            $tree_data[$v->id]['name'] = $v->title;
            $child_list = DB::table('mine_list')->where('parent_id',$v->id)->where('deleted_at',null)->get();
            if(count($child_list)>0){
                $tree_data[$v->id]['type'] = 'folder';
                $tree_data[$v->id]['additionalParameters']['children'] = $this->mineTree($child_list,$v->id);
            }else{
                $tree_data[$v->id]['type'] = 'item';
            }
        }
        return $tree_data;
    }

    //摄像头管理区域列表ajax
    public function cameraUpdate(Request $request){
        //查询当前登录用户权限内的区域
        $user = DB::table('users')->where('staff_num',$request->user()->staff_num)->first();
        $mine_array = explode(';',$user->mine_role);//权限内的矿区
        $mine_all = DB::table('mine_list')->where('deleted_at',null)->get();//所有区域
        $mine_use = [];//权限内矿区下所有区域
        foreach($mine_all as $k=>$v){
            if(in_array(explode('|',$v->degree)[0],$mine_array)){
                $mine_use[] = $v->id;
            }
        }

        $camera_list = CameraList::join(
            'mine_list', 'camera_list.mine_id', '=', 'mine_list.id'
        )->whereIn('mine_list.id',$mine_use)->orderBy('camera_list.sort', 'desc');

        if ($_GET['mine_id'] != '' && $_GET['mine_id'] != null) {
            $camera_list = $camera_list->where('camera_list.mine_id', $_GET['mine_id']);
        }

        if ($_GET['camera_name'] != '' && $_GET['camera_name'] != null) {
            $camera_list = $camera_list->where('camera_list.camera_name', 'like', '%' . $_GET['camera_name'] . '%');
        }

        if ($_GET['camera_type'] != 0) {
            $camera_list = $camera_list->where('camera_list.camera_type', $_GET['camera_type']);
        }

        if ($_GET['camera_source'] != 0) {
            $camera_list = $camera_list->where('camera_list.camera_source', $_GET['camera_source']);
        }

        $camera_list = $camera_list->select(
            'camera_list.id',
            'camera_list.camera_url',
            'camera_list.camera_name',
            'camera_list.revert_id',
            'camera_list.sort',
            'camera_list.camera_type',
            'camera_list.camera_source',
            'camera_list.is_show',
            'mine_list.title',
            'mine_list.degree'
        )->paginate(CameraEnum::PAGE_SIZE);

        foreach ($camera_list as $key => $val) {
            $camera_list[$key]->parent_title = MineList::where('id', explode('|', $val->degree)[0])->value('title');
            $val->camera_type =   CameraEnum::CAMERA_TYPE_EXCHANGE[$val->camera_type];
            $val->camera_source =   CameraEnum::CAMERA_SOURCE_EXCHANGE[$val->camera_source];
        }

        return $camera_list;
    }

    //摄像头管理区域列表ajax
    public function camerapage(Request $request){
        //查询当前登录用户权限内的区域
        $user = DB::table('users')->where('staff_num',$request->user()->staff_num)->first();
        $mine_array = explode(';',$user->mine_role);//权限内的矿区
        $mine_all = DB::table('mine_list')->where('deleted_at',null)->get();//所有区域
        $mine_use = [];//权限内矿区下所有区域
        foreach($mine_all as $k=>$v){
            if(in_array(explode('|',$v->degree)[0],$mine_array)){
                $mine_use[] = $v->id;
            }
        }

        $camera_list = CameraList::join(
            'mine_list', 'camera_list.mine_id', '=', 'mine_list.id'
        )->whereIn('mine_list.id',$mine_use)->orderBy('camera_list.sort', 'desc');

        if ($_GET['mine_id'] != '' && $_GET['mine_id'] != null) {
            $camera_list = $camera_list->where('camera_list.mine_id', $_GET['mine_id']);
        }

        if ($_GET['camera_name'] != '' && $_GET['camera_name'] != null) {
            $camera_list = $camera_list->where('camera_list.camera_name', 'like', '%' . $_GET['camera_name'] . '%');
        }

        if ($_GET['camera_type'] != 0) {
            $camera_list = $camera_list->where('camera_list.camera_type', $_GET['camera_type']);
        }

        if ($_GET['camera_source'] != 0) {
            $camera_list = $camera_list->where('camera_list.camera_source', $_GET['camera_source']);
        }

        $camera_list = $camera_list->select(
            'camera_list.id',
            'camera_list.camera_url',
            'camera_list.camera_name',
            'camera_list.revert_id',
            'camera_list.sort',
            'camera_list.camera_type',
            'camera_list.camera_source',
            'camera_list.is_show',
            'mine_list.title',
            'mine_list.degree'
        )->paginate(CameraEnum::PAGE_SIZE);

        foreach ($camera_list as $key => $val) {
            $camera_list[$key]->parent_title = MineList::where('id', explode('|', $val->degree)[0])->value('title');
            $val->camera_type =   CameraEnum::CAMERA_TYPE_EXCHANGE[$val->camera_type];
            $val->camera_source =   CameraEnum::CAMERA_SOURCE_EXCHANGE[$val->camera_source];
        }

        return $camera_list;
    }

    public function mineTree($regions, $parent_id)
    {
        $arr = [];
        foreach ($regions as $key => $value) {
            $arr[$key]['id'] = $value->id;
            $arr[$key]['name'] = $value->title;
            $mine_child = DB::table('mine_list')->where('parent_id', $value->id)->where('deleted_at',null)->get();
            if (count($mine_child) > 0) {
                $arr[$key]['type'] = 'folder';
                $arr[$key]['additionalParameters']['children'] = self::mineTree($mine_child, $value->id);
            } else {
                $arr[$key]['type'] = 'item';
            }
        }
        return array_values($arr);
    }

    //查看摄像头列表详情
    public function cameraListView(Request $request)
    {
        if (!$request->has('id')) {
            return $this->redirect('admin/camera/list');
        }

        $camera_list = CameraList::join(
            'mine_list', 'camera_list.mine_id', '=', 'mine_list.id'
        )->where('camera_list.id', $request->input('id'))->select(
            'camera_list.id',
            'camera_list.ip',
            'camera_list.port',
            'camera_list.user_name',
            'camera_list.password',
            'camera_list.com_number',
            'camera_list.camera_url',
            'camera_list.camera_name',
            'camera_list.revert_id',
            'camera_list.sort',
            'camera_list.camera_type',
            'camera_list.camera_source',
            'camera_list.cover_picture',
            'camera_list.video_recorder',
            'mine_list.title',
            'mine_list.degree'
        )->first();
        //工作面列表
        $working_surface = WorkingSurfaceCamera::join(
            'working_surface', 'working_surface_camera.surface_id', '=', 'working_surface.id'
        )->where('working_surface_camera.camera_id', $request->input('id'))->value('surface_name');

        $camera_list->parent_title    = MineList::where('id', explode('|', $camera_list->degree)[0])->value('title');
        $camera_list->working_surface = $working_surface;
        $tableObj = new TableAuxiliary('', $camera_list);

        $tableObj->column('parent_title', '矿区名称');
        $tableObj->column('title', '区域名称');
        $tableObj->column('camera_name', '摄像头名称');
        $tableObj->column('camera_type', '摄像头类型', function ($camera_type) {
            return CameraEnum::CAMERA_TYPE_EXCHANGE[$camera_type];
        });
        $tableObj->column('camera_source', '摄像头来源', function ($camera_source) {
            return CameraEnum::CAMERA_SOURCE_EXCHANGE[$camera_source];
        });
        $tableObj->column('video_recorder', '视频服务器来源', function ($video_recorder) {
            return CameraEnum::VIDEO_RECORDER_EXCHANGE[$video_recorder];
        });

        if($camera_list->revert_id != '' && $camera_list->revert_id != null){
            $tableObj->column('revert_id', '流媒体id');
        }

        if($camera_list->user_name != '' && $camera_list->user_name != null){
            $tableObj->column('user_name', '用户名');
        }

        if($camera_list->password != '' && $camera_list->password != null){
            $tableObj->column('password', '密码');
        }

        if($camera_list->ip != '' && $camera_list->ip != null){
            $tableObj->column('ip', 'ip地址');
        }

        if($camera_list->port != '' && $camera_list->port != null){
            $tableObj->column('port', '端口');
        }

        if($camera_list->com_number != '' && $camera_list->com_number != null){
            $tableObj->column('com_number', '通道号');
        }

        if($camera_list->camera_url != '' && $camera_list->camera_url != null){
            $tableObj->column('camera_url', '链接');
        }

        if($camera_list->sort != '' && $camera_list->sort != null){
            $tableObj->column('sort', '排序');
        }

        if($camera_list->working_surface != '' && $camera_list->working_surface != null){
            $tableObj->column('working_surface', '所属矿区区域');
        }

        if($camera_list->cover_picture != '' && $camera_list->cover_picture != null){
            $tableObj->column('cover_picture', '封面图片', function($cover_picture) {
                return '<img src="'.$cover_picture.'">';
            });
        }

        $tableObj->title = '摄像头详情';
        return $this->tableView($tableObj);
    }

    //添加摄像头列表
    public function cameraListAdd(Request $request)
    {
        $mineService = new MineServices();
        $mineService->initMineList();
        $titleList = $mineService->getTierList();
        $formObj = new FromAuxiliary('camera/list/add');
        if(isset($_GET['mine_id']) && $_GET['mine_id'] != ''){
            $formObj->tree_select('mine_id', '所属区域', $titleList,$_GET['mine_id']);
        }else{
            $formObj->tree_select('mine_id', '所属区域', $titleList);
        }
        $formObj->input('camera_name', '摄像头名称');
        $formObj->select('camera_type', '摄像头类型', CameraEnum::CAMERA_TYPE_EXCHANGE, CameraEnum::CAMERA_TYPE_NORMAL);
        $formObj->select('camera_source', '摄像头来源', CameraEnum::CAMERA_SOURCE_EXCHANGE, CameraEnum::CAMERA_SOURCE_1);
        $formObj->radio('video_recorder', '硬盘录像机品牌',CameraEnum::VIDEO_RECORDER_EXCHANGE, CameraEnum::VIDEO_RECORDER_HK);
        $formObj->input('camera_url', '摄像头链接');
        $formObj->input('user_name', '用户名');
        $formObj->input('password', '密码');
        $formObj->input('ip', 'ip地址');
        $formObj->input('port', '端口');
        $formObj->input('com_number', '通道号');
        $formObj->select('code_stream', '视频码流', CameraEnum::CODE_STREAM_EXCHANGE, CameraEnum::CODE_STREAM_SUB);
        $formObj->input('sort', '排序', CameraEnum::DEFAULT_SORT);
        $formObj->uploadImg('cover_picture', '封面图片', '/admin/camera/upload/cover_picture', '');
        $formObj->title = '添加摄像头';
        return $this->from($formObj);
    }

    //添加摄像头列表操作
    public function cameraListAddPost(Request $request)
    {
        if (!$request->input('mine_id')) {
            return $this->error(1, '请选择区域');
        }

        if (!$request->input('camera_type')) {
            return $this->error(1, '请选择摄像头类型');
        }

        if (!$request->input('camera_name')) {
            return $this->error(1, '请输入摄像头名称');
        }

        if ($request->input('camera_source') == CameraEnum::CAMERA_SOURCE_1 && (!$request->input('ip') || !$request->input('port') || !$request->input('user_name')
                || !$request->input('pass') ||!$request->input('com_number'))) {
            return $this->error(1, '请输入必填字段');
        }
        if ($request->input('camera_source') == CameraEnum::CAMERA_SOURCE_2 && $request->input('code_stream') == 0) {
            return $this->error(1, '请选择视频码流');
        }

        if ($request->input('camera_source') == CameraEnum::CAMERA_SOURCE_3 && !$request->input('camera_url')) {
            return $this->error(1, '请输入摄像头链接');
        }

        if (!$request->input('code_stream')) {
            return $this->error(1, '请选择视频码流类型');
        }

        $result = CameraServices::addCameraList($request->all());

        if (!$result['status']) {
            return $this->error(1, $result['msg']);
        }

        return $this->redirect('admin/camera/list');
    }

    //编辑摄像头列表
    public function cameraListEdit(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            return $this->redirect('admin/camera/list');
        }

        $mineService = new MineServices();
        $mineService->initMineList();
        $titleList = $mineService->getTierList();

        $camera_list  = CameraList::find($id);

        if ($request->has('type') && $request->input('type') == 'show') {
            if ($camera_list->is_show == 1) {
                $camera_list->is_show = 2;
            } else {
                $camera_list->is_show = 1;
            }
            $camera_list->save();
            return $this->success();
        }

        //工作面列表
        $degree       = MineList::where('id', $camera_list->mine_id)->value('degree');
        $parent_id    = explode('|', $degree)[0];
        $surface_list = WorkingSurface::where('mine_id', $parent_id)->get()->toArray();
        $surface_arr  = [];
        foreach ($surface_list as $key => $val) {
            $surface_arr[$val['id']] = $val['surface_name'];
        }
        $default_surface = WorkingSurfaceCamera::where('camera_id', $id)->value('surface_id');

        $formObj = new FromAuxiliary('camera/list/edit', $camera_list);
        $formObj->tree_select('mine_id', '所属区域', $titleList);
        $formObj->input('camera_name', '摄像头名称');
        $formObj->select('camera_type', '摄像头类型', CameraEnum::CAMERA_TYPE_EXCHANGE);
        $formObj->select('camera_source', '摄像头来源', CameraEnum::CAMERA_SOURCE_EXCHANGE);
        $formObj->radio('video_recorder', '视频服务器来源',CameraEnum::VIDEO_RECORDER_EXCHANGE);
        $formObj->input('camera_url', '摄像头链接');
        $formObj->input('user_name', '用户名');
        $formObj->input('password', '密码');
        $formObj->input('ip', 'ip地址');
        $formObj->input('port', '端口');
        $formObj->input('com_number', '通道号');
        $formObj->select('code_stream', '视频码流', CameraEnum::CODE_STREAM_EXCHANGE);
        $formObj->input('sort', '排序');
        $formObj->select('surface_id', '所属矿区区域', $surface_arr, $default_surface);
        $formObj->uploadImg('cover_picture', '封面图片', '/admin/camera/upload/cover_picture', $camera_list->cover_picture);
        $formObj->title = '编辑摄像头';
        return $this->from($formObj);
    }

    //编辑摄像头操作
    public function cameraListEditPost(Request $request)
    {
        if (!$request->input('mine_id')) {
            return $this->error(1, '请选择所属区域');
        }

        if (!$request->input('camera_type')) {
            return $this->error(1, '请选择摄像头类型');
        }

        if (!$request->input('camera_name')) {
            return $this->error(1, '请输入摄像头名称');
        }

        if ($request->input('camera_source') == CameraEnum::CAMERA_SOURCE_3 && !$request->input('camera_url')) {
            return $this->error(1, '请输入摄像头链接');
        }

        if (!$request->input('code_stream')) {
            return $this->error(1, '请选择视频码流类型');
        }

//        if (
//            !$request->input('ip') || !$request->input('port') || !$request->input('user_name')
//            || !$request->input('password') ||!$request->input('com_number')
//        ) {
//            return $this->error(1, '请输入必填字段');
//        }

        $result = CameraServices::editCameraList($request->all());

        if (!$result['status']) {
            return $this->error(1, $result['msg']);
        }

        //更新摄像头与工作面关联表
        SurfaceServices::updateSrufaceCamera($request->all());

        return $this->redirect('admin/camera/list');
    }

    //删除摄像头列表记录
    public function cameraListDelPost(Request $request)
    {
        if (!$request->has('id')) {
            return $this->error(1, '缺少必要参数');
        }

        $result = CameraServices::delCameraList($request->all());
        if (!$result['status']) {
            return $this->error(1, $result['msg']);
        }
        return $this->success();
    }

    //导入摄像头列表
    public function cameraListImport(Request $request)
    {
        if (!$request->hasFile('excel')) {
            return $this->error(9001, '缺少上传文件');
        }
        ini_set('memory_limit', '500M');
        set_time_limit(300);
        $reader = ReaderEntityFactory::createReaderFromFile($request->file('excel')->getClientOriginalName());
        $reader->open($request->file('excel')->getRealPath());
        $sheet = $reader->getSheetIterator();  // 获取sheet对象
        $sheet->rewind();  // 指定当前sheet序数
        $sheet = $sheet->current();  // 获取指定sheet
        $arr   = [];
        foreach ($sheet->getRowIterator() as $index => $row) {  // 读取每行记录
            $arr[] = $row->toArray();
        }
        $reader->close();
        $result = CameraListExcel::dispatch($arr);
        if (!$result) {
            return $this->error(1, '所传矿区信息不存在');
        }
        return $this->success();
    }

    //下载摄像头列表导入模板
    public function cameraListTemplate()
    {
        return $this->redirect('/template/摄像头列表导入模板_v3.0.xlsx');
    }

    //字段列表
    public function fieldList(Request $request)
    {
        $this->menusActive[0] = 'adminCamera';
        $this->menusActive[1] = 'adminCameraFieldlist';

        $field_list = CameraFieldList::get();

        $tableObj             = new TableAuxiliary('camera/field_list', $field_list);
        $tableObj->actionBtns = ['edit', 'del'];
        $tableObj->column('field_name', '字段名称');
        return $this->tableList($tableObj);
    }

    //添加字段
    public function fieldListAdd(Request $request)
    {
        $formObj = new FromAuxiliary('camera/field_list/add');
        $formObj->input('field_name', '字段名称');
        $formObj->title = '添加字段';
        return $this->from($formObj);
    }

    //添加字段操作
    public function fieldListAddPost(Request $request)
    {
        if (!$request->input('field_name')) {
            return $this->error(1, '请输入字段名称');
        }

        $result = CameraServices::addCameraFieldList($request->all());

        if (!$result['status']) {
            return $this->error(1, $result['msg']);
        }

        return $this->redirect('admin/camera/field_list');
    }

    //编辑字段
    public function fieldListEdit(Request $request)
    {
        $field_list_id = $request->input('id');

        if (empty($field_list_id)) {
            return $this->redirect('admin/camera/field_list');
        }

        $field_list = CameraFieldList::find($field_list_id);

        $fromObj = new FromAuxiliary('camera/field_list/edit', $field_list);
        $fromObj->input("field_name", "字段名称", $field_list->field_name);
        $fromObj->title = '修改字段';
        return $this->from($fromObj);
    }

    //编辑字段操作
    public function fieldListEditPost(Request $request)
    {
        if (!$request->input('field_name')) {
            return $this->error(1, '请输入字段名称');
        }

        $result = CameraServices::editCameraFieldList($request->all());

        if (!$result['status']) {
            return $this->error(1, $result['msg']);
        }

        return $this->redirect('admin/camera/field_list');
    }

    //删除字段
    public function fieldListDelPost(Request $request)
    {
        if (!$request->has('id')) {
            return $this->error(1, '缺少必要参数');
        }

        $result = CameraServices::delCameraFieldList($request->all());
        if (!$result['status']) {
            return $this->error(1, $result['msg']);
        }
        return $this->success();
    }

    //上传摄像头封面图片
    public function cameraUploadCpicture(Request $request)
    {
        $file = $request->file('file');

        $file_name = CameraServices::uploadFile($file);

        $file_path = public_path() . '/filedownload/';

        $res = CameraServices::uploadOssFile($file_name, $file_path);

        CameraServices::delUploadFile();

        $result['image_path'] = $res;

        return $this->success($result);
    }
}
