package com.pqs9000.serviceimpl.report;

import com.njcn.utils.PubUtils;
import com.pqs9000.mapper.report.GetVdeviationDataMapper;
import com.pqs9000.pojo.report.Condition;
import com.pqs9000.pojo.report.ReportValue;
import com.pqs9000.service.report.GetVdeviationDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class GetVdeviationDataServiceImpl implements GetVdeviationDataService {
    @Autowired
    private GetVdeviationDataMapper getVdeviationDataMapper;

    @Override
    public List<ReportValue> getVdeviation(Condition condition) {
        List<ReportValue> list = new ArrayList<>();
        List<ReportValue> listU = new ArrayList<>();
        List<ReportValue> listL = new ArrayList<>();

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

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

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

        return list;
    }

    @Override
    public List<ReportValue> getVdeviationData(Condition condition) {
        List<ReportValue> listU = new ArrayList<>();
        List<ReportValue> listL = new ArrayList<>();
        List<Map<String, Object>> lists = getVdeviationDataMapper.getHVdeviationData(condition);

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

        return listU;
    }

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

    private void setHData(String str, List<ReportValue> list, Map<String, Object> map, ReportValue reportValue) {
        reportValue.setPhaseType(map.get("PHASETYPE").toString());
        try {
            reportValue.setStrTime(PubUtils.date2String((Date) map.get("TIMEID"), "yyyy-MM-dd"));
        } catch (Exception e) {

        }
        reportValue.setFmaxValue(Float.parseFloat(map.get("V" + str + "_DEV_MAX").toString()));
        reportValue.setMinValue(Float.parseFloat(map.get("V" + str + "_DEV_MIN").toString()));
        reportValue.setMeanValue(Float.parseFloat(map.get("V" + str + "_DEV").toString()));

        if (map.containsKey("V" + str + "_DEV_CP95")) {
            reportValue.setCp95Value(Float.parseFloat(map.get("V" + str + "_DEV_CP95").toString()));
        } else {
            reportValue.setCp95Value(null);
        }

        list.add(reportValue);
    }

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

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

        if (map.containsKey("V" + str + "_DEV_CP95")) {
            reportValue.setCp95Value(Float.parseFloat(map.get("V" + str + "_DEV_CP95").toString()));
        } else {
            reportValue.setCp95Value(null);
        }

        list.add(reportValue);
    }
}
