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

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

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 org.springframework.util.CollectionUtils;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dao.TransformerEpaDataOriginDao;
import com.huatek.busi.entity.TransformerEpaAbsIncreaseCache;
import com.huatek.busi.entity.TransformerEpaData;
import com.huatek.busi.entity.TransformerEpaDataOrigin;
import com.huatek.busi.entity.TransformerEpaIncreaseCache;
import com.huatek.busi.mapper.TransformerEpaDataOriginMapper;
import com.huatek.busi.service.transformer.TransformerEpaDataOriginService;
import com.huatek.busi.vo.epa.EpaOneDataVo;
import com.huatek.busi.vo.stability.OilStabilityVo;

/**
 * 变压器油色谱原始数据(未根据偏差率修改的数据) ServiceImpl
 *
 * @author anker_cheng
 * @date Jul 15, 2019 11:13:56 AM
 */
@Component
@Service(interfaceClass = TransformerEpaDataOriginService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerEpaDataOriginServiceImpl implements TransformerEpaDataOriginService {

    @Autowired
    private TransformerEpaDataOriginMapper transformerEpaDataOriginMapper;
    @Autowired
    private TransformerEpaDataOriginDao transformerEpaDataOriginDao;

    @Override
    public void saveTransformerEpaDataOrigin(TransformerEpaData cacEntity) {
        TransformerEpaDataOrigin transformerEpaDataOrigin = new TransformerEpaDataOrigin();
        transformerEpaDataOrigin.setC2h2(StrVal(cacEntity.getC2h2()));
        transformerEpaDataOrigin.setH2(StrVal(cacEntity.getH2()));
        transformerEpaDataOrigin.setCo(StrVal(cacEntity.getCo()));
        transformerEpaDataOrigin.setCo2(StrVal(cacEntity.getCo2()));
        transformerEpaDataOrigin.setCh4(StrVal(cacEntity.getCh4()));
        transformerEpaDataOrigin.setC2h6(StrVal(cacEntity.getC2h6()));
        transformerEpaDataOrigin.setC2h4(StrVal(cacEntity.getC2h4()));
        transformerEpaDataOrigin.setH2o(StrVal(cacEntity.getH2o()));
        transformerEpaDataOrigin.setTmp(StrVal(cacEntity.getTemperature()));
        transformerEpaDataOrigin.setDew(StrVal(cacEntity.getMicroMoisture()));
        transformerEpaDataOrigin.setMst(StrVal(cacEntity.getMicroMoisture()));
        transformerEpaDataOrigin.setFlatGas(StrVal(cacEntity.getFlatGas()));
        transformerEpaDataOrigin.setCmbuGas(StrVal(cacEntity.getCmbuGas()));
        transformerEpaDataOrigin.setTotalHydrocarbon(StrVal(cacEntity.getTotalHydrocarbon()));
        transformerEpaDataOrigin.setO2(StrVal(cacEntity.getO2()));
        transformerEpaDataOrigin.setN2(StrVal(cacEntity.getN2()));
        transformerEpaDataOrigin.setSensorId(cacEntity.getSensorId());
        transformerEpaDataOrigin.setCacId(cacEntity.getCacId());
        transformerEpaDataOrigin.setPhase(cacEntity.getPhase());
        transformerEpaDataOrigin.setSiteId(cacEntity.getSiteId());
        transformerEpaDataOrigin.setEquipmentId(cacEntity.getEquipmentId());
        transformerEpaDataOrigin.setMainEquipmentId(cacEntity.getMainEquipmentId());
        transformerEpaDataOrigin.setSubEquipmentId(cacEntity.getSubEquipmentId());
        transformerEpaDataOrigin.setDTime(cacEntity.getTimeStamp());
        transformerEpaDataOrigin.setMainTreeId(cacEntity.getMainTreeId());
        transformerEpaDataOrigin.setSubTreeId(cacEntity.getSubTreeId());
        transformerEpaDataOrigin.setGasPress(cacEntity.getGasPress());
        transformerEpaDataOriginDao.saveOrUpdate(transformerEpaDataOrigin);
    }

    @Override
    public EpaOneDataVo getOneData(LocalDateTime startTime, LocalDateTime endTime, String sensorCode) {
        List<EpaOneDataVo> list = transformerEpaDataOriginMapper.getOneData(startTime, endTime, sensorCode);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    private static String StrVal(String val) {
        if (StringUtils.isBlank(val)) {
            return "0";
        }
        if (new BigDecimal(val).compareTo(BigDecimal.ZERO) == 0) {
            return val;
        }
        return new BigDecimal(val).setScale(8, RoundingMode.HALF_DOWN).toString();
    }

    @Override
    public TransformerEpaAbsIncreaseCache getEpaAbsIncreaseCacheInfo(String sensorCode) {
        return transformerEpaDataOriginMapper.getEpaAbsIncreaseCacheInfo(sensorCode);
    }


    @Override
    public List<OilStabilityVo> getByStatus(String sensorCode, LocalDateTime begin, LocalDateTime end) {
        List<OilStabilityVo> list = transformerEpaDataOriginMapper.getByStatus(sensorCode, begin, end);
        return list;
    }

    @Override
    public TransformerEpaIncreaseCache getEpaRelativeIncreaseCacheInfo(String sensorCode) {
        // TODO Auto-generated method stub
        return transformerEpaDataOriginMapper.getEpaRelativeIncreaseCacheInfo(sensorCode);
    }

}