<?php

namespace Home\Controller;

use Think\Controller;


class OperationMonitoringController extends ComController
{
    //站点列表数据查询
    public function getSiteList(){
        if (!isset($_SESSION['phone'])) {
            $res['msg'] = '操作失败';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }

        if (empty($_SESSION['permissionLabel'])) {
            $R['msg'] = '操作失败，该用户无权限请联系管理员！';
            $R['flag'] = false;
            echo json_encode($R);
            exit();
        }

        $Data = I('');
        if(!empty($Data['currentPage'])&&!empty($Data['size'])){
            $start_number = ($Data['currentPage']-1)*$Data['size'];
            $limit = " limit ".$start_number.",".$Data['size'];
        }
        $R = M()->query("select count(*) as sl from personnel where personnel_phone='".$_SESSION['phone']."'");
        $siteIdList = M()->query("select site_id as siteId from personnel where personnel_phone='".$_SESSION['phone']."' ");
        $str = "";
        foreach ($siteIdList as $key => $value) {
            $str .= "'" . $siteIdList[$key]['siteId'] . "',";
        }
        if (!empty($Data['siteName'])) {
            $where = " and d.site_name like '%" . $Data['siteName'] . "%'";
        }

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

        $where1='';
        if ($_SESSION['permissionLabel'] == 4) {
//            $where1 .= " and a.device_type in(2,3,4) ";
        }elseif($_SESSION['permissionLabel']==1){
//            $where1 .= " and a.device_type in(1,2,3,4) ";
        }else{
            $where1 .= " and a.device_type in(1,2) ";
        }

        $siteId = substr($str, 0, strlen($str) - 1);
        $siteList = M()->query("SELECT d.id,d.site_name as siteName,IFNULL(c.count,0) as count,c.route_name FROM 
                                (SELECT b.id,b.site_name as siteName,count(a.id) AS count,s.route_name from device AS a 
                                JOIN site as b ON a.site_id=b.id  JOIN route as s ON b.route_id=s.id  
                                where b.enable=1 and a.enable=1 ".$where1." GROUP BY b.id) AS c 
                                RIGHT JOIN site AS d ON c.id=d.id WHERE d.enable=1 and d.id in(".$siteId.") ".$where." ".$limit);
        foreach ($siteList as $key => $value){
            $siteList[$key]['companyCode']=$siteList[$key]['id'];
        }
        $res['msg'] = '操作成功';
        $res['flag'] = true;
        $res['data'] = $siteList;
        $res['total'] = $R[0]['sl'];
        $res['currentPage'] = $Data['currentPage'];
        $res['size'] = $Data['size'];
        echo json_encode($res);
    }

    //站点各类型设备数量统计
    public function getDeviceStatistics(){

        if (empty($_SESSION['permissionLabel'])) {
            $R['msg'] = '操作失败，该用户无权限请联系管理员！';
            $R['flag'] = false;
            echo json_encode($R);
            exit();
        }

        $Data = I('');
        if (empty($Data['companyCode'])) {
            $res['msg'] = '操作失败，缺少参数companyCode';
            $res['flag'] = false;
            $res['data'] = null;
            echo json_encode($res);
            exit();
        }
        $where = "";
        if ($_SESSION['permissionLabel'] == 4) {
            $where .= " and device_type in(1,3,4,5,7) ";
        }elseif($_SESSION['permissionLabel']==1){
            $where .= " and device_type in(1,3,4,5,7) ";
        }else{
            $where .= " and device_type=1 ";
        }

        $powerDeviceCount = M()->query("select count(*) as powerDeviceCount from device where enable=1 and site_id=".$Data['companyCode']." ".$where);
        $videoDeviceCount = M()->query("select count(*) as videoDeviceCount from device where enable=1 and  device_type=2 and site_id=".$Data['companyCode']);
        $data[0]['powerDeviceCount']=(int)$powerDeviceCount[0]['powerDeviceCount'];
        $data[0]['videoDeviceCount']=(int)$videoDeviceCount[0]['videoDeviceCount'];
        $res['msg'] = '操作成功';
        $res['flag'] = true;
        $res['companyCode'] = $Data['companyCode'];
        $res['data'] = $data;
        echo json_encode($res);
    }

    //设备管理-设备列表查询
    public function getDeviceList(){
        if (empty($_SESSION['permissionLabel'])) {
            $R['msg'] = '操作失败，该用户无权限请联系管理员！';
            $R['flag'] = false;
            echo json_encode($R);
            exit();
        }

        $Data = I('');
        if (!isset($Data['companyCode'])||!isset($Data['deviceType'])) {
            $res['msg'] = '操作失败，缺少参数';
            $res['flag'] = false;
            $res['data'] = null;
            echo json_encode($res);
            exit();
        }
        $deviceType = $Data['deviceType'];
        if ($_SESSION['permissionLabel']==4){
            if($Data['deviceType']==1){
                $deviceType = '1,3,4,5,7';
            }
        }elseif($_SESSION['permissionLabel']==1){
            if($Data['deviceType']==1){
                $deviceType = '1,3,4,5,7';
            }
        }else{
            if($Data['deviceType']==1){
                $deviceType = 1;
            }
        }

        $deviceList = M()->query("select a.id,a.device_code as deviceCode,a.device_name as deviceName,b.device_status as trueStatus from device as a 
                                    join device_status as b on a.device_code=b.device_code 
                                    where a.enable=1 and a.device_type in(".$deviceType.") and a.site_id=".$Data['companyCode']);
        $res['msg'] = '操作成功';
        $res['flag'] = true;
        $res['companyCode'] = $Data['companyCode'];
        $res['data'] = $deviceList;
        echo json_encode($res);
    }

    //监控画面数据查询
    public function getMonitoringScreen(){
//        if (empty($_SESSION['permissionLabel'])) {
//            $R['msg'] = '操作失败，该用户无权限请联系管理员！';
//            $R['flag'] = false;
//            echo json_encode($R);
//            exit();
//        }
        $Data = I('');
        if (!isset($Data['companyCode'])||!isset($Data['deviceCode'])||!isset($Data['deviceType'])) {
            $res['msg'] = '操作失败，缺少参数';
            $res['flag'] = false;
            $res['data'] = null;
            echo json_encode($res);
            exit();
        }
        if ($Data['deviceType']==1){
            $device_type = M()->query("select device_type from device where device_code='".$Data['deviceCode']."'" );
            if (empty($device_type)){
                $res['msg'] = '设备不存在，请找管理员查询';
                $res['flag'] = false;
                $res['data'] = null;
                echo json_encode($res);
                exit();
            }

            if ($device_type[0]['device_type']==3){
                $table = 'rt_analog_171_data';
            }elseif($device_type[0]['device_type']==4){
                $table = 'rt_analog_173_data';
            }elseif($device_type[0]['device_type']==1){
                $table = 'rt_analog_data';
            }elseif($device_type[0]['device_type']==5){
                $table = 'rt_analog_158_data';
            }elseif($device_type[0]['device_type']==7){
                $table = 'rt_analog_176_data';
            }
            $deviceData = M()->query("SELECT a.device_name,b.device_status,r.* FROM device as a JOIN device_status as b 
                                        ON a.device_code=b.device_code LEFT JOIN ".$table." as r ON a.device_code=r.deviceName 
                                        where a.device_code='".$Data['deviceCode']."' order by r.id desc limit 1");
            foreach ($deviceData as $key => $value){
                if ($device_type[0]['device_type']==3){
                    $deviceData[$key]['Uab'] = $deviceData[$key]['voltageAB'];
                    $deviceData[$key]['Ubc'] = $deviceData[$key]['voltageBC'];
                    $deviceData[$key]['Uca'] = $deviceData[$key]['voltageCA'];
                    $deviceData[$key]['Ia'] = "";
                    $deviceData[$key]['Ib'] = "";
                    $deviceData[$key]['Ic'] = "";
                    $deviceData[$key]['F'] = "";
                    $deviceData[$key]['P'] = "";
                    $deviceData[$key]['Q'] = "";
                    $deviceData[$key]['COS'] = "";
                }elseif($device_type[0]['device_type']==5){
                    $deviceData[$key]['Uab'] = "";
                    $deviceData[$key]['Ubc'] = "";
                    $deviceData[$key]['Uca'] = "";
                    $deviceData[$key]['Ia'] = "";
                    $deviceData[$key]['Ib'] = "";
                    $deviceData[$key]['Ic'] = "";
                    $deviceData[$key]['F'] = "";
                    $deviceData[$key]['P'] = "";
                    $deviceData[$key]['Q'] = "";
                    $deviceData[$key]['COS'] = "";
                }elseif($device_type[0]['device_type']==7){
                    $deviceData[$key]['Uab'] = $deviceData[$key]['voltageL1'];
                    $deviceData[$key]['Ubc'] = $deviceData[$key]['voltageL2'];
                    $deviceData[$key]['Uca'] = $deviceData[$key]['voltageL3'];
                    $deviceData[$key]['Ia'] = $deviceData[$key]['electricCurrentL1'];
                    $deviceData[$key]['Ib'] = $deviceData[$key]['electricCurrentL2'];
                    $deviceData[$key]['Ic'] = $deviceData[$key]['electricCurrentL3'];
                    $deviceData[$key]['F'] = "";
                    $deviceData[$key]['P'] = "";
                    $deviceData[$key]['Q'] = "";
                    $deviceData[$key]['COS'] = "";
                }
                if ($deviceData[$key]['device_status']==1){
                    $deviceData[$key]['status'] = '离线';
                }else{
                    $deviceData[$key]['status'] = '在线';
                }

            }

        }else if ($Data['deviceType']==2){
            $deviceData = M()->query("select a.device_code as deviceCode,a.device_name as deviceName,a.device_address as deviceInstallationPosition,
                                        a.install_time as addTime,c.site_address as address,a.sim from device as a join site as c on a.site_id=c.id 
                                        where a.device_code='".$Data['deviceCode']."'");
        }

        $res['msg'] = '操作成功';
        $res['flag'] = true;
        $res['companyCode'] = $Data['companyCode'];
        $res['data'] = $deviceData;
        echo json_encode($res);

    }
}