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

import java.math.BigDecimal;
import java.time.LocalDateTime;

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.TransformerEpaDataStatisticsDao;
import com.huatek.busi.entity.TransformerEpaDataStatistics;
import com.huatek.busi.mapper.TransformerEpaDataStatisticsMapper;
import com.huatek.busi.service.transformer.TransformerEpaDataStatisticsService;
import com.huatek.busi.vo.epa.EpaOneDataVo;
import com.huatek.torch.util.uuid.Uuid;

/**
 * 油色谱统计数据表 ServiceImpl
 *
 * @author anker_cheng
 * @date Jul 15, 2019 11:13:56 AM
 */
@Component
@Service(interfaceClass = TransformerEpaDataStatisticsService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerEpaDataStatisticsServiceImpl implements TransformerEpaDataStatisticsService {

    @Autowired
    private TransformerEpaDataStatisticsMapper transformerEpaDataStatisticsMapper;
    @Autowired
    private TransformerEpaDataStatisticsDao transformerEpaDataStatisticsDao;

    @Override
    public TransformerEpaDataStatistics saveTransformerEpaDataStatistics(LocalDateTime dateTime, EpaOneDataVo epaOneDataVo) {
        TransformerEpaDataStatistics temp = new TransformerEpaDataStatistics();
        temp.setC2h2(strVal(epaOneDataVo.getAvgC2H2()));
        temp.setC2h4(strVal(epaOneDataVo.getAvgC2H4()));
        temp.setC2h6(strVal(epaOneDataVo.getAvgC2H6()));
        temp.setCh4(strVal(epaOneDataVo.getAvgCH4()));
        temp.setCo2(strVal(epaOneDataVo.getAvgCO2()));
        temp.setCo(strVal(epaOneDataVo.getAvgCO()));
        temp.setH2(strVal(epaOneDataVo.getAvgH2()));
        temp.setN2(strVal(epaOneDataVo.getAvgN2()));
        temp.setO2(strVal(epaOneDataVo.getAvgO2()));
        temp.setH2o(strVal(epaOneDataVo.getAvgH2O()));
        temp.setGasPress(strVal(BigDecimal.ONE));
//        temp.setGasPress(strVal(BigDecimal.ZERO));
        temp.setTotalHydrocarbon(strVal(epaOneDataVo.getAvgTotalHydrocarbon()));

        temp.setDTime(dateTime);

        temp.setSiteId(epaOneDataVo.getSiteId());
        temp.setCacId(epaOneDataVo.getCacId());
        temp.setSensorId(epaOneDataVo.getSensorId());
        temp.setMainEquipmentId(epaOneDataVo.getMainEquipmentId());
        temp.setSubEquipmentId(epaOneDataVo.getSubEquipmentId());
        temp.setEquipmentId(epaOneDataVo.getEquipmentId());
        temp.setMainTreeId(epaOneDataVo.getMainTreeId());
        temp.setSubTreeId(epaOneDataVo.getSubTreeId());

        temp.setId(Uuid.getUUID());
        transformerEpaDataStatisticsDao.add(temp);
        return temp;
    }

    private static String strVal(BigDecimal val) {
        return val == null ? "0" : val.toString();
    }

    @Override
    public TransformerEpaDataStatistics findIntervalDate(String siteId, String mainEquipmentId, String subEquipmentId,
        LocalDateTime dateTime) {
        return transformerEpaDataStatisticsMapper.findIntervalDate(siteId, mainEquipmentId, subEquipmentId, dateTime);
    }
}