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

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.huatek.busi.common.SiteConstants.IecTypeEnum;
import com.huatek.busi.common.util.DecimalUtil;
import com.huatek.busi.dao.RptAretTemperSummaryChaobiaolvDao;
import com.huatek.busi.entity.RptAretTemperSummaryChaobiaolv;
import com.huatek.busi.mapper.RptAretTemperSummaryChaobiaolvMapper;
import com.huatek.busi.service.arrester.RptAretTemperSummaryChaobiaolvService;
import com.huatek.busi.service.threshold.InfraredThresholdService;
import com.huatek.busi.vo.InfraredThresholdVo;

/**
 * 避雷器--红外测温超标率每日汇总
 * 
 * @author buck_guo
 * @date 2019/07/16
 */
@Repository
public class RptAretTemperSummaryChaobiaolvServiceImpl implements RptAretTemperSummaryChaobiaolvService {

    @Autowired
    private RptAretTemperSummaryChaobiaolvMapper rptAretTemperSummaryChaobiaolvMapper;
    @Autowired
    private RptAretTemperSummaryChaobiaolvDao rptAretTemperSummaryChaobiaolvDao;

    @Autowired
    private InfraredThresholdService infraredThresholdService;

    @Override
    public Integer inserData2Summary(LocalDate day) {
        // 删除数据
        rptAretTemperSummaryChaobiaolvMapper.deleteOneDayData(day);
        // 总数统计
        LocalDateTime startTime = day.atStartOfDay();
        LocalDateTime endTime = day.plusDays(1).atStartOfDay();
        List<Map<String, Object>> list = rptAretTemperSummaryChaobiaolvMapper.findOneDayCountData(startTime, endTime);
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        // 按工作状态统计超标数
        for (Map<String, Object> map : list) {
            String siteId = (String)map.get("siteId");
            String mainTreeId = (String)map.get("mainTreeId");
            String subTreeId = (String)map.get("subTreeId");
            String mainEquipmentId = (String)map.get("mainEquipmentId");
            String subEquipmentId = (String)map.get("subEquipmentId");
            String sensorId = (String)map.get("sensorId");
            Long dataCountStr = (Long)map.remove("dataCount");
            // 总数
            BigDecimal dataCount = new BigDecimal(dataCountStr);
            BigDecimal chaoBiaoCount = BigDecimal.ZERO;

            // 开始时间和结束时间作为参数
            map.put("startTime", startTime);
            map.put("endTime", endTime);

            // 发电
            InfraredThresholdVo thresholdFd =
                infraredThresholdService.queryInfraredThreshold(sensorId, IecTypeEnum.FD.getValue());
            if (thresholdFd != null && !StringUtils.isEmpty(thresholdFd.getRegionThreshold())) {
                map.put("regionThreshold", new BigDecimal(thresholdFd.getRegionThreshold()));
                map.put("runState", IecTypeEnum.FD.getValue());
                chaoBiaoCount = chaoBiaoCount.add(rptAretTemperSummaryChaobiaolvMapper.getChaoBiaoCount(map));
            }
            // 抽水
            InfraredThresholdVo thresholdCs =
                infraredThresholdService.queryInfraredThreshold(sensorId, IecTypeEnum.CS.getValue());
            if (thresholdCs != null && !StringUtils.isEmpty(thresholdCs.getRegionThreshold())) {
                map.put("regionThreshold", new BigDecimal(thresholdCs.getRegionThreshold()));
                map.put("runState", IecTypeEnum.CS.getValue());
                chaoBiaoCount = chaoBiaoCount.add(rptAretTemperSummaryChaobiaolvMapper.getChaoBiaoCount(map));
            }
            // 空闲
            InfraredThresholdVo thresholdKx =
                infraredThresholdService.queryInfraredThreshold(sensorId, IecTypeEnum.KX.getValue());
            if (thresholdKx != null && !StringUtils.isEmpty(thresholdKx.getRegionThreshold())) {
                map.put("regionThreshold", new BigDecimal(thresholdKx.getRegionThreshold()));
                map.put("runState", IecTypeEnum.KX.getValue());
                chaoBiaoCount = chaoBiaoCount.add(rptAretTemperSummaryChaobiaolvMapper.getChaoBiaoCount(map));
            }
            RptAretTemperSummaryChaobiaolv entity = new RptAretTemperSummaryChaobiaolv();
            entity.setSiteId(siteId);
            entity.setMainTreeId(mainTreeId);
            entity.setSubTreeId(subTreeId);
            entity.setMainEquipmentId(mainEquipmentId);
            entity.setSubEquipmentId(subEquipmentId);
            entity.setSensorId(sensorId);
            entity.setDataCount(dataCountStr.toString());
            entity.setChaobiaoCount(chaoBiaoCount.toEngineeringString());
            entity.setRtpTime(day);
            entity.setCreatetime(LocalDateTime.now());
            // 超标率
            if (BigDecimal.ZERO.compareTo(chaoBiaoCount) == 0) {
                entity.setChaobiaolv("0");
            } else {
                entity.setChaobiaolv(DecimalUtil.divide(chaoBiaoCount, dataCount, 8).toEngineeringString());
            }
            rptAretTemperSummaryChaobiaolvDao.saveOrUpdate(entity);
        }
        return list.size();
    }

}
