package com.njcn.serviceimpl.configuration;

import com.njcn.enums.PlateRateEnum;
import com.njcn.mapper.configuration.DeviceMapper;
import com.njcn.mapper.plate.PlateDataMapper;
import com.njcn.mapper.plate.PlateReportDataMapper;
import com.njcn.pojo.configuration.PlateResult;
import com.njcn.pojo.configuration.PlateResultDetail;
import com.njcn.pojo.plate.PlateData;
import com.njcn.pojo.plate.PlateReportData;
import com.njcn.pojo.plate.PlateReportDetail;
import com.njcn.service.configuration.PlateResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.PreparedStatement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

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

@Service
public class PlateResultServiceImpl implements PlateResultService {
    @Autowired
    PlateDataMapper plateDataMapper;
    @Autowired
    DeviceMapper deviceMapper;
    @Autowired
    PlateReportDataMapper plateReportDataMapper;

    @Override
    public PlateResult getPlateResult(String startTime, String endTime, Long lineIndex, String type, String number, Integer condition) {
        PlateResult historyResult = new PlateResult();
        List<PlateData> result = new ArrayList<>();
        if (type.equals("getZYGDNValue")) {
            result = plateDataMapper.getZYGDNValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXZYGDNValue")) {
            result = plateDataMapper.getSXZYGDNValue(lineIndex, startTime, endTime);
        } else if (type.equals("getFYGDNValue")) {
            result = plateDataMapper.getFYGDNValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXFYGDNValue")) {
            result = plateDataMapper.getSXFYGDNValue(lineIndex, startTime, endTime);
        } else if (type.equals("getZHYGDNValue")) {
            result = plateDataMapper.getZHYGDNValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXZHYGDNValue")) {
            result = plateDataMapper.getSXZHYGDNValue(lineIndex, startTime, endTime);
        } else if (type.equals("getXOneWGValue")) {
            result = plateDataMapper.getXOneWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXXOneWGValue")) {
            result = plateDataMapper.getSXXOneWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getXTwoWGValue")) {
            result = plateDataMapper.getXTwoWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXXTwoWGValue")) {
            result = plateDataMapper.getSXXTwoWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getXThreeWGValue")) {
            result = plateDataMapper.getXThreeWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXXThreeWGValue")) {
            result = plateDataMapper.getSXXThreeWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getXFourWGValue")) {
            result = plateDataMapper.getXFourWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXXFourWGValue")) {
            result = plateDataMapper.getSXXFourWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getZHOneValue")) {
            result = plateDataMapper.getZHOneValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXZHOneValue")) {
            result = plateDataMapper.getSXZHOneValue(lineIndex, startTime, endTime);
        } else if (type.equals("getZHTwoValue")) {
            result = plateDataMapper.getZHTwoValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXZHTwoValue")) {
            result = plateDataMapper.getSXZHTwoValue(lineIndex, startTime, endTime);
        } else if (type.equals("getZSZDNValue")) {
            result = plateDataMapper.getZSZDNValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXZSZDNValue")) {
            result = plateDataMapper.getSXZSZDNValue(lineIndex, startTime, endTime);
        } else if (type.equals("getFSZDNValue")) {
            result = plateDataMapper.getFSZDNValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXFSZDNValue")) {
            result = plateDataMapper.getSXFSZDNValue(lineIndex, startTime, endTime);
        } else if (type.equals("getZJBYGValue")) {
            result = plateDataMapper.getZJBYGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXZJBYGValue")) {
            result = plateDataMapper.getSXZJBYGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getFJBYGValue")) {
            result = plateDataMapper.getFJBYGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXFJBYGValue")) {
            result = plateDataMapper.getSXFJBYGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getZHJBYGValue")) {
            result = plateDataMapper.getZHJBYGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXZHJBYGValue")) {
            result = plateDataMapper.getSXZHJBYGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXXOneJBWGValue")) {
            result = plateDataMapper.getSXXOneJBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getXOneJBWGValue")) {
            result = plateDataMapper.getXOneJBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXXOneJBWGValue")) {
            result = plateDataMapper.getSXXOneJBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getXTwoJBWGValue")) {
            result = plateDataMapper.getXTwoJBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXXTwoJBWGValue")) {
            result = plateDataMapper.getSXXTwoJBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getXThreeJBWGValue")) {
            result = plateDataMapper.getXThreeJBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXXThreeJBWGValue")) {
            result = plateDataMapper.getSXXThreeJBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getXFourJBWGValue")) {
            result = plateDataMapper.getXFourJBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXXFourJBWGValue")) {
            result = plateDataMapper.getSXXFourJBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getZHOneJBValue")) {
            result = plateDataMapper.getZHOneJBValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXZHOneJBValue")) {
            result = plateDataMapper.getSXZHOneJBValue(lineIndex, startTime, endTime);
        } else if (type.equals("getZHTwoJBValue")) {
            result = plateDataMapper.getZHTwoJBValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXZHTwoJBValue")) {
            result = plateDataMapper.getSXZHTwoJBValue(lineIndex, startTime, endTime);
        } else if (type.equals("getZZXBYGValue")) {
            result = plateDataMapper.getZZXBYGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXZZXBYGValue")) {
            result = plateDataMapper.getSXZZXBYGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getFZXBYGValue")) {
            result = plateDataMapper.getFZXBYGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXFZXBYGValue")) {
            result = plateDataMapper.getSXFZXBYGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getZHZXBYGValue")) {
            result = plateDataMapper.getZHZXBYGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXZHZXBYGValue")) {
            result = plateDataMapper.getSXZHZXBYGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getXOneZXBWGValue")) {
            result = plateDataMapper.getXOneZXBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXXOneZXBWGValue")) {
            result = plateDataMapper.getSXXOneZXBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getXTwoZXBWGValue")) {
            result = plateDataMapper.getXTwoZXBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXXTwoZXBWGValue")) {
            result = plateDataMapper.getSXXTwoZXBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getXThreeZXBWGValue")) {
            result = plateDataMapper.getXThreeZXBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXXThreeZXBWGValue")) {
            result = plateDataMapper.getSXXThreeZXBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getXFourZXBWGValue")) {
            result = plateDataMapper.getXFourZXBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXXFourZXBWGValue")) {
            result = plateDataMapper.getSXXFourZXBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getZHOneZXBWGValue")) {
            result = plateDataMapper.getZHOneZXBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXZHOneZXBWGValue")) {
            result = plateDataMapper.getSXZHOneZXBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getZHTwoZXBWGValue")) {
            result = plateDataMapper.getZHTwoZXBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getSXZHTwoZXBWGValue")) {
            result = plateDataMapper.getSXZHTwoZXBWGValue(lineIndex, startTime, endTime);
        } else if (type.equals("getZXBYGValue")) {
            result = plateDataMapper.getZXBYGValue(number, lineIndex, startTime, endTime);
        } else if (type.equals("getFXBYGValue")) {
            result = plateDataMapper.getFXBYGValue(number, lineIndex, startTime, endTime);
        } else if (type.equals("getXOneXBWGValue")) {
            result = plateDataMapper.getXOneXBWGValue(number, lineIndex, startTime, endTime);
        } else if (type.equals("getXTwoXBWGValue")) {
            result = plateDataMapper.getXTwoXBWGValue(number, lineIndex, startTime, endTime);
        } else if (type.equals("getXThreeXBWGValue")) {
            result = plateDataMapper.getXThreeXBWGValue(number, lineIndex, startTime, endTime);
        } else if (type.equals("getXFourXBWGValue")) {
            result = plateDataMapper.getXFourXBWGValue(number, lineIndex, startTime, endTime);
        } else if (type.equals("getZYGXLValue")) {
            result = plateDataMapper.getZYGXLValue(lineIndex, startTime, endTime);
        } else if (type.equals("getFYGXLValue")) {
            result = plateDataMapper.getFYGXLValue(lineIndex, startTime, endTime);
        } else if (type.equals("getZHYGXLValue")) {
            result = plateDataMapper.getZHYGXLValue(lineIndex, startTime, endTime);
        } else if (type.equals("getXOneWGXLValue")) {
            result = plateDataMapper.getXOneWGXLValue(lineIndex, startTime, endTime);
        } else if (type.equals("getXTwoWGXLValue")) {
            result = plateDataMapper.getXTwoWGXLValue(lineIndex, startTime, endTime);
        } else if (type.equals("getXThreeWGXLValue")) {
            result = plateDataMapper.getXThreeWGXLValue(lineIndex, startTime, endTime);
        } else if (type.equals("getXFourWGXLValue")) {
            result = plateDataMapper.getXFourWGXLValue(lineIndex, startTime, endTime);
        } else if (type.equals("getZHOneWGXLValue")) {
            result = plateDataMapper.getZHOneWGXLValue(lineIndex, startTime, endTime);
        } else if (type.equals("getZHTwoWGXLValue")) {
            result = plateDataMapper.getZHTwoWGXLValue(lineIndex, startTime, endTime);
        }
        int pttype = deviceMapper.getConnect(lineIndex);
        if (!result.isEmpty()) {
            if (condition == 111 || condition == 121 || condition == 131 || condition == 141 || condition == 151
                    || condition == 161 || condition == 171) {
                for (int i = 0; i < result.size(); i++) {
                    List<Double> list = new ArrayList<>();
                    list.add(Double.valueOf(lineIndex));//保存当前监测点
                    list.add(Double.valueOf(result.get(i).getTimeId().getTime()));
                    list.add(result.get(i).getfAvrValue());//三相总平均值
                    list.add(Double.valueOf(pttype));
                    historyResult.getData().add(list);
                }
            } else {
                for (int i = 0; i < result.size(); i++) {
                    List<Double> list = new ArrayList<>();
                    list.add(Double.valueOf(lineIndex));//保存当前监测点
                    list.add(Double.valueOf(result.get(i).getTimeId().getTime()));
                    list.add(result.get(i).getaAvrValue());//a相平均值
                    list.add(result.get(i).getbAvrValue());//b相平均值
                    list.add(result.get(i).getcAvrValue());//c相平均值
                    list.add(Double.valueOf(pttype));
                    historyResult.getData().add(list);
                }
            }
        }
        return historyResult;
    }

    @Override
    public PlateReportDetail getPlateReportResult(String startTime, String endTime, Long lineIndex, String syllableName, Integer condition, Integer timeType) throws ParseException {
        List<PlateReportData> plateReportData = new ArrayList<>();
        PlateReportDetail plateReportDetail = new PlateReportDetail();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM-dd");
        switch (timeType) {
            case 1:
                plateReportData = plateReportDataMapper.getYGDNMonthValue(PlateRateEnum.rateA.getName(), PlateRateEnum.rateB.getName(),
                        PlateRateEnum.rateC.getName(), PlateRateEnum.rateD.getName(), lineIndex, syllableName, startTime, endTime);
                break;
            case 2:
                plateReportData = plateReportDataMapper.getYGDNYearValue(PlateRateEnum.rateA.getName(), PlateRateEnum.rateB.getName(),
                        PlateRateEnum.rateC.getName(), PlateRateEnum.rateD.getName(), lineIndex, syllableName, startTime, endTime);
                break;
            case 3:
                plateReportData = plateReportDataMapper.getYGDNYearValue(PlateRateEnum.rateA.getName(), PlateRateEnum.rateB.getName(),
                        PlateRateEnum.rateC.getName(), PlateRateEnum.rateD.getName(), lineIndex, syllableName, startTime, endTime);
                break;
            default:
                break;
        }

        if (!plateReportData.isEmpty()) {
            List<String> timeid = new ArrayList<>();
            String monthYear = "";
            List<PlateResultDetail> plateResultDetailList = new ArrayList<>();
            PlateResultDetail plateResultDetailRateA = new PlateResultDetail();
            PlateResultDetail plateResultDetailRateB = new PlateResultDetail();
            PlateResultDetail plateResultDetailRateC = new PlateResultDetail();
            PlateResultDetail plateResultDetailRateD = new PlateResultDetail();
            plateResultDetailRateA.setName(PlateRateEnum.rateA.getName());
            plateResultDetailRateB.setName(PlateRateEnum.rateB.getName());
            plateResultDetailRateC.setName(PlateRateEnum.rateC.getName());
            plateResultDetailRateD.setName(PlateRateEnum.rateD.getName());
            List<Double> plateResultDetailDataRateA = new ArrayList<>();
            List<Double> plateResultDetailDataRateB = new ArrayList<>();
            List<Double> plateResultDetailDataRateC = new ArrayList<>();
            List<Double> plateResultDetailDataRateD = new ArrayList<>();
            switch (timeType) {
                case 1:
                    for (int i = 0; i < plateReportData.size(); i++) {
                        Calendar calendar = Calendar.getInstance();
                        Date date = sdf.parse(plateReportData.get(i).getTimeId());
                        calendar.setTime(date);
                        // 获取当前年
                        int year = calendar.get(Calendar.YEAR);
                        // 获取当前月
                        int month = calendar.get(Calendar.MONTH) + 1;
                        String months = "";
                        if (month < 10) {
                            months = "0" + month;
                        } else {
                            months = String.valueOf(month);
                        }
                        // 获取当前日
                        int day = calendar.get(Calendar.DATE);
                        String days = "";
                        if (day < 10) {
                            days += "0" + day;
                        }else {
                            days = String.valueOf(day);
                        }
                        timeid.add(days);
                        monthYear = year + "-" + months;
                        plateResultDetailDataRateA.add(plateReportData.get(i).getTrorseValue());
                        plateResultDetailDataRateB.add(plateReportData.get(i).getRetrorseValue());
                        plateResultDetailDataRateC.add(plateReportData.get(i).getComposeValue());
                        plateResultDetailDataRateD.add(plateReportData.get(i).getOneQuadrantValue());
                    }
                    plateReportDetail.setMonthYear(monthYear);
                    plateReportDetail.setTimeId(timeid);
                    plateResultDetailRateA.setData(plateResultDetailDataRateA);
                    plateResultDetailRateB.setData(plateResultDetailDataRateB);
                    plateResultDetailRateC.setData(plateResultDetailDataRateC);
                    plateResultDetailRateD.setData(plateResultDetailDataRateD);
                    plateResultDetailList.add(plateResultDetailRateA);
                    plateResultDetailList.add(plateResultDetailRateB);
                    plateResultDetailList.add(plateResultDetailRateC);
                    plateResultDetailList.add(plateResultDetailRateD);
                    plateReportDetail.setPlateResultDetail(plateResultDetailList);
                    break;
                case 2:
                    for (int i = 0; i < plateReportData.size(); i++) {
                        String[] timeID = plateReportData.get(i).getTimeId().split("-");
                        // 获取当前年
                        int year = Integer.parseInt(timeID[0]);
                        // 获取当前月
                        int month = Integer.parseInt(timeID[1]);
                        String months = "";
                        if (month < 10) {
                            months = "0" + month;
                        } else {
                            months = String.valueOf(month);
                        }
                        timeid.add(String.valueOf(months));
                        monthYear = String.valueOf(year);
                        plateResultDetailDataRateA.add(plateReportData.get(i).getTrorseValue());
                        plateResultDetailDataRateB.add(plateReportData.get(i).getRetrorseValue());
                        plateResultDetailDataRateC.add(plateReportData.get(i).getComposeValue());
                        plateResultDetailDataRateD.add(plateReportData.get(i).getOneQuadrantValue());
                    }
                    plateReportDetail.setMonthYear(monthYear);
                    plateReportDetail.setTimeId(timeid);
                    plateReportDetail.setMonthYear(monthYear);
                    plateReportDetail.setTimeId(timeid);
                    plateResultDetailRateA.setData(plateResultDetailDataRateA);
                    plateResultDetailRateB.setData(plateResultDetailDataRateB);
                    plateResultDetailRateC.setData(plateResultDetailDataRateC);
                    plateResultDetailRateD.setData(plateResultDetailDataRateD);
                    plateResultDetailList.add(plateResultDetailRateA);
                    plateResultDetailList.add(plateResultDetailRateB);
                    plateResultDetailList.add(plateResultDetailRateC);
                    plateResultDetailList.add(plateResultDetailRateD);
                    plateReportDetail.setPlateResultDetail(plateResultDetailList);
                    break;
                case 3:
                    for (int i = 0; i < plateReportData.size(); i++) {
                        String[] timeID = plateReportData.get(i).getTimeId().split("-");
                        // 获取当前年
                        int year = Integer.parseInt(timeID[0]);
                        // 获取当前月
                        int month = Integer.parseInt(timeID[1]);
                        String months = "";
                        if (month < 10) {
                            months = "0" + month;
                        } else {
                            months = String.valueOf(month);
                        }
                        timeid.add(String.valueOf(months));
                        monthYear = String.valueOf(year);
                        plateResultDetailDataRateA.add(plateReportData.get(i).getTrorseValue());
                        plateResultDetailDataRateB.add(plateReportData.get(i).getRetrorseValue());
                        plateResultDetailDataRateC.add(plateReportData.get(i).getComposeValue());
                        plateResultDetailDataRateD.add(plateReportData.get(i).getOneQuadrantValue());
                    }
                    plateReportDetail.setMonthYear(monthYear);
                    plateReportDetail.setTimeId(timeid);
                    plateResultDetailRateA.setData(plateResultDetailDataRateA);
                    plateResultDetailRateB.setData(plateResultDetailDataRateB);
                    plateResultDetailRateC.setData(plateResultDetailDataRateC);
                    plateResultDetailRateD.setData(plateResultDetailDataRateD);
                    plateResultDetailList.add(plateResultDetailRateA);
                    plateResultDetailList.add(plateResultDetailRateB);
                    plateResultDetailList.add(plateResultDetailRateC);
                    plateResultDetailList.add(plateResultDetailRateD);
                    plateReportDetail.setPlateResultDetail(plateResultDetailList);
                    break;
                default:
                    break;
            }
        }
        return plateReportDetail;
    }
}
