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

import java.math.BigDecimal;
import java.time.LocalDate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dao.TransformerEpaDeviationStatisticsDao;
import com.huatek.busi.entity.TransformerEpaDeviationStatistics;
import com.huatek.busi.mapper.TransformerEpaDeviationStatisticsMapper;
import com.huatek.busi.service.transformer.TransformerEpaDeviationStatisticsService;
import com.huatek.busi.vo.epa.EpaDeviationOneDataVo;

/**
 * 变压器油色谱数据偏差率统计 ServiceImpl
 *
 * @author anker_cheng
 * @date Jul 15, 2019 11:49:08 AM
 */
@Component
@Service(interfaceClass = TransformerEpaDeviationStatisticsService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerEpaDeviationStatisticsServiceImpl implements TransformerEpaDeviationStatisticsService {

    @Autowired
    private TransformerEpaDeviationStatisticsMapper transformerEpaDeviationStatisticsMapper;
    @Autowired
    private TransformerEpaDeviationStatisticsDao transformerEpaDeviationStatisticsDao;

    @Override
    public synchronized int deleteTransformerEpaDeviationStatistics(LocalDate day) {
        return transformerEpaDeviationStatisticsMapper.deleteTransformerEpaDeviationStatistics(day);
    }

    @Override
    public synchronized int saveTransformerEpaDeviationStatistics(EpaDeviationOneDataVo vo, LocalDate day) {
        TransformerEpaDeviationStatistics temp = new TransformerEpaDeviationStatistics();
        temp.setC2h2Deviation(calcDeviation(vo.getC2h2(), vo.getTotalCount()));
        temp.setC2h4Deviation(calcDeviation(vo.getC2h4(), vo.getTotalCount()));
        temp.setC2h6Deviation(calcDeviation(vo.getC2h6(), vo.getTotalCount()));
        temp.setCh4Deviation(calcDeviation(vo.getCh4(), vo.getTotalCount()));
        temp.setCo2Deviation(calcDeviation(vo.getCo2(), vo.getTotalCount()));
        temp.setCoDeviation(calcDeviation(vo.getCo(), vo.getTotalCount()));
        temp.setDTime(day);
        temp.setH2Deviation(calcDeviation(vo.getH2(), vo.getTotalCount()));
        temp.setH2oDeviation(calcDeviation(vo.getH2o(), vo.getTotalCount()));
        temp.setTotalHydrocarbonDeviation(calcDeviation(vo.getTotalHydrocarbon(), vo.getTotalCount()));
        temp.setSiteId(vo.getSiteId());
        temp.setEquipmentId(vo.getEquipmentId());
        temp.setMainEquipmentId(vo.getMainEquipmentId());
        temp.setSubEquipmentId(vo.getSubEquipmentId());
        temp.setMainTreeId(vo.getMainTreeId());
        temp.setSubTreeId(vo.getSubTreeId());
        temp.setSensorCode(vo.getSensorId());
        return transformerEpaDeviationStatisticsDao.saveOrUpdate(temp);
    }

    private static BigDecimal calcDeviation(BigDecimal value, BigDecimal count) {
        if (value == null || count == null) {
            return BigDecimal.ZERO;
        }
        if (BigDecimal.ZERO.compareTo(value) == 0 || BigDecimal.ZERO.compareTo(count) == 0) {
            return BigDecimal.ZERO;
        }
        return value.divide(count, 2, BigDecimal.ROUND_HALF_UP);
    }

    @Override
    public BigDecimal getWorstEpaDeviation(String sensorCode) {
    	LocalDate yesterday = LocalDate.now().minusDays(1);
        return transformerEpaDeviationStatisticsMapper.getWorstEpaDeviation(sensorCode,yesterday);
    }

	@Override
	public TransformerEpaDeviationStatistics getTransformerEpaDeviationStatisticsInfo(String sensorCode,
			LocalDate day) {
		// TODO Auto-generated method stub
		return transformerEpaDeviationStatisticsMapper.getTransformerEpaDeviationStatisticsInfo(sensorCode,day);
	}
}