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 org.springframework.util.ObjectUtils;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dao.RptAretMonitorSummaryDao;
import com.huatek.busi.entity.RptAretMonitorSummary;
import com.huatek.busi.mapper.RptAretMonitorSummaryMapper;
import com.huatek.busi.service.arrester.RptAretMonitorSummaryService;
import com.huatek.busi.vo.rpt.RptAretMonitorSummaryQueryVo;

/**
 * 避雷器--基础信息--每天汇总
 * 
 * @author buck_guo
 * @date 2019/07/16
 */
@Component
@Service(interfaceClass = RptAretMonitorSummaryService.class)
@Transactional(rollbackFor = Exception.class)
public class RptAretMonitorSummaryServiceImpl implements RptAretMonitorSummaryService {

    @Autowired
    private RptAretMonitorSummaryMapper rptAretMonitorSummaryMapper;
    @Autowired
    private RptAretMonitorSummaryDao rptAretMonitorSummaryDao;

    @Override
    public synchronized Integer inserData2Summary(LocalDate day) {
        // 删除这一点的数据
        rptAretMonitorSummaryMapper.deleteOneDayDate(day);

        // 重新统计保存数据
        LocalDateTime startTime = day.atStartOfDay();
        LocalDateTime endTime = day.plusDays(1).atStartOfDay();
        List<RptAretMonitorSummaryQueryVo> list = rptAretMonitorSummaryMapper.findOneDayData(startTime, endTime);
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        for (RptAretMonitorSummaryQueryVo vo : list) {
        	if (ObjectUtils.isEmpty(vo)) {
        		continue;
        	}
            RptAretMonitorSummary entity = new RptAretMonitorSummary();
            entity.setSiteId(vo.getSiteId());
            entity.setMainTreeId(vo.getMainTreeId());
            entity.setSubTreeId(vo.getSubTreeId());
            entity.setMainEquipmentId(vo.getMainEquipmentId());
            entity.setSubEquipmentId(vo.getSubEquipmentId());
            entity.setASenserId(vo.getPhaseA());
            entity.setBSenserId(vo.getPhaseB());
            entity.setCSenserId(vo.getPhaseC());

            entity.setAFullAlc(vo.getTotalCurrentA());
            entity.setBFullAlc(vo.getTotalCurrentB());
            entity.setCFullAlc(vo.getTotalCurrentC());

            entity.setArresterCount(vo.getTotalActionCount());

            entity.setAHindranceAlc(vo.getResistiveCurrentA());
            entity.setBHindranceAlc(vo.getResistiveCurrentB());
            entity.setCHindranceAlc(vo.getResistiveCurrentC());

            entity.setRtpTime(day);
            entity.setCreateTime(LocalDateTime.now());

            rptAretMonitorSummaryDao.saveOrUpdate(entity);
        }
        return list.size();
    }
}
