package com.pqs9300.serviceimpl.report;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pqs9300.mapper.report.GetDistortionDataMapper;
import com.pqs9300.mapper.report.GetInVoltageRateMapper;
import com.pqs9300.pojo.report.Condition;
import com.pqs9300.pojo.report.ReportValue;
import com.pqs9300.service.report.GetInVoltageRateService;

@Service
public class GetInVoltageRateServiceImpl implements GetInVoltageRateService {
    @Autowired
    private GetInVoltageRateMapper getInVoltageRateMapper;
    @Autowired
    private GetDistortionDataMapper getDistortionDataMapper;

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

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

        //获取基波电压幅值，单位kV
        List<ReportValue> listFirstHarmonic = getInVoltageRateMapper.getInFirstHarmonic(condition);

        if (0 == listFirstHarmonic.size() || listFirstHarmonic.get(0) == null) {
            RegroupData.regroupData(list, true, true);
        } else {
            list.addAll(getInVoltageRateMapper.getInFirstHarmonic(condition));
        }

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

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

        //获取电压畸变率
        List<ReportValue> listU = getDistortionDataMapper.getDistortionData(condition, "V");
        RegroupData.regroupData(listU, true);
        list.addAll(listU);

        return list;
    }

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

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

                list.add(reportValue);
            }
        }
    }
}
