<?php

namespace Home\Controller;

use Think\Controller;


class IndexController extends ComController
{
    //首页数据拉取
    public function getDataStatistics()
    {
        $Data = I('');
        if (empty($_SESSION['phone'])) {
            $R['msg'] = '操作失败，请重新加载初始化方法！';
            $R['flag'] = false;
            echo json_encode($R);
            exit();
        }
        if (empty($_SESSION['permissionLabel'])) {
            $R['msg'] = '操作失败，该用户无权限请联系管理员！';
            $R['flag'] = false;
            echo json_encode($R);
            exit();
        }

//        print_r("select siteId from wj_rights_management where  phone='".$_SESSION['phone']."'");
        $res1 = M()->query("select site_id as siteId from personnel where  personnel_phone='" . $_SESSION['phone'] . "'");
        $str = "";
        foreach ($res1 as $key => $value) {
            $str .= $res1[$key]['siteId'] . ",";
        }
        $companyCode = substr($str, 0, strlen($str) - 1);
        $where = "";
        $where1 = "";
        if ($_SESSION['permissionLabel'] == 3) {
            $where .= " and a.alarm_name in('PhaseLoss','OverV','UnderV','BusOutage') ";
            $where1 = " and alarm_name in('PhaseLoss','OverV','UnderV','BusOutage') ";
        }
        if ($_SESSION['permissionLabel'] == 4){
            $deviceList = $this->getDeviceCodeOne($companyCode,"1,2,3,4,5,6,7");
        }elseif($_SESSION['permissionLabel'] == 1){
            $deviceList = $this->getDeviceCodeOne($companyCode,"1,2,3,4,5,6,7");
        }else{
            $deviceList = $this->getDeviceCode($companyCode);
        }

        if ($_SESSION['permissionLabel'] == 4){
//            $where .= " and b.device_type in(3,4) ";
        }elseif($_SESSION['permissionLabel'] == 1){
//            $where .= " and b.device_type in(1,2,3,4,5,6,7) ";
        }else{
            $where .= " and b.device_type=1 ";
        }

        $end_time = date("Y-m-d");
        $start_time = date("Y-m-d", strtotime("-1 months", strtotime($end_time)));
        //时间段
        $statisticalPeriod = $start_time . '~' . $end_time;
        //智慧用电报警总数
        $smartElectricity = M()->query("select count(*) as smartElectricityCount from alarm_power where device_code in (" . $deviceList . ") and soe_time >= 
                                        '" . $start_time . "'" . $where1);
        $smartElectricityCount = $smartElectricity[0]['smartElectricityCount'];

        //视频监测报警总数
        $videoMonitoring = query_187_jdxf("select count(*) as videoMonitoringCount from sp_video2017 where device_code in (" . $deviceList . ") and time>=
                                            '" . $start_time . "'");
        $videoMonitoringCount = $videoMonitoring[0]['videoMonitoringCount'];

        //告警总数
        $alarmCount = $smartElectricityCount + $videoMonitoringCount;

        //事件总数
        $eventCount = 0;

        //隐患总数
        $hiddenDangerCount = 0;

        //离线设备总数
        $offline = M()->query("select count(*) as offlineCount from device_status where device_code in (" . $deviceList . ") and device_status=1");
        $offlineCount = (int)$offline[0]['offlineCount'];

        //故障设备总数
        $faultCount = 0;

        //预警设备总数
        $earlyWarningCount = 0;

        //其他设备总数
        $otherCount = 0;

        //正常设备总数
        $normal = M()->query("select count(*) as normalCount from device_status where device_code in (" . $deviceList . ") and device_status!=1");
        $normalCount = (int)$normal[0]['normalCount'];

        //综合报警总数
        $integratedAlarmCount = $alarmCount;

        //综合告警未处理数量

        $data1 = M()->query("SELECT COUNT(a.id) AS count FROM alarm_power AS a JOIN device AS b ON a.device_code = b.device_code JOIN site AS c 
                            ON b.site_id = c.id WHERE a.handling_status = 0 AND c.id IN(" . $companyCode . ")" . $where);
        $data2 = query_187_jdxf("select count(*) as count from sp_video2017 where device_code in (" . $deviceList . ") and clzt=0");


//        $data1 = M()->query("select count(*) as count from wj_power_monitoring_alarm where deviceName in (" . $deviceList . ") and clzt=0");
//        $data2 = query_187_jdxf("select count(*) as count from sp_video2017 where device_code in (" . $deviceList . ") and clzt=0");
        $unprocessedCount = (int)$data1[0]['count'] + (int)$data2[0]['count'];

        $data = array();
        $data[0]['statisticalPeriod'] = $statisticalPeriod;
        $data[0]['smartElectricityCount'] = $smartElectricityCount;
        $data[0]['videoMonitoringCount'] = $videoMonitoringCount;
        $data[0]['alarmCount'] = $alarmCount;
        $data[0]['eventCount'] = $eventCount;
        $data[0]['hiddenDangerCount'] = $hiddenDangerCount;
        $data[0]['offlineCount'] = $offlineCount;
        $data[0]['faultCount'] = $faultCount;
        $data[0]['earlyWarningCount'] = $earlyWarningCount;
        $data[0]['otherCount'] = $otherCount;
        $data[0]['normalCount'] = $normalCount;
        $data[0]['integratedAlarmCount'] = $integratedAlarmCount;
        $data[0]['unprocessedCount'] = $unprocessedCount;
        $res = array();
        $res['msg'] = '操作成功';
        $res['flag'] = true;
        $res['companyCode'] = $companyCode;
        $res['data'] = $data;

        echo json_encode($res);
    }


    public function getSiteDropDownBox()
    {
        $Data = I('');
        if (empty($_SESSION['phone'])) {
            $res['msg'] = '操作失败,缺少参数';
            $res['flag'] = false;
            $res['data'] = null;
            echo json_encode($res);
            exit();
        }

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

        $siteList = M()->query("select a.id,a.site_id as siteId,a.personnel_function as permissionLabel,c.site_name as siteName from personnel as a 
                                join site as c on a.site_id=c.id where a.personnel_phone='" . $_SESSION['phone'] . "'" . $where . " group by a.id");
        if (empty($siteList)) {
            $res['msg'] = '操作失败';
            $res['flag'] = false;
            $res['data'] = null;
            echo json_encode($res);
            exit();
        }

        $res['msg'] = '操作成功';
        $res['flag'] = true;
        $res['data'] = $siteList;
        echo json_encode($res);
    }


    public function getDeviceCode($companyCode)
    {
        $device = M()->query("select device_code as deviceCode from device where device_type in(1,2) and site_id in (" . $companyCode . ")");
        $str = "";
        foreach ($device as $key => $value) {
            $str .= "'" . $device[$key]['deviceCode'] . "',";
        }
        $newstr = substr($str, 0, strlen($str) - 1);
        return $newstr;
    }

    public function getDeviceCodeOne($companyCode,$typeData)
    {
        $device = M()->query("select device_code as deviceCode from device where device_type in(".$typeData.") and site_id in (" . $companyCode . ")");
        $str = "";
        foreach ($device as $key => $value) {
            $str .= "'" . $device[$key]['deviceCode'] . "',";
        }
        $newstr = substr($str, 0, strlen($str) - 1);
        return $newstr;
    }

}