package com.pqs9300.serviceimpl.report;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.njcn.utils.PubUtils;
import com.pqs9300.mapper.report.GetVdeviationDataMapper;
import com.pqs9300.pojo.report.Condition;
import com.pqs9300.pojo.report.ReportValue;
import com.pqs9300.service.report.GetVdeviationDataService;

@Service
public class GetVdeviationDataServiceImpl implements GetVdeviationDataService {
    private static final Logger logger = LoggerFactory.getLogger(GetVdeviationDataServiceImpl.class);
    @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) {
            logger.error(e.getMessage());
        }
        reportValue.setFmaxValue(new Float(((Double) map.get("V" + str + "_DEV_MAX")).floatValue()));
        reportValue.setMinValue(new Float(((Double) map.get("V" + str + "_DEV_MIN")).floatValue()));
        reportValue.setMeanValue(new Float(((Double) map.get("V" + str + "_DEV")).floatValue()));

        if (map.containsKey("V" + str + "_DEV_CP95")) {
            reportValue.setCp95Value(new Float(((Double) map.get("V" + str + "_DEV_CP95")).floatValue()));
        } 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(new Float(((BigDecimal) map.get("V" + str + "_DEV_MAX")).floatValue()));
        reportValue.setMinValue(new Float(((BigDecimal) map.get("V" + str + "_DEV_MIN")).floatValue()));
        reportValue.setMeanValue(new Float(((BigDecimal) map.get("V" + str + "_DEV")).floatValue()));

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

        list.add(reportValue);
    }
}
