<?php
/**
 * Created by PhpStorm.
 * User: qiuzijian
 * Date: 2021-04-20
 * Time: 15:33
 */

namespace Modules\Camera\Services;

use App\Enum\ApiEnum;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Modules\Admin\Enum\AdminEnum;
use Modules\Camera\Entities\CameraFieldList;
use Modules\Camera\Entities\CameraList;
use Modules\Camera\Enum\CameraEnum;
use Modules\Mine\Entities\MineList;

class CameraServices
{
    //添加摄像头链接
    public static function addCameraList($params)
    {
        $result['status'] = true;
        $result['msg']    = AdminEnum::RETURN_SUCCESS;

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

        if ($query) {
            $result['status'] = false;
            $result['msg']    = AdminEnum::RECORD_ALREADY_EXIST;
            return $result;
        }

        $camera_url = self::makeCameraUrl($params);

        //入库动作
        $res = CameraList::insert(
            [
                'mine_id'     => $params['mine_id'],
                'ip'          => $params['ip'],
                'port'        => $params['port'],
                'user_name'   => $params['user_name'],
                'password'    => $params['password'],
                'com_number'  => $params['com_number'],
                'camera_name' => $params['camera_name'],
                'revert_id'   => CameraEnum::CAMERA_DEFAULT_REVERT_ID,
//                'status'     => $params['status'],
                'camera_url'  => $camera_url,
                'sort'        => $params['sort'],
                'created_at'  => date('Y-m-d H:i:s'),
                'updated_at'  => date('Y-m-d H:i:s'),
            ]
        );

        if (!$res) {
            $result['status'] = false;
            $result['msg']    = AdminEnum::DATABASE_ERROR;
        }

        return $result;
    }

    //生成摄像头请求链接
    public static function makeCameraUrl($params)
    {
        $url  = 'rtsp://' . $params['user_name'] . ':' . $params['password'] . '@';
        $url .= $params['ip'] . ':' . $params['port'] . '/' . 'h264' . '/' . 'ch' . $params['com_number'] . '/';
        $url .= 'sub/av_stream';     //sub 子码流
//        $url .= 'main/av_stream';  //main 主码流
        return $url;
    }

    //编辑摄像头链接
    public static function editCameraList($params)
    {
        $result['status'] = true;
        $result['msg']    = AdminEnum::RETURN_SUCCESS;

        $camera_url = self::makeCameraUrl($params);

        //判断记录是否存在
        $query = CameraList::find($params['id']);

        if (!$query) {
            $result['status'] = false;
            $result['msg']    = AdminEnum::RECORD_NOT_EXIST;
            return $result;
        }

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

        if ($query) {
            $result['status'] = false;
            $result['msg']    = AdminEnum::RECORD_ALREADY_EXIST;
            return $result;
        }

        //更新动作
        $res = CameraList::where('id', $params['id'])->update(
            [
                'mine_id'     => $params['mine_id'],
                'ip'          => $params['ip'],
                'port'        => $params['port'],
                'user_name'   => $params['user_name'],
                'password'    => $params['password'],
                'com_number'  => $params['com_number'],
                'camera_name' => $params['camera_name'],
//                'status'     => $params['status'],
                'camera_url'  => $camera_url,
                'sort'        => $params['sort'],
                'updated_at'  => date('Y-m-d H:i:s'),
            ]
        );

        if (!$res) {
            $result['status'] = false;
            $result['msg']    = AdminEnum::DATABASE_ERROR;
        }

        return $result;
    }

    //删除摄像头列表记录
    public static function delCameraList($params)
    {
        $result['status'] = true;
        $result['msg']    = AdminEnum::RETURN_SUCCESS;

        //判断字段是否存在
        $query = CameraList::find($params['id']);

        if (!$query) {
            $result['status'] = false;
            $result['msg']    = AdminEnum::RECORD_NOT_EXIST;
            return $result;
        }

        $res = CameraList::where('id', $params['id'])->delete();

        if (!$res) {
            $result['status'] = false;
            $result['msg']    = AdminEnum::DATABASE_ERROR;
        }

        return $result;
    }


    //添加摄像头链接生成字段
    public static function addCameraFieldList($params)
    {
        $result['status'] = true;
        $result['msg']    = AdminEnum::RETURN_SUCCESS;

        //判断字段是否已存在
        $query = CameraFieldList::where('field_name', $params['field_name'])->first();

        if ($query) {
            $result['status'] = false;
            $result['msg']    = AdminEnum::RECORD_ALREADY_EXIST;
            return $result;
        }

        //入库动作
        $res = CameraFieldList::insert(
            [
                'field_name' => $params['field_name'],
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ]
        );

        if (!$res) {
            $result['status'] = false;
            $result['msg']    = AdminEnum::DATABASE_ERROR;
        }

        return $result;
    }

    //编辑摄像头链接生成字段
    public static function editCameraFieldList($params)
    {
        $result['status'] = true;
        $result['msg']    = AdminEnum::RETURN_SUCCESS;

        //判断字段是否存在
        $query = CameraFieldList::find($params['id']);

        if (!$query) {
            $result['status'] = false;
            $result['msg']    = AdminEnum::RECORD_NOT_EXIST;
            return $result;
        }

        $res = CameraFieldList::where('id', $params['id'])->update(
            [
                'field_name' => $params['field_name'],
                'updated_at' => date('Y-m-d H:i:s'),
            ]
        );

        if (!$res) {
            $result['status'] = false;
            $result['msg']    = AdminEnum::DATABASE_ERROR;
        }

        return $result;
    }

    //删除摄像头链接生成字段
    public static function delCameraFieldList($params)
    {
        $result['status'] = true;
        $result['msg']    = AdminEnum::RETURN_SUCCESS;

        //判断字段是否存在
        $query = CameraFieldList::find($params['id']);

        if (!$query) {
            $result['status'] = false;
            $result['msg']    = AdminEnum::RECORD_NOT_EXIST;
            return $result;
        }

        $res = CameraFieldList::where('id', $params['id'])->delete();

        if (!$res) {
            $result['status'] = false;
            $result['msg']    = AdminEnum::DATABASE_ERROR;
        }

        return $result;
    }

    //根据矿区id获取摄像头列表
    public static function getCameraListByMineId($mine_id)
    {
        $result['status'] = true;
        $result['msg']    = ApiEnum::RETURN_SUCCESS;

        $query = CameraList::where('mine_id', $mine_id)->orderBy('sort', 'desc')->select(
            [
                'id as camera_id',
                'mine_id',
                'camera_name'
            ]
        )->get()->toArray();

        $result['data'] = $query;

        return $result;
    }

    //根据摄像头id获取摄像头播放链接
    public static function getCameraUrlByCameraId($camera_id)
    {
        $result['status'] = true;
        $result['msg']    = ApiEnum::RETURN_SUCCESS;

        $query = CameraList::where('id', $camera_id)->first();

        if (!$query) {
            $result['status'] = false;
            $result['msg'] = ApiEnum::NO_CAMERA_URL;
            return $result;
        }

        $degree = MineList::where('id', $query->mine_id)->value('degree');

        $mine_list = MineList::whereIn('id', explode('|', $degree))->select(['title'])->get()->toArray();

        $path = '';
        foreach ($mine_list as $key => $val) {
            $path .= '/' . $val['title'];
        }

        $data = [
            'url'        => $query->camera_url,
            'customPath' => $path,
        ];

        $curl = env('EASY_DARWIN_URL') . CameraEnum::API_STREAM_START . '?' . http_build_query($data);

        //请求流媒体服务器拉流
        $curl_res = curl_request($curl);

        Log::info('请求流媒体服务器result------------');
        Log::info($curl_res);

        if (strpos($curl_res,' ')) {
            $result['status'] = false;
            $result['msg'] = ApiEnum::EASY_DAWIN_REQUEST_FAIL;
            return $result;
        }

        //更新拉流id
        CameraList::where('id', $camera_id)->update(
            [
                'revert_id'  => $curl_res,
                'updated_at' => date('Y-m-d H:i:s'),
            ]
        );

        $result['data'] = [
            'camera_id' => trim($camera_id, '"'),
            'url' => env('VIDEO_SYSTEM_URL') . CameraEnum::M3U8_FILE_PATH . $path . '/' . date('Ymd', time()) . '/' . CameraEnum::M3U8_FILE_NAME,
        ];

        return $result;
    }
}