<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
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;

//产品类

/**
 * @author lyuis
 *    小程序首页相应功能类
 */
class IndexController extends MainController
{
    private $pages = 15;


    /*
     * @name 站点列表
     *
     * */
    public function index()
    {
        //站点信息
        $index = Db::table('primarydev')->select();

        foreach ($index as $k => &$v) {
            $res = $this->bd_decrypt($v['DVICLONGITUDE'], $v['DVICLATITUDE']);
            $v['DVICLONGITUDE'] = $res['gg_lon'];
            $v['DVICLATITUDE'] = $res['gg_lat'];
        }
        $ALARMSTARTTIME=config('app.ALARMSTARTTIME');
        $ALARMENDTIME=config('app.ALARMENDTIME');


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


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

    }

    /*
     *
     * @name 报警设备
     * */
    public function reportdata()
    {
        $PRIMARYADDR = $this->request->param('PRIMARYADDR');
        $type = $this->request->param('type', 0);
        if (!intval($this->userid)) {
            $this->api_return_json(array('msg' => '请重新登录'), 0);
        }
        if (isset($PRIMARYADDR) && intval($PRIMARYADDR)) {
            //如果为真查询关注报警设备
            if ($type) {
                $reportdata = Db::table('follow')
                    ->alias('f')
                    ->where(array(array('f.UID', '=', $this->userid), array('rp.ALARMENDTIME', '=', ''), array('f.PID', '=', $PRIMARYADDR)))
                    ->join('reportdata rp', 'f.DID=rp.DEVICEID and F.PID=rp.PRIMARYADDR', 'left')
                    ->join('realtimedata rt', 'f.DID=rt.DEVICEID and F.PID=rt.PRIMARYADDR', 'left')
                    ->field('rp.*,rt.STATUS,rt.LASTTRANSDATE')
                    ->select();
            } else {
                //报警设备 查站点id下的
                $reportdata = Db::table('reportdata')
                    ->alias('rp')
                    ->where(array(array('rp.ALARMENDTIME', '=', ''), array('rp.PRIMARYADDR', '=', $PRIMARYADDR)))
                    ->join('realtimedata rt', 'rt.DEVICEID=rp.DEVICEID and rt.PRIMARYADDR=rp.PRIMARYADDR', 'left')
                    ->field('rp.*,rt.STATUS,rt.LASTTRANSDATE')
                    ->select();
            }

        } else {
            if ($type) {
                $reportdata = Db::table('follow')
                    ->alias('f')
                    ->where(array(array('f.UID', '=', $this->userid), array('rp.ALARMENDTIME', '=', '')))
                    ->join('reportdata rp', 'f.DID=rp.DEVICEID and F.PID=rp.PRIMARYADDR', 'left')
                    ->join('realtimedata rt', 'f.DID=rt.DEVICEID and F.PID=rt.PRIMARYADDR', 'left')
                    ->field('rp.*,rt.STATUS,rt.LASTTRANSDATE')
                    ->select();
            } else {
                //报警设备 查所有
                $reportdata = Db::table('reportdata')
                    ->alias('rp')
                    ->where(array(array('rp.ALARMENDTIME', '=', '')))
                    ->join('realtimedata rt', 'rt.DEVICEID=rp.DEVICEID and rt.PRIMARYADDR=rp.PRIMARYADDR', 'left')
                    ->field('rp.*,rt.STATUS,rt.LASTTRANSDATE')
                    ->select();
            }
        }
        $arr = array(1 => '80℃', 2 => '2:100℃', 3 => '130℃');
        $sta = array('在线', '报警', '不在线', '未配置');
        foreach ($reportdata as &$v) {
            $v['TEMPERTHRESHOLD'] = $arr[$v['TEMPERTHRESHOLD']];
            $v['STATUS'] = $sta[$v['STATUS']];
            $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 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'])) {
           $ID=$param['ID'];
           unset($param['ID']);
            $res = Db::name('primarydev')->where([['ID','=',$ID]])->update($data);
        } else {
            $res = Db::name('primarydev')->insert($data);
        }

        if ($res) {
            $this->api_return_json(array('msg' => '添加成功'), 1);
        }else
            $this->api_return_json(array('msg' => '操作失败'));
    }
    /*
     * @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(){
        $ALARMSTARTTIME = $this->request->param('ALARMSTARTTIME');
        $ALARMENDTIME = $this->request->param('ALARMENDTIME');
        if(isset($ALARMSTARTTIME)&&isset($ALARMENDTIME)){
            config('app.ALARMENDTIME',strtotime($ALARMENDTIME));
            config('app.ALARMSTARTTIME',strtotime($ALARMSTARTTIME));
        }
        $this->api_return_json(array('msg' => '时间周期不能为空'), 1);
    }


    /*
     *
     * @name FPA列表
     * */
    public function deviceinfo(){
        $PRIMARYADDR = $this->request->param('PRIMARYADDR',0);

        $deviceinfo = Db::table('deviceinfo')
            ->alias('d')
            ->where( array(array('d.PRIMARYADDR', '=', $PRIMARYADDR)))
            ->join('realtimedata rt', 'rt.DEVICEID=d.DEVICEID and rt.PRIMARYADDR=d.PRIMARYADDR', 'left')
            ->field('d.*,rt.STATUS,rt.LASTTRANSDATE')
            ->select();
        $arr = array(1 => '80℃', 2 => '2:100℃', 3 => '130℃');
        $sta = array('在线', '报警', '不在线', '未配置');
        foreach ($deviceinfo as $k => &$v) {
            $v['TEMPERTHRESHOLD'] = array_key_exists((int)$v['TEMPERTHRESHOLD'],$arr)?$arr[(int)$v['TEMPERTHRESHOLD']]:'80℃';
            $v['STATUS'] = $sta[is_null($v['STATUS'])? 3:$v['STATUS']];
        }

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

    }

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

        if ($ID) {
            $edit = 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']);
            $str='添加';
            $res = Db::name('deviceinfo')->where([['ID','=',$ID]])->update($data);
        } else {
            $str='编辑';
            $res = Db::name('deviceinfo')->insert($data);
        }
        if ($res) {
            $this->api_return_json(array('msg' => $str.'成功'), 1);
        }
    }
}




