<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;

class TbEtpGxcw extends Controller {

    public function index() {
        $edate = date("Y-m-d");
        $sdate = date('Y-m-d', strtotime("$edate -1 month"));
        if (!empty($_GET['stime'])) {
            $sdate = $_GET['stime'];
        }
        if (!empty($_GET['etime'])) {
            $edate = substr($_GET['etime'], 0, 10);
            $edate = date("Y-m-d", strtotime("$edate +1 day"));
        }
        if (empty($_GET['id']))
            die('无效的编号');
        $id = $_GET['id'];
        $warn = Db::table('warn_cfg')->where('eqmid', $id)->find();
        session('warn', $warn);
        $this->assign('warn', $warn);
        $data = Db::table('data_etp_h')->field('d_Time,t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16')->where('eqmid', $id)->where('d_Time', 'between time', [$sdate, $edate])->order('d_Time desc')->paginate(10, false, ['query' => request()->param(),]);
        $data1 = Db::table('data_etp_h')->field('d_Time,t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16')->where('eqmid', $id)->where('d_Time', 'between time', [$sdate, $edate])->select();
        session('data', $data);
        session('data1', $data1);
        $this->assign('data', $data);
        return $this->fetch('tb_etp_gxcw');
    }

    public function ExcelOne() {
        $list = session('data');
        $xlsCell = array(
            array('d_Time', '时间'),
            array('t1', '1通道温度'),
            array('t2', '2通道温度'),
            array('t3', '3通道温度'),
            array('t4', '4通道温度'),
            array('t5', '5通道温度'),
            array('t6', '6通道温度'),
            array('t7', '7通道温度'),
            array('t8', '8通道温度'),
            array('t9', '9通道温度'),
            array('t10', '10通道温度'),
            array('t11', '11通道温度'),
            array('t12', '12通道温度'),
            array('t13', '13通道温度'),
            array('t14', '14通道温度'),
            array('t15', '15通道温度'),
            array('t16', '16通道温度')
        );
        $this->exportExcel("当前数据导出", $xlsCell, $list);
    }

    public function ExcelAll() {
        $list = session('data1');
        $xlsCell = array(
            array('d_Time', '时间'),
            array('t1', '1通道温度'),
            array('t2', '2通道温度'),
            array('t3', '3通道温度'),
            array('t4', '4通道温度'),
            array('t5', '5通道温度'),
            array('t6', '6通道温度'),
            array('t7', '7通道温度'),
            array('t8', '8通道温度'),
            array('t9', '9通道温度'),
            array('t10', '10通道温度'),
            array('t11', '11通道温度'),
            array('t12', '12通道温度'),
            array('t13', '13通道温度'),
            array('t14', '14通道温度'),
            array('t15', '15通道温度'),
            array('t16', '16通道温度')
        );
        $this->exportExcel("全部数据导出", $xlsCell, $list);
    }

    public function exportExcel($expTitle, $expCellName, $expTableData) {
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);
        $fileName = $expTitle . date('_YmdHis');
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);
        import('PhpExcel', EXTEND_PATH);
        $objPHPExcel = new \PhpExcel();
        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
        $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
        $objPHPExcel->getActiveSheet(0)->mergeCells('A1:' . $cellName[$cellNum - 1] . '1');
        for ($i = 0; $i < $cellNum; $i++) {
            $objPHPExcel->getActiveSheet(0)->getColumnDimension($cellName[$i])->setWidth(18);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '2', $expCellName[$i][1]);
        }
        for ($i = 0; $i < $dataNum; $i++) {
            for ($j = 0; $j < $cellNum; $j++) {
                if ($expTableData[$i]['t1'] < session('warn.warnmin1') || $expTableData[$i]['t1'] > session('warn.warnmax1')) {
                    $objPHPExcel->getActiveSheet(0)->getStyle('B' . ($i + 3))->getFont()->getColor()->setARGB('FFFF0000');
                }
                if ($expTableData[$i]['t2'] < session('warn.warnmin2') || $expTableData[$i]['t2'] > session('warn.warnmax2')) {
                    $objPHPExcel->getActiveSheet(0)->getStyle('C' . ($i + 3))->getFont()->getColor()->setARGB('FFFF0000');
                }
                if ($expTableData[$i]['t3'] < session('warn.warnmin3') || $expTableData[$i]['t3'] > session('warn.warnmax3')) {
                    $objPHPExcel->getActiveSheet(0)->getStyle('D' . ($i + 3))->getFont()->getColor()->setARGB('FFFF0000');
                }
                if ($expTableData[$i]['t4'] < session('warn.warnmin4') || $expTableData[$i]['t4'] > session('warn.warnmax4')) {
                    $objPHPExcel->getActiveSheet(0)->getStyle('E' . ($i + 3))->getFont()->getColor()->setARGB('FFFF0000');
                }
                if ($expTableData[$i]['t5'] < session('warn.warnmin5') || $expTableData[$i]['t5'] > session('warn.warnmax5')) {
                    $objPHPExcel->getActiveSheet(0)->getStyle('F' . ($i + 3))->getFont()->getColor()->setARGB('FFFF0000');
                }
                if ($expTableData[$i]['t6'] < session('warn.warnmin6') || $expTableData[$i]['t6'] > session('warn.warnmax6')) {
                    $objPHPExcel->getActiveSheet(0)->getStyle('G' . ($i + 3))->getFont()->getColor()->setARGB('FFFF0000');
                }
                if ($expTableData[$i]['t7'] < session('warn.warnmin7') || $expTableData[$i]['t7'] > session('warn.warnmax7')) {
                    $objPHPExcel->getActiveSheet(0)->getStyle('H' . ($i + 3))->getFont()->getColor()->setARGB('FFFF0000');
                }
                if ($expTableData[$i]['t8'] < session('warn.warnmin8') || $expTableData[$i]['t8'] > session('warn.warnmax8')) {
                    $objPHPExcel->getActiveSheet(0)->getStyle('I' . ($i + 3))->getFont()->getColor()->setARGB('FFFF0000');
                }
                if ($expTableData[$i]['t9'] < session('warn.warnmin9') || $expTableData[$i]['t9'] > session('warn.warnmax9')) {
                    $objPHPExcel->getActiveSheet(0)->getStyle('J' . ($i + 3))->getFont()->getColor()->setARGB('FFFF0000');
                }
                if ($expTableData[$i]['t10'] < session('warn.warnmin10') || $expTableData[$i]['t10'] > session('warn.warnmax10')) {
                    $objPHPExcel->getActiveSheet(0)->getStyle('K' . ($i + 3))->getFont()->getColor()->setARGB('FFFF0000');
                }
                if ($expTableData[$i]['t11'] < session('warn.warnmin11') || $expTableData[$i]['t11'] > session('warn.warnmax11')) {
                    $objPHPExcel->getActiveSheet(0)->getStyle('L' . ($i + 3))->getFont()->getColor()->setARGB('FFFF0000');
                }
                if ($expTableData[$i]['t12'] < session('warn.warnmin12') || $expTableData[$i]['t12'] > session('warn.warnmax12')) {
                    $objPHPExcel->getActiveSheet(0)->getStyle('M' . ($i + 3))->getFont()->getColor()->setARGB('FFFF0000');
                }
                if ($expTableData[$i]['t13'] < session('warn.warnmin13') || $expTableData[$i]['t13'] > session('warn.warnmax13')) {
                    $objPHPExcel->getActiveSheet(0)->getStyle('J' . ($i + 3))->getFont()->getColor()->setARGB('FFFF0000');
                }
                if ($expTableData[$i]['t14'] < session('warn.warnmin14') || $expTableData[$i]['t14'] > session('warn.warnmax14')) {
                    $objPHPExcel->getActiveSheet(0)->getStyle('K' . ($i + 3))->getFont()->getColor()->setARGB('FFFF0000');
                }
                if ($expTableData[$i]['t15'] < session('warn.warnmin15') || $expTableData[$i]['t15'] > session('warn.warnmax15')) {
                    $objPHPExcel->getActiveSheet(0)->getStyle('L' . ($i + 3))->getFont()->getColor()->setARGB('FFFF0000');
                }
                if ($expTableData[$i]['t16'] < session('warn.warnmin16') || $expTableData[$i]['t16'] > session('warn.warnmax16')) {
                    $objPHPExcel->getActiveSheet(0)->getStyle('M' . ($i + 3))->getFont()->getColor()->setARGB('FFFF0000');
                }
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 3), $expTableData[$i][$expCellName[$j][0]]);
            }
        }
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $xlsTitle . '.xls"');
        header('Cache-Control: max-age=0');
        $objWriter->save('php://output');
        exit;
    }

}
