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

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.RptAretTemperSummaryDao;
import com.huatek.busi.entity.RptAretTemperSummary;
import com.huatek.busi.mapper.RptAretTemperSummaryMapper;
import com.huatek.busi.service.arrester.RptAretTemperSummaryService;

/**
 * 避雷器-红外测温每日汇总汇总
 * 
 * @author buck_guo
 * @date 2019/07/16
 */
@Component
@Service(interfaceClass = RptAretTemperSummaryService.class)
@Transactional(rollbackFor = Exception.class)
public class RptAretTemperSummaryServiceImpl implements RptAretTemperSummaryService {

    @Autowired
    private RptAretTemperSummaryMapper rptAretTemperSummaryMapper;
    @Autowired
    private RptAretTemperSummaryDao rptAretTemperSummaryDao;

    @Override
    public Integer inserData2Summary(LocalDate day) {
        //删除数据
        rptAretTemperSummaryMapper.deleteOneDayData(day);
        //统计并保存
        LocalDateTime startTime = day.atStartOfDay();
        LocalDateTime endTime = day.plusDays(1).atStartOfDay();
        List<RptAretTemperSummary> list = rptAretTemperSummaryMapper.findOneDayData(startTime, endTime);
        if(CollectionUtils.isEmpty(list)) {
            return 0;
        }
        for(RptAretTemperSummary entity : list) {
            entity.setRtpTime(day);
            entity.setCreatetime(LocalDateTime.now());
            rptAretTemperSummaryDao.saveOrUpate(entity);
        }
        return list.size();
    }

}
