package com.pqs9000.serviceimpl.report;

import com.njcn.utils.PubUtils;
import com.pqs9000.mapper.report.GetFrequencyDataMapper;
import com.pqs9000.pojo.report.Condition;
import com.pqs9000.pojo.report.ReportValue;
import com.pqs9000.service.report.GetFrequencyDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class GetFrequencyDataServiceImpl implements GetFrequencyDataService {
    private static final Logger logger = LoggerFactory.getLogger(GetFrequencyDataServiceImpl.class);
    @Autowired
    private GetFrequencyDataMapper getFrequencyDataMapper;

    @Override
    public List<ReportValue> getFrequencyData(Condition condition) {
        List<ReportValue> list = new ArrayList<>();

        List<Map<String, Object>> listFre = getFrequencyDataMapper.getFrequencyData(condition);

        if (0 < listFre.size() && null != listFre.get(0)) {
            transformData(list, listFre);
        }

        if (0 == list.size()) {
            for (int i = 0; i < 2; i++) {
                List<ReportValue> list1 = new ArrayList<>();
                RegroupData.regroupData(list1, false);
                list.addAll(list1);
            }
        }

        return list;
    }

    @Override
    public List<ReportValue> getFreqData(Condition condition) {
        List<ReportValue> list = new ArrayList<>();
        List<Map<String, Object>> listFre = getFrequencyDataMapper.getHFrequencyData(condition);

        if (0 < listFre.size() && null != listFre.get(0)) {
            transform(list, listFre);
        }

        return list;
    }

    private void transform(List<ReportValue> list1, List<Map<String, Object>> list) {
        for (Map<String, Object> map : list) {
            //频率
            ReportValue reportValue1 = new ReportValue();
            setHData(reportValue1, "_DEV", list1, map);
        }
    }

    private void setHData(ReportValue reportValue, String str, List<ReportValue> list, Map<String, Object> map) {
        reportValue.setFmaxValue(Float.parseFloat(map.get("FREQ" + str + "_MAX").toString()));
        reportValue.setMinValue(Float.parseFloat(map.get("FREQ" + str + "_MIN").toString()));
        reportValue.setMeanValue(Float.parseFloat(map.get("FREQ" + str).toString()));

        try {
            reportValue.setStrTime(PubUtils.date2String((Date) map.get("TIMEID"), "yyyy-MM-dd"));
        } catch (Exception e) {
           logger.error(e.getMessage());
        }

        if (map.containsKey("FREQ" + str + "_CP95")) {
            reportValue.setCp95Value(Float.parseFloat(map.get("FREQ" + str + "_CP95").toString()));
        } else {
            reportValue.setCp95Value(null);
        }

        list.add(reportValue);
    }


    private void transformData(List<ReportValue> list1, List<Map<String, Object>> list) {
        for (Map<String, Object> map : list) {
            //频率
            ReportValue reportValue1 = new ReportValue();
            //频率偏差
            ReportValue reportValue2 = new ReportValue();

            setData(reportValue1, "", list1, map);
            setData(reportValue2, "_DEV", list1, map);
        }
    }

    private void setData(ReportValue reportValue, String str, List<ReportValue> list, Map<String, Object> map) {
        reportValue.setFmaxValue(Float.parseFloat(map.get("FREQ" + str + "_MAX").toString()));
        reportValue.setMinValue(Float.parseFloat(map.get("FREQ" + str + "_MIN").toString()));
        reportValue.setMeanValue(Float.parseFloat(map.get("FREQ" + str).toString()));

        if (map.containsKey("FREQ" + str + "_CP95")) {
            reportValue.setCp95Value(Float.parseFloat(map.get("FREQ" + str + "_CP95").toString()));
        } else {
            reportValue.setCp95Value(null);
        }

        list.add(reportValue);
    }
}
