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

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dao.RptGisTemperSummaryDao;
import com.huatek.busi.entity.RptGisTemperSummary;
import com.huatek.busi.mapper.RptGisTemperSummaryMapper;
import com.huatek.busi.service.gis.RptGisTemperSummaryService;

@Component
@Service(interfaceClass = RptGisTemperSummaryService.class)
@Transactional(rollbackFor = Exception.class)
public class RptGisTemperSummaryServiceImpl implements RptGisTemperSummaryService {

    @Autowired
    private RptGisTemperSummaryDao rptGisTemperSummaryDao;
    @Autowired
    private RptGisTemperSummaryMapper rptGisTemperSummaryMapper;

    @Override
    public int inserData2Summary(LocalDate day) {
        // 删除数据
        rptGisTemperSummaryMapper.deleteOneDayData(day);
        // 统计并保存
        LocalDateTime startTime = day.atStartOfDay();
        LocalDateTime endTime = day.plusDays(1).atStartOfDay();
        List<RptGisTemperSummary> list = rptGisTemperSummaryMapper.findOneDayData(startTime, endTime);
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        for(RptGisTemperSummary entity : list) {
            entity.setRtpTime(day);
            entity.setCreateTime(LocalDateTime.now());
            rptGisTemperSummaryDao.saveOrUpdate(entity);
        }
        return list.size();
    }

}
