<?php

namespace Home\Controller;

use Think\Controller;


class DeviceManagementController extends ComController
{
    //设备列表查询
    public function getDeviceList()
    {
        $Data = I('');
        if ($_SESSION['permissionLabel'] != 1) {
            $res['msg'] = '操作失败，无此权限请联系管理员';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }

        if (!empty($Data['deviceCode'])) {
            $where = " and device_code = '" . $Data['deviceCode'] . "'";
        }

        if (!empty($Data['deviceName'])) {
            $where = " and device_name like '%" . $Data['deviceName'] . "%'";
        }

        if (!empty($Data['siteId'])) {
            $where .= " and site_id =" . $Data['siteId'];
        }

        if (!empty($Data['id'])) {
            $where .= " and id = " . $Data['id'];
        }

        $data = M()->query("select id,device_code as deviceCode,device_name as deviceName,device_address as deviceInstallationPosition,site_id as siteId,device_type as deviceType,creator as addUser,install_time as addTime,floor from device where enable=1 " . $where);
        $res['msg'] = '操作成功';
        $res['flag'] = true;
        $res['data'] = $data;
        echo json_encode($res);
    }

    //设备新增
    public function setAddDevice()
    {
        $Data = I('');
        if ($_SESSION['permissionLabel'] != 1) {
            $res['msg'] = '操作失败，无此权限请联系管理员';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }

        if (!isset($_SESSION['phone'])) {
            $res['msg'] = '请重新进入公众号';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }

        $deviceCode = $Data['deviceCode'];
        $deviceName = $Data['deviceName'];
        $deviceInstallationPosition = $Data['deviceInstallationPosition'];
        $floor = $Data['floor'];
        $siteId = $Data['siteId'];
        $addTime = date('Y-m-d h:i:s', time());
        $companyId = "11111";
        $deviceType = $Data['deviceType'];
        $addUser = $_SESSION['phone'];
        if (!isset($deviceCode) || !isset($deviceName) || !isset($deviceInstallationPosition) || !isset($floor) || !isset($siteId) || !isset($deviceType)) {
            $res['msg'] = '操作失败，缺少关键参数';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        $arr['device_code'] = $deviceCode;
        $arr['device_name'] = $deviceName;
        $arr['device_address'] = $deviceInstallationPosition;
        $arr['site_id'] = $siteId;
        $arr['install_time'] = $addTime;
        $arr['device_type'] = $deviceType;
        $arr['creator'] = $addUser;
        $arr['enable'] = 1;
        $arr['floor'] = $floor;

        $arr1['device_code'] = $deviceCode;
        $arr1['device_status'] = 1;
        $arr1['site_id'] = $siteId;
        $arr1['status_time'] = date('Y-m-d h:i:s', time());

        $deviceData=M()->query("select * from device where device_code='".$arr['device_code']."'");
        if (!empty($deviceData)){
            if ($deviceData[0]['enable']==0){
                $where['device_code']=$arr['device_code'];
                $R = M("device")->where($where)->save($arr);
                $R3 = M("device_status")->add($arr1);
            }else{
                $res['msg'] = '操作失败，该设备已存在';
                $res['flag'] = false;
                echo json_encode($res);
                exit();
            }
        }else{
            $R = M("device")->add($arr);
            $R3 = M("device_status")->add($arr1);
        }
        
        switch ($deviceType) {
            case '2':
                $R2 = insert_187_jdxf("insert into sp_owner (owner_code,owner_name,unitinfo,louyu,install_time,install_man,dwtype,company,push_param2) 
                      values ('" . $deviceCode . "','" . $deviceName . "','" . $deviceInstallationPosition . "','" . $floor . "','" . $addTime . "','admin','16','" . $companyId . "','" . $siteId . "')");
                $R4 = insert_187_jdxf("insert into sp_devices_status (deviceid,deviceno,devicestatus,statustime,f_evt,c_evt,w_evt,true_status,lastcommtime) 
                      values ('" . $deviceCode . "','0','75','" . $addTime . "','40','00','','1','" . $addTime . "')");
                $R5 = insert_187_jdxf("insert into sp_owner_video (device_code,company_code,dev_name,comm_type,rtsp_path) values ('" . $deviceCode . "','11111','" . $deviceName . "','3','')");
                break;
            case '1':
            case '3':
            case '4':
                break;
            default:
                $res['msg'] = '操作失败，无此类型设备';
                $res['flag'] = false;
                echo json_encode($res);
                exit();
                break;
        }

        if ($R) {
            $res['msg'] = '操作成功';
            $res['flag'] = true;
            echo json_encode($res);
        } else {
            $res['msg'] = '操作失败';
            $res['flag'] = false;
            echo json_encode($res);
        }
    }

    //设备编辑
    public function setEditDevice()
    {
        $Data = I('');
        if ($_SESSION['permissionLabel'] != 1) {
            $res['msg'] = '操作失败，无此权限请联系管理员';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        if (!isset($Data['deviceCode'])) {
            $res['msg'] = '操作失败，缺少关键参数';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }

        $deviceCode = $Data['deviceCode'];
        $deviceName = $Data['deviceName'];
        $deviceInstallationPosition = $Data['deviceInstallationPosition'];
        $floor = $Data['floor'];
        $siteId = $Data['siteId'];
        $deviceType = $Data['deviceType'];
        $where['device_code'] = $deviceCode;
        if (isset($deviceName)) {
            $data['device_name'] = $deviceName;
        }
        if (isset($deviceInstallationPosition)) {
            $data['device_address'] = $deviceInstallationPosition;
        }
        if (isset($floor)) {
            $data['floor'] = $floor;
        }
        if (isset($siteId)) {
            $data['site_id'] = $siteId;
        }
        if (isset($deviceType)) {
            $data['device_type'] = $deviceType;
        }
        $R = M("device")->where($where)->save($data);
        switch ($deviceType) {
            case '2':
                insert_187_jdxf("UPDATE sp_owner set owner_name='" . $deviceName . "',unitinfo='" . $deviceInstallationPosition . "',louyu='" . $floor . "',push_param2='" . $siteId . "' where owner_code='".$deviceCode."'");
                insert_187_jdxf("UPDATE  sp_owner_video set dev_name='" . $deviceName . "' where device_code='".$deviceCode."'");
                break;
            case '1':
                break;
            default:
                $res['msg'] = '操作失败，无此类型设备';
                $res['flag'] = false;
                echo json_encode($res);
                exit();
                break;
        }

        if ($R) {
            $res['msg'] = '操作成功';
            $res['flag'] = true;
            echo json_encode($res);
        } else {
            $res['msg'] = '操作失败';
            $res['flag'] = false;
            echo json_encode($res);
        }
    }

    //设备删除
    public function setDelSite()
    {
        $Data = I('');
        if ($_SESSION['permissionLabel'] != 1) {
            $res['msg'] = '操作失败，无此权限请联系管理员';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        if (!isset($Data['id'])) {
            $res['msg'] = '操作失败，缺少关键参数';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        $where['id'] = $Data['id'];

        $list = M()->query("select device_code as deviceCode,device_type as deviceType from device where id=" . $Data['id']);
        if (!isset($list)) {
            $res['msg'] = '操作失败，系统参数出现错误';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
//        print_r($list);
//        exit();
        $where1['device_code']=$list[0]['deviceCode'];
        $data['enable']=0;
        $R = M("device")->where($where)->save($data);
        $R1 = M("device_status")->where($where1)->delete();

        switch ($list[0]['deviceType']) {
            case '2':
                insert_187_jdxf("delete from sp_owner where owner_code='" . $list[0]['deviceCode'] . "'");
                insert_187_jdxf("delete from sp_devices_status where deviceid='" . $list[0]['deviceCode'] . "'");
                insert_187_jdxf("delete from sp_owner_video where device_code='" . $list[0]['deviceCode'] . "'");
                break;
            case '1':
                break;
            default:
                $res['msg'] = '操作失败，无此类型，请联系管理员';
                $res['flag'] = false;
                echo json_encode($res);
                exit();
                break;
        }
        if ($R) {
            $res['msg'] = '操作成功';
            $res['flag'] = true;
            echo json_encode($res);
        } else {
            $res['msg'] = '操作失败';
            $res['flag'] = false;
            echo json_encode($res);
        }
    }
}