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

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.common.BusiConstant.TemperPointType;
import com.huatek.busi.common.CacMonitorTypeCode;
import com.huatek.busi.dao.RptTemperChartsDao;
import com.huatek.busi.dto.CwgTemperatureStatisticsDto;
import com.huatek.busi.entity.RptTemperCharts;
import com.huatek.busi.mapper.RptTemperChartsMapper;
import com.huatek.busi.service.temperature.BusiTemperPointService;
import com.huatek.busi.service.temperature.CwgTemperatureService;
import com.huatek.busi.service.temperature.RptTemperChartsService;
import com.huatek.busi.vo.BusiTemperPointVo;
import com.huatek.busi.vo.CwgTemperatureStatisticsVo;

import lombok.extern.slf4j.Slf4j;

/**
 * 测温柜-温度曲线
 * 
 * @author buck_guo
 * @date 2019/07/14
 */
@Slf4j
@Component
@Service(interfaceClass = RptTemperChartsService.class)
@Transactional(rollbackFor = Exception.class)
public class RptTemperChartsServiceImpl implements RptTemperChartsService {

    @Autowired
    private RptTemperChartsDao rptTemperChartsDao;
    @Autowired
    private RptTemperChartsMapper rptTemperChartsMapper;

    @Autowired
    private CwgTemperatureService cwgTemperatureService;
    @Autowired
    private BusiTemperPointService busiTemperPointService;

    @Override
    public void saveRptTemperCharts(LocalDate day, String siteId, String mainTreeId) {
        RptTemperCharts rptTemperCharts = rptTemperChartsMapper.getRptTemperCharts(mainTreeId, day);
        if (rptTemperCharts == null) {
            rptTemperCharts = new RptTemperCharts();
            rptTemperCharts.setMainTreeId(mainTreeId);
            // rptTemperCharts.setMainEquipmentId("");
            rptTemperCharts.setSiteId(siteId);
            rptTemperCharts.setHzTime(day);
            rptTemperCharts.setCreateTime(LocalDateTime.now());
        }
        CwgTemperatureStatisticsDto param = new CwgTemperatureStatisticsDto();
        param.setCacMonitorTypeCode(CacMonitorTypeCode.CWG.getCode());
        param.setStartTime(day.atStartOfDay());
        param.setEndTime(day.plusDays(1).atStartOfDay());
        param.setMainTreeId(mainTreeId);
        param.setSiteId(siteId);
        List<CwgTemperatureStatisticsVo> list = cwgTemperatureService.getListEquInfo(param);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (CwgTemperatureStatisticsVo item : list) {
            BusiTemperPointVo busiTemperPointVo =
                busiTemperPointService.getBusiTemperPointVoBySensor(item.getSensorCode());
            if (busiTemperPointVo == null) {
                log.debug("传感器：{}不存在或已被删除", item.getSensorCode());
                continue;
            }
            TemperPointType temperPointType = TemperPointType.from(busiTemperPointVo.getPosition());
            if(temperPointType == null) {
                log.debug("传感器：{}安装位置：{}不存在", item.getSensorCode(), busiTemperPointVo.getPosition());
                continue;
            }
            switch(temperPointType) {
                case A_I:
                    rptTemperCharts.setAiTemper(item.getTmp());
                    rptTemperCharts.setAiSensorId(item.getSensorCode());
                    break;
                case A_O:
                    rptTemperCharts.setAoTemper(item.getTmp());
                    rptTemperCharts.setAoSensorId(item.getSensorCode());
                    break;
                case B_I:
                    rptTemperCharts.setBiTemper(item.getTmp());
                    rptTemperCharts.setBiSensorId(item.getSensorCode());
                    break;
                case B_O:
                    rptTemperCharts.setBoTemper(item.getTmp());
                    rptTemperCharts.setBoSensorId(item.getSensorCode());
                    break;
                case C_I:
                    rptTemperCharts.setCiTemper(item.getTmp());
                    rptTemperCharts.setCiSensorId(item.getSensorCode());
                    break;
                case C_O:
                    rptTemperCharts.setCoTemper(item.getTmp());
                    rptTemperCharts.setCoSensorId(item.getSensorCode());
                    break;
                case IN_SIDE:
                    rptTemperCharts.setBoxTemper(item.getTmp());
                    rptTemperCharts.setBoxSensorId(item.getSensorCode());
                    break;
                case OUT_SIDE:
                    rptTemperCharts.setBoxOutsideTemper(item.getTmp());
                    rptTemperCharts.setBoxOutsideSensorId(item.getSensorCode());
                    break;
                default:
                    break;
            }
        }
        rptTemperChartsDao.saveOrUpdate(rptTemperCharts);
    }
}