<?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'];
        }

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

    }

    /*
     *
     * @name 报警设备
     * */
    public function reportdata()
    {
        $PRIMARYADDR = $this->request->param('PRIMARYADDR');
        if (isset($PRIMARYADDR) && intval($PRIMARYADDR)) {
            //报警设备 查站点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 {
            //报警设备 查所有
            $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']:'未命名';
        }
        $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' => '请填写纬度'));
        }

        $res = Db::name('device')->insertGetId($param);
        if ($res) {
            $this->api_return_json(array('msg' => '添加成功'), 1);
        }
    }
}



