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

import java.time.LocalDate;
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.TransformerMeteorologicalStatisticsDao;
import com.huatek.busi.entity.TransformerMeteorologicalStatistics;
import com.huatek.busi.mapper.TransformerMeteorologicalStatisticsMapper;
import com.huatek.busi.service.BusiTransformerLoadService;
import com.huatek.busi.service.transformer.TransformerMeteorologicalStatisticsService;
import com.huatek.busi.vo.MeteorologicalStatisticsVo;

/**
 * 变压器气象数据统计 ServiceImpl
 *
 * @author anker_cheng
 * @date Oct 28, 2019 12:30:48 PM
 */
@Component
@Service(interfaceClass = TransformerMeteorologicalStatisticsService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerMeteorologicalStatisticsServiceImpl implements TransformerMeteorologicalStatisticsService {

    @Autowired
    private TransformerMeteorologicalStatisticsMapper transformerMeteorologicalStatisticsMapper;
    @Autowired
    private TransformerMeteorologicalStatisticsDao transformerMeteorologicalStatisticsDao;

    @Autowired
    private BusiTransformerLoadService busiTransformerLoadService;

    @Override
    public void saveStatistics(LocalDate day, MeteorologicalStatisticsVo item) {
        // 删除旧数据
        transformerMeteorologicalStatisticsMapper.deleteItem(item.getSensorCode(), day, item.getCacMonitorTypeCode());
        // 保存新数据
        TransformerMeteorologicalStatistics entity = new TransformerMeteorologicalStatistics();
        entity.setSiteId(item.getSiteId());
        entity.setMainTreeId(item.getMainTreeId());
        entity.setSubTreeId(item.getSubTreeId());
        entity.setCacMonitorTypeCode(item.getCacMonitorTypeCode());
        entity.setDTime(day);
        entity.setCreateTime(LocalDateTime.now());
        entity.setAvgAirPressure(item.getAvgAirPressure());
        entity.setAvgAirTemperature(item.getAvgAirTemperature());
        entity.setAvgHumidity(item.getAvgHumidity());
        entity.setAvgPrecipitation(item.getAvgPrecipitation());
        entity.setAvgPrecipitationIntensity(item.getAvgPrecipitationIntensity());
        entity.setAvgRadiationIntensity(item.getAvgRadiationIntensity());
        entity.setLoadRate(busiTransformerLoadService.getLoadRate(item.getMainTreeId(), day));
        transformerMeteorologicalStatisticsDao.saveOrUpdate(entity);
    }

}