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

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dao.BusiGeneratorPdStatisticsDao;
import com.huatek.busi.dao.TransformerPdStatisticsDao;
import com.huatek.busi.entity.BusiGeneratorPdStatistics;
import com.huatek.busi.entity.TransformerPdStatistics;
import com.huatek.busi.mapper.BusiGeneratorPdMapper;
import com.huatek.busi.mapper.GeneratorPdStatisticsMapper;
import com.huatek.busi.mapper.TransformerPdStatisticsMapper;
import com.huatek.busi.service.transformer.GeneratorPdStatisticsService;
import com.huatek.busi.service.transformer.TransformerPdStatisticsService;
import com.huatek.busi.vo.GeneratorPdCountVo;
import com.huatek.busi.vo.TransformerPdCountVo;
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;
import java.util.List;

@Component
@Service(interfaceClass = GeneratorPdStatisticsService.class)
@Transactional(rollbackFor = Exception.class)
public class GeneratorPdStatisticsServiceImpl implements GeneratorPdStatisticsService {

    @Autowired
    private GeneratorPdStatisticsMapper generatorPdStatisticsMapper;
    @Autowired
    private BusiGeneratorPdStatisticsDao busiGeneratorPdStatisticsDao;

    @Override
    public List<BusiGeneratorPdStatistics>
    findGeneratorPdStatistics(BusiGeneratorPdStatistics busiGeneratorPdStatistics) {
        return generatorPdStatisticsMapper.findGeneratorPdStatistics(busiGeneratorPdStatistics);
    }

    @Override
    public void saveTransformerPdStatistics(GeneratorPdCountVo item, LocalDate day) {
        // 删除记录
        generatorPdStatisticsMapper.deleteItem(item.getMainTreeId(), day);
        // 保存新记录
        BusiGeneratorPdStatistics entity = new BusiGeneratorPdStatistics();
        entity.setSiteId(item.getSiteId());
        entity.setMainTreeId(item.getMainTreeId());
        entity.setDTime(day);
        entity.setCount(item.getDataCount());
        entity.setCratetime(LocalDateTime.now());
        busiGeneratorPdStatisticsDao.saveOrUpdate(entity);
    }
}