package com.huatek.busi.service.impl.transformer;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.common.util.DecimalUtil;
import com.huatek.busi.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.huatek.busi.common.BusiConstant.DealStatus;
import com.huatek.busi.common.BusiConstant.EqType;
import com.huatek.busi.common.BusiConstant.GzType;
import com.huatek.busi.common.SiteConstants.ExternalTypeEnum;
import com.huatek.busi.common.SiteConstants.SiteTreeEnum;
import com.huatek.busi.common.SiteConstants.Status;
import com.huatek.busi.common.SiteConstants.TransfromerTreeEnum;
import com.huatek.busi.common.util.TimeUtil;
import com.huatek.busi.common.util.Tools;
import com.huatek.busi.dao.BusiEqmMalfunctionDao;
import com.huatek.busi.dao.TransformerEpaDataDao;
import com.huatek.busi.dto.EquipmentInfoDto;
import com.huatek.busi.dto.curve.BaseCurveQueryDto;
import com.huatek.busi.dto.curve.PointItemDto;
import com.huatek.busi.entity.BusiEqmMalfunction;
import com.huatek.busi.entity.BusiEqmWarning;
import com.huatek.busi.entity.BusiEqmWarningInfo;
import com.huatek.busi.entity.BusiOnlineMonitorData;
import com.huatek.busi.entity.BusiSiteTree;
import com.huatek.busi.entity.BusiTransformerCorrect;
import com.huatek.busi.entity.BusiTransformerExperiment;
import com.huatek.busi.entity.TransformerEpaData;
import com.huatek.busi.entity.TransformerEpaDeviation;
import com.huatek.busi.entity.TransformerEpaDeviationCache;
import com.huatek.busi.mapper.TransformerEpaDataMapper;
import com.huatek.busi.service.BusiEqmWarningInfoService;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.service.IecBydworkParamService;
import com.huatek.busi.service.impl.cac.AbstractCacEtlService;
import com.huatek.busi.service.threshold.TransformerOilThresholdService;
import com.huatek.busi.service.transformer.BusiTransformerExperimentService;
import com.huatek.busi.service.transformer.TransformerEpaAbsIncreaseStatisticsService;
import com.huatek.busi.service.transformer.TransformerEpaDataCacheService;
import com.huatek.busi.service.transformer.TransformerEpaDataOriginService;
import com.huatek.busi.service.transformer.TransformerEpaDataService;
import com.huatek.busi.service.transformer.TransformerEpaDeviationCacheService;
import com.huatek.busi.service.transformer.TransformerEpaDeviationService;
import com.huatek.busi.service.transformer.TransformerEpaDeviationStatisticsService;
import com.huatek.busi.service.transformer.TransformerEpaIncreaseStatisticsService;
import com.huatek.busi.util.EqaUtil;
import com.huatek.busi.vo.TransformerEpaDataVo;
import com.huatek.busi.vo.TransformerOilThresholdVo;
import com.huatek.busi.vo.curve.PointItemVo;
import com.huatek.busi.vo.monitor.BusiOnlineMonitorDataVo;
import com.huatek.busi.vo.warning.BusiOiTmplVo;
import com.huatek.busi.vo.warning.BusiOilVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import com.huatek.torch.util.uuid.Uuid;

import lombok.extern.slf4j.Slf4j;

/**
 * 变压器/电抗器——油中溶解气体：021002 ServiceImpl
 *
 * @author anker_cheng
 * @date Jun 26, 2019 1:11:48 PM
 */
@Slf4j
@Component
@Service(interfaceClass = TransformerEpaDataService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerEpaDataServiceImpl extends AbstractCacEtlService<TransformerEpaData>
    implements TransformerEpaDataService {
    @Autowired
    private TransformerEpaDataMapper transformerEpaDataMapper;
    @Autowired
    private TransformerEpaDataDao transformerEpaDataDao;
    @Autowired
    private BusiEqmMalfunctionDao busiEqmMalfunctionDao;
    @Autowired
    private TransformerEpaDeviationStatisticsService transformerEpaDeviationStatisticsService;
    /** @Fields busiTransformerExperimentService : 例行试验中间表服务 */
    @Autowired
    private BusiTransformerExperimentService busiTransformerExperimentService;
    /** @Fields transformerEpaDeviationService : 偏差率数据服务 */
    @Autowired
    private TransformerEpaDeviationService transformerEpaDeviationService;
    /** @Fields transformerEpaDeviationCacheService : 偏差率实时数据服务 */
    @Autowired
    private TransformerEpaDeviationCacheService transformerEpaDeviationCacheService;
    /** @Fields transformerEpaDataOriginService :原始数据服务 */
    @Autowired
    private TransformerEpaDataOriginService transformerEpaDataOriginService;
    /** @Fields transformerEpaDataCacheService : 实时数据服务 */
    @Autowired
    private TransformerEpaDataCacheService transformerEpaDataCacheService;
    /** @Fields transformerOilThresholdService : 油色谱阈值服务 */
    @Autowired
    private TransformerOilThresholdService transformerOilThresholdService;
    /** @Fields busiEqmWarningInfoService :告警详情服务 */
    @Autowired
    private BusiEqmWarningInfoService busiEqmWarningInfoService;
    @Autowired
    private BusiSiteTreeService busiSiteTreeService;
    @Autowired
    private TransformerEpaIncreaseStatisticsService transformerEpaIncreaseStatisticsService;
    @Autowired
    private TransformerEpaAbsIncreaseStatisticsService transformerEpaAbsIncreaseStatisticsService;
    @Reference(check = false)
    private IecBydworkParamService iecBydworkParamService;

    private TransformerEpaDataVo findBySensorId(String sensorId) {
        return transformerEpaDataMapper.findBySensorId(sensorId);
    }

	/***
     *
     * 油色谱数据保存：
     *
     * 保存历史数据
     *
     * 1: 载气异常
     * 处理：
     * 		1.1 生成载气异常告警(上层处理)
     *      1.2 获取前一天 天七气一水的数据(油色谱定时任务中统计的数据) ---->>>>>作为新的数据
     *      1.3 使用新数据更新油色谱实时数据
     *
     * 2：载气正常
     * 处理：
     *      2.1 使用上传的数据判断气体是否告警 --- > 有告警 保存告警信息 (上层处理)---> 进行三比值比较 保存故障
     *      2.2 使用上传的数据更新油色谱实时数据
     *      2.3 获取例行试验一天七气一水的数据  BusiTransformerExperiment（）
     *      2.3.1 如果例行试验发生改变  ----> 和例行数据进行比较 -- >保存偏差率 ---》保存/更新偏差率实时数据  ---》修正上传数据并且保存
     *      2.3.2 如果例行试验发生未改变 --- >获取前一天  七气一水的数据(油色谱定时任务中统计的数据) ---》保存偏差率 ---》保存/更新偏差率实时数据---》保存上传数据
     *
     */
    @Override
	public void saveHistoryData(TransformerEpaData cacEntity, EquipmentInfoDto equipmentInfo) {
    	String pasPress = cacEntity.getGasPress();
    	boolean flag = EqaUtil.judgePasPressIsExist(pasPress);
    	String siteId = cacEntity.getSiteId();
    	String mainEquipmentId = cacEntity.getMainEquipmentId();

    	String oilDetailType = equipmentInfo.getOilDetailType();

//    	String sensorId = cacEntity.getSensorId();
    	log.info("--->>  油色谱载气状态[{}]",flag ? "载气正常" : "载气异常");
		transformerEpaDataOriginService.saveTransformerEpaDataOrigin(cacEntity);
		//设置rowkey，导入hbase使用
		cacEntity.setHbaseRowkey(Tools.strReverse(TimeUtil.getTimeStr(cacEntity.getTimeStamp())) + "_"
				+ cacEntity.getSensorId() + "_" + TimeUtil.getTimeStr(cacEntity.getTimeStamp()) + "_"
				+ cacEntity.getCacMonitorTypeCode() + "_" + cacEntity.getMainTreeId());
		if (oilDetailType.equals(SiteConstants.oilDetailType.OIL_DETAIL_TYPE_1.getValue()) && !flag) {
//    		TransformerEpaDataVo find = this.findBySensorId(sensorId);
//    		if(null != find){
//    			cacEntity.setC2h2(find.getC2h2());
//        		cacEntity.setC2h4(find.getC2h4());
//        		cacEntity.setC2h6(find.getC2h6());
//        		cacEntity.setCh4(find.getCh4());
//        		cacEntity.setCo(find.getCo());
//        		cacEntity.setCo2(find.getCo2());
//        		cacEntity.setH2(find.getH2());
//        		cacEntity.setH2o(find.getH2o());
//        		cacEntity.setO2(find.getO2());
//        		cacEntity.setN2(find.getN2());
//        		cacEntity.setTmp(find.getTmp());
//        		cacEntity.setDew(find.getDew());
//        		cacEntity.setMst(find.getMst());
//        		cacEntity.setFlatGas(find.getFlatGas());
//        		cacEntity.setCmbuGas(find.getCmbuGas());
//        		cacEntity.setTotalHydrocarbon(find.getTotalHydrocarbon());
//    		    cacEntity.setHbaseRowkey(Tools.strReverse(TimeUtil.getTimeStr(cacEntity.getTimeStamp())) + "_"
//    	                + cacEntity.getSensorId() + "_" + TimeUtil.getTimeStr(cacEntity.getTimeStamp()) + "_"
//    	                + cacEntity.getCacMonitorTypeCode() + "_" + cacEntity.getMainTreeId());
//    		}
			super.saveHistoryData(cacEntity, equipmentInfo);
		} else {
			BusiTransformerExperiment experimentData = this.getRoutineTest(siteId, mainEquipmentId);

			if (null != experimentData) {
				log.info("--->>  油色谱离线数据以录入......");
				this.doWithExperimentData(cacEntity, experimentData);
				super.saveHistoryData(cacEntity, equipmentInfo);
			} else {
				log.info("--->>  油色谱离线数据未录入......");
				transformerEpaDataDao.add(cacEntity);
			}
		}
	}

	/**
	* @Title: diffEqaData
	* @Description: 对比数据
	* @createDate: Jul 15, 2019 1:38:15 PM
	* @param
	* @return   void
	* @throws
	*/
	private void doWithExperimentData(TransformerEpaData entity, Object object ) {
		TransformerEpaData cacEntity = new TransformerEpaData();
	    BeanUtils.copyPropertiesWithIgnoreNull(entity, cacEntity);
		boolean mark = false ;
		BigDecimal c2h2_tmp = BigDecimal.ZERO ;
		BigDecimal c2h4_tmp = BigDecimal.ZERO ;
		BigDecimal c2h6_tmp = BigDecimal.ZERO ;
		BigDecimal ch4_tmp  = BigDecimal.ZERO ;
		BigDecimal co2_tmp = BigDecimal.ZERO ;
		BigDecimal co_tmp = BigDecimal.ZERO ;
		BigDecimal h2_tmp = BigDecimal.ZERO ;
		BigDecimal h2o_tmp = BigDecimal.ZERO ;
		BigDecimal tmp_tmp = BigDecimal.ZERO ;
		BigDecimal dew_tmp = BigDecimal.ZERO ;
		BigDecimal mst_tmp = BigDecimal.ZERO ;
		BigDecimal flatgas_tmp = BigDecimal.ZERO ;
		BigDecimal cmbugas_tmp = BigDecimal.ZERO ;
		BigDecimal zj_tmp = BigDecimal.ZERO ;
		if (object instanceof BusiTransformerExperiment) {
			mark = true ;
			BusiTransformerExperiment experimentData = (BusiTransformerExperiment) object;
			c2h2_tmp = EqaUtil.compareGasValue(cacEntity.getC2h2(), experimentData.getC2h2());
			c2h4_tmp = EqaUtil.compareGasValue(cacEntity.getC2h4(), experimentData.getC2h4());
			c2h6_tmp = EqaUtil.compareGasValue(cacEntity.getC2h6(), experimentData.getC2h6());
			ch4_tmp = EqaUtil.compareGasValue(cacEntity.getCh4(), experimentData.getCh4());
			co2_tmp = EqaUtil.compareGasValue(cacEntity.getCo2(), experimentData.getCo2());
			co_tmp = EqaUtil.compareGasValue(cacEntity.getCo(), experimentData.getCo());
			h2_tmp = EqaUtil.compareGasValue(cacEntity.getH2(), experimentData.getH2());
			h2o_tmp = EqaUtil.compareGasValue(cacEntity.getH2o(), experimentData.getH2o());
			tmp_tmp = EqaUtil.compareGasValue(cacEntity.getTemperature(), experimentData.getTmp());
			dew_tmp = EqaUtil.compareGasValue(cacEntity.getHumidity(), experimentData.getDew());
			mst_tmp = EqaUtil.compareGasValue(cacEntity.getMicroMoisture(), experimentData.getMst());
			flatgas_tmp = EqaUtil.compareGasValue(cacEntity.getFlatGas(), experimentData.getFlatGas());
			cmbugas_tmp = EqaUtil.compareGasValue(cacEntity.getCmbuGas(), experimentData.getCmbuGas());
			zj_tmp = EqaUtil.compareGasValue(cacEntity.getTotalHydrocarbon(), experimentData.getZj());
		} else if (object instanceof BusiTransformerCorrect) {
			BusiTransformerCorrect correct = (BusiTransformerCorrect) object;
			c2h2_tmp = EqaUtil.compareGasValue(cacEntity.getC2h2(), correct.getC2h2());
			c2h4_tmp = EqaUtil.compareGasValue(cacEntity.getC2h4(), correct.getC2h4());
			c2h6_tmp = EqaUtil.compareGasValue(cacEntity.getC2h6(), correct.getC2h6());
			ch4_tmp = EqaUtil.compareGasValue(cacEntity.getCh4(), correct.getCh4());
			co2_tmp = EqaUtil.compareGasValue(cacEntity.getCo2(), correct.getCo2());
			co_tmp = EqaUtil.compareGasValue(cacEntity.getCo(), correct.getCo());
			h2_tmp = EqaUtil.compareGasValue(cacEntity.getH2(), correct.getH2());
			h2o_tmp = EqaUtil.compareGasValue(cacEntity.getH2o(), correct.getH2o());
			tmp_tmp = EqaUtil.compareGasValue(cacEntity.getTemperature(), correct.getTmp());
			dew_tmp = EqaUtil.compareGasValue(cacEntity.getHumidity(), correct.getDew());
			mst_tmp = EqaUtil.compareGasValue(cacEntity.getMicroMoisture(), correct.getMst());
			flatgas_tmp = EqaUtil.compareGasValue(cacEntity.getFlatGas(), correct.getFlatGas());
			cmbugas_tmp = EqaUtil.compareGasValue(cacEntity.getCmbuGas(), correct.getCmbuGas());
			zj_tmp = EqaUtil.compareGasValue(cacEntity.getTotalHydrocarbon(), correct.getTotalHydrocarbon());
		}

		log.info("--->> 偏差率开始........");
		TransformerEpaDeviation deviation = new TransformerEpaDeviation();
		deviation.setC2h2Deviation( c2h2_tmp);
		deviation.setC2h4Deviation(c2h4_tmp );
		deviation.setC2h6Deviation(c2h6_tmp );
		deviation.setCh4Deviation(ch4_tmp );
		deviation.setCoDeviation(co_tmp );
		deviation.setCo2Deviation(co2_tmp );
		deviation.setH2Deviation(h2_tmp);
		deviation.setDTime(cacEntity.getTimeStamp());
		deviation.setH2oDeviation(h2o_tmp );
		deviation.setTmpDeviation(tmp_tmp);
		deviation.setDewDeviation(dew_tmp);
		deviation.setMstDeviation(mst_tmp);
		deviation.setFlatGasDeviation(flatgas_tmp);
		deviation.setCmbuGasDeviation(cmbugas_tmp);
		deviation.setTotalHydrocarbonDeviation(zj_tmp);
		log.info("--->> 偏差率结果：c2h2-->>{},c2h4-->>{},c2h6-->>{},ch4-->>{},co-->>{},co2-->>{},h2-->>{},温度-->>{},湿度-->>{},微水-->>{},瓦斯继电器中故障气-->>{},总可燃气体-->>{},总烃-->>{}",
									c2h2_tmp,c2h4_tmp,c2h6_tmp,ch4_tmp,co_tmp,co2_tmp,h2_tmp,tmp_tmp,dew_tmp,mst_tmp,flatgas_tmp,cmbugas_tmp,zj_tmp);
		transformerEpaDeviationService.saveTransformerEpaDeviation(deviation, cacEntity);
		log.info("--->> 偏差率结束........");


		TransformerEpaDeviationCache cache = new TransformerEpaDeviationCache();
		cache.setC2h2( c2h2_tmp);
		cache.setC2h4(c2h4_tmp );
		cache.setC2h6(c2h6_tmp );
		cache.setCh4(ch4_tmp );
		cache.setCo(co_tmp );
		cache.setCo2(co2_tmp );
		cache.setH2(h2_tmp);
		cache.setDTime(cacEntity.getTimeStamp());
		cache.setH2o(h2o_tmp );
		cache.setTmp(tmp_tmp);
		cache.setDew(dew_tmp);
		cache.setMst(mst_tmp);
		cache.setFlatGas(flatgas_tmp);
		cache.setCmbuGas(cmbugas_tmp);
		cache.setTotalHydrocarbon(zj_tmp);
		transformerEpaDeviationCacheService.saveTransformerEpaDeviationCache(cache, cacEntity);

		if(mark){
			if(StringUtils.isNotBlank(cacEntity.getC2h2())) {
				cacEntity.setC2h2(EqaUtil.validationGasValue(c2h2_tmp, new BigDecimal(cacEntity.getC2h2())).toString());
			}
			if(StringUtils.isNotBlank(cacEntity.getC2h4())) {
				cacEntity.setC2h4(EqaUtil.validationGasValue(c2h4_tmp, new BigDecimal(cacEntity.getC2h4())).toString());
			}
			if(StringUtils.isNotBlank(cacEntity.getC2h6())) {
				cacEntity.setC2h6(EqaUtil.validationGasValue(c2h6_tmp, new BigDecimal(cacEntity.getC2h6())).toString());
			}
			if(StringUtils.isNotBlank(cacEntity.getCh4())) {
				cacEntity.setCh4(EqaUtil.validationGasValue(ch4_tmp, new BigDecimal(cacEntity.getCh4())).toString());
			}
			if(StringUtils.isNotBlank(cacEntity.getCo2())) {
				cacEntity.setCo2(EqaUtil.validationGasValue(co2_tmp, new BigDecimal(cacEntity.getCo2())).toString());
			}
			if(StringUtils.isNotBlank(cacEntity.getCo())) {
				cacEntity.setCo(EqaUtil.validationGasValue(co_tmp, new BigDecimal(cacEntity.getCo())).toString());
			}
			if(StringUtils.isNotBlank(cacEntity.getH2o())) {
				cacEntity.setH2o(EqaUtil.validationGasValue(h2o_tmp, new BigDecimal(cacEntity.getH2o())).toString());
			}
			if(StringUtils.isNotBlank(cacEntity.getH2())) {
				cacEntity.setH2(EqaUtil.validationGasValue(h2_tmp, new BigDecimal(cacEntity.getH2())).toString());
			}
			if(StringUtils.isNotBlank(cacEntity.getTemperature())) {
				cacEntity.setTemperature(EqaUtil.validationGasValue(tmp_tmp, new BigDecimal(cacEntity.getTemperature())).toString());
			}
			if(StringUtils.isNotBlank(cacEntity.getHumidity())) {
				cacEntity.setHumidity(EqaUtil.validationGasValue(dew_tmp, new BigDecimal(cacEntity.getHumidity())).toString());
			}
			if(StringUtils.isNotBlank(cacEntity.getMicroMoisture())) {
				cacEntity.setMicroMoisture(EqaUtil.validationGasValue(mst_tmp, new BigDecimal(cacEntity.getMicroMoisture())).toString());
			}
			if(StringUtils.isNotBlank(cacEntity.getFlatGas())) {
				cacEntity.setFlatGas(EqaUtil.validationGasValue(flatgas_tmp, new BigDecimal(cacEntity.getFlatGas())).toString());
			}
			if(StringUtils.isNotBlank(cacEntity.getCmbuGas())) {
				cacEntity.setCmbuGas(EqaUtil.validationGasValue(cmbugas_tmp, new BigDecimal(cacEntity.getCmbuGas())).toString());
			}
			if(StringUtils.isNotBlank(cacEntity.getTotalHydrocarbon())) {
				cacEntity.setTotalHydrocarbon(EqaUtil.validationGasValue(zj_tmp, new BigDecimal(cacEntity.getTotalHydrocarbon())).toString());
			}
		}
		//transformerEpaDataDao.add(cacEntity);
		//this.saveData(cacEntity);
	}

	@Override
	public void saveBusiWarning(TransformerEpaData cacEntity, String cacMonitorTypeCode, String sensorId,
			EquipmentInfoDto equipmentInfo) {
		Map<String, String> resultMap = new HashMap<>();
		String oilType = equipmentInfo.getOilDetailType();
		if(oilType.equals(SiteConstants.oilDetailType.OIL_DETAIL_TYPE_1.getValue())) {
			resultMap = buildWarningMsg(cacEntity);
		} else if(oilType.equals(SiteConstants.oilDetailType.OIL_DETAIL_TYPE_2.getValue())){
			resultMap = buildWarningMsg2(cacEntity);
		}
		if(!resultMap.isEmpty()){
			String errorMsg = resultMap.get("errorMsg") ;
			String errorType = resultMap.get("errorType");
			// 保存告警
            BusiEqmWarning busiEqmWarning = newEw(EqType.EQ_TYPE1, GzType.GZ_TYPE1, errorMsg, cacMonitorTypeCode,"1");
			busiEqmWarning.setWarningTime(cacEntity.getTimeStamp());
			busiEqmWarning.setWarningFlag("1");
			busiEqmWarning = this.buildWarning(busiEqmWarning, equipmentInfo);
			//保存告警详情
			this.buildWarningInfo(busiEqmWarning, cacEntity, errorType, equipmentInfo);
		}

	}

	/**
	* @Title: buildWarningInfo
	* @Description: 告警详情
	* @createDate: Aug 8, 2019 1:30:31 PM
	* @param
	* @return   void
	* @throws
	*/
	private void buildWarningInfo(BusiEqmWarning busiEqmWarning, TransformerEpaData cacEntity, String errorType, EquipmentInfoDto equipmentInfo){
		String warningId = busiEqmWarning.getId();
		String sensorId = cacEntity.getSensorId();
		TransformerOilThresholdVo threshold = getOilThreshold(sensorId);

		String siteId = cacEntity.getSiteId();
    	String mainEquipmentId = cacEntity.getMainEquipmentId();
		BusiTransformerExperiment experimentData = this.getRoutineTest(siteId, mainEquipmentId);

		BigDecimal c2h2Deviation = BigDecimal.ZERO ;
		BigDecimal c2h4Deviation = BigDecimal.ZERO ;
		BigDecimal c2h6Deviation = BigDecimal.ZERO ;
		BigDecimal ch4Deviation  = BigDecimal.ZERO ;
		BigDecimal co2Deviation = BigDecimal.ZERO ;
		BigDecimal coDeviation = BigDecimal.ZERO ;
		BigDecimal h2Deviation = BigDecimal.ZERO ;
		BigDecimal h2oDeviation = BigDecimal.ZERO ;
		BigDecimal zjDeviation = BigDecimal.ZERO ;
		BigDecimal n2Deviation = BigDecimal.ZERO ;
		BigDecimal o2Deviation = BigDecimal.ZERO ;
		BigDecimal tmpDeviation = BigDecimal.ZERO ;
		BigDecimal dewDeviation = BigDecimal.ZERO ;
		BigDecimal mstDeviation = BigDecimal.ZERO ;
		BigDecimal flatGasDeviation = BigDecimal.ZERO ;
		BigDecimal cmbuGasDeviation = BigDecimal.ZERO ;

		if(null != experimentData ){
			c2h2Deviation = EqaUtil.compareGasValue(cacEntity.getC2h2(), experimentData.getC2h2());
			c2h4Deviation = EqaUtil.compareGasValue(cacEntity.getC2h4(), experimentData.getC2h4());
			c2h6Deviation = EqaUtil.compareGasValue(cacEntity.getC2h6(), experimentData.getC2h6());
			ch4Deviation = EqaUtil.compareGasValue(cacEntity.getCh4(), experimentData.getCh4());
			co2Deviation = EqaUtil.compareGasValue(cacEntity.getCo2(), experimentData.getCo2());
			coDeviation = EqaUtil.compareGasValue(cacEntity.getCo(), experimentData.getCo());
			h2Deviation = EqaUtil.compareGasValue(cacEntity.getH2(), experimentData.getH2());
			h2oDeviation = EqaUtil.compareGasValue(cacEntity.getH2o(), experimentData.getH2o());
			zjDeviation = EqaUtil.compareGasValue(cacEntity.getTotalHydrocarbon(), experimentData.getZj());
			n2Deviation = EqaUtil.compareGasValue(cacEntity.getN2(), experimentData.getN2());
			o2Deviation = EqaUtil.compareGasValue(cacEntity.getO2(), experimentData.getO2());
			tmpDeviation = EqaUtil.compareGasValue(cacEntity.getTemperature(), experimentData.getTmp());
			dewDeviation = EqaUtil.compareGasValue(cacEntity.getHumidity(), experimentData.getDew());
			mstDeviation = EqaUtil.compareGasValue(cacEntity.getMicroMoisture(), experimentData.getMst());
			flatGasDeviation = EqaUtil.compareGasValue(cacEntity.getFlatGas(), experimentData.getFlatGas());
			cmbuGasDeviation = EqaUtil.compareGasValue(cacEntity.getCmbuGas(), experimentData.getCmbuGas());
		}

		BusiOiTmplVo vo2 = new BusiOiTmplVo();
		BusiOiTmplVo vo3 = new BusiOiTmplVo();
		BusiOiTmplVo vo4 = new BusiOiTmplVo();
		BusiOiTmplVo vo5 = new BusiOiTmplVo();
		BusiOiTmplVo vo1 = new BusiOiTmplVo();
		BusiOiTmplVo vo6 = new BusiOiTmplVo();
		BusiOiTmplVo vo7 = new BusiOiTmplVo();
		BusiOiTmplVo vo8 = new BusiOiTmplVo();
		BusiOiTmplVo vo9 = new BusiOiTmplVo();
		BusiOiTmplVo vo10 = new BusiOiTmplVo();
		BusiOiTmplVo vo11 = new BusiOiTmplVo();
		BusiOiTmplVo vo12 = new BusiOiTmplVo();
		BusiOiTmplVo vo13 = new BusiOiTmplVo();
		BusiOiTmplVo vo14 = new BusiOiTmplVo();
		BusiOiTmplVo vo15 = new BusiOiTmplVo();
		BusiOiTmplVo vo16 = new BusiOiTmplVo();

		vo2.setGasName("CO");
		vo2.setGasValue(cacEntity.getCo());
		vo2.setIncreaseRate("-");
		vo2.setStabilityIndex("-");

		vo3.setGasName("H2");
		vo3.setGasValue(cacEntity.getH2());
		vo3.setIncreaseRate("-");
		vo3.setStabilityIndex("-");

		vo4.setGasName("CO2");
		vo4.setGasValue(cacEntity.getCo2());
		vo4.setIncreaseRate("-");
		vo4.setStabilityIndex("-");

		vo5.setGasName("CH4");
		vo5.setGasValue(cacEntity.getCh4());
		vo5.setIncreaseRate("-");
		vo5.setStabilityIndex("-");

		vo1.setGasName("C2H2");
		vo1.setGasValue(cacEntity.getC2h2());
		vo1.setIncreaseRate("-");
		vo1.setStabilityIndex("-");


		vo6.setGasName("C2H4");
		vo6.setGasValue(cacEntity.getC2h4());
		vo6.setIncreaseRate("-");
		vo6.setStabilityIndex("-");

		vo7.setGasName("C2H6");
		vo7.setGasValue(cacEntity.getC2h6());
		vo7.setIncreaseRate("-");
		vo7.setStabilityIndex("-");

		vo8.setGasName("N2");
		vo8.setGasValue(cacEntity.getN2());
		vo8.setIncreaseRate("-");
		vo8.setStabilityIndex("-");

		vo9.setGasName("O2");
		vo9.setGasValue(cacEntity.getO2());
		vo9.setIncreaseRate("-");
		vo9.setStabilityIndex("-");

		vo10.setGasName("H2O");
		vo10.setGasValue(cacEntity.getH2o());
		vo10.setIncreaseRate("-");
		vo10.setStabilityIndex("-");

		vo11.setGasName("总烃");
		vo11.setDeviationRate(zjDeviation.toString());
		vo11.setGasValue(cacEntity.getTotalHydrocarbon());
		vo11.setIncreaseRate("-");
		vo11.setStabilityIndex("-");

		vo12.setGasName("温度");
		vo12.setGasValue(cacEntity.getTemperature());
		vo12.setIncreaseRate("-");
		vo12.setStabilityIndex("-");

		vo13.setGasName("湿度");
		vo13.setGasValue(cacEntity.getHumidity());
		vo13.setIncreaseRate("-");
		vo13.setStabilityIndex("-");

		vo14.setGasName("微水");
		vo14.setGasValue(cacEntity.getMicroMoisture());
		vo14.setIncreaseRate("-");
		vo14.setStabilityIndex("-");

		vo15.setGasName("瓦斯继电器中故障气");
		vo15.setGasValue(cacEntity.getFlatGas());
		vo15.setIncreaseRate("-");
		vo15.setStabilityIndex("-");

		vo16.setGasName("总可燃气体");
		vo16.setGasValue(cacEntity.getCmbuGas());
		vo16.setIncreaseRate("-");
		vo16.setStabilityIndex("-");

		boolean flag = errorType.equals("2") ? true : false ;
		vo1.setDeviationRate(flag ? c2h2Deviation.toString() : "-");
		vo2.setDeviationRate(flag ?  coDeviation.toString() : "-" );
		vo3.setDeviationRate(flag ? h2Deviation.toString() : "-");
		vo4.setDeviationRate(flag ? co2Deviation.toString() : "-");
		vo5.setDeviationRate(flag ? ch4Deviation.toString() : "-");
		vo6.setDeviationRate(flag ? c2h4Deviation.toString() : "-");
		vo7.setDeviationRate(flag ? c2h6Deviation.toString() : "-");
		vo8.setDeviationRate(flag ? n2Deviation.toString() : "-");
		vo9.setDeviationRate(flag ? o2Deviation.toString() : "-");
		vo10.setDeviationRate(flag ? h2oDeviation.toString() : "-");
		vo11.setDeviationRate(flag ? zjDeviation.toString() : "-");
		vo12.setDeviationRate(flag ? tmpDeviation.toString() : "-");
		vo13.setDeviationRate(flag ? dewDeviation.toString() : "-");
		vo14.setDeviationRate(flag ? mstDeviation.toString() : "-");
		vo15.setDeviationRate(flag ? flatGasDeviation.toString() : "-");
		vo16.setDeviationRate(flag ? cmbuGasDeviation.toString() : "-");

		if(null != threshold){
			vo2.setAccuracyIndex(threshold.getCoAccuracy());
			vo2.setThresholdValue(threshold.getCoThreshold());
			vo2.setWarningValue(threshold.getCoWarning());

			vo3.setAccuracyIndex(threshold.getH2Accuracy());
			vo3.setThresholdValue(threshold.getH2Threshold());
			vo3.setWarningValue(threshold.getH2Warning());

			vo4.setAccuracyIndex(threshold.getCo2Accuracy());
			vo4.setThresholdValue(threshold.getCo2Threshold());
			vo4.setWarningValue(threshold.getCo2Warning());

			vo5.setAccuracyIndex(threshold.getCh4Accuracy());
			vo5.setThresholdValue(threshold.getCh4Threshold());
			vo5.setWarningValue(threshold.getCh4Warning());

			vo1.setAccuracyIndex(threshold.getC2h2Accuracy());
			vo1.setThresholdValue(threshold.getC2h2Threshold());
			vo1.setWarningValue(threshold.getC2h2Warning());

			vo6.setAccuracyIndex(threshold.getC2h4Accuracy());

			vo6.setThresholdValue(threshold.getC2h4Threshold());
			vo6.setWarningValue(threshold.getC2h4Warning());

			vo7.setAccuracyIndex(threshold.getC2h6Accuracy());
			vo7.setThresholdValue(threshold.getC2h6Threshold());
			vo7.setWarningValue(threshold.getC2h6Warning());

			vo8.setAccuracyIndex(threshold.getN2Accuracy());
			vo8.setThresholdValue(threshold.getN2Threshold());
			vo8.setWarningValue(threshold.getN2Warning());

			vo9.setAccuracyIndex(threshold.getO2Accuracy());
			vo9.setThresholdValue(threshold.getO2Threshold());
			vo9.setWarningValue(threshold.getO2Warning());

			vo10.setAccuracyIndex(threshold.getH2oAccuracy());
			vo10.setThresholdValue(threshold.getH2oThreshold());
			vo10.setWarningValue(threshold.getH2oWarning());

			vo11.setAccuracyIndex(threshold.getTotalHydrocarbonAccuracy());
			vo11.setThresholdValue(threshold.getTotalHydrocarbonThreshold());
			vo11.setWarningValue(threshold.getTotalHydrocarbonThreshold());

			vo12.setAccuracyIndex(threshold.getTmpAccuracy());
			vo12.setThresholdValue(threshold.getTmpThreshold());
			vo12.setWarningValue(threshold.getTmpWarning());

			vo13.setAccuracyIndex(threshold.getDewAccuracy());
			vo13.setThresholdValue(threshold.getDewThreshold());
			vo13.setWarningValue(threshold.getDewWarning());

			vo14.setAccuracyIndex(threshold.getMstAccuracy());
			vo14.setThresholdValue(threshold.getMstThreshold());
			vo14.setWarningValue(threshold.getMstWarning());

			vo15.setAccuracyIndex(threshold.getFlatGasAccuracy());
			vo15.setThresholdValue(threshold.getFlatGasThreshold());
			vo15.setWarningValue(threshold.getFlatGasWarning());

			vo16.setAccuracyIndex(threshold.getCmbuGasAccuracy());
			vo16.setThresholdValue(threshold.getCmbuGasThreshold());
			vo16.setWarningValue(threshold.getCmbuGasWarning());


			vo1.setAlarmValue(flag ? EqaUtil.diffValue2(threshold.getC2h2Threshold(),cacEntity.getC2h2()) : "-");
			vo2.setAlarmValue(flag ? EqaUtil.diffValue2(threshold.getCoThreshold(),cacEntity.getCo()): "-");
			vo3.setAlarmValue(flag ? EqaUtil.diffValue2(threshold.getH2Threshold(),cacEntity.getH2()): "-");
			vo4.setAlarmValue(flag ? EqaUtil.diffValue2(threshold.getCo2Threshold(),cacEntity.getCo2()): "-");
			vo5.setAlarmValue(flag ? EqaUtil.diffValue2(threshold.getCh4Threshold(),cacEntity.getCh4()): "-");
			vo6.setAlarmValue(flag ? EqaUtil.diffValue2(threshold.getC2h4Threshold(),cacEntity.getC2h4()): "-");
			vo7.setAlarmValue(flag ? EqaUtil.diffValue2(threshold.getC2h6Threshold(),cacEntity.getC2h6()): "-");
			vo8.setAlarmValue(flag ? EqaUtil.diffValue2(threshold.getN2Threshold(),cacEntity.getN2()): "-");
			vo9.setAlarmValue(flag ? EqaUtil.diffValue2(threshold.getO2Threshold(),cacEntity.getO2()): "-");
			vo10.setAlarmValue(flag ? EqaUtil.diffValue2(threshold.getH2oThreshold(),cacEntity.getH2o()): "-");
			vo11.setAlarmValue(flag ? EqaUtil.diffValue2(threshold.getTotalHydrocarbonThreshold(),cacEntity.getTotalHydrocarbon()): "-");
			vo12.setAlarmValue(flag ? EqaUtil.diffValue2(threshold.getTmpThreshold(),cacEntity.getTemperature()): "-");
			vo13.setAlarmValue(flag ? EqaUtil.diffValue2(threshold.getDewThreshold(),cacEntity.getHumidity()): "-");
			vo14.setAlarmValue(flag ? EqaUtil.diffValue2(threshold.getMstThreshold(),cacEntity.getMicroMoisture()): "-");
			vo15.setAlarmValue(flag ? EqaUtil.diffValue2(threshold.getFlatGasThreshold(),cacEntity.getFlatGas()): "-");
			vo16.setAlarmValue(flag ? EqaUtil.diffValue2(threshold.getCmbuGasThreshold(),cacEntity.getCmbuGas()): "-");
		}

		BusiOilVo busiOilVo = new BusiOilVo();
		busiOilVo.setCollectDate(cacEntity.getTimeStamp());
		busiOilVo.setGasPress(cacEntity.getGasPress());
//		if(errorType.equals("1")){
//			busiOilVo.setGasStatus("1");
//			busiOilVo.setThreeRatio("");
//		}else{
//			busiOilVo.setGasStatus("2");
//			busiOilVo.setThreeRatio(calcThreeRatio(cacEntity, busiEqmWarning));
//		}

		//把载气和三比值也放到list里面
		BusiOiTmplVo vo17 = new BusiOiTmplVo();
		vo17.setGasName("载气");
		vo17.setDeviationRate("");
		vo17.setGasValue(cacEntity.getGasPress());
		vo17.setIncreaseRate("-");
		vo17.setStabilityIndex("-");
		vo17.setAlarmValue(cacEntity.getGasPress());
		if(StringUtils.isEmpty(vo17.getAlarmValue())) {
			vo17.setAlarmValue("-");
		}
		BusiOiTmplVo vo18 = new BusiOiTmplVo();
		vo18.setGasName("三比值");
		vo18.setDeviationRate("");
		vo18.setGasValue(busiOilVo.getThreeRatio());
		vo18.setIncreaseRate("-");
		vo18.setStabilityIndex("-");
		vo18.setAlarmValue(busiOilVo.getThreeRatio());
		if(StringUtils.isEmpty(vo18.getAlarmValue())) {
			vo18.setAlarmValue("-");
        }
		String oilType = equipmentInfo.getOilType();
		String oilDetailType = equipmentInfo.getOilDetailType();
		if(oilDetailType.equals(SiteConstants.oilDetailType.OIL_DETAIL_TYPE_1.getValue())){
			if(oilType.equals(SiteConstants.oilType.OILTYPE1.getValue())){
				if (errorType.equals("1")) {
					busiOilVo.setGasStatus("1");
					busiOilVo.setThreeRatio("");
				} else {
					busiOilVo.setGasStatus("2");
					busiOilVo.setThreeRatio(calcThreeRatio(cacEntity, busiEqmWarning));
				}
				busiOilVo.setList(Arrays.asList(vo1, vo2, vo3, vo4, vo5, vo6, vo7, vo8, vo9, vo10, vo11, vo17, vo18));
			}else if(oilType.equals(SiteConstants.oilType.OILTYPE2.getValue())){
				busiOilVo.setThreeRatio("");
				if (errorType.equals("1")) {
					busiOilVo.setGasStatus("1");
				} else {
					busiOilVo.setGasStatus("2");
				}
				busiOilVo.setList(Arrays.asList(vo12, vo13, vo14, vo16, vo17));
			}
		}else if(oilDetailType.equals(SiteConstants.oilDetailType.OIL_DETAIL_TYPE_2.getValue())){
			busiOilVo.setGasStatus("");
			if(oilType.equals(SiteConstants.oilType.OILTYPE1.getValue())){
				busiOilVo.setThreeRatio(calcThreeRatio(cacEntity, busiEqmWarning));
				busiOilVo.setList(Arrays.asList(vo1, vo2, vo3, vo4, vo5, vo6, vo7, vo8, vo9, vo11, vo14, vo16, vo18));
			}else if(oilType.equals(SiteConstants.oilType.OILTYPE2.getValue())){
				busiOilVo.setThreeRatio("");
				busiOilVo.setList(Arrays.asList(vo12, vo13, vo14, vo16));
			}
		}
		BusiEqmWarningInfo entity = new BusiEqmWarningInfo();
		entity.setWarningId(warningId);
		entity.setWarningInfo(JSON.toJSONString(busiOilVo));
		busiEqmWarningInfoService.save(entity );
	}

    /**
    * @Title: buildWarningMsg
    * @Description: 告警信息
    * @createDate: Aug 8, 2019 4:35:23 PM
    * @param
    * @return   Map<String,String>
    */
    // 色谱柱
	private Map<String,String>  buildWarningMsg(TransformerEpaData cacEntity) {
        // 判断是否有载气信号
    	Map<String,String> map = new HashMap<>();
        //BigDecimal gasPress = DecimalUtil.getVal(cacEntity.getGasPress());
        boolean flag = EqaUtil.judgePasPressIsExist(cacEntity.getGasPress());
        if (!flag) {
        	map.put("errorType", "1");
        	map.put("errorMsg", "油色谱载气压力异常");
        }else{
        	String warningMsg = this.diffGasUpperlimit(cacEntity);
        	if(StringUtils.isNotBlank(warningMsg.trim())){
        		map.put("errorType", "2");
            	map.put("errorMsg", warningMsg);
        	}
        }
        return map;
    }

	// 光声光谱
	private Map<String,String>  buildWarningMsg2(TransformerEpaData cacEntity) {
		Map<String,String> map = new HashMap<>();
		String warningMsg = this.diffGasUpperlimit(cacEntity);
		if(StringUtils.isNotBlank(warningMsg.trim())){
			map.put("errorType", "2");
			map.put("errorMsg", warningMsg);
		}
		return map;
	}

	private String calcThreeRatio(TransformerEpaData cacEntity,  BusiEqmWarning busiEqmWarning) {
		Map<String, String> resultMap = EqaUtil.threeRatioCalculation(cacEntity);
		BusiEqmMalfunction busiEqmMalfunction =  new BusiEqmMalfunction();
        busiEqmMalfunction.setId(Uuid.getUUID());
        busiEqmMalfunction.setWarningId(busiEqmWarning.getId());
        busiEqmMalfunction.setSensorId( cacEntity.getSensorId());
        busiEqmMalfunction.setSiteId( cacEntity.getSiteId());
        busiEqmMalfunction.setEqType(EqType.EQ_TYPE1);
        busiEqmMalfunction.setGzType(GzType.GZ_TYPE1);
        busiEqmMalfunction.setWarningTypeCode("021002");
        busiEqmMalfunction.setWarningMsg(resultMap.get("codemsg"));
        busiEqmMalfunction.setWarningTime(cacEntity.getTimeStamp());
        busiEqmMalfunction.setCreateTime(LocalDateTime.now());
        busiEqmMalfunction.setDelFlag(Status.NORMAL.getValue());
        busiEqmMalfunction.setDealStatus(DealStatus.TO_DEAL);
        busiEqmMalfunction.setMalfunctionSource("2");
        busiEqmMalfunction.setSiteId(cacEntity.getSiteId());
        busiEqmMalfunction.setMainEquipmentId(cacEntity.getMainEquipmentId());
        busiEqmMalfunction.setSubEquipmentId(cacEntity.getSubEquipmentId());

        busiEqmMalfunction.setSubEquipmentInstallLocation(busiEqmWarning.getSubEquipmentInstallLocation());
        busiEqmMalfunction.setSubEquipmentBrand(busiEqmWarning.getSubEquipmentBrand());
        busiEqmMalfunction.setSubEquipmentManufactor(busiEqmWarning.getSubEquipmentManufactor());

        busiEqmMalfunction.setSensorBrand(busiEqmWarning.getSensorBrand());
        busiEqmMalfunction.setSensorManufactor(busiEqmWarning.getSensorManufactor());
        busiEqmMalfunction.setSensorInstallLocation(busiEqmWarning.getSensorInstallLocation());
        busiEqmMalfunction.setSubEquipmentName(busiEqmWarning.getSubEquipmentName());
        busiEqmMalfunction.setSensorName(busiEqmWarning.getSensorName());
        busiEqmMalfunction.setSubTreeId(busiEqmWarning.getSubTreeId());
        busiEqmMalfunction.setMainTreeId(busiEqmWarning.getMainTreeId());
        busiEqmMalfunction.setThreeRatio(resultMap.get("code"));
        busiEqmMalfunctionDao.add(busiEqmMalfunction);

        return resultMap.get("code");
	}
    /***
     *
    * @Title: diffGasUpperlimit
    * @Description: 对比气体阈值
    * @createDate: Jul 12, 2019 5:14:17 PM
    * @param
    * @return   void
     */
	private  String diffGasUpperlimit(TransformerEpaData cacEntity){
    	StringBuffer sb  =new StringBuffer();
    	boolean flag = false ;
    	//  1： 获取阈值
    	TransformerOilThresholdVo threshold = getOilThreshold(cacEntity.getSensorId());
    	if(null != threshold){
    		//2：比较
        	boolean c2h2Flag =  EqaUtil.diffValue(threshold.getC2h2Threshold(),cacEntity.getC2h2());
        	if(c2h2Flag){
    			sb.append("[C2H2]");
    			flag = true ;
    		}
        	boolean c2h4Flag =  EqaUtil.diffValue(threshold.getC2h4Threshold(),cacEntity.getC2h4());
        	if(c2h4Flag){
    			sb.append("[C2H4]");
    			flag = true ;
    		}
        	boolean c2h6Flag =  EqaUtil.diffValue(threshold.getC2h6Threshold(),cacEntity.getC2h6());
        	if(c2h6Flag){
    			sb.append("[C2H6]");
    			flag = true ;
    		}
        	boolean ch4Flag =  EqaUtil.diffValue(threshold.getCh4Threshold(),cacEntity.getCh4());
        	if(ch4Flag){
    			sb.append("[CH4]");
    			flag = true ;
    		}
        	boolean h2Flag =  EqaUtil.diffValue(threshold.getH2Threshold(),cacEntity.getH2());
        	if(h2Flag){
    			sb.append("[H2]");
    			flag = true ;
    		}
        	boolean coFlag =  EqaUtil.diffValue(threshold.getCoThreshold(),cacEntity.getCo());
        	if(coFlag){
    			sb.append("[CO]");
    			flag = true ;
    		}
        	boolean co2Flag =  EqaUtil.diffValue(threshold.getCo2Threshold(),cacEntity.getCo2());
        	if(co2Flag){
    			sb.append("[CO2]");
    			flag = true ;
    		}
        	boolean h2oFlag =  EqaUtil.diffValue(threshold.getH2oThreshold(),cacEntity.getH2o());
        	if(h2oFlag){
    			sb.append("[H2O]");
    			flag = true ;
    		}
        	boolean n2Flag =  EqaUtil.diffValue(threshold.getN2Threshold(),cacEntity.getN2());
        	if(n2Flag){
    			sb.append("[N2]");
    			flag = true ;
    		}
        	boolean o2Flag =  EqaUtil.diffValue(threshold.getO2Threshold(),cacEntity.getO2());
        	if(o2Flag){
    			sb.append("[O2]");
    			flag = true ;
    		}
			boolean tmpFlag =  EqaUtil.diffValue(threshold.getTmpThreshold(),cacEntity.getTemperature());
			if(tmpFlag){
				sb.append("[温度]");
				flag = true ;
			}
			boolean dewFlag =  EqaUtil.diffValue(threshold.getDewThreshold(),cacEntity.getHumidity());
			if(dewFlag){
				sb.append("[湿度]");
				flag = true ;
			}
			boolean mstFlag =  EqaUtil.diffValue(threshold.getMstThreshold(),cacEntity.getMicroMoisture());
			if(mstFlag){
				sb.append("[微水]");
				flag = true ;
			}
			boolean flatGasFlag =  EqaUtil.diffValue(threshold.getFlatGasThreshold(),cacEntity.getFlatGas());
			if(flatGasFlag){
				sb.append("[瓦斯继电器中故障气]");
				flag = true ;
			}
			boolean cmbuGasFlag =  EqaUtil.diffValue(threshold.getCmbuGasThreshold(),cacEntity.getCmbuGas());
			if(cmbuGasFlag){
				sb.append("[总可燃气体]");
				flag = true ;
			}
        	boolean zt =  EqaUtil.diffValue(threshold.getTotalHydrocarbonThreshold(),cacEntity.getTotalHydrocarbon());
        	if(zt){
    			sb.append("[总烃]");
    			flag = true ;
    		}
        	if(flag){
        		sb.append("超标");
        	}
    	}

    	return sb.toString();
    }

	/**
	* @Title: getOilThreshold
	* @Description: 获取油色谱阈值
	* @createDate: Aug 8, 2019 1:35:50 PM
	* @param    sensorId 传感器ID
	* @return   TransformerOilThresholdVo
	*/
	private TransformerOilThresholdVo getOilThreshold(String sensorId) {
		TransformerOilThresholdVo threshold = this.transformerOilThresholdService.getBySensorCode(sensorId);
		return threshold;
	}

    @Override
    public void updateCurrentData(TransformerEpaData entity, EquipmentInfoDto equipmentInfo) {
		transformerEpaDataCacheService.saveOrUpdateTransformerEpaDataCache(entity, equipmentInfo);
    }

	/**
	* @Title: getRoutineTest
	* @Description: 获取油色谱例行试验的值(busi_transformer_experiment  --> 例行试验每次更新表数据 )
	* @createDate: Jul 12, 2019 5:44:23 PM
	* @param
	* @return   Object
	* @throws
	*/
    private  BusiTransformerExperiment getRoutineTest(String siteId, String mainEquipmentId) {
		return busiTransformerExperimentService.findByEquipmentId(siteId, mainEquipmentId);
	}

    @Override
    public Map<String, Object> findCACPageData(Map<String, Object> params) {
        if (params.get("page") != null && params.get("limit") != null) {
            PageHelper.startPage(page(params), limit(params));
        }
        Page<Map<String, Object>> page = transformerEpaDataMapper.findCACPageData(params);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, page.getTotal(), "", page.getResult());
    }

	@Override
	public Map<String, Object> exportCacMonitorData(Map<String, Object> mapParams) {
		Map<String, Object> returnMap = new HashMap<>();
		String oilType = mapParams.get("oilType").toString();
		String oilDetailType = mapParams.get("oilDetailType").toString();
		if (mapParams.containsKey("mainTreeId") && StringUtils.isNotEmpty(mapParams.get("mainTreeId")+"")) {
			BusiSiteTree busiSiteTree = busiSiteTreeService.findByPK(mapParams.get("mainTreeId")+"");
			if(oilType.equals(SiteConstants.oilType.OILTYPE1.getValue()) && oilDetailType.equals(SiteConstants.oilDetailType.OIL_DETAIL_TYPE_1.getValue())) {
				List<TransformerEpaDataVo> transformerEpaDataList = transformerEpaDataMapper.exportCacMonitorData(mapParams);
				if (transformerEpaDataList.isEmpty()) {
					transformerEpaDataList = new ArrayList<>();
					transformerEpaDataList.add(new TransformerEpaDataVo());
				}
				returnMap.put("data", transformerEpaDataList);
			}
			if(oilType.equals(SiteConstants.oilType.OILTYPE1.getValue()) && oilDetailType.equals(SiteConstants.oilDetailType.OIL_DETAIL_TYPE_2.getValue())) {
				List<TransformerEpaDataType2Vo> transformerEpaDataList = transformerEpaDataMapper.exportCacType2MonitorData(mapParams);
				if (transformerEpaDataList.isEmpty()) {
					transformerEpaDataList = new ArrayList<>();
					transformerEpaDataList.add(new TransformerEpaDataType2Vo());
				}
				returnMap.put("data", transformerEpaDataList);
			}
			if(oilType.equals(SiteConstants.oilType.OILTYPE2.getValue()) && oilDetailType.equals(SiteConstants.oilDetailType.OIL_DETAIL_TYPE_1.getValue())) {
				List<TransformerEpaDataType3Vo> transformerEpaDataList = transformerEpaDataMapper.exportCacType3MonitorData(mapParams);
				if (transformerEpaDataList.isEmpty()) {
					transformerEpaDataList = new ArrayList<>();
					transformerEpaDataList.add(new TransformerEpaDataType3Vo());
				}
				returnMap.put("data", transformerEpaDataList);
			}
			if(oilType.equals(SiteConstants.oilType.OILTYPE2.getValue()) && oilDetailType.equals(SiteConstants.oilDetailType.OIL_DETAIL_TYPE_2.getValue())) {
				List<TransformerEpaDataType4Vo> transformerEpaDataList = transformerEpaDataMapper.exportCacType4MonitorData(mapParams);
				if (transformerEpaDataList.isEmpty()) {
					transformerEpaDataList = new ArrayList<>();
					transformerEpaDataList.add(new TransformerEpaDataType4Vo());
				}
				returnMap.put("data", transformerEpaDataList);
			}
			String replaceRegex = "[\\\\n`~!@#$%^&*()+=|{}':;',\\\\\\\\[\\\\\\\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。， 、？]";
            String treeName = busiSiteTree.getName().replaceAll(replaceRegex, "-");
			returnMap.put("fileName", String.format("%s%s", treeName,"油色谱监测数据"));
			returnMap.put("title", String.format("%s%s", treeName,"油色谱监测数据"));
		} else {
			throw new RuntimeException("主设备编码不正确");
		}
		return returnMap;
	}

    @Override
    public List<BaseCurveQueryDto> findSensorCodeList(LocalDate day) {
        return transformerEpaDataMapper.findSensorCodeList(day.atStartOfDay(), day.plusDays(1).atStartOfDay());
    }

    @Override
    public List<PointItemVo> findDaylyPointList(BaseCurveQueryDto sensorCode, LocalDate day, String subType) {
        return transformerEpaDataMapper.findDaylyPointList(sensorCode, subType, day.atStartOfDay(),
            day.plusDays(1).atStartOfDay());
    }

    @Override
    public PointItemDto getDaylyPointStatistics(BaseCurveQueryDto param) {
        return transformerEpaDataMapper.getDaylyPointStatistics(param);
    }

    // *****************************在线监测监测数据部分***********************\\

    @Override
    protected List<Map<String, Object>> queryMonitorDataList(LocalDateTime startTime, LocalDateTime endTime) {
        return transformerEpaDataMapper.queryMonitorDataList(startTime, endTime);
    }

    @Override
    protected void saveBusiOnlineMonitorData(BusiOnlineMonitorData busiOnlineMonitorData, Map<String, Object> item) {
        busiOnlineMonitorData.setSubEquipmentType(TransfromerTreeEnum.YSP_EQM.getValue());
        busiOnlineMonitorData.setExtType(ExternalTypeEnum.BYQ.getValue());
        busiOnlineMonitorData.setEquipmentType(SiteTreeEnum.BYQ_EQM.getValue());
        busiOnlineMonitorData.setRunState("0");
        // 这里将保存的任务交给父类方法完成，子类只需要修改指定几个字段就可以了
        super.saveBusiOnlineMonitorData(busiOnlineMonitorData, item);
    }

    @Override
    protected Map<String, Object> thresholdDataMap(BusiOnlineMonitorDataVo vo) {
    	 Map<String, Object> map = transformerOilThresholdService.getThresholdMap(vo.getSensorCode());
    	 //BigDecimal bigDecimal = transformerEpaDeviationStatisticsService.getWorstEpaDeviation(vo.getSensorCode());
    	 //map.put("totalHydrocarbonAccuracyThreshold", transformerEpaDeviationStatisticsService.getWorstEpaDeviation(vo.getSensorCode()));
        return map;
    }

    @Override
    protected Map<String, Object> otherDataMap(BusiOnlineMonitorDataVo vo) {
        Map<String, Object> map1 =
            transformerEpaIncreaseStatisticsService.getGrowthRateMap(vo.getSensorCode(), vo.getDay());
        Map<String, Object> map2 =
            transformerEpaAbsIncreaseStatisticsService.getGrowthRateMap(vo.getSensorCode(), vo.getDay());
        if (map1 == null) {
            map1 = Maps.newHashMap();
        }
        if (map2 != null) {
            map1.putAll(map2);
        }
        map1.put("accuracyIndex", transformerEpaDeviationStatisticsService.getWorstEpaDeviation(vo.getSensorCode()));
        return map1;
    }
}
