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

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.SubstationMicCacheDao;
import com.huatek.busi.dto.SubstationMicCacheDto;
import com.huatek.busi.entity.SubstationMicCache;
import com.huatek.busi.entity.TransformerMeteorological;
import com.huatek.busi.mapper.SubstationMicCacheMapper;
import com.huatek.busi.service.substation.SubstationMicCacheService;
import com.huatek.busi.vo.MeteorologicalVo;
import com.huatek.busi.vo.SubstationMicCacheVo;

/**
 * 变电站微气象暂存数据 ServiceImpl
 *
 * @author anker_cheng
 * @date Jun 28, 2019 4:19:38 PM
 */
@Component
@Service(interfaceClass = SubstationMicCacheService.class)
@Transactional(rollbackFor = Exception.class)
public class SubstationMicCacheServiceImpl implements SubstationMicCacheService {

    @Autowired
    private SubstationMicCacheMapper substationMicCacheMapper;
    @Autowired
    private SubstationMicCacheDao substationMicCacheDao;

    @Override
    public Page<SubstationMicCacheVo> findSubstationMicCachePage(SubstationMicCacheDto substationMicCacheDto) {
        if (substationMicCacheDto.getPage() != null && substationMicCacheDto.getLimit() != null) {
            PageHelper.startPage(substationMicCacheDto.getPage(), substationMicCacheDto.getLimit());
        }
        Page<SubstationMicCacheVo> substationMicCaches = substationMicCacheMapper
                .selectSubstationMicCachePage(substationMicCacheDto);
        return substationMicCaches;
    }

    @Override
    public synchronized int saveSubstationMicCache(TransformerMeteorological cacEntity) {
        SubstationMicCache substationMicCache = substationMicCacheMapper.getSubstationMicCache(cacEntity.getSiteId(),
                cacEntity.getEquipmentCode());
        if (substationMicCache == null) {
            substationMicCache = new SubstationMicCache();
        }
        substationMicCache.setAirPressure(DecimalUtil.getVal(cacEntity.getAirPressure()));
        substationMicCache.setAirTemperature(DecimalUtil.getVal(cacEntity.getAirTemperature()));
        substationMicCache.setDTime(cacEntity.getTimeStamp());
        substationMicCache.setHumidity(DecimalUtil.getVal(cacEntity.getHumidity()));
        substationMicCache.setPrecipitation(DecimalUtil.getVal(cacEntity.getPrecipitation()));
        substationMicCache.setPrecipitationIntensity(DecimalUtil.getVal(cacEntity.getPrecipitationIntensity()));
        substationMicCache.setRadiationIntensity(DecimalUtil.getVal(cacEntity.getRadiationIntensity()));
        // substationMicCache.setSiteId(Long.valueOf(substationMic.getSubstationid()));
        substationMicCache.setSubstationId(cacEntity.getEquipmentId());
        return substationMicCacheDao.saveOrUpdate(substationMicCache);
    }

    @Override
    public MeteorologicalVo queryMeteorologicalInfo() {
        // TODO Auto-generated method stub
         return substationMicCacheMapper.queryMeteorologicalInfo();
    }
}