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.TransformerRunStatusStatisticsDao;
import com.huatek.busi.entity.TransformerRunStatusStatistics;
import com.huatek.busi.mapper.TransformerRunStatusStatisticsMapper;
import com.huatek.busi.service.transformer.TransformerRunStatusStatisticsService;
import com.huatek.busi.vo.TransformerRunStatusCountVo;

/**
 * 变压器运行工况数据统计 ServiceImpl
 *
 * @author anker_cheng
 * @date Oct 28, 2019 2:05:50 PM
 */
@Component
@Service(interfaceClass = TransformerRunStatusStatisticsService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerRunStatusStatisticsServiceImpl implements TransformerRunStatusStatisticsService {

    @Autowired
    private TransformerRunStatusStatisticsMapper transformerRunStatusStatisticsMapper;
    @Autowired
    private TransformerRunStatusStatisticsDao transformerRunStatusStatisticsDao;

    @Override
    public synchronized void saveStatistics(LocalDate day, TransformerRunStatusCountVo item) {
        // 删除旧数据
        transformerRunStatusStatisticsMapper.deleteItem(item.getMainTreeId(), day);
        // 保存新数据
        TransformerRunStatusStatistics entity = new TransformerRunStatusStatistics();
        entity.setSiteId(item.getSiteId());
        entity.setMainTreeId(item.getMainTreeId());
        entity.setAvgCoolingEntryPressure(item.getAvgCoolingEntryPressure());
        entity.setAvgCoolingExitPressure(item.getAvgCoolingExitPressure());
        entity.setAvgOilPosition(item.getAvgOilPosition());
        entity.setAvgOilTemperature(item.getAvgOilTemperature());
        entity.setAvgCoolingWaterInletTemperature(item.getAvgCoolingWaterInletTemperature());
        entity.setAvgCoolingWaterOutletTemperature(item.getAvgCoolingWaterOutletTemperature());
        entity.setDTime(day);
        entity.setCreateTime(LocalDateTime.now());
        transformerRunStatusStatisticsDao.saveOrUpdate(entity);
    }

}