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

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dao.GeneratorAmplMonitoringStatisticsDao;
import com.huatek.busi.dao.GeneratorPressureMonitoringStatisticsDao;
import com.huatek.busi.entity.GeneratorAmplMonitoringStatistics;
import com.huatek.busi.entity.GeneratorPressureMonitoringStatistics;
import com.huatek.busi.mapper.GeneratorAmplStatisticsMapper;
import com.huatek.busi.mapper.GeneratorPressureStatisticsMapper;
import com.huatek.busi.service.generatro.GeneratorAmplStatisticsService;
import com.huatek.busi.service.generatro.GeneratorPressureStatisticsService;
import com.huatek.busi.vo.GeneratorAmplStatisticsVo;
import com.huatek.busi.vo.GeneratorPressureStatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Component
@Service(interfaceClass = GeneratorPressureStatisticsService.class)
@Transactional(rollbackFor = Exception.class)
public class GeneratorPressureStatisticsServiceImpl
    implements GeneratorPressureStatisticsService {

    @Autowired
    private GeneratorAmplStatisticsMapper generatorAmplStatisticsMapper;

    @Autowired
    private GeneratorPressureStatisticsMapper generatorPressureStatisticsMapper;

    @Autowired
    private GeneratorAmplMonitoringStatisticsDao generatorAmplMonitoringStatisticsDao;

    @Autowired
    private GeneratorPressureMonitoringStatisticsDao generatorPressureMonitoringStatisticsDao;

    @Override
    public void saveStatistics(LocalDate day, GeneratorPressureStatisticsVo item) {
        // 删除旧数据
        generatorPressureStatisticsMapper.deleteItem(item.getSensorCode(), day);
        // 保存新数据
        GeneratorPressureMonitoringStatistics entity = new GeneratorPressureMonitoringStatistics();
        entity.setSiteId(item.getSiteId());
        entity.setSensorId(item.getSensorCode());
        entity.setMainTreeId(item.getMainTreeId());
        entity.setSubTreeId(item.getSubTreeId());
        entity.setDTime(day);
        entity.setCreateTime(LocalDateTime.now());
        entity.setTypeId(item.getTypeId());
        entity.setPvA(item.getPvA());
        entity.setPvB(item.getPvB());
        generatorPressureMonitoringStatisticsDao.saveOrUpdate(entity);
    }

}