package com.huatek.busi.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.service.transformer.BusiGeneratorPdService;
import com.huatek.busi.vo.monitor.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.huatek.busi.common.BusiConstant;
import com.huatek.busi.common.SiteConstants.ArresterTreeEnum;
import com.huatek.busi.common.SiteConstants.GisTreeEnum;
import com.huatek.busi.common.SiteConstants.OtherTreeEnum;
import com.huatek.busi.common.SiteConstants.SiteTreeEnum;
import com.huatek.busi.common.SiteConstants.TransfromerTreeEnum;
import com.huatek.busi.common.ValueNamePair;
import com.huatek.busi.common.util.ExcelExport;
import com.huatek.busi.dto.BusiSensorQueryParamDto;
import com.huatek.busi.dto.TransformerPdDto;
import com.huatek.busi.dto.monitor.BusiOnlineMonitorDataDto;
import com.huatek.busi.dto.monitor.BusiOnlineMonitorTrendDataDto;
import com.huatek.busi.service.BusiSensorService;
import com.huatek.busi.service.Other.OtherPdCacheService;
import com.huatek.busi.service.gis.GisPdCacheService;
import com.huatek.busi.service.monitor.BusiOnlineMonitorDataService;
import com.huatek.busi.service.temperature.BusiTemperPointService;
import com.huatek.busi.service.transformer.TransformerPdCacheService;
import com.huatek.busi.service.transformer.TransformerPdService;
import com.huatek.busi.vo.BusiSensorVo;
import com.huatek.busi.vo.BusiTreeNodeVo;
import com.huatek.busi.vo.echarts.EchartLineDataVo;
import com.huatek.torch.frame.exception.OwlException;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 检测数据
 *
 * @author buck_guo
 * @date 2019/08/27
 */
@Api(tags = "在线监测数据查询API接口")
@Slf4j
@RestController
@RequestMapping("/api/busiOnlineMonitorData")
public class BusiOnlineMonitorDataController {
    @Reference(check = false)
    private BusiOnlineMonitorDataService busiOnlineMonitorDataService;
    @Reference(check = false)
    private BusiSensorService busiSensorService;
    @Reference(check = false)
    private TransformerPdCacheService transformerPdCacheService;
    @Reference(check = false)
    private GisPdCacheService gisPdCacheService;
    @Reference(check = false)
    private BusiTemperPointService busiTemperPointService;
    @Reference(check = false)
    private TransformerPdService transformerPdService;
    @Reference(check = false)
    private OtherPdCacheService otherPdCacheService;
    @Reference(check = false)
    private BusiGeneratorPdService busiGeneratorPdService;

    @ApiOperation(value = "传感器Info信息", response = BusiSensorVo.class)
    @GetMapping(value = "/info")
    public Map<String, Object> info(@RequestParam(value = "mainType", required = true) final String equipmentType,
        @RequestParam(value = "sensorCode", required = true) final String sensorCode) {
        // 设置稳定性指标
        BusiSensorVo vo = busiOnlineMonitorDataService.findBusiSensorVo(equipmentType, sensorCode);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", vo);
    }

    @ApiOperation(value = "分页查询检测数据", response = BusiOnlineMonitorDataVo.class)
    @PostMapping(value = "/queryList")
    public Map<String, Object> queryList(@RequestBody BusiOnlineMonitorDataDto param) {
        param.setSubEquipmentType(param.getSubType());
        if (StringUtils.isEmpty(param.getEquipmentType()) || StringUtils.isEmpty(param.getSubEquipmentType())) {
            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, 0, "", Collections.emptyList());
        }
        // 局放返回历史数据
        TransformerPdDto pd = new TransformerPdDto();
        if(isTransPd(param)) {
            pd.setPage(param.getPage());
            pd.setLimit(param.getLimit());
            pd.setCacMonitorTypeCode("021001");
            pd.setMainTreeId(param.getTreeId());
            pd.setSubTreeId(param.getExternalId());
            pd.setSensorId(param.getSensorCode());
            if(param.getDay()!= null)
            {
                pd.setTimeStamp(LocalDateTime.of(param.getDay(), LocalTime.parse("00:00:00")));
            }
            if(param.getDayEnd()!= null)
            {
                pd.setTimeStampEnd(LocalDateTime.of(param.getDayEnd(), LocalTime.parse("23:59:59")));
            }
            PageInfo<Map<String, Object>> pageInfo = transformerPdService.findTransformerPdPage(pd);;
            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
        }else if(isGisPd(param)) {
            pd.setPage(param.getPage());
            pd.setLimit(param.getLimit());
            pd.setCacMonitorTypeCode("024001");
            pd.setMainTreeId(param.getTreeId());
            pd.setSubTreeId(param.getExternalId());
            pd.setSensorId(param.getSensorCode());
            if(param.getDay()!= null)
            {
                pd.setTimeStamp(LocalDateTime.of(param.getDay(), LocalTime.parse("00:00:00")));
            }
            if(param.getDayEnd()!= null)
            {
                pd.setTimeStampEnd(LocalDateTime.of(param.getDayEnd(), LocalTime.parse("23:59:59")));
            }
            PageInfo<Map<String, Object>> pageInfo = transformerPdService.findGisPdPage(pd);
            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());

        }else if(isElePd(param)) {
//            log.debug("电缆局放无数据");
//            throw new OwlException("电缆局放无数据");
        	pd.setPage(param.getPage());
            pd.setLimit(param.getLimit());
            pd.setCacMonitorTypeCode("027001");
            pd.setMainTreeId(param.getTreeId());
            pd.setSubTreeId(param.getExternalId());
            pd.setSensorId(param.getSensorCode());
            if(param.getDay()!= null)
            {
                pd.setTimeStamp(LocalDateTime.of(param.getDay(), LocalTime.parse("00:00:00")));
            }
            if(param.getDayEnd()!= null)
            {
                pd.setTimeStampEnd(LocalDateTime.of(param.getDayEnd(), LocalTime.parse("23:59:59")));
            }
            PageInfo<Map<String, Object>> pageInfo = otherPdCacheService.findOtherPdCachePage(pd);
            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
        } else if (isCablePd(param)) {
            pd.setPage(param.getPage());
            pd.setLimit(param.getLimit());
            pd.setCacMonitorTypeCode("028001");
            pd.setMainTreeId(param.getTreeId());
            pd.setSubTreeId(param.getExternalId());
            pd.setSensorId(param.getSensorCode());
            if(param.getDay()!= null)
            {
                pd.setTimeStamp(LocalDateTime.of(param.getDay(), LocalTime.parse("00:00:00")));
            }
            if(param.getDayEnd()!= null)
            {
                pd.setTimeStampEnd(LocalDateTime.of(param.getDayEnd(), LocalTime.parse("23:59:59")));
            }
            PageInfo<Map<String, Object>> pageInfo = transformerPdService.findCablePdPage(pd);
            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
        } else if (isGeneratorPd(param)) {
            pd.setPage(param.getPage());
            pd.setLimit(param.getLimit());
            pd.setMainTreeId(param.getTreeId());
            pd.setSubTreeId(param.getExternalId());
            pd.setSensorId(param.getSensorCode());
            if(param.getDay()!= null)
            {
                pd.setTimeStamp(LocalDateTime.of(param.getDay(), LocalTime.parse("00:00:00")));
            }
            if(param.getDayEnd()!= null)
            {
                pd.setTimeStampEnd(LocalDateTime.of(param.getDayEnd(), LocalTime.parse("23:59:59")));
            }
            PageInfo<Map<String, Object>> pageInfo = busiGeneratorPdService.findGeneratorPdPage(pd);
            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
        }
        // 其他返回汇总数据
        PageInfo<BusiOnlineMonitorDataVo> pageInfo = busiOnlineMonitorDataService.findBusiOnlineMonitorDataPage(param);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }

    @ApiOperation(value = "趋势图", response = EchartLineDataVo.class)
    @PostMapping(value = "/trend")
    public Map<String, Object> trend(@RequestBody BusiOnlineMonitorTrendDataDto param) {
        if (StringUtils.isEmpty(param.getEquipmentType()) || StringUtils.isEmpty(param.getSubEquipmentType())) {
            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", new EchartLineDataVo(null));
        }
        EchartLineDataVo vo = busiOnlineMonitorDataService.trend(param);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", vo);
    }
    ////////////////////////////////////////////////////////////////////////////
    @ApiOperation(value = "查询主设备类型列表")
    @GetMapping(value = "/mainTypeList")
    public Map<String, Object> mainTypeList() {
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", SiteTreeEnum.EQM_LIST);
    }

    @ApiOperation(value = "查询监测装置列表")
    @ApiImplicitParam(name = "mainType", value = "主设备类型：111——变压器设备; 211——GIS设备 ; 311——测温设备 ; 411——避雷器设备 ; 511--其他 ; 611-发电机 ; 711--电缆")
    @GetMapping(value = "/subTypeList")
    public Map<String, Object> subTypeList(@RequestParam("mainType") String mainType) {
        if (SiteTreeEnum.BYQ_EQM.getValue().equals(mainType)) {

            List<ValueNamePair> list = TransfromerTreeEnum.EQM_LIST;
            Iterator<ValueNamePair> it_b=list.iterator();
            while(it_b.hasNext()){
                ValueNamePair valueNamePair=it_b.next();
                if(TransfromerTreeEnum.JJ_EQM.getValue().equals(valueNamePair.getValue()))
                {
                    it_b.remove();
                }
                if(TransfromerTreeEnum.TX_EQM.getValue().equals(valueNamePair.getValue()))
                {
                    valueNamePair.setName("铁芯夹件");
                }
            }

//            if(!list.isEmpty())
//            {
//                for(ValueNamePair valueNamePair:list)
//                {
//                    if(TransfromerTreeEnum.JJ_EQM.getValue().equals(valueNamePair.getValue()))
//                    {
//                        list.remove(valueNamePair);
//                    }
//                    if(TransfromerTreeEnum.TX_EQM.getValue().equals(valueNamePair.getValue()))
//                    {
//                        valueNamePair.setName("铁芯夹件");
//                    }
//                }
//            }
            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", list);
//            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", TransfromerTreeEnum.EQM_LIST);
        }
        if (SiteTreeEnum.GIS_HL_EQM.getValue().equals(mainType)) {
            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", GisTreeEnum.EQM_LIST);
        }
        if (SiteTreeEnum.BLQ_HL_EQM.getValue().equals(mainType)) {
            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", ArresterTreeEnum.EQM_LIST);
        }
        if (SiteTreeEnum.CW_HL_EQM.getValue().equals(mainType)) {
            // 测温没有外挂装置
            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", Arrays.asList(BusiConstant.TMP_POINT_EQM));
        }
        if (SiteTreeEnum.GEN_EQM.getValue().equals(mainType)) {
            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", SiteConstants.GeneratorTreeEnum.EQM_LIST);
        }
        if (SiteTreeEnum.DL_EQM.getValue().equals(mainType)) {
            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", SiteConstants.CableTreeEnum.EQM_LIST);
        }
        if (SiteTreeEnum.OTHER_EQM.getValue().equals(mainType)) {
            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", OtherTreeEnum.EQM_LIST);
        }
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", null);
    }

    @ApiOperation(value = "外挂设备列表")
    @GetMapping(value = "/subEquipmentList")
    public Map<String, Object> subEquipmentList(
        @RequestParam(value = "mainType", required = true) String mainType,
        @RequestParam(value = "subType", required = true) String subType,
        @RequestParam(value = "mainEquipmentId", required = false) String mainEquipmentId) {
        List<BusiTreeNodeVo> list = busiOnlineMonitorDataService.subEquipmentList(mainType, subType, mainEquipmentId);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", list);
    }

    @ApiOperation(value = "查询传感器列表")
    @GetMapping(value = "/sensorList")
    public Map<String, Object> sensorList(
        @RequestParam(value = "mainType", required = false) String mainType,
        @RequestParam(value = "subType", required = false) String subType,
        @RequestParam(value = "mainEquipmentId", required = false) String mainEquipmentId,
        @RequestParam(value = "subEquipmentId", required = false) String subEquipmentId) {

        List<BusiSensorVo> sensorList = new ArrayList<BusiSensorVo>();
        BusiSensorQueryParamDto param = new BusiSensorQueryParamDto();
        //测温
        if(mainType.equals(SiteTreeEnum.CW_HL_EQM.getValue()))
        {
            sensorList = busiTemperPointService.findSensorList(mainEquipmentId);
        }
        else
        {
            SiteTreeEnum siteTreeEnum = SiteTreeEnum.fromValue(mainType);
            param.setMainEquipmentType(mainType);
            if(TransfromerTreeEnum.TX_EQM.getValue().equals(subType))
            {
                List<String> list = new ArrayList<String>();
                list.add(subType);
                list.add(TransfromerTreeEnum.JJ_EQM.getValue());
                param.setEquipmentTypeList(list);
            }
            else
            {
                param.setEquipmentType(subType);
            }
            param.setMainEquipmentId(mainEquipmentId);
            param.setEquipmentId(subEquipmentId);
            sensorList = busiSensorService.findSensorList(siteTreeEnum, param);
        }
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", sensorList);
    }

    private static boolean isTransPd(BusiOnlineMonitorDataDto param) {
        return SiteTreeEnum.BYQ_EQM.getValue().equals(param.getEquipmentType())
            && TransfromerTreeEnum.JF_EQM.getValue().equals(param.getSubType());
    }

    private static boolean isGisPd(BusiOnlineMonitorDataDto param) {
        return SiteTreeEnum.GIS_HL_EQM.getValue().equals(param.getEquipmentType())
            && GisTreeEnum.JF_EQM.getValue().equals(param.getSubType());
    }
    private static boolean isElePd(BusiOnlineMonitorDataDto param) {
        return SiteTreeEnum.OTHER_EQM.getValue().equals(param.getEquipmentType())
            && OtherTreeEnum.DL_EQM.getValue().equals(param.getSubType());
    }

    private static boolean isCablePd(BusiOnlineMonitorDataDto param) {
        return SiteTreeEnum.DL_EQM.getValue().equals(param.getEquipmentType())
                && SiteConstants.CableTreeEnum.DLJF_EQM.getValue().equals(param.getSubType());
    }

    private static boolean isGeneratorPd(BusiOnlineMonitorDataDto param) {
        return SiteTreeEnum.GEN_EQM.getValue().equals(param.getEquipmentType())
                && SiteConstants.GeneratorTreeEnum.JF_EQM.getValue().equals(param.getSubType());
    }

    /**
	 * @Title: 导出在线监测数据
	 * @param response
	 * @param request
	 * @param params
	 * 参数：</br>
	 *       <p>equipmentType:主设备类型</p>
	 *       <p>subEquipmentType：监测类型</p>
	 *       <p>监测时间范围：day~dayEnd</p>
	 *       <p>外挂监测装置：externalId</p>
	 *       <p>传感器：sensorCode</p>
	 *       <p>treeId</p>
	 * @throws IOException
	 */
	@ApiOperation(value = "导出在线监测数据")
	@GetMapping("/exportBusiMonitorData")
	public void exportCacList(HttpServletResponse response , HttpServletRequest request,@RequestParam Map<String, Object> params) throws IOException {
		request.setCharacterEncoding("UTF-8");
		BusiOnlineMonitorDataDto monitorDataDto = new BusiOnlineMonitorDataDto();
		String equipmentType = StringUtils.EMPTY;
		String subEquipmentType = StringUtils.EMPTY;
		if (params.containsKey("equipmentType")) {
			equipmentType = params.get("equipmentType")+"";
		}
		if (params.containsKey("subEquipmentType")) {
			subEquipmentType = params.get("subEquipmentType")+"";
		}
		if (params.containsKey("treeId")) {
			monitorDataDto.setTreeId(params.get("treeId")+"");
		}
		if (params.containsKey("externalId")) {
			monitorDataDto.setExternalId(params.get("externalId")+"");
		}
		if (params.containsKey("sensorCode")) {
			monitorDataDto.setSensorCode(params.get("sensorCode")+"");
		}
		if (params.containsKey("day") && StringUtils.isNotEmpty(params.get("day")+"")) {
			monitorDataDto.setDay(LocalDate.parse(params.get("day")+"", DateTimeFormatter.ofPattern("yyyy-MM-dd")));
		}
		if (params.containsKey("dayEnd") && StringUtils.isNotEmpty(params.get("dayEnd")+"")) {
			monitorDataDto.setDayEnd(LocalDate.parse(params.get("dayEnd")+"", DateTimeFormatter.ofPattern("yyyy-MM-dd")));
		}
		monitorDataDto.setEquipmentType(equipmentType);
		monitorDataDto.setSubEquipmentType(subEquipmentType);
		monitorDataDto.setSubType(subEquipmentType);

		List<Object> exportMonitorData = Lists.newArrayList();
		String fileName = "监测数据";
        // 局放返回历史数据
        TransformerPdDto pd = new TransformerPdDto();
        if(isTransPd(monitorDataDto)) {
            pd.setCacMonitorTypeCode("021001");
            pd.setMainTreeId(monitorDataDto.getTreeId());
            pd.setSubTreeId(monitorDataDto.getExternalId());
            pd.setSensorId(monitorDataDto.getSensorCode());
            if(monitorDataDto.getDay()!= null)
            {
                pd.setTimeStamp(LocalDateTime.of(monitorDataDto.getDay(), LocalTime.parse("00:00:00")));
            }
            if(monitorDataDto.getDayEnd()!= null)
            {
                pd.setTimeStampEnd(LocalDateTime.of(monitorDataDto.getDayEnd(), LocalTime.parse("23:59:59")));
            }
            exportMonitorData = transformerPdService.exportTransformerPdPage(pd);
        }else if(isGisPd(monitorDataDto)) {
            pd.setCacMonitorTypeCode("024001");
            pd.setMainTreeId(monitorDataDto.getTreeId());
            pd.setSubTreeId(monitorDataDto.getExternalId());
            pd.setSensorId(monitorDataDto.getSensorCode());
            if(monitorDataDto.getDay()!= null)
            {
                pd.setTimeStamp(LocalDateTime.of(monitorDataDto.getDay(), LocalTime.parse("00:00:00")));
            }
            if(monitorDataDto.getDayEnd()!= null)
            {
                pd.setTimeStampEnd(LocalDateTime.of(monitorDataDto.getDayEnd(), LocalTime.parse("23:59:59")));
            }
            exportMonitorData = transformerPdService.exportGisPdPage(pd);
        } else if(isElePd(monitorDataDto)) {
//            log.debug("电缆局放无数据");
//            throw new OwlException("电缆局放无数据");
            pd.setCacMonitorTypeCode("027001");
            pd.setMainTreeId(monitorDataDto.getTreeId());
            pd.setSubTreeId(monitorDataDto.getExternalId());
            pd.setSensorId(monitorDataDto.getSensorCode());
            if(monitorDataDto.getDay()!= null)
            {
                pd.setTimeStamp(LocalDateTime.of(monitorDataDto.getDay(), LocalTime.parse("00:00:00")));
            }
            if(monitorDataDto.getDayEnd()!= null)
            {
                pd.setTimeStampEnd(LocalDateTime.of(monitorDataDto.getDayEnd(), LocalTime.parse("23:59:59")));
            }
            exportMonitorData = transformerPdService.exportOtherPdPage(pd);
        } else if(isCablePd(monitorDataDto)) {
            pd.setCacMonitorTypeCode("028001");
            pd.setMainTreeId(monitorDataDto.getTreeId());
            pd.setSubTreeId(monitorDataDto.getExternalId());
            pd.setSensorId(monitorDataDto.getSensorCode());
            if(monitorDataDto.getDay()!= null)
            {
                pd.setTimeStamp(LocalDateTime.of(monitorDataDto.getDay(), LocalTime.parse("00:00:00")));
            }
            if(monitorDataDto.getDayEnd()!= null)
            {
                pd.setTimeStampEnd(LocalDateTime.of(monitorDataDto.getDayEnd(), LocalTime.parse("23:59:59")));
            }
            exportMonitorData = transformerPdService.exportCablePdPage(pd);
        } else if (isGeneratorPd(monitorDataDto)) {
            pd.setMainTreeId(monitorDataDto.getTreeId());
            pd.setSubTreeId(monitorDataDto.getExternalId());
            pd.setSensorId(monitorDataDto.getSensorCode());
            if(monitorDataDto.getDay()!= null)
            {
                pd.setTimeStamp(LocalDateTime.of(monitorDataDto.getDay(), LocalTime.parse("00:00:00")));
            }
            if(monitorDataDto.getDayEnd()!= null)
            {
                pd.setTimeStampEnd(LocalDateTime.of(monitorDataDto.getDayEnd(), LocalTime.parse("23:59:59")));
            }
            exportMonitorData = busiGeneratorPdService.exportGeneratorPdPage(pd);
        }
        else {
    		List<BusiOnlineMonitorDataVo> list = busiOnlineMonitorDataService.exportBusiOnlineMonitorDataPage(monitorDataDto);
    		for (int i = 0; i < list.size(); i++) {
    			JSONObject montorJson = JSON.parseObject(JSON.toJSONString(list.get(i)));
    			if (equipmentType.equals(SiteTreeEnum.BYQ_EQM.getValue())) {
    				//油色谱
    				if (subEquipmentType.equals(TransfromerTreeEnum.YSP_EQM.getValue())) {
    					BusiMonitorOilDataVo oilDataVo = new BusiMonitorOilDataVo();
    					oilDataVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorOilDataVo>() {});
    					if(StringUtils.isNotEmpty(oilDataVo.getStabilityIndex())) {
    						JSONObject stabilityInfo = JSONObject.parseObject(oilDataVo.getStabilityIndex());
    						if (stabilityInfo.containsKey("zj")) {
    							oilDataVo.setStabilityIndex(stabilityInfo.getString("zj"));
    						}
    					}
    					exportMonitorData.add(oilDataVo);
    				}
    				//铁芯-夹件
    				if (subEquipmentType.equals(TransfromerTreeEnum.TX_EQM.getValue())) {
    					BusiMonitorIcgeDataVo icgeDataVo = new BusiMonitorIcgeDataVo();
    					icgeDataVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorIcgeDataVo>() {});
    					exportMonitorData.add(icgeDataVo);
    				}
    				//红外
    				if (subEquipmentType.equals(TransfromerTreeEnum.HW_EQM.getValue())) {
    					BusiMonitorInfraredDataVo infraredVo = new BusiMonitorInfraredDataVo();
    					infraredVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorInfraredDataVo>() {});
    					exportMonitorData.add(infraredVo);
    				}
    				//图片
    				if (subEquipmentType.equals(TransfromerTreeEnum.TP_EQM.getValue())) {
    					BusiMonitorImageDataVo infraredVo = new BusiMonitorImageDataVo();
    					infraredVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorImageDataVo>() {});
    					exportMonitorData.add(infraredVo);
    				}
    				//容性
    				if (subEquipmentType.equals(TransfromerTreeEnum.RX_EQM.getValue())) {
    					BusiMonitorCapacitiveDataVo infraredVo = new BusiMonitorCapacitiveDataVo();
    					infraredVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorCapacitiveDataVo>() {});
    					exportMonitorData.add(infraredVo);
    				}
    				//气象装置
    				if (subEquipmentType.equals(TransfromerTreeEnum.QX_EQM.getValue())) {
    					BusiMonitorMeteorologyDataVo infraredVo = new BusiMonitorMeteorologyDataVo();
    					infraredVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorMeteorologyDataVo>() {});
    					exportMonitorData.add(infraredVo);
    				}
    				//局放
    				if (subEquipmentType.equals(TransfromerTreeEnum.JF_EQM.getValue())) {
    					TransformerPdVo infraredVo = new TransformerPdVo();
    					infraredVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<TransformerPdVo>() {});
    					exportMonitorData.add(infraredVo);
    				}
                    //测温装置
                    if (subEquipmentType.equals(TransfromerTreeEnum.CW_EQM.getValue())) {
                        BusiMonitorCwzzDataVo infraredVo = new BusiMonitorCwzzDataVo();
                        infraredVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorCwzzDataVo>() {});
                        exportMonitorData.add(infraredVo);
                    }
                    //绕组测温
                    if (subEquipmentType.equals(TransfromerTreeEnum.RZCW_EQM.getValue())) {
                        BusiMonitorRzcwDataVo infraredVo = new BusiMonitorRzcwDataVo();
                        infraredVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorRzcwDataVo>() {});
                        exportMonitorData.add(infraredVo);
                    }
    			}
    			//GIS
    			if (equipmentType.equals(SiteTreeEnum.GIS_HL_EQM.getValue())) {
    				//SF6
    				if (subEquipmentType.equals(GisTreeEnum.SF6_EQM.getValue())) {
    					BusiMonitorSf6DataVo sf6DataVo = new BusiMonitorSf6DataVo();
    					sf6DataVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorSf6DataVo>() {});
    					exportMonitorData.add(sf6DataVo);
    				}
    				//红外
    				if (subEquipmentType.equals(GisTreeEnum.HW_EQM.getValue())) {
    					BusiMonitorInfraredDataVo infraredVo = new BusiMonitorInfraredDataVo();
    					infraredVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorInfraredDataVo>() {});
    					exportMonitorData.add(infraredVo);
    				}
    				//图片
    				if (subEquipmentType.equals(GisTreeEnum.TP_EQM.getValue())) {
    					BusiMonitorImageDataVo infraredVo = new BusiMonitorImageDataVo();
    					infraredVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorImageDataVo>() {});
    					exportMonitorData.add(infraredVo);
    				}
    				//气象装置
    				if (subEquipmentType.equals(GisTreeEnum.QX_EQM.getValue())) {
    					BusiMonitorMeteorologyDataVo infraredVo = new BusiMonitorMeteorologyDataVo();
    					infraredVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorMeteorologyDataVo>() {});
    					exportMonitorData.add(infraredVo);
    				}
    				//局放
    				if (subEquipmentType.equals(GisTreeEnum.JF_EQM.getValue())) {
    					TransformerPdVo infraredVo = new TransformerPdVo();
    					infraredVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<TransformerPdVo>() {});
    					exportMonitorData.add(infraredVo);
    				}
                    //SF6 Env
                    if (subEquipmentType.equals(GisTreeEnum.HJJC_EQM.getValue())) {
                        BusiMonitorSf6EnvDataVo infraredVo = new BusiMonitorSf6EnvDataVo();
                        infraredVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorSf6EnvDataVo>() {});
                        exportMonitorData.add(infraredVo);
                    }
    			}
    			//测温柜
    			if (equipmentType.equals(SiteTreeEnum.CW_HL_EQM.getValue())) {
    				BusiMonitorThermometryDataVo thermometryVo = new BusiMonitorThermometryDataVo();
    				thermometryVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorThermometryDataVo>() {});
    				exportMonitorData.add(thermometryVo);
    			}
    			//避雷器
    			if (equipmentType.equals(SiteTreeEnum.BLQ_HL_EQM.getValue())) {
    				//绝缘监测
    				if (subEquipmentType.equals(ArresterTreeEnum.JY_EQM.getValue())) {
    					BusiMonitorInsulationDataVo oilDataVo = new BusiMonitorInsulationDataVo();
    					oilDataVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorInsulationDataVo>() {});
    					exportMonitorData.add(oilDataVo);
    				}
    				//红外
    				if (subEquipmentType.equals(ArresterTreeEnum.HW_EQM.getValue())) {
    					BusiMonitorInfraredDataVo infraredVo = new BusiMonitorInfraredDataVo();
    					infraredVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorInfraredDataVo>() {});
    					exportMonitorData.add(infraredVo);
    				}
    				//容性
    				if (subEquipmentType.equals(ArresterTreeEnum.RX_EQM.getValue())) {
    					BusiMonitorCapacitiveDataVo infraredVo = new BusiMonitorCapacitiveDataVo();
    					infraredVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorCapacitiveDataVo>() {});
    					exportMonitorData.add(infraredVo);
    				}
    				//气象装置
    				if (subEquipmentType.equals(ArresterTreeEnum.QX_EQM.getValue())) {
    					BusiMonitorMeteorologyDataVo infraredVo = new BusiMonitorMeteorologyDataVo();
    					infraredVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorMeteorologyDataVo>() {});
    					exportMonitorData.add(infraredVo);
    				}
    				//图片
    				if (subEquipmentType.equals(ArresterTreeEnum.TP_EQM.getValue())) {
    					BusiMonitorImageDataVo infraredVo = new BusiMonitorImageDataVo();
    					infraredVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorImageDataVo>() {});
    					exportMonitorData.add(infraredVo);
    				}
    			}
    			// 电缆环流
                if (equipmentType.equals(SiteTreeEnum.OTHER_EQM.getValue())) {
                    BusiMonitorCableDataVo cableDataVo = new BusiMonitorCableDataVo();
                    cableDataVo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorCableDataVo>() {});
                    exportMonitorData.add(cableDataVo);
                }
                // 电缆
                if (equipmentType.equals(SiteTreeEnum.DL_EQM.getValue())) {
                    //测温装置
                    if (subEquipmentType.equals(SiteConstants.CableTreeEnum.CW_EQM.getValue())) {
                        BusiMonitorCwzzDataVo vo = new BusiMonitorCwzzDataVo();
                        vo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorCwzzDataVo>() {});
                        exportMonitorData.add(vo);
                    }
                    // 红外监控
                    if (subEquipmentType.equals(SiteConstants.CableTreeEnum.HW_EQM.getValue())) {
                        BusiMonitorInfraredDataVo vo = new BusiMonitorInfraredDataVo();
                        vo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorInfraredDataVo>() {});
                        exportMonitorData.add(vo);
                    }
                    // 电缆环流
                    if (subEquipmentType.equals(SiteConstants.CableTreeEnum.DLHL_EQM.getValue())) {
                        BusiMonitorCableDataVo vo = new BusiMonitorCableDataVo();
                        vo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorCableDataVo>() {});
                        exportMonitorData.add(vo);
                    }
                }
                // 发电机
                if (equipmentType.equals(SiteTreeEnum.GEN_EQM.getValue())) {
                    // 振动装置
                    if (subEquipmentType.equals(SiteConstants.GeneratorTreeEnum.ZD_EQM.getValue())) {
                        BusiMonitorGenAmplDataVo vo = new BusiMonitorGenAmplDataVo();
                        vo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorGenAmplDataVo>() {});
                        vo.setEquipmentType(SiteTreeEnum.GEN_EQM.getName());
                        vo.setSubEquipmentType(SiteConstants.GeneratorTreeEnum.ZD_EQM.getName());
                        vo.setSiteTreename(montorJson.getString("mainTreeName"));
                        exportMonitorData.add(vo);
                    }
                    // 压力
                    if (subEquipmentType.equals(SiteConstants.GeneratorTreeEnum.YL_EQM.getValue())) {
                        BusiMonitorGenYlDataVo vo = new BusiMonitorGenYlDataVo();
                        vo = JSON.parseObject(montorJson.toJSONString(), new TypeReference<BusiMonitorGenYlDataVo>() {});
                        vo.setEquipmentType(SiteTreeEnum.GEN_EQM.getName());
                        vo.setSubEquipmentType(SiteConstants.GeneratorTreeEnum.YL_EQM.getName());
                        vo.setSiteTreename(montorJson.getString("mainTreeName"));
                        exportMonitorData.add(vo);
                    }
                }
    		}
        }

		if (CollectionUtils.isEmpty(exportMonitorData)) {
			log.warn("==> " + fileName + ":无数据导出.");
		}
		OutputStream out = response.getOutputStream();
		response.reset();
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Content-Type","application/msexcel");
		String sheetName = fileName;
		fileName = new String(fileName.getBytes("gb2312"), "iso8859-1");
		response.setHeader("Content-disposition","attachment; filename="+fileName+".xls");
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
        ExcelExport<Object> ex = new ExcelExport<>();
		HSSFWorkbook wb = ex.exportExcel(sheetName, exportMonitorData);
		log.info("==> 正在写入文件:" + fileName + ",请稍后...");
		wb.write(out);
		wb.close();
		log.info("==> " + fileName + ":数据导出完成.");
		out.flush();
		out.close();
	}

}
