package com.njcn.serviceimpl.configuration;

import com.njcn.mapper.configuration.DeviceMapper;
import com.njcn.pojo.commons.HistoryData;
import com.njcn.pojo.configuration.HistoryResult;
import com.njcn.service.configuration.DeviceService;
import com.njcn.service.configuration.HistoryResultService;
import com.njcn.service.data.EventDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @autor denghuajun
* @Date 2018/8/3 9:46
*/

@Service
public class HistoryResultServiceImpl implements HistoryResultService {
    @Autowired
    DeviceService deviceService;
    @Resource
    DeviceMapper deviceMapper;
    @Autowired
    EventDetailService eventDetailService;
    @Override
    public HistoryResult getHistroyResult(String startTime, String endTime, Long lineIndex, String start, String end,
                                          String type, String number,int num,int g,int condition,int flagday,int dataType) {
        HistoryResult historyResult=new HistoryResult();

        List<HistoryData> historyData=deviceService.getHistoryData(startTime,endTime,lineIndex,start,end,type,number,flagday,dataType);
        int pttype = deviceMapper.getConnect(lineIndex);
        if(!historyData.isEmpty()){
            if(type.equals("getSxbphd")||type.equals("getPl")||type.equals("getYgzgl")||type.equals("getWgzgl")||type.equals("getSzzgl")
            ||type.equals("getZSzglys")||type.equals("getZWyglys")||type.equals("getFxdl")){
                for(int i=0;i<historyData.size();i++){
                    List<Double> list=new ArrayList<>();
                    list.add(Double.valueOf(condition));
                    list.add(Double.valueOf(historyData.get(i).getTimeId().getTime()));
                    list.add(historyData.get(i).getfAvrValue());
                    list.add(historyData.get(i).getfMaxValue());
                    list.add(historyData.get(i).getfMinValue());
                    list.add(historyData.get(i).getfCP95Value());
                    list.add(Double.valueOf(pttype));
                    if(num==1){
                        historyResult.getData1().add(list);
                    }
                    if(num==2){
                        historyResult.getData2().add(list);
                    }
                    if(num==3){
                        historyResult.getData3().add(list);
                    }
                }
            }else {
                for(int i=0;i<historyData.size();i++){
                    List<Double> list=new ArrayList<>();
                    list.add(Double.valueOf(condition));//保存当前监测点
                    list.add(Double.valueOf(historyData.get(i).getTimeId().getTime()));
                    list.add(historyData.get(i).getaAvrValue());//a相平均值
                    list.add(historyData.get(i).getbAvrValue());//b相平均值
                    list.add(historyData.get(i).getcAvrValue());//c相平均值

                    list.add(historyData.get(i).getaMinValue());//a相最小值
                    list.add(historyData.get(i).getbMinValue());//b相最小值
                    list.add(historyData.get(i).getcMinValue());//c相最小值

                    list.add(historyData.get(i).getaMaxValue());//a相最大值
                    list.add(historyData.get(i).getbMaxValue());//b相最大值
                    list.add(historyData.get(i).getcMaxValue());//c相最大值

                    list.add(historyData.get(i).getaCP95Value());//a相cp95值
                    list.add(historyData.get(i).getbCP95Value());//b相cp95值
                    list.add(historyData.get(i).getcCP95Value());//c相cp95值

                    list.add(Double.valueOf(pttype));

                    if(num==1){
                        historyResult.getData1().add(list);
                    }
                    if(num==2){
                        historyResult.getData2().add(list);
                    }
                    if(num==3){
                        historyResult.getData3().add(list);
                    }
                }
            }
        }
        return historyResult;
    }
}
