<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
class  TbEia 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);

        //var_dump(request()->param());
        $data = Db::table('data_eia_h')->field('d_time,d_pt_1,d_pt_2,d_pt_3,d_ct_1,d_ct_2,d_ct_3,d_pf_1,d_pf_2,d_pf_3,d_jx_1,d_jx_2,d_jx_3')->where('eqmid', $id)->where('d_time', 'between time', [$sdate,$edate])->order('d_time desc')->paginate(10,false,['query' => request()->param()]);
        $data1 = Db::table('data_eia_h')->field('d_time,d_pt_1,d_pt_2,d_pt_3,d_ct_1,d_ct_2,d_ct_3,d_pf_1,d_pf_2,d_pf_3,d_jx_1,d_jx_2,d_jx_3')->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_eia');
    }
	
	
	 public function ExcelOne()
    {
		$list = session('data');
		//session(null);
		//dump($list);die;
		$xlsCell  = array(
        array('d_time','时间'),
        array('d_pt_1','A相母线电压'),
        array('d_pt_2','B相母线电压'),
        array('d_pt_3','C相母线电压'),
        array('d_ct_1','A相泄露电流'),
        array('d_ct_2','B相泄露电流'),
        array('d_ct_3','C相泄露电流'),
        array('d_pf_1','A相电容量'),
        array('d_pf_2','B相电容量'),
        array('d_pf_3','C相电容量'),
        array('d_jx_1','A相介质损耗因数'),
        array('d_jx_2','B相介质损耗因数'),  		
        array('d_jx_3','C相介质损耗因数')		
        );
        $this->exportExcel("当前数据导出",$xlsCell,$list);
		
       
    }
	public function ExcelAll()
    {
		$list = session('data1');

		$xlsCell  = array(
        array('d_time','时间'),
        array('d_pt_1','A相母线电压'),
        array('d_pt_2','B相母线电压'),
        array('d_pt_3','C相母线电压'),
        array('d_ct_1','A相泄露电流'),
        array('d_ct_2','B相泄露电流'),
        array('d_ct_3','C相泄露电流'),
        array('d_pf_1','A相电容量'),
        array('d_pf_2','B相电容量'),
        array('d_pf_3','C相电容量'),
        array('d_jx_1','A相介质损耗因数'),
        array('d_jx_2','B相介质损耗因数'),  		
        array('d_jx_3','C相介质损耗因数')		
        );
        $this->exportExcel("全部数据导出",$xlsCell,$list);
		
       
    }
	public function exportExcel($expTitle,$expCellName,$expTableData)
	{
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = $expTitle.date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);
        //vendor("PHPExcel.PHPExcel");   
		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');//合并单元格
        // $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  Export time:'.date('Y-m-d H:i:s'));  
        //$objPHPExcel->getActiveSheet(0)->getDefaueltRowDimension()->setRowHeight(15);
		for($i=0;$i<$cellNum;$i++){
			$objPHPExcel->getActiveSheet(0)->getColumnDimension($cellName[$i])->setWidth(18);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]); 
        } 
          // Miscellaneous glyphs, UTF-8   
        for($i=0;$i<$dataNum;$i++){
			
			 
          for($j=0;$j<$cellNum;$j++){

			 if($expTableData[$i]['d_pt_1']<session('warn.warnmin1') || $expTableData[$i]['d_pt_1']>session('warn.warnmax1')){
				 $objPHPExcel->getActiveSheet(0)->getStyle('B'.($i+3))->getFont()->getColor()->setARGB('FFFF0000');// 设置文字颜色		 
			 }
			 if($expTableData[$i]['d_pt_2']<session('warn.warnmin2') || $expTableData[$i]['d_pt_2']>session('warn.warnmax2')){
				 $objPHPExcel->getActiveSheet(0)->getStyle('C'.($i+3))->getFont()->getColor()->setARGB('FFFF0000');// 设置文字颜色		 
			 }
			 if($expTableData[$i]['d_pt_3']<session('warn.warnmin3') || $expTableData[$i]['d_pt_3']>session('warn.warnmax3')){
				 $objPHPExcel->getActiveSheet(0)->getStyle('D'.($i+3))->getFont()->getColor()->setARGB('FFFF0000');// 设置文字颜色		 
			 }
			 if($expTableData[$i]['d_ct_1']<session('warn.warnmin5') || $expTableData[$i]['d_ct_1']>session('warn.warnmax5')){
				 $objPHPExcel->getActiveSheet(0)->getStyle('E'.($i+3))->getFont()->getColor()->setARGB('FFFF0000');// 设置文字颜色		 
			 }
			 if($expTableData[$i]['d_ct_2']<session('warn.warnmin6') || $expTableData[$i]['d_ct_2']>session('warn.warnmax6')){
				 $objPHPExcel->getActiveSheet(0)->getStyle('F'.($i+3))->getFont()->getColor()->setARGB('FFFF0000');// 设置文字颜色		 
			 }
			 if($expTableData[$i]['d_ct_3']<session('warn.warnmin7') || $expTableData[$i]['d_ct_3']>session('warn.warnmax7')){
				 $objPHPExcel->getActiveSheet(0)->getStyle('G'.($i+3))->getFont()->getColor()->setARGB('FFFF0000');// 设置文字颜色		 
			 }
			 if($expTableData[$i]['d_pf_1']<session('warn.warnmin10') || $expTableData[$i]['d_pf_1']>session('warn.warnmax10')){
				 $objPHPExcel->getActiveSheet(0)->getStyle('H'.($i+3))->getFont()->getColor()->setARGB('FFFF0000');// 设置文字颜色		 
			 }
			 if($expTableData[$i]['d_pf_2']<session('warn.warnmin11') || $expTableData[$i]['d_pf_2']>session('warn.warnmax11')){
				 $objPHPExcel->getActiveSheet(0)->getStyle('I'.($i+3))->getFont()->getColor()->setARGB('FFFF0000');// 设置文字颜色		 
			 }
			 if($expTableData[$i]['d_pf_3']<session('warn.warnmin12') || $expTableData[$i]['d_pf_3']>session('warn.warnmax12')){
				 $objPHPExcel->getActiveSheet(0)->getStyle('J'.($i+3))->getFont()->getColor()->setARGB('FFFF0000');// 设置文字颜色		 
			 }
			 if($expTableData[$i]['d_jx_1']<session('warn.warnmin13') || $expTableData[$i]['d_jx_1']>session('warn.warnmax13')){
				 $objPHPExcel->getActiveSheet(0)->getStyle('K'.($i+3))->getFont()->getColor()->setARGB('FFFF0000');// 设置文字颜色		 
			 }
			 if($expTableData[$i]['d_jx_2']<session('warn.warnmin15') || $expTableData[$i]['d_jx_2']>session('warn.warnmax15')){
				 $objPHPExcel->getActiveSheet(0)->getStyle('L'.($i+3))->getFont()->getColor()->setARGB('FFFF0000');// 设置文字颜色		 
			 }
			 if($expTableData[$i]['d_jx_3']<session('warn.warnmin16') || $expTableData[$i]['d_jx_3']>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]]);
			
			}  
 //dump($expTableData[$i]['d_ct_1']);
        }  
		
        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; 

	
    }
	

}

























































