package com.njcn.serviceimpl.excelreport;

import com.njcn.mapper.excelreport.GetThreephaseMapper;
import com.njcn.pojo.excelreport.ConditionData;
import com.njcn.pojo.excelreport.ReportValueData;
import com.njcn.service.excelreport.GetThreephaseService;
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 GetThreephaseServiceImpl implements GetThreephaseService {
    @Autowired
    private GetThreephaseMapper getThreephaseMapper;

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

        //电压三相不平衡度
        List<Map<String, Object>> listU = getThreephaseMapper.getThreephase(conditionData, "V");
        //电流三相不平衡度
        List<Map<String, Object>> listI = getThreephaseMapper.getThreephase(conditionData, "I");

        if (0 < listU.size() && null != listU.get(0)) {
            transformData(list, listU, "V");
        } else {
            regroupData(list);
        }

        if (0 < listI.size() && null != listI.get(0)) {
            transformData(list, listI, "I");
        } else {
            regroupData(list);
        }

        return list;
    }

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

    private void transformData(List<ReportValueData> list, List<Map<String, Object>> listThreephase, String str) {
        for (Map<String, Object> map : listThreephase) {
            ReportValueData reportValueData1 = new ReportValueData();
            ReportValueData reportValueData2 = new ReportValueData();
            ReportValueData reportValueData3 = new ReportValueData();
            ReportValueData reportValueData4 = new ReportValueData();
            setData(str, "UNBALANCE", list, map, reportValueData1);
            setData(str, "POS", list, map, reportValueData2);
            setData(str, "NEG", list, map, reportValueData3);
            setData(str, "ZERO", list, map, reportValueData4);
        }
    }

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

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

        list.add(reportValueData);
    }
}
