package com.njcn.serviceimpl.excelreport;

import com.njcn.mapper.excelreport.GetDistortionMapper;
import com.njcn.mapper.excelreport.GetInVoltageRateDataMapper;
import com.njcn.pojo.excelreport.ConditionData;
import com.njcn.pojo.excelreport.ReportValueData;
import com.njcn.service.excelreport.GetInVoltageRateDataService;
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 GetInVoltageRateDataServiceImpl implements GetInVoltageRateDataService {
    @Autowired
    private GetInVoltageRateDataMapper getInVoltageRateDataMapper;
    @Autowired
    private GetDistortionMapper getDistortionMapper;

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

        for (int i = 2; i < 51; i++) {
            listValue.add(new Integer(i));
        }

        //获取基波电压幅值，单位kV
        List<ReportValueData> listFirstHarmonic = getInVoltageRateDataMapper.getInFirstHarmonic(conditionData);

        if (0 == listFirstHarmonic.size() || listFirstHarmonic.get(0) == null) {
            Regroup.regroupData(list, true, true);
        } else {
            list.addAll(getInVoltageRateDataMapper.getInFirstHarmonic(conditionData));
        }

        //获取间谐波电压含有率
        List<Map<String, Object>> listRate = getInVoltageRateDataMapper.getInVoltageRate(conditionData, listValue);

        if (0 == listRate.size() || null == listRate.get(0)) {
            for (int i = 0; i < 49; i++) {
                Regroup.regroupData(list, true, true);
            }
        } else {
            transformData(list, listRate, listValue);
        }

        //获取电压畸变率
        List<ReportValueData> listU = getDistortionMapper.getDistortionData(conditionData, "V");
        Regroup.regroupData(listU, true);
        list.addAll(listU);

        return list;
    }

    private void transformData(List<ReportValueData> list, List<Map<String, Object>> maps, List<Integer> listValue) {
        for (Integer integer : listValue) {
            for (Map<String, Object> map : maps) {
                ReportValueData reportValueData = new ReportValueData();
                reportValueData.setPhaseType(map.get("PHASETYPE").toString());
                reportValueData.setFmaxValue(new Float(((BigDecimal) map.get("V_" + integer + "_MAX")).floatValue()));
                reportValueData.setMinValue(new Float(((BigDecimal) map.get("V_" + integer + "_MIN")).floatValue()));
                reportValueData.setMeanValue(new Float(((BigDecimal) map.get("V_" + integer)).floatValue()));

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

                list.add(reportValueData);
            }
        }
    }
}
