package com.unis.detectionnum.server.service.impl;

import java.util.*;

import com.unis.detectionnum.common.vo.CableVO;
import com.unis.detectionnum.server.entity.WaterTempCollectionVO;
import com.unis.detectionnum.server.entity.WaterTempVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;

import com.unis.detectionnum.common.vo.DetectionWaterHistoryVO;
import com.unis.detectionnum.common.vo.DetectionWaterReportVO;
import com.unis.detectionnum.server.domain.DetectionWaterDomain;
import com.unis.detectionnum.server.service.DetectionWaterService;

/** 
 * @ClassName: DetectionWaterServiceImpl 
 * @Description: TODO 
 * @author LiuFeng 
 * @date 2019年9月10日 下午3:12:50  
 */
@Service
public class DetectionWaterServiceImpl implements DetectionWaterService{

	@Autowired
	private DetectionWaterDomain waterDomain;
	
	/**
	 * <p>Title: readWaterData</p> 
	 * <p>Description: </p> 
	 * @return 
	 * @throws ModbusInitException 
	 * @throws ErrorResponseException 
	 * @throws ModbusTransportException 
	 * @see com.unis.detectionnum.server.service.DetectionWaterService#readWaterData() 
	 */
	@Override
	public int readWaterData() throws ModbusTransportException, ErrorResponseException, ModbusInitException {
				
		return waterDomain.readWaterData();
	}

	/**
	 * <p>Title: getDetectionWaterReport</p> 
	 * <p>Description: </p> 
	 * @param cfbh
	 * @return 
	 * @see com.unis.detectionnum.server.service.DetectionWaterService#getDetectionWaterReport(java.lang.String) 
	 */
	@Override
	public DetectionWaterReportVO getDetectionWaterReport(String cfbh,Integer historyId) {
		
		return waterDomain.getDetectionWaterReport(cfbh,historyId);
	}

	/**
	 * <p>Title: getWaterHistory</p> 
	 * <p>Description: </p> 
	 * @param cfbh
	 * @return 
	 * @see com.unis.detectionnum.server.service.DetectionWaterService#getWaterHistory(java.lang.String) 
	 */
	@Override
	public List<DetectionWaterHistoryVO> getWaterHistory(String cfbh, String time) {
		
		return waterDomain.getWaterHistory(cfbh, time);
	}

	@Override
	public Map<String, Object> getWaterTempView(String cfbh) {
		Map<String, Object> resultMap = new HashMap<>();

		//定义六个折线图数据
		List<Object> avgTemperatureList = new ArrayList<>();
		List<Object> maxTemperatureList = new ArrayList<>();
		List<Object> minTemperatureList = new ArrayList<>();
		List<Object> avgHumidityList = new ArrayList<>();
		List<Object> maxHumidityList = new ArrayList<>();
		List<Object> minHumidityList = new ArrayList<>();

		//通过CFBH获取温度，水分折线图数据
		List<WaterTempVO> waterTempVOList = waterDomain.getWaterTempView(cfbh);

		//通过CFBH获取对应的监测时间
		//List<String> createTimes = waterDomain.getCreateTimes(cfbh);

		Calendar calendar = Calendar.getInstance();
		//获得当前系统时间所在月份的最大天数
		String actualMaximum = String.valueOf(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

		//定义一个包含31天的数组
		List<String> days = Arrays.asList(
				"1", "1", "1", "1","2","2","2","2","3","3","3","3",
				"4","4","4","4","5","5","5","5","6","6","6","6",
				"7","7","7","7","8","8","8","8","9","9","9","9",
				"10", "10", "10", "10", "11", "11", "11", "11", "12", "12", "12",
				"12", "13", "13", "13", "13", "14", "14", "14", "14", "15", "15",
				"15", "15", "16", "16", "16", "16", "17", "17", "17", "17", "18",
				"18", "18", "18", "19", "19", "19", "19", "20", "20", "20", "20",
				"21", "21", "21", "21", "22", "22", "22", "22", "23", "23", "23",
				"23", "24", "24", "24", "24", "25", "25", "25", "25", "26", "26",
				"26", "26", "27", "27", "27", "27", "28", "28", "28", "28", "29",
				"29", "29", "29", "30", "30", "30", "30", "31", "31", "31", "31"
		);
		int i = days.lastIndexOf(actualMaximum);
		String[] daysArray = (String[]) days.subList(0, i+1).toArray(new String[0]);

		//遍历waterTempVO,将没有监测的时间点的值设为'-'
		//一天中四个时间点分别是00，06，12，18
		int index = 0;
		for (String day: days){
			String dayTemp = day;
			if (index == 4){
				index = 0;
			}
			String hour = null;
			switch (index){
				case 0: hour = "00"; break;
				case 1: hour = "06"; break;
				case 2: hour = "12"; break;
				case 3: hour = "18"; break;
			}
			int count = 0;
			int size = waterTempVOList.size();
			for (WaterTempVO waterTempVO: waterTempVOList){
				if (dayTemp.length() == 1){
					day = "0" + dayTemp + " " + hour;
				}else {
					day = dayTemp + " " + hour;
				}

				String subTime = waterTempVO.getDetectionDate().substring(8, 13);//某个监测时间点
				//subTime = subTime.substring(0,1).contains("0") ?  : subTime : subTime.substring(1, 5);
				//判断两个时间是否是同一时间
				if (subTime.equals(day)){
					//是同一时刻，将测量值设置到折线图数据中
					avgTemperatureList.add(waterTempVO.getAvgTemperature());
					maxTemperatureList.add(waterTempVO.getMaxTemperature());
					minTemperatureList.add(waterTempVO.getMinTemperature());
					avgHumidityList.add(waterTempVO.getAvgHumidity());
					maxHumidityList.add(waterTempVO.getMaxHumidity());
					minHumidityList.add(waterTempVO.getMinHumidity());
					index++;
					break;
				}

				while ( ++count == size) {
					//不是同一时间，将折线图对应值设置为'-'
					avgTemperatureList.add('-');
					maxTemperatureList.add('-');
					minTemperatureList.add('-');
					avgHumidityList.add('-');
					maxHumidityList.add('-');
					minHumidityList.add('-');
					index++;
				}
			}
		}

		WaterTempCollectionVO waterTempCollectionVO = new WaterTempCollectionVO();
		waterTempCollectionVO.setAvgTemperatureArr(avgTemperatureList.toArray());
		waterTempCollectionVO.setMaxTemperatureArr(maxTemperatureList.toArray());
		waterTempCollectionVO.setMinTemperatureArr(minTemperatureList.toArray());
		waterTempCollectionVO.setAvgHumidityArr(avgHumidityList.toArray());
		waterTempCollectionVO.setMaxHumidityArr(maxHumidityList.toArray());
		waterTempCollectionVO.setMinHumidityArr(minHumidityList.toArray());

		resultMap.put("waterTempCollectionVO", waterTempCollectionVO);
		resultMap.put("daysArray", daysArray);

		return resultMap;
	}

    @Override
    public List<CableVO> selectDatasByCfbh(String cfbh, Integer historyId) {
        return waterDomain.selectDatasByCfbh(cfbh,historyId);
    }

}
