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

import java.math.BigDecimal;
import java.math.RoundingMode;

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.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huatek.busi.common.util.DecimalUtil;
import com.huatek.busi.dao.TransformerMeteorologicalCacheDao;
import com.huatek.busi.dto.TransformerMeteorologicalCacheDto;
import com.huatek.busi.entity.TransformerMeteorological;
import com.huatek.busi.entity.TransformerMeteorologicalCache;
import com.huatek.busi.mapper.TransformerMeteorologicalCacheMapper;
import com.huatek.busi.service.transformer.TransformerMeteorologicalCacheService;
import com.huatek.busi.vo.MeteorologicalVo;
import com.huatek.busi.vo.TransformerMeteorologicalCacheVo;

/**
 * 变压器气象实时数据 ServiceImpl
 *
 * @author anker_cheng
 * @date Jun 28, 2019 4:19:38 PM
 */
@Component
@Service(interfaceClass = TransformerMeteorologicalCacheService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerMeteorologicalCacheServiceImpl implements TransformerMeteorologicalCacheService {

    @Autowired
    private TransformerMeteorologicalCacheMapper transformerMeteorologicalCacheMapper;
    @Autowired
    private TransformerMeteorologicalCacheDao transformerMeteorologicalCacheDao;

    @Override
    public Page<TransformerMeteorologicalCacheVo> findTransformerMeteorologicalCachePage(
            TransformerMeteorologicalCacheDto transformerMeteorologicalCacheDto) {
        if (transformerMeteorologicalCacheDto.getPage() != null
                && transformerMeteorologicalCacheDto.getLimit() != null) {
            PageHelper.startPage(transformerMeteorologicalCacheDto.getPage(),
                    transformerMeteorologicalCacheDto.getLimit());
        }
        Page<TransformerMeteorologicalCacheVo> transformerMeteorologicalCaches = transformerMeteorologicalCacheMapper
                .selectTransformerMeteorologicalCachePage(transformerMeteorologicalCacheDto);
        return transformerMeteorologicalCaches;
    }

    @Override
    public synchronized int saveTransformerMeteorologicalCache(TransformerMeteorological cacEntity) {
        TransformerMeteorologicalCache transformerMeteorologicalCache = transformerMeteorologicalCacheMapper
                .getTransformerMeteorologicalCache(cacEntity.getSiteId(), cacEntity.getEquipmentCode());
        if (transformerMeteorologicalCache == null) {
            transformerMeteorologicalCache = new TransformerMeteorologicalCache();
        }
        transformerMeteorologicalCache.setAirPressure(transData(cacEntity.getAirPressure()));
        transformerMeteorologicalCache.setAirTemperature(transData(cacEntity.getAirTemperature()));
        transformerMeteorologicalCache.setDTime(cacEntity.getTimeStamp());
        transformerMeteorologicalCache.setHumidity(transData(cacEntity.getHumidity()));
        // 20230101fdh新增风速
        transformerMeteorologicalCache.setWdgustspd(transData(cacEntity.getWdgustspd()));
        transformerMeteorologicalCache.setSiteEqmCode(cacEntity.getEquipmentCode());
        transformerMeteorologicalCache.setSiteId(cacEntity.getSiteId());
        return transformerMeteorologicalCacheDao.saveOrUpdate(transformerMeteorologicalCache);
    }

    private static BigDecimal transData(String val) {
        return DecimalUtil.getVal(val).setScale(2, RoundingMode.HALF_DOWN);
    }

    @Override
    public MeteorologicalVo queryMeteorologicalInfo(String sensorCode) {
         return transformerMeteorologicalCacheMapper.queryMeteorologicalInfo(sensorCode);
    }
}