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

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.huatek.busi.common.BusiConstant.EqType;
import com.huatek.busi.common.BusiConstant.GzType;
import com.huatek.busi.common.SiteConstants.SiteTreeEnum;
import com.huatek.busi.common.util.DecimalUtil;
import com.huatek.busi.common.util.Tools;
import com.huatek.busi.dao.GisSf6EnvMonitoringStatisticsDao;
import com.huatek.busi.dao.GisSf6IncreaseRateCacheDao;
import com.huatek.busi.dto.EquipmentInfoDto;
import com.huatek.busi.entity.BusiEqmWarning;
import com.huatek.busi.entity.BusiGisSensor;
import com.huatek.busi.entity.GisSf6EnvMonitoringStatistics;
import com.huatek.busi.entity.GisSf6IncreaseRateCache;
import com.huatek.busi.mapper.GisSf6EnvMonitoringStatisticsMapper;
import com.huatek.busi.mapper.GisSf6IncreaseRateCacheMapper;
import com.huatek.busi.mapper.GisSf6PressureMapper;
import com.huatek.busi.mapper.Sf6EnvMapper;
import com.huatek.busi.service.BusiEqmWarningService;
import com.huatek.busi.service.BusiSubTreeService;
import com.huatek.busi.service.gis.BusiGisSensorService;
import com.huatek.busi.service.gis.GisSF6ThresholdService;
import com.huatek.busi.service.gis.GisSf6IncreaseRateCacheService;
import com.huatek.busi.service.gis.GisSf6MonitoringStatisticsService;
import com.huatek.busi.util.CalcUtil;
import com.huatek.busi.vo.GisSf6PressureStatisticsVo;
import com.huatek.busi.vo.GisSf6ThresholdVo;
import com.huatek.busi.vo.Sf6EnvStatisticsVo;
import com.huatek.busi.vo.warning.BusiGisSf6ItemVo;
import com.huatek.busi.vo.warning.BusiGisSf6ItemVo.BusiGisSf6ItemVoBuilder;
import com.huatek.busi.vo.warning.BusiGisSf6Vo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
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 java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

/**
 * sf6Env 统计
 *
 * @author anker_cheng
 * @date Oct 23, 2019 8:55:28 PM
 */
@Slf4j
@Component
@Service(interfaceClass = GisSf6MonitoringStatisticsService.class)
@Transactional(rollbackFor = Exception.class)
public class GisSf6MonitoringStatisticsServiceImpl implements GisSf6MonitoringStatisticsService {

	@Autowired
	private GisSf6EnvMonitoringStatisticsMapper gisSf6EnvMonitoringStatisticsMapper;
	@Autowired
	private GisSf6EnvMonitoringStatisticsDao gisSf6EnvMonitoringStatisticsDao;
	@Autowired
	private Sf6EnvMapper sf6EnvMapper;
	@Autowired
	private GisSF6ThresholdService gisSF6ThresholdService;
	@Autowired
	private BusiSubTreeService busiSubTreeService;
	@Autowired
	private BusiEqmWarningService busiEqmWarningService;
	@Autowired
	private BusiGisSensorService busiGisSensorService;

	@Override
	public synchronized void saveGisSf6MonitoringStatistics(Sf6EnvStatisticsVo item, LocalDate day) {
		// 删除当天的
		gisSf6EnvMonitoringStatisticsMapper.deleteItem(item.getSensorCode(), day);
		// 保存新记录
		GisSf6EnvMonitoringStatistics entity = new GisSf6EnvMonitoringStatistics();
		entity.setCacMonitorTypeCode(item.getCacMonitorTypeCode());
		entity.setSiteId(item.getSiteId());
		entity.setSensorId(item.getSensorCode());
		entity.setMainTreeId(item.getMainTreeId());
		entity.setSubTreeId(item.getSubTreeId());
		entity.setDTime(day);
		entity.setCreateTime(LocalDateTime.now());
		entity.setConc(item.getConc());
		entity.setDew(item.getDew());
		entity.setOContent(item.getOcontent());
		entity.setTmp(item.getTmp());
		String rate1="0";
		String rate2="0";
		String rate3="0";
		String rate4="0";
		String rate11="0";
		String rate21="0";
		String rate31="0";
		String rate41="0";
		GisSf6EnvMonitoringStatistics gisSf6EnvMonitoringStatistics1 = gisSf6EnvMonitoringStatisticsMapper.getItem(item.getSensorCode(),
				day.minusDays(1));

		GisSf6EnvMonitoringStatistics gisSf6EnvMonitoringStatistics2 = gisSf6EnvMonitoringStatisticsMapper.getItem(item.getSensorCode(),
				day.minusDays(2));
		GisSf6EnvMonitoringStatistics gisSf6EnvMonitoringStatistics30 = gisSf6EnvMonitoringStatisticsMapper.getItem(item.getSensorCode(),
				day.minusDays(30));
		if(ObjectUtils.isNotEmpty(gisSf6EnvMonitoringStatistics1) && ObjectUtils.isNotEmpty(gisSf6EnvMonitoringStatistics2) ){
			if(StringUtils.isNotEmpty(gisSf6EnvMonitoringStatistics1.getConc()) && StringUtils.isNotEmpty(gisSf6EnvMonitoringStatistics2.getConc())){
				rate1=CalcUtil.calcRateEight(new BigDecimal(gisSf6EnvMonitoringStatistics1.getConc()).
						subtract(new BigDecimal(gisSf6EnvMonitoringStatistics2.getConc())),new BigDecimal(gisSf6EnvMonitoringStatistics2.getConc())).abs().toString();

			}
			if(StringUtils.isNotEmpty(gisSf6EnvMonitoringStatistics1.getDew()) && StringUtils.isNotEmpty(gisSf6EnvMonitoringStatistics2.getDew())){
				rate2=CalcUtil.calcRateEight(new BigDecimal(gisSf6EnvMonitoringStatistics1.getDew()).
						subtract(new BigDecimal(gisSf6EnvMonitoringStatistics2.getDew())),new BigDecimal(gisSf6EnvMonitoringStatistics2.getDew())).abs().toString();

			}
			if(StringUtils.isNotEmpty(gisSf6EnvMonitoringStatistics1.getTmp()) && StringUtils.isNotEmpty(gisSf6EnvMonitoringStatistics2.getTmp())){
				rate3=CalcUtil.calcRateEight(new BigDecimal(gisSf6EnvMonitoringStatistics1.getTmp()).
						subtract(new BigDecimal(gisSf6EnvMonitoringStatistics2.getTmp())),new BigDecimal(gisSf6EnvMonitoringStatistics2.getTmp())).abs().toString();

			}
			if(StringUtils.isNotEmpty(gisSf6EnvMonitoringStatistics1.getOContent()) && StringUtils.isNotEmpty(gisSf6EnvMonitoringStatistics2.getOContent())){
				rate4=CalcUtil.calcRateEight(new BigDecimal(gisSf6EnvMonitoringStatistics1.getOContent()).
						subtract(new BigDecimal(gisSf6EnvMonitoringStatistics2.getOContent())),new BigDecimal(gisSf6EnvMonitoringStatistics2.getOContent())).abs().toString();

			}
		}
		if(ObjectUtils.isNotEmpty(gisSf6EnvMonitoringStatistics1) && ObjectUtils.isNotEmpty(gisSf6EnvMonitoringStatistics30) ){
			if(StringUtils.isNotEmpty(gisSf6EnvMonitoringStatistics1.getConc()) && StringUtils.isNotEmpty(gisSf6EnvMonitoringStatistics30.getConc())){
				rate11=CalcUtil.calcRateEight(new BigDecimal(gisSf6EnvMonitoringStatistics1.getConc()).
						subtract(new BigDecimal(gisSf6EnvMonitoringStatistics30.getConc())),new BigDecimal(gisSf6EnvMonitoringStatistics30.getConc())).abs().toString();

			}
			if(StringUtils.isNotEmpty(gisSf6EnvMonitoringStatistics1.getDew()) && StringUtils.isNotEmpty(gisSf6EnvMonitoringStatistics30.getDew())){
				rate21=CalcUtil.calcRateEight(new BigDecimal(gisSf6EnvMonitoringStatistics1.getDew()).
						subtract(new BigDecimal(gisSf6EnvMonitoringStatistics30.getDew())),new BigDecimal(gisSf6EnvMonitoringStatistics30.getDew())).abs().toString();

			}
			if(StringUtils.isNotEmpty(gisSf6EnvMonitoringStatistics1.getTmp()) && StringUtils.isNotEmpty(gisSf6EnvMonitoringStatistics30.getTmp())){
				rate31=CalcUtil.calcRateEight(new BigDecimal(gisSf6EnvMonitoringStatistics1.getTmp()).
						subtract(new BigDecimal(gisSf6EnvMonitoringStatistics30.getTmp())),new BigDecimal(gisSf6EnvMonitoringStatistics30.getTmp())).abs().toString();

			}
			if(StringUtils.isNotEmpty(gisSf6EnvMonitoringStatistics1.getOContent()) && StringUtils.isNotEmpty(gisSf6EnvMonitoringStatistics30.getOContent())){
				rate41=CalcUtil.calcRateEight(new BigDecimal(gisSf6EnvMonitoringStatistics1.getOContent()).
						subtract(new BigDecimal(gisSf6EnvMonitoringStatistics30.getOContent())),new BigDecimal(gisSf6EnvMonitoringStatistics30.getOContent())).abs().toString();

			}
		}
		entity.setIncreaseConc(rate1);
		entity.setIncreaseDew(rate2);
		entity.setIncreaseTmp(rate3);
		entity.setIncreaseOContent(rate4);
		entity.setMonthIncreaseConc(rate11);
		entity.setMonthIncreaseDew(rate21);
		entity.setMonthIncreaseTmp(rate31);
		entity.setMonthIncreaseOContent(rate41);
		gisSf6EnvMonitoringStatisticsDao.saveOrUpdate(entity);

	}





}