package com.njcn.serviceimpl.excelreport;

import com.njcn.mapper.excelreport.GetFrequencyMapper;
import com.njcn.pojo.excelreport.ConditionData;
import com.njcn.pojo.excelreport.ReportValueData;
import com.njcn.service.excelreport.GetFrequencyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class GetFrequencyServiceImpl implements GetFrequencyService {
    @Autowired
    private GetFrequencyMapper getFrequencyMapper;

    @Override
    public List<ReportValueData> getFrequencyData(ConditionData conditionData) {
        List<ReportValueData> list = new ArrayList<>();

        List<Map<String, Object>> listFre = getFrequencyMapper.getFrequencyData(conditionData);

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

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

        return list;
    }

    @Override
    public List<ReportValueData> getFreqData(ConditionData conditionData) {
        List<ReportValueData> list = new ArrayList<>();
        List<Map<String, Object>> listFre = getFrequencyMapper.getFrequencyData(conditionData);

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

        if (0 == list.size()) {
            List<ReportValueData> list1 = new ArrayList<>();
            Regroup.regroupData(list1, false);
            list.addAll(list1);
        }

        return list;
    }

    private void transform(List<ReportValueData> list1, List<Map<String, Object>> list) {
        for (Map<String, Object> map : list) {
            //频率
            ReportValueData reportValueData1 = new ReportValueData();
            setData(reportValueData1, "", list1, map);
        }
    }


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

            setData(reportValueData1, "", list1, map);
            setData(reportValueData2, "_DEV", list1, map);
        }
    }

    private void setData(ReportValueData reportValueData, String str, List<ReportValueData> list, Map<String, Object> map) {
        reportValueData.setFmaxValue(new Float(((BigDecimal) map.get("FREQ" + str + "_MAX")).floatValue()));
        reportValueData.setMinValue(new Float(((BigDecimal) map.get("FREQ" + str + "_MIN")).floatValue()));
        reportValueData.setMeanValue(new Float(((BigDecimal) map.get("FREQ" + str)).floatValue()));

        if (map.containsKey("FREQ" + str + "_CP95")) {
            reportValueData.setCp95Value(new Float(((BigDecimal) map.get("FREQ" + str + "_CP95")).floatValue()));
        } else {
            reportValueData.setCp95Value(null);
        }

        list.add(reportValueData);
    }
}
