package com.njcn.serviceimpl.excelreport;

import com.njcn.mapper.excelreport.GetVdeviationMapper;
import com.njcn.pojo.excelreport.ConditionData;
import com.njcn.pojo.excelreport.ReportValueData;
import com.njcn.service.excelreport.GetVdeviationService;
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 GetVdeviationServiceImpl implements GetVdeviationService {
    @Autowired
    private GetVdeviationMapper getVdeviationMapper;

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

        //获取电压偏差
        List<Map<String, Object>> lists = getVdeviationMapper.getVdeviationData(conditionData);

        if (0 < lists.size() && null != lists.get(0)) {
            transformData(listU, listL, lists);
        }

        Regroup.regroupData(listU, true);
        Regroup.regroupData(listL, true);
        list.addAll(listU);
        list.addAll(listL);

        return list;
    }

    @Override
    public List<ReportValueData> getVdeviationData(ConditionData conditionData) {
        List<ReportValueData> listU = new ArrayList<>();
        List<ReportValueData> listL = new ArrayList<>();
        List<Map<String, Object>> lists = getVdeviationMapper.getVdeviationData(conditionData);

        if (0 < lists.size() && null != lists.get(0)) {
            transformData(listU, listL, lists);
        }

        Regroup.regroupData(listU, true);
        return listU;
    }

    private void transformData(List<ReportValueData> listU, List<ReportValueData> listL, List<Map<String, Object>> list) {
        for (Map<String, Object> map : list) {
            ReportValueData uValue = new ReportValueData();
            ReportValueData lValue = new ReportValueData();
            setData("U", listU, map, uValue);
            setData("L", listL, map, lValue);
        }
    }

    private void setData(String str, List<ReportValueData> list, Map<String, Object> map, ReportValueData reportValueData) {
        reportValueData.setPhaseType(map.get("PHASETYPE").toString());
        reportValueData.setFmaxValue(new Float(((BigDecimal) map.get("V" + str + "_DEV_MAX")).floatValue()));
        reportValueData.setMinValue(new Float(((BigDecimal) map.get("V" + str + "_DEV_MIN")).floatValue()));
        reportValueData.setMeanValue(new Float(((BigDecimal) map.get("V" + str + "_DEV")).floatValue()));

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

        list.add(reportValueData);
    }
}
