/**
 * 
 */
package com.huatek.busi.task.cwg;

import java.math.BigDecimal;
import java.text.ParseException;
import java.time.LocalDate;
import java.util.List;

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

import com.huatek.busi.common.CacMonitorTypeCode;
import com.huatek.busi.common.SiteConstants.SiteTreeEnum;
import com.huatek.busi.common.UploadCycle.ConfigField;
import com.huatek.busi.dto.CwgTemperatureStatisticsDto;
import com.huatek.busi.service.BusiSensorService;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.service.FailureRateService;
import com.huatek.busi.service.SiteUploadCycleService;
import com.huatek.busi.service.temperature.CwgTemperatureService;
import com.huatek.busi.service.temperature.RptTemperChartsService;
import com.huatek.busi.service.temperature.RptTemperRecordChartsService;
import com.huatek.busi.task.base.AbstractBusiTaskExecutor;
import com.huatek.busi.vo.BusiSensorVo;
import com.huatek.busi.vo.BusiTreeNodeVo;
import com.huatek.busi.vo.CwgTemperatureStatisticsVo;

/**
 * 测温柜定时器
 * 
 * @author anker_cheng
 * @date 2019/10/28
 */
@Component("rptTemperChartsTask")
public class RptTemperChartsTask extends AbstractBusiTaskExecutor {
    @Autowired
    private RptTemperChartsService rptTemperChartsService;
    @Autowired
    private FailureRateService failureRateService;
    @Autowired
    private RptTemperRecordChartsService rptTemperRecordChartsService;
    @Autowired
    private SiteUploadCycleService siteUploadCycleService;
    @Autowired
    private CwgTemperatureService cwgTemperatureService;
    @Autowired
    private BusiSiteTreeService busiSiteTreeService;
    @Autowired
    private BusiSensorService busiSensorService;

    @Override
    public void execute()throws Exception {
        super.execute();
    }

    @Override
    public void execute(LocalDate day) {
        // 测温柜统计信息
        saveCwgTemperatureStatistics(day);

        // 温控柜 -- 测温点 -- 温度不合格率 (每天汇总一次) //参数为高温预警值
        // rptTemperRateService.inserData2Summary();

        // 获取测温柜前一天的各项平均温度
        saveRptTemperCharts(day);
    }

    private void saveRptTemperCharts(LocalDate day) {
        List<BusiTreeNodeVo> list =
            busiSiteTreeService.findMainEquipmentList(new String[] {SiteTreeEnum.CW_HL_EQM.getValue()});
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (BusiTreeNodeVo item : list) {
            rptTemperChartsService.saveRptTemperCharts(day, item.getSiteId(), item.getId());
        }
    }

    private void saveCwgTemperatureStatistics(LocalDate day) {
        List<BusiSensorVo> sensorList = findSensorList();
        if (CollectionUtils.isEmpty(sensorList)) {
            return;
        }
        CwgTemperatureStatisticsDto param = new CwgTemperatureStatisticsDto();
        param.setCacMonitorTypeCode(CacMonitorTypeCode.CWG.getCode());
        param.setStartTime(day.atStartOfDay());
        param.setEndTime(day.plusDays(1).atStartOfDay());
        for (BusiSensorVo sensor : sensorList) {
            // 上传周期
            BigDecimal cycle = siteUploadCycleService.getCycle(sensor.getSiteId(), ConfigField.cwzz);
            // 数据统计
            param.setSensorCode(sensor.getSensorCode());
            List<CwgTemperatureStatisticsVo> list = cwgTemperatureService.getListEquInfo(param);
            CwgTemperatureStatisticsVo item = CollectionUtils.isEmpty(list) ? null : list.get(0);
            // 保存缺失率
            failureRateService.saveSensorFailureRate(sensor, day, cycle, item == null ? 0 : item.getDataCount());
            // TODO 保存各项平均温度
            if (item != null) {
                rptTemperRecordChartsService.saveRptTemperRecordCharts(day, item);
            }
        }
    }

    private List<BusiSensorVo> findSensorList() {
        return busiSensorService.findSensorList(SiteTreeEnum.CW, (String[])null);
    }
}