<?php
namespace app\web\controller;

use think\Db;
use app\common\plugin\Varbinary;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use \IGeTui,\IGtAppMessage,\IGtNotificationTemplate;
//产品类
define('APPKEY','hWPSNCBrnU6nk1SOLjZF67');
define('APPID','xpoTHGLgyQ6ykBzyv42dt1');
define('MASTERSECRET','aoY8L0SYeW8ofcV71W7097');
define('CID','dd25afb14fa2485fcf050b5dbac482b3');
define('HOST',"http://api.getui.com/apiex.htm");
/**
 * @author lyuis
 *    小程序首页相应功能类
 */
class IndexController extends MainController
{
    private $pages = 8;
    public $normalDay=7;//默认天数

    /*
     * @name 站点列表
     *
     * */
    public function index()
    {
        if(empty(cache('day'))){
            cache('day',$this->normalDay);
        }
        $keywords = $this->request->param('keywords');
        $user_id=$this->userid;
        $where=array();
        $whe=array('ID','>',0);
        if($user_id!=1){
            $puser=$this->userList();
            $where[]=array('PRIMARYADDR', 'in', $puser);
            $whe=array('PRIMARYADDR', 'in', $puser);
        }
		if(!empty($keywords))
			$where[]=array('REPEATERNAME|STATIONNAME', 'like', "%$keywords%");
        //站点信息
        $index = Db::table('primarydev')->where($where)->select();

        foreach ($index as $k => &$v) {
            $res = $this->bd_decrypt($v['DVICLONGITUDE'], $v['DVICLATITUDE']);
            $v['DVICLONGITUDE'] = $res['gg_lon'];
            $v['DVICLATITUDE'] = $res['gg_lat'];
            $v['status'] = 'normal';
            $v['text'] = $v['STATIONNAME'];
        }
        $ALARMSTARTTIME =date('Y-m-d H:i:s',time()-intval(empty(cache('day'))?$this->normalDay*24*3600:(int)cache('day')*24*3600));//cache('ALARMSTARTTIME');
        $ALARMENDTIME = date('Y-m-d H:i:s');


        $datatime = array('ALARMSTARTTIME' => $ALARMSTARTTIME, 'ALARMENDTIME' => $ALARMENDTIME);
        $num = Db::table('reportdata')->where([['ALARMSTARTTIME', '>', $ALARMSTARTTIME], ['ALARMENDTIME', '<', $ALARMENDTIME],$whe])->count();


        $this->api_return_json(compact('index', 'datatime', 'num','user_id'), 1);

    }
    /**
     * @name
     * */


    /*
     * @name 报警设备
     * */
    public function reportdata()
    {
        $PRIMARYADDR = $this->request->param('PRIMARYADDR');
        $type = $this->request->param('type', 0);
        $page = $this->request->param('page', 0);
        if (!intval($this->userid)) {
            $this->api_return_json(array('msg' => '请重新登录'), 0);
        }
       // $this->api_return_json($this->request->param(),1);
        $ALARMSTARTTIME=date('Y-m-d H:i:s',time()-intval(empty(cache('day'))?$this->normalDay*24*3600:(int)cache('day')*24*3600));//cache('ALARMSTARTTIME');
        $ALARMENDTIME=date('Y-m-d H:i:s');

        if (isset($PRIMARYADDR) && intval($PRIMARYADDR)) {
            //如果为真查询关注报警设备
            if ($type) {
                $where=array('p.ID','>',0);
                if($this->userid!=1){
                    $puser=$this->userList();
                    $where=array('p.PRIMARYADDR', 'in', $puser);
                }
                $t = 1;
                $reportdata = Db::table('follow')
                    ->alias('f')
                    ->where(array(array('f.UID', '=', $this->userid),$where))
                    ->join('primarydev p', 'f.PID=p.PRIMARYADDR', 'left')
                    ->join('deviceinfo d', 'f.DID=d.DEVICEID and f.PID=d.PRIMARYADDR', 'left')
                    ->field('p.STATIONNAME,d.*')
                    ->page($page,$this->pages)
                    ->select();
            } else {
                //报警设备 查站点id下的
                $reportdata = Db::table('reportdata')
                    ->where(array(array('ALARMSTARTTIME', '>', $ALARMSTARTTIME),array('ALARMENDTIME', '<', $ALARMENDTIME), array('PRIMARYADDR', '=', $PRIMARYADDR)))
                    ->page($page,$this->pages)
                    ->order('ALARMSTARTTIME desc')
                    ->select();
            }

        } else {
            if ($type) {
                    $where=array('p.ID','>',0);
                    if($this->userid!=1){
                        $puser=$this->userList();
                        $where=array('p.PRIMARYADDR', 'in', $puser);
                    }
                $t = 1;
                $reportdata = Db::table('follow')
                    ->alias('f')
                    ->where(array(array('f.UID', '=', $this->userid),$where))
                    ->join('primarydev p', 'f.PID=p.PRIMARYADDR', 'left')
                    ->join('deviceinfo d', 'f.DID=d.DEVICEID and f.PID=d.PRIMARYADDR', 'left')
                    ->field('p.STATIONNAME,d.*')
                    ->page($page,$this->pages)
                    ->select();
            } else {
                $where=array('ID','>',0);
                if($this->userid!=1){
                    $puser=$this->userList();
                    $where=array('PRIMARYADDR', 'in', $puser);
                }
                //报警设备 查所有
                $reportdata = Db::table('reportdata')
                    ->where(array(array('ALARMSTARTTIME', '>', $ALARMSTARTTIME),array('ALARMENDTIME', '<', $ALARMENDTIME),$where))
                    ->page($page,$this->pages)
                    ->order('ALARMSTARTTIME desc')
                    ->select();
            }
        }
        // echo Db::table('follow')->getLastSql();
        $arr = array(1 => '80℃', 2 => '100℃', 3 => '130℃');
        $sta = array('在线', '报警', '不在线', '未配置');
        foreach ($reportdata as &$v) {

            $report=Db::table('reportdata')->where([['DEVICEID','=',$v['DEVICEID']],['PRIMARYADDR','=',$v['PRIMARYADDR']]])->field('ALARMSTARTTIME,REPORTINFO')->order('ALARMSTARTTIME desc')->find();
            $realtimedata=Db::table('realtimedata')->where([['DEVICEID','=',$v['DEVICEID']],['PRIMARYADDR','=',$v['PRIMARYADDR']]])->field('ID,STATUS,LASTTRANSDATE')->order('ID desc')->find();
            $v['REPORTINFO']=$report['REPORTINFO'];
            $v['ALARMSTARTTIME']=$report['ALARMSTARTTIME'];
            $v['LASTTRANSDATE']=$realtimedata['LASTTRANSDATE'];


            $v['TEMPERTHRESHOLD'] = $arr[$v['TEMPERTHRESHOLD']];
            if (!isset($realtimedata['STATUS']) || $realtimedata['STATUS'] == null || $realtimedata['STATUS'] == 'null') {
                $v['statu']=3;
                $v['STATUS'] = $sta[3];
            }else{
                $v['statu']=$realtimedata['STATUS'];
                $v['STATUS'] = $sta[$realtimedata['STATUS']];
            }

            $v['ALARMSTARTTIME'] = empty($v['ALARMSTARTTIME']) ? '无' : $v['ALARMSTARTTIME'];
            $v['REPORTINFO'] = !empty($v['REPORTINFO']) ? $v['REPORTINFO'] : '无';

            //$v['ADDRESS'] = empty($v['STATIONNAME'])?"未命名":$v['STATIONNAME'] . empty($v['POWERDISCABNAME'])?"未命名":$v['POWERDISCABNAME'] . empty($v['CELLNAME'])?"未命名":$v['CELLNAME'];
            $v['ADDRESS'] = $v['STATIONNAME'] .$v['POWERDISCABNAME'] . $v['CELLNAME'];
            $v['DEVICENAME'] = $v['DEVICENAME'] ? $v['DEVICENAME'] : '未命名';

                $res = Db::table('follow')->where([['PID', '=', $v['PRIMARYADDR']], ['DID', '=', $v['DEVICEID']]])->count();

            $v['is_follow'] = $res ? 1 : 0;

        }
        $this->api_return_json(compact('reportdata'), 1);
    }


    /*
     * @name 删除
     *
     * */
    public function delete()
    {

    }

    /*
     *
     * @name 添加/编辑设备
     * */
    public function addequipment()
    {
        $param = $this->request->param();
        if (!isset($param['PRIMARYADDR']) || empty($param['PRIMARYADDR'])) {
            $this->api_return_json(array('msg' => '请填写中继器ID'));
        }
        if (!isset($param['REPEATERNAME']) || empty($param['REPEATERNAME'])) {
            $this->api_return_json(array('msg' => '请填写中继器名称'));
        }
        if (!isset($param['STATIONNAME']) || empty($param['STATIONNAME'])) {
            $this->api_return_json(array('msg' => '请填写站点名称'));
        }
        if (!isset($param['DVICLONGITUDE']) || empty($param['DVICLONGITUDE'])) {
            $this->api_return_json(array('msg' => '请填写经度'));
        }
        if (!isset($param['DVICLATITUDE']) || empty($param['DVICLATITUDE'])) {
            $this->api_return_json(array('msg' => '请填写纬度'));
        }

        $data = array();
        $data['PRIMARYADDR'] = $param['PRIMARYADDR'];
        $data['REPEATERNAME'] = $param['REPEATERNAME'];
        $data['STATIONNAME'] = $param['STATIONNAME'];
        $data['DVICLONGITUDE'] = $param['DVICLONGITUDE'];
        $data['DVICLATITUDE'] = $param['DVICLATITUDE'];


        //修改
        if (!empty($param['ID'])) {
            $str="编辑";
            $ID = $param['ID'];
            unset($param['ID']);
            $is_ok = Db::name('primarydev')->where([['PRIMARYADDR', '=', $param['PRIMARYADDR']], ['ID', 'neq', $ID]])->count();
            if ($is_ok) {
                $this->api_return_json(array('msg' => '中继器ID已经存在'));
            }
            $res = Db::name('primarydev')->where([['ID', '=', $ID]])->update($data);
        } else {
            $str="添加";
            $is_ok = Db::name('primarydev')->where([['PRIMARYADDR', '=', $param['PRIMARYADDR']]])->count();
            if ($is_ok) {
                $this->api_return_json(array('msg' => '中继器ID已经存在'));
            }
            $res = Db::name('primarydev')->insert($data);
        }

        if ($res) {
            $this->api_return_json(array('msg' => $str.'成功'), 1);
        } else
            $this->api_return_json(array('msg' => $str.'失败'));
    }

    /*
     * @name 编辑页面数据接口
     * */
    public function edit()
    {
        $ID = $this->request->param('ID');
        $edit['PRIMARYADDR'] = "";
        $edit['REPEATERNAME'] = "";
        $edit['STATIONNAME'] = "";
        $edit['DVICLONGITUDE'] = "";
        $edit['DVICLATITUDE'] = "";

        if ($ID) {
            $edit = Db::name('primarydev')->where([['ID', '=', $ID]])->find();
        }
        $this->api_return_json(compact('edit'), 1);
    }

    /*
     * @name 添加时间段
     * */

    public function timeadd()
    {
        $day = $this->request->param('day');
        //$ALARMENDTIME = $this->request->param('ALARMENDTIME');
        if (!empty($day)) {
               cache('day',$day);
               //cache('ALARMENDTIME',$ALARMENDTIME);
           
        }else{
            $this->api_return_json(array('msg' => '时间周期不能为空'));
        }

        $this->api_return_json(array('msg' => '设置时间周期成功'),1);
    }
    /*
       * @name 添加时间段
       * */

    public function datetime()
    {
        $day=cache('day');
        //$ALARMENDTIME=cache('ALARMENDTIME');
        $this->api_return_json(compact('day'), 1);

    }

    /*
     *
     * @name FPA列表
     * */
    public function deviceinfo()
    {
        $PRIMARYADDR = $this->request->param('PRIMARYADDR', 0);
        $page = $this->request->param('page', 1);
		$keywords=$this->request->param('keywords','');
		$where=array(array('d.PRIMARYADDR', '=', $PRIMARYADDR));
		if(!empty($keywords))
			$where[]=array('POWERDISCABNAME|DEVICENAME|CELLNAME', 'like', "%$keywords%");
        $deviceinfo = Db::table('deviceinfo')
            ->alias('d')
            ->where($where)
            ->join('realtimedata rt', 'rt.DEVICEID=d.DEVICEID and rt.PRIMARYADDR=d.PRIMARYADDR', 'left')
            ->field('d.*,rt.STATUS,rt.LASTTRANSDATE')
            ->page($page, $this->pages)
            ->select();
        $arr = array(1 => '80℃', 2 => '100℃', 3 => '130℃');
        $sta = array('在线', '报警', '不在线', '未配置');
        $che = array('正常', '正在报警');
        foreach ($deviceinfo as $k => &$v) {
            $v['statu']=$v['STATUS'];
            $v['TEMPERTHRESHOLD'] = array_key_exists((int)$v['TEMPERTHRESHOLD'], $arr) ? $arr[(int)$v['TEMPERTHRESHOLD']] : '80℃';
            $v['STATUS'] = $sta[is_null($v['STATUS']) ? 3 : $v['STATUS']];
            $v['is_follow'] = Db::table('follow')->where([['UID', '=', $this->userid], ['PID', '=', $v['PRIMARYADDR']], ['DID', '=', $v['DEVICEID']]])->count();
            $CHE = Db::table('reportdata')->where(array(array('ALARMENDTIME', '=', ''), array('PRIMARYADDR', '=', $v['PRIMARYADDR']), array('DEVICEID', '=', $v['DEVICEID'])))->count();
            $v['CHECK'] = $che[$CHE];
            $v['POWERDISCABNAME']=empty($v['POWERDISCABNAME'])?'未命名':$v['POWERDISCABNAME'];
            $v['DEVICENAME']=empty($v['DEVICENAME'])?'未命名':$v['DEVICENAME'];

        }
        $user_id=$this->userid;
        $num = Db::table('deviceinfo')->where(array('PRIMARYADDR' => $PRIMARYADDR))->count();
        $this->api_return_json(compact('deviceinfo', 'num','user_id'), 1);

    }

    /*
     * @name 编辑页面数据接口
     * */
    public function deviceedit()
    {
        $ID = $this->request->param('ID');
        $deviceedit['DEVICEID'] = '';
        $deviceedit['DEVICENAME'] = '';
        $deviceedit['TEMPERTHRESHOLD'] = '';
        $deviceedit['CELLNAME'] = '';
        $deviceedit['POWERDISCABVOLUME'] = '';
        $deviceedit['TEMPERPATCHNUM'] = '';
        $deviceedit['POWERDISCABNAME'] = '';

        if ($ID) {
            $deviceedit = Db::name('deviceinfo')->where([['ID', '=', $ID]])->find();
        }
        $this->api_return_json(compact('deviceedit'), 1);
    }

    /*
     *
     * @name FPA添加编辑
     * */
    public function deviceinfoinfo()
    {
        $param = $this->request->param();
        if (!isset($param['DEVICEID']) || empty($param['DEVICEID'])) {
            $this->api_return_json(array('msg' => '请填写监测点ID'));
        }
        if (!isset($param['DEVICENAME']) || empty($param['DEVICENAME'])) {
            $this->api_return_json(array('msg' => '请填写FPA名称'));
        }
        if (!isset($param['TEMPERTHRESHOLD']) || empty($param['TEMPERTHRESHOLD'])) {
            $this->api_return_json(array('msg' => '请选择温度阈值'));
        }
        if (!isset($param['CELLNAME']) || empty($param['CELLNAME'])) {
            $this->api_return_json(array('msg' => '请填写小室名称'));
        }
        if (!isset($param['POWERDISCABVOLUME']) || empty($param['POWERDISCABVOLUME'])) {
            $this->api_return_json(array('msg' => '请填写开关柜小室体积'));
        }
        if (!isset($param['TEMPERPATCHNUM']) || empty($param['TEMPERPATCHNUM'])) {
            $this->api_return_json(array('msg' => '请填写感温贴片数量'));
        }
        //修改
        $data['DEVICEID'] = $param['DEVICEID'];
        $data['DEVICENAME'] = $param['DEVICENAME'];
        $data['TEMPERTHRESHOLD'] = $param['TEMPERTHRESHOLD'];
        $data['CELLNAME'] = $param['CELLNAME'];
        $data['POWERDISCABVOLUME'] = $param['POWERDISCABVOLUME'];
        $data['TEMPERPATCHNUM'] = $param['TEMPERPATCHNUM'];
        $data['PRIMARYADDR'] = $param['PRIMARYADDR'];
        $data['POWERDISCABNAME'] = $param['POWERDISCABNAME'];

        if (!empty($param['ID'])) {
            $ID = $param['ID'];
            unset($param['ID']);

            $is_ok = Db::name('deviceinfo')->where([['PRIMARYADDR', '=', $param['PRIMARYADDR']], ['DEVICEID', '=', $param['DEVICEID']], ['ID', 'neq', $ID]])->count();

            if ($is_ok) {
                $this->api_return_json(array('msg' => '监测点ID不能重复'));
            }

            $str = '编辑';
            $res = Db::name('deviceinfo')->where([['ID', '=', $ID]])->update($data);
        } else {
            $is_ok = Db::name('deviceinfo')->where([['PRIMARYADDR', '=', $param['PRIMARYADDR']], ['DEVICEID', '=', $param['DEVICEID']]])->count();
            if ($is_ok) {
                $this->api_return_json(array('msg' => '监测点ID不能重复'));
            }

            $str = '添加';
            $res = Db::name('deviceinfo')->insert($data);
        }
        if ($res) {
            $this->api_return_json(array('msg' => $str . '成功'), 1);
        }
    }

    /*
     * @name 数据页面列表
     * */
    public function datalist()
    {

        $page = $this->request->param('page');
        $ALARMENDTIME = $this->request->param('ALARMENDTIME');//结束时间
        $ALARMSTARTTIME = $this->request->param('ALARMSTARTTIME');//开始时间
        $PRIMARYADDR = $this->request->param('PRIMARYADDR');//中继器ID

        if (empty($ALARMENDTIME)||empty($ALARMSTARTTIME)) {
            $ALARMENDTIME = date('Y-m-d H:i:s');//cache('ALARMENDTIME');
        }
        if (empty($ALARMSTARTTIME)) {
            $ALARMSTARTTIME = date('Y-m-d H:i:s',time()-intval(empty(cache('day'))?$this->normalDay*24*3600:(int)cache('day')*24*3600));//cache('ALARMSTARTTIME');
        }
        if ($PRIMARYADDR) {
            $where = ['re.PRIMARYADDR', '=', $PRIMARYADDR];
            $where1 = ['PRIMARYADDR', '=', $PRIMARYADDR];
        } else {
            $where = ['re.ID', '>', 0];
            $where1 = ['ID', '>', 0];
            if($this->userid!=1){
                $puser=$this->userList();
                $where=array('re.PRIMARYADDR', 'in', $puser);
                $where1 = array('PRIMARYADDR', 'in', $puser);
            }


        }


        $datatime = array('ALARMSTARTTIME' => $ALARMSTARTTIME, 'ALARMENDTIME' => $ALARMENDTIME);
        $datalist = Db::table('reportdata')
            ->alias('re')
            ->where([['re.ALARMSTARTTIME', '>', $ALARMSTARTTIME], ['re.ALARMENDTIME', '<', $ALARMENDTIME], $where])
            ->join('realtimedata rt', 're.DEVICEID=rt.DEVICEID and re.PRIMARYADDR=rt.PRIMARYADDR', 'left')
            ->join('deviceinfo d', 're.DEVICEID=d.DEVICEID and re.PRIMARYADDR=d.PRIMARYADDR', 'left')
            ->field('d.*,re.ALARMSTARTTIME,re.ALARMENDTIME,re.REPORTINFO,re.STATIONNAME,rt.STATUS')
            ->order('ID desc')
            ->page($page, $this->pages)
            ->select();
        $arr = array(1 => '80℃', 2 => '100℃', 3 => '130℃');
        $sta = array('在线', '报警', '不在线', '未配置');
        //echo Db::table('reportdata')->getLastSql();
        foreach ($datalist as $k => &$v) {
            $v['statu']=$v['STATUS'];
            $v['TEMPERTHRESHOLD'] = array_key_exists((int)$v['TEMPERTHRESHOLD'], $arr) ? $arr[(int)$v['TEMPERTHRESHOLD']] : '80℃';
            $v['STATUS'] = $sta[is_null($v['STATUS']) ? 3 : $v['STATUS']];
            $v['is_follow'] = Db::table('follow')->where([['UID', '=', $this->userid], ['PID', '=', $v['PRIMARYADDR']], ['DID', '=', $v['DEVICEID']]])->count();
            $v['ADDRESS'] = $v['STATIONNAME'] . $v['POWERDISCABNAME'] . $v['CELLNAME'];

        }

        $number =Db::table('reportdata')->where([['ALARMSTARTTIME', '>', $ALARMSTARTTIME], ['ALARMENDTIME', '<', $ALARMENDTIME], $where1])->count();
        $this->api_return_json(compact('datalist', 'datatime', 'number'), 1);
    }

    /*
     *
     * @name 数据详情页
     *
     * */
    public function dataDetails()
    {
        $page = $this->request->param('page');
        $ALARMENDTIME = $this->request->param('ALARMENDTIME');//结束时间
        $ALARMSTARTTIME = $this->request->param('ALARMSTARTTIME');//开始时间
        $PRIMARYADDR = $this->request->param('PRIMARYADDR');//中继器ID
        $DEVICEID = $this->request->param('DEVICEID');//报警监测点ID
        if (empty($ALARMENDTIME)) {
            $ALARMENDTIME = date('Y-m-d H:i:s');//cache('ALARMENDTIME');
        }
        if (empty($ALARMSTARTTIME)) {
            $ALARMSTARTTIME = date('Y-m-d H:i:s',time()-intval(empty(cache('day'))?$this->normalDay*24*3600:(int)cache('day')*24*3600));//cache('ALARMSTARTTIME');//cache('ALARMSTARTTIME');
        }
        if (empty($PRIMARYADDR)||empty($DEVICEID)) {
            $this->api_return_json(array('msg' => '数据错误请重新打开。'));
        }
//['re.ALARMSTARTTIME', '<', $ALARMSTARTTIME], ['re.ALARMENDTIME', '>', $ALARMENDTIME]
        $datatime = array('ALARMSTARTTIME' => $ALARMSTARTTIME, 'ALARMENDTIME' => $ALARMENDTIME);
        $dataDetails = Db::table('reportdata')
            ->alias('re')
            ->where([['re.PRIMARYADDR','=',$PRIMARYADDR],['re.DEVICEID','=',$DEVICEID]])
            ->join('realtimedata rt', 're.DEVICEID=rt.DEVICEID and re.PRIMARYADDR=rt.PRIMARYADDR', 'left')
            ->join('deviceinfo d', 're.DEVICEID=d.DEVICEID and re.PRIMARYADDR=d.PRIMARYADDR', 'left')
            ->field('d.*,re.ALARMSTARTTIME,re.ALARMENDTIME,re.REPORTINFO,re.STATIONNAME,rt.STATUS')
            ->order('ALARMSTARTTIME desc')
            ->page($page, $this->pages)
            ->select();
        //$arr = array(1 => '80℃', 2 => '100℃', 3 => '130℃');
        $arrt = array(1 => 80, 2 => 100, 3 => 130);
        $sta = array('在线', '报警', '不在线', '未配置');
        $xAxis=$yValue=array();
        foreach ($dataDetails as $k => &$v) {
            if($k==0){
                $TEMPERTHRESHOLD=empty($v['TEMPERTHRESHOLD'])?0:$arrt[$v['TEMPERTHRESHOLD']];
                if(!empty($v['ALARMSTARTTIME'])) {
                    array_push($xAxis, date('H:i:s',strtotime($v['ALARMSTARTTIME'])));
                    array_push($yValue, $TEMPERTHRESHOLD);
                }
                if(!empty($v['ALARMENDTIME'])) {
                    array_push($xAxis, date('H:i:s',strtotime($v['ALARMENDTIME'])));
                    array_push($yValue, 0);
                }

            }
            $v['statu']=$v['STATUS'];
            $v['TEMPERTHRESHOLD'] = array_key_exists((int)$v['TEMPERTHRESHOLD'], $arrt) ? $arrt[(int)$v['TEMPERTHRESHOLD']] : '80℃';
            $v['STATUS'] = $sta[is_null($v['STATUS']) ? 3 : $v['STATUS']];
            $v['is_follow'] = Db::table('follow')->where([['UID', '=', $this->userid], ['PID', '=', $v['PRIMARYADDR']], ['DID', '=', $v['DEVICEID']]])->count();
            $v['ADDRESS'] = $v['STATIONNAME'] . $v['POWERDISCABNAME'] . $v['CELLNAME'];


        }
        if(count($yValue)==1){
            array_push($yValue,$yValue[0]);
        }else{
            array_push($yValue,0);
        }


        array_unshift($yValue,0);

        array_push($xAxis,'23:59:59');
        array_unshift($xAxis,'00:00:00');
        $data=array('xAxis'=>$xAxis,'yValue'=>$yValue);
        $this->api_return_json(compact('dataDetails', 'data'), 1);
    }
    
    //自动报警
    public function autoBaojing(){
    	$realtimedata=Db::table('realtimedata')->where(array('STATUS'=>1))->field('LASTTRANSDATE')->order('LASTTRANSDATE desc')->find();
    	if(!empty($realtimedata['LASTTRANSDATE'])&&strtotime($realtimedata['LASTTRANSDATE'])>time()-10){
    		$res=$this->sendPush();
    		var_dump($res);
    	}
    	echo 'ok';
    }
    
    //消息推送
    public function sendPush(){
    	$igt = new IGeTui(HOST,APPKEY,MASTERSECRET);
    	// STEP2：选择通知模板
    	//定义透传模板，设置透传内容，和收到消息是否立即启动启用
    	$template = $this->IGtNotificationTemplateDemo();
    	// STEP5：定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
    	$message = new IGtAppMessage();
    	$message->set_isOffline(true);
    	$message->set_offlineExpireTime(10 * 60 * 1000);//离线时间单位为毫秒，例，两个小时离线为3600*1000*2
    	$message->set_data($template);
    	
    	$appIdList=array(APPID);
    	// $phoneTypeList=array('ANDROID');
    	// $provinceList=array('浙江');
    	// $tagList=array('haha');
    	
    	$message->set_appIdList($appIdList);
    	//$message->set_conditions($cdt->getCondition());
    	// STEP6：执行推送
    	$rep = $igt->pushMessageToApp($message,"任务组名");
    	
    	return $rep;
    } 
    
    private function IGtNotificationTemplateDemo(){
    	$template =  new IGtNotificationTemplate();
    	$template->set_appId(APPID);                   //应用appid
    	$template->set_appkey(APPKEY);                 //应用appkey
    	$template->set_transmissionType(1);            //透传消息类型
    	$template->set_transmissionContent("测试离线");//透传内容
    	// STEP3：设置推送标题、推送内容
    	$template->set_title("温度报警通知");      //通知栏标题
    	$template->set_text("FIPRES收到报警信息一条，请前往查看 ".date('H:i:s'));     //通知栏内容
    	$template->set_logo("");                       //通知栏logo
    	$template->set_logoURL("");                    //通知栏logo链接
    	//STEP4：设置响铃、震动等推送效果
    	$template->set_isRing(true);                   //是否响铃
    	$template->set_isVibrate(true);                //是否震动
    	$template->set_isClearable(true);              //通知栏是否可清除
    
    	return $template;
    }
}




