package com.unis.detectionnum.server.domain;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Handler;

import com.unis.detectionnum.common.vo.*;
import com.unis.detectionnum.server.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.unis.common.utils.LsBeanUtils;
import com.unis.detectionnum.server.dao.CableDao;
import com.unis.detectionnum.server.dao.DCfxxsjyDao;
import com.unis.detectionnum.server.dao.DetectionDataDao;
import com.unis.detectionnum.server.utils.Modbus4jUtils;
import com.unis.detectionnum.server.utils.RegisterWater;

/**
 * @ClassName: DetectionWaterDomain
 * @Description: TODO
 * @author LiuFeng
 * @date 2019年9月10日 下午3:15:34
 */
@Component
public class DetectionWaterDomain extends BaseDomain{

    @Autowired
    private CableDao cableDao;

    @Autowired
    private DetectionDataDao dataDao;

    @Autowired
    private DCfxxsjyDao cfxxsjyDao;

    private static final String CFBH = "911202236794497949001001";
    //private static final String CFBH = "9112022367944979490871";

    /**
     * @throws ModbusInitException
     * @throws ErrorResponseException
     * @throws ModbusTransportException
     * @Title: readWaterData
     * @Description: TODO
     * @param @return
     * @return ResponseWrapper
     * @throws
     */
    public int readWaterData() throws ModbusTransportException, ErrorResponseException, ModbusInitException {

        int result = 0;
        //1.根据获取仓房编号获取电缆列表
        List<CableDO> cableDOs = cableDao.selectByCfbh(CFBH);

        DetectionHistoryDO historyDO = new DetectionHistoryDO();
        historyDO.setCreateDate(new Date());
        historyDO.setCfbh(CFBH);
        historyDO.setCfmc("高大平房仓 25号仓");
        dataDao.insertHistory(historyDO);

        List<DetectionDataDO> dataDOs = new ArrayList<DetectionDataDO>();
        if(cableDOs != null && cableDOs.size() > 0) {
            //循环电缆列表
            for(int index = 0;index < cableDOs.size();index++) {
                //每条电缆的三个检测点
                for(int i = 0;i < 3;i++) {
                    //温度，湿度，测量值，NC值
                    Number temperature = Modbus4jUtils.readHoldingRegister(RegisterWater.SFJC_SLAVE_IP,RegisterWater.SFJC_SLAVE_PORT,RegisterWater.SFJC_SLAVE_ID, RegisterWater.SFJC_WD + 4 * i + 12 * index,DataType.TWO_BYTE_INT_SIGNED);
                    Number humidity = Modbus4jUtils.readHoldingRegister(RegisterWater.SFJC_SLAVE_IP,RegisterWater.SFJC_SLAVE_PORT,RegisterWater.SFJC_SLAVE_ID, RegisterWater.SFJC_SF + 4 * i + 12 * index,DataType.TWO_BYTE_INT_SIGNED);
                    Number measured = Modbus4jUtils.readHoldingRegister(RegisterWater.SFJC_SLAVE_IP,RegisterWater.SFJC_SLAVE_PORT,RegisterWater.SFJC_SLAVE_ID, RegisterWater.SFJC_CLZ + 4 * i + 12 * index,DataType.TWO_BYTE_INT_SIGNED);
                    Number measuredNc = Modbus4jUtils.readHoldingRegister(RegisterWater.SFJC_SLAVE_IP,RegisterWater.SFJC_SLAVE_PORT,RegisterWater.SFJC_SLAVE_ID, RegisterWater.SFJC_NC + 4 * i + 12 * index,DataType.TWO_BYTE_INT_SIGNED);
					/*
					Number temperature = 21.9;
					Number humidity = 19.7;
					Number measured = 20.8;
					Number measuredNc = 23;
					*/
                    DetectionDataDO dataDO = new DetectionDataDO();
                    dataDO.setCableId(cableDOs.get(index).getCableId());
                    dataDO.setTemperature(temperature.doubleValue());
                    dataDO.setHumidity(humidity.doubleValue());
                    dataDO.setMeasured(measured.intValue());
                    dataDO.setMeasuredNc(measuredNc.intValue());
                    dataDO.setCreateDate(new Date());
                    dataDO.setDetectorNo(i+1);
                    dataDO.setHistoryId(historyDO.getId());

                    dataDOs.add(dataDO);
                }

            }

        }
        result = dataDao.insertBatch(dataDOs);

        return result;
    }

    /**
     * @Title: getDetectionWaterReport
     * @Description: TODO
     * @param @param cfbh2
     * @param @return
     * @return List<DetectionWaterReportVO>
     * @throws
     */
    public DetectionWaterReportVO getDetectionWaterReport(String cfbh,Integer historyId) {

        DetectionWaterReportVO result = new DetectionWaterReportVO();

        //这一步设置了监测时间
        List<CableVO> dataVOs = cableDao.selectDatasByCfbh(cfbh,historyId);
        result.setCableDatas(dataVOs);

        DCfxxsjyDO cfxxsjyDO = cfxxsjyDao.getDCfxxxsjyInfo(cfbh);
        DCfxxsjyVO cfxxsjyVO = new DCfxxsjyVO();
        LsBeanUtils.copyAllProperties(cfxxsjyDO, cfxxsjyVO);
        result.setCfxxsjyVO(cfxxsjyVO);

        //获取温度和水分
        WaterStatisticsVO statisticsVO =  dataDao.getDataStatistics(cfbh,historyId);
        result.setStatisticsVO(statisticsVO);
        return result;
    }

    /**
     * @Title: getWaterHistory
     * @Description: TODO
     * @param @param cfbh2
     * @param @return
     * @return List<DetectionWaterHistoryVO>
     * @throws
     */
    public List<DetectionWaterHistoryVO> getWaterHistory(String cfbh, String time) {
        HashMap<Object, Object> selectMap = new HashMap<>();
        selectMap.put("cfbh", cfbh);
        selectMap.put("time", time);
        return cfxxsjyDao.getWaterHistory(selectMap);
    }

    /**
     * 通过CFBH获取温度，水分折线图数据
     * @param cfbh
     * @return
     */
    public List<WaterTempVO> getWaterTempView(String cfbh) {
        HashMap<Object, Object> selectMap = new HashMap<>();
        selectMap.put("cfbh", cfbh);
        //获取系统当前时间
        String currentTime = new SimpleDateFormat("yyyy-MM").format(new Date());
        selectMap.put("currentTime", currentTime);
        List<WaterTempVO> waterTempVOList = dataDao.getWaterTempView(selectMap);

        return waterTempVOList;
    }

    /**
     * 通过CFBH获取对应的监测时间
     * @param cfbh
     * @return
     */
    public List<String> getCreateTimes(String cfbh) {
        HashMap<Object, Object> selectMap = new HashMap<>();
        selectMap.put("cfbh", cfbh);
        //获取系统当前时间
        String currentTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        selectMap.put("currentTime", currentTime);
        return dataDao.getCreateTimes(selectMap);
    }

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