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

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.ArresterMeteorologicalCacheDao;
import com.huatek.busi.dto.ArresterMeteorologicalCacheDto;
import com.huatek.busi.entity.ArresterMeteorologicalCache;
import com.huatek.busi.entity.TransformerMeteorological;
import com.huatek.busi.mapper.ArresterMeteorologicalCacheMapper;
import com.huatek.busi.service.arrester.ArresterMeteorologicalCacheService;
import com.huatek.busi.vo.ArresterMeteorologicalCacheVo;
import com.huatek.busi.vo.MeteorologicalVo;

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

    @Autowired
    private ArresterMeteorologicalCacheMapper arresterMeteorologicalCacheMapper;
    @Autowired
    private ArresterMeteorologicalCacheDao arresterMeteorologicalCacheDao;

    @Override
    public Page<ArresterMeteorologicalCacheVo> findArresterMeteorologicalCachePage(
            ArresterMeteorologicalCacheDto arresterMeteorologicalCacheDto) {
        if (arresterMeteorologicalCacheDto.getPage() != null && arresterMeteorologicalCacheDto.getLimit() != null) {
            PageHelper.startPage(arresterMeteorologicalCacheDto.getPage(), arresterMeteorologicalCacheDto.getLimit());
        }
        Page<ArresterMeteorologicalCacheVo> arresterMeteorologicalCaches = arresterMeteorologicalCacheMapper
                .selectArresterMeteorologicalCachePage(arresterMeteorologicalCacheDto);
        return arresterMeteorologicalCaches;
    }

    @Override
    public synchronized int saveArresterMeteorologicalCache(TransformerMeteorological cacEntity) {
        ArresterMeteorologicalCache arresterMeteorologicalCache = arresterMeteorologicalCacheMapper
                .getArresterMeteorologicalCache(cacEntity.getSiteId(), cacEntity.getEquipmentCode());
        if (arresterMeteorologicalCache == null) {
            arresterMeteorologicalCache = new ArresterMeteorologicalCache();
        }
        arresterMeteorologicalCache.setAirPressure(transData(cacEntity.getAirPressure()));
        arresterMeteorologicalCache.setAirTemperature(transData(cacEntity.getAirTemperature()));
        arresterMeteorologicalCache.setDTime(cacEntity.getTimeStamp());
        arresterMeteorologicalCache.setHumidity(transData(cacEntity.getHumidity()));
        arresterMeteorologicalCache.setSiteEqmCode(cacEntity.getEquipmentCode());
        arresterMeteorologicalCache.setSiteId(cacEntity.getSiteId());
        return arresterMeteorologicalCacheDao.saveOrUpdate(arresterMeteorologicalCache);
    }

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

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

    @Override
    public MeteorologicalVo defaultMeteorologicalInfo() {
         return arresterMeteorologicalCacheMapper.defaultMeteorologicalInfo();
    }
}