<?php
/**
 * Created by PhpStorm.
 * User: qiuzijian
 * Date: 2021-05-31
 * Time: 09:20
 */

namespace Modules\Mine\Services;

use App\Enum\ApiEnum;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Log;
use Modules\Admin\Enum\AdminEnum;
use Modules\Camera\Enum\CameraEnum;
use Modules\Mine\Entities\MineList;
use Modules\Mine\Entities\WorkingSurface;
use Modules\Mine\Entities\WorkingSurfaceCamera;
use Modules\Mine\Enum\SurfaceEnum;
use DB;

class SurfaceServices{

    //获取工作面列表
    public static function getSurfaceList($staff_num)
    {
        $user = DB::table('users')->where('staff_num',$staff_num)->first();
        $mine_array = explode(';',$user->mine_role);

        $query = WorkingSurface::join(
            'mine_list', 'working_surface.mine_id', '=', 'mine_list.id'
        )->whereIn('mine_id',$mine_array)->orderBy('working_surface.created_at', 'desc')->select(
            [
                'working_surface.id',
                'working_surface.surface_name',
                'working_surface.working_area',
                'mine_list.title'
            ]
        )->get();

        return $query;
    }

    //查询工作面详情
    public static function getSrufaceDetail($surface_id)
    {
        $query = WorkingSurface::join(
            'mine_list', 'working_surface.mine_id', '=', 'mine_list.id'
        )->where('working_surface.id', $surface_id)->select(
            [
                'mine_list.title',
                'working_surface.surface_name',
                'working_surface.working_area',
            ]
        )->first();

        return $query;
    }

    //添加工作面操作
    public static function addSurfaceList($params)
    {
        $result['status'] = true;
        $result['msg']    = AdminEnum::RETURN_SUCCESS;

        $res = WorkingSurface::insert(
            [
                'mine_id'      => $params['mine_id'],
                'surface_name' => $params['surface_name'],
                'working_area' => $params['working_area'],
                'created_at'   => date('Y-m-d H:i:s', time()),
                'updated_at'   => date('Y-m-d H:i:s', time()),
            ]
        );

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

        return $result;
    }

    //编辑工作面操作
    public static function editSurfaceList($params)
    {
        $result['status'] = true;
        $result['msg']    = AdminEnum::RETURN_SUCCESS;

        //判断数据是否存在
        $query = WorkingSurface::find($params['id']);

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

        $res = WorkingSurface::where('id', $params['id'])->update(
            [
                'mine_id'      => $params['mine_id'],
                'surface_name' => $params['surface_name'],
                'working_area' => $params['working_area'],
                'updated_at'   => date('Y-m-d H:i:s', time()),
            ]
        );

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

        return $result;
    }

    //删除工作面操作
    public static function delSurfaceList($params)
    {
        $result['status'] = true;
        $result['msg']    = AdminEnum::RETURN_SUCCESS;

        //判断数据是否存在
        $query = WorkingSurface::find($params['id']);

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

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

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

        return $result;
    }

    //更新摄像头与工作面关联
    public static function updateSrufaceCamera($params)
    {
        $query = WorkingSurfaceCamera::where('camera_id', $params['id'])->exists();

        if (!$query) {
            if ($params['surface_id']) {
                WorkingSurfaceCamera::insert(
                    [
                        'surface_id' => $params['surface_id'],
                        'camera_id'  => $params['id'],
                        'created_at' => date('Y-m-d H:i:s', time()),
                        'updated_at' => date('Y-m-d H:i:s', time()),
                    ]
                );
            }
        } else {
            WorkingSurfaceCamera::where('camera_id', $params['id'])->update(
                [
                    'surface_id' => $params['surface_id'],
                    'updated_at' => date('Y-m-d H:i:s', time()),
                ]
            );
        }
    }

    //获取工作面列表
    public static function getApiSurfaceList($working_area = SurfaceEnum::WORKING_AREA_WORK)
    {
        $result['status'] = true;
        $result['msg']    = ApiEnum::RETURN_SUCCESS;

        $query = MineList::where('parent_id', 0)->select(
            [
                'id as parent_id',
                'title as mine_name',
            ]
        )->get()->toArray();

        foreach ($query as $key => $val) {
            $surface = WorkingSurface::where(
                [
                    'mine_id'      => $val['parent_id'],
                    'working_area' => $working_area
                ]
            )->orderBy('created_at', 'desc')->select(
                [
                    'id as surface_id',
                    'surface_name'
                ]
            )->get()->toArray();

            $query[$key]['surface_list'] = $surface;
        }

        $result['data'] = $query;

        return $result;
    }
}