<?php

namespace Home\Controller;

use Think\Controller;

include '../Common/function.php';

class PowerwarnController extends PushWarnController
{
    public function message_dl()
    {
        header("Access-Control-Allow-Origin:*");
        header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");
        header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding");
        $Data = I();
        $log = "[时间" . date("Y-m-d H:i:s") . "]报警数据：" . json_encode($Data) . "\r\n";
        $filename_log = "/var/www/html/USKYOF/log/dl_log" . date("Y-m-d") . ".log";
        file_put_contents($filename_log, $log, FILE_APPEND | LOCK_EX);

        $insert_id = $Data['insert_id'];
        $device_code = $Data['device_code'];
        $evt_name = $Data['evt_name'];
        $time = $Data['time'];
        $where="";
        switch ($Data['meas_name']) {
            case 'DI1'://开入1
                $where .=" and alarm_setting like '%1%' ";
                break;
            case 'DI2'://开入2
                $where .=" and alarm_setting like '%2%' ";
                break;
            case 'DI3'://开入3
                $where .=" and alarm_setting like '%3%' ";
                break;
            case 'FireAlarm'://火灾预警总
                $where .=" and alarm_setting like '%4%' ";
                break;
            case 'EleAlarm'://线路电气故障总
                $where .=" and alarm_setting like '%5%' ";
                break;
            case 'PhaseLoss'://缺相
                $where .=" and alarm_setting like '%6%' ";
                break;
            case 'LoadLive'://线路带电
                $where .=" and alarm_setting like '%7%' ";
                break;
            case 'LoadBrkState'://线路开关状态
                $where .=" and alarm_setting like '%8%' ";
                break;
            case 'OverV'://过压
                $where .=" and alarm_setting like '%9%' ";
                break;
            case 'UnderV'://低压
                $where .=" and alarm_setting like '%a%' ";
                break;
            case 'OverI'://电流越限
                $where .=" and alarm_setting like '%b%' ";
                break;
            case 'Trip'://过流跳闸
                $where .=" and alarm_setting like '%c%' ";
                break;
            case 'BusOutage'://失压
                $where .=" and alarm_setting like '%d%' ";
                break;
            case 'OverIr'://剩余电流越限
                $where .=" and alarm_setting like '%e%' ";
                break;
            case 'OverT1'://测温点1超温
                $where .=" and alarm_setting like '%f%' ";
                break;
            case 'OverT2'://测温点2超温
                $where .=" and alarm_setting like '%g%' ";
                break;
            case 'OverT3'://测温点3超温
                $where .=" and alarm_setting like '%h%' ";
                break;
            case 'OverT4'://测温点4超温
                $where .=" and alarm_setting like '%i%' ";
                break;
            case 'OverEvT'://环境温度越限
                $where .=" and alarm_setting like '%j%' ";
                break;
            case 'ArcA'://A相燃弧
                $where .=" and alarm_setting like '%k%' ";
                break;
            case 'ArcB'://B相燃弧
                $where .=" and alarm_setting like '%l%' ";
                break;
            case 'ArcC'://C相燃弧
                $where .=" and alarm_setting like '%m%' ";
                break;
            case 'OverUH'://电压谐波越限
                $where .=" and alarm_setting like '%n%' ";
                break;
            case 'OverIH'://电流谐波越限
                $where .=" and alarm_setting like '%o%' ";
                break;
            case 'OverDemandP'://有功需量超限
                $where .=" and alarm_setting like '%p%' ";
                break;
            case 'OverDemandS'://视在需量超限
                $where .=" and alarm_setting like '%q%' ";
                break;
            case 'MeasFailure'://测量通道故障
                $where .=" and alarm_setting like '%r%' ";
                break;
            case 'DevAbnormal'://设备异常
                $where .=" and alarm_setting like '%s%' ";
                break;
            case 'Mute'://消音
                $where .=" and alarm_setting like '%t%' ";
                break;
            case 'DO1'://开出1
                $where .=" and alarm_setting like '%u%' ";
                break;
            case 'DO2'://开出2
                $where .=" and alarm_setting like '%v%' ";
                break;
            case 'busLive'://母线带电
            case 'highVoltageAlarm'://电压越上限
            case 'lowVoltageAlarm'://电压越下限
            case 'busLackPhase'://缺相
            case 'analog1Alarm'://直流量告警1
            case 'analog2Alarm'://直流量告警2
            case 'analog3Alarm'://直流量告警3
            case 'analog4Alarm'://直流量告警4
            case 'analog5Alarm'://直流量告警5
            case 'digitalReserved1'://预留1
            case 'digitalReserved2'://预留2
            case 'digitalReserved3'://预留3
            case 'digitalReserved4'://预留4
            case 'digitalReserved5'://预留5
            case 'digitalReserved6'://预留6
            case 'LoadLiveL1'://预留2
            case 'TripL1'://L1故障跳闸
            case 'OverIL1'://L1电流越限
            case 'OverVL1'://L1过压
            case 'UnderVL1'://L1低压
            case 'ArcL1'://L1燃弧
            case 'LoadLiveL2'://L2带电
            case 'TripL2'://L2故障跳闸
            case 'OverIL2'://L2电流越限
            case 'OverVL2'://L2过压
            case 'UnderVL2'://L2低压
            case 'ArcL2'://L2燃弧
            case 'LoadLiveL3'://L3带电
            case 'TripL3'://L3故障跳闸
            case 'OverIL3'://L3电流越限
            case 'OverVL3'://L3过压
            case 'UnderVL3'://L3低压
            case 'ArcL3'://L3燃弧
            case 'ResDI5'://备用遥信5
            case 'ResDI6'://备用遥信6
            case 'LoadLiveL4'://L4带电
            case 'TripL4'://L4故障跳闸
            case 'OverIL4'://L4电流越限
            case 'OverVL4'://L4过压
            case 'UnderVL4'://L4低压
            case 'ArcL4'://L4燃弧
            case 'ResDI7'://备用遥信7
            case 'ResDI8'://备用遥信8
            case 'UL'://出口电压
            case 'ResDI1'://备用遥信1
            case 'ResDI2'://备用遥信2
            case 'ResDI3'://备用遥信3
            case 'ResDI4'://备用遥信4
            case 'commState'://通信状态
                break;
//            case 'ResDI1'://备用遥信1
//                $where .=" and alarm_setting like '%u%' ";
//                break;
//            case 'ResDI2'://备用遥信2
//                $where .=" and alarm_setting like '%v%' ";
//                break;
//            case 'ResDI3'://备用遥信3
//                $where .=" and alarm_setting like '%%' ";
//                break;
//            case 'ResDI4'://备用遥信4
//                $where .=" and alarm_setting like '%%' ";
//                break;
            default:
                echo "未定义类型报警";
                exit();
                break;
        }

        $res = M()->query("select b.site_address as address,b.site_name as siteName,a.site_id as siteId,a.device_name as deviceName from device as a join site as b on a.site_id=b.id where a.device_code='" . $device_code . "'");
        $outage_plan = M()->query("SELECT * FROM wc_blackout_plan WHERE site_id=".$res[0]['siteId']." AND enable=1 AND start_time<'".date("Y-m-d H:i:s")."' AND end_time>='".date("Y-m-d H:i:s")."'");
        if (!empty($outage_plan)){
            echo '计划停电';
            exit();
        }

        if ($evt_name == '电压缺相' || $evt_name == '电压高' || $evt_name == '电压低' || $evt_name == '失压') {
            $phone_sql = M()->query("select personnel_phone as phone from personnel where site_id=" . $res[0]['siteId']." ".$where." group by personnel_phone");
        } else {
            $phone_sql = M()->query("select personnel_phone as phone from personnel where personnel_function in(1,2,4) and site_id=" . $res[0]['siteId']." ".$where." group by personnel_phone");
        }

        print_r($phone_sql);
        if (count($phone_sql) == 0) {
            echo '系统内部出现问题';
            exit;
        }
        $openids = $this->array_column1($phone_sql, "phone");
        if (empty($openids)) {
            echo 'open is null';
            exit;
        }

        if ($device_code=='DA00013165'||$device_code=='DA00012784'||$device_code=='DA00013596'||$device_code=='DA00013433'
            ||$device_code=='DA00012827'||$device_code=='DA00021164'||$device_code=='DA00021241'||$device_code=='DA00021248'
            ||$device_code=='DA00021152'||$device_code=='DA00020371'){
            $openids='15122423833,13611609628,13916238398,18765618544,17621295894,15821080723,13761123560,13770221163';
        }
//        if ($device_code=='DA00013165'||$device_code=='DA00012784'||$device_code=='DA00013596'||$device_code=='DA00013433'||$device_code=='DA00012827'){
//            $openids='15122423833,15821080723,13611609628,18321772381,13122007679,13761579313,18601666983,13916238398';
//        }
//        print_r("SELECT open_id as openid FROM wc_user WHERE registration_type='wx' and phone in(".$openids.")");
        $op = M()->query("SELECT open_id as openid FROM wc_user WHERE registration_type='wx' and phone in(".$openids.")");
        $url="https://wx.ewoogi.com/wx/index.html#/pages/processDetailEle1/processDetailEle1?companyCode=".$res[0]['siteId']."&id=".$insert_id."&type=1";
        $access_token = GetAccess_Token();
        $siteName = $res[0]['siteName'];
        $deviceName = $res[0]['deviceName'];
//        $deviceName = "****";
        $evtName = $evt_name;
        if ($Data['alarm_status'] == 1) {
            $alarmStatus = '动作';
        } else {
            $alarmStatus = '复归';
        }
//        print_r("$op",$op);
//        print_r($siteName);
        foreach ($op as $o) {
            if (!empty($o['openid'])) {
                $arr = $this->SendDeviceAlarm4($access_token, $o['openid'], $siteName, $deviceName, $evtName, $alarmStatus,$time, $url);
                $log = $o['openid'].'第一次推送'.json_encode($arr) . "\r\n";
                $filename_log = "/var/www/html/USKYOF/log/dl_log" . date("Y-m-d") . ".log";
                file_put_contents($filename_log, $log, FILE_APPEND | LOCK_EX);
                print_r($arr);
                if ($arr['errcode']==40001||$arr['errcode']==-1){
                    $access_token1 = GetAccess_Token1();
                    $arr1 = $this->SendDeviceAlarm4($access_token1, $o['openid'], $siteName, $deviceName, $evtName, $alarmStatus,$time, $url);
                    $log = $o['openid'].'第二次推送'.json_encode($arr1) . "\r\n";
                    $filename_log = "/var/www/html/USKYOF/log/dl_log" . date("Y-m-d") . ".log";
                    file_put_contents($filename_log, $log, FILE_APPEND | LOCK_EX);
                }
            }
        }
    }

}