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.GetThreephaseDataMapper;
import com.pqs9300.pojo.report.Condition;
import com.pqs9300.pojo.report.ReportValue;
import com.pqs9300.service.report.GetThreephaseDataService;

@Service
public class GetThreephaseDataServiceImpl implements GetThreephaseDataService {
    private static final Logger logger = LoggerFactory.getLogger(GetThreephaseDataServiceImpl.class);
    @Autowired
    private GetThreephaseDataMapper getThreephaseDataMapper;

    @Override
    public List<ReportValue> getHThreephase(Condition condition) {
        List<ReportValue> list = new ArrayList<>();
        List<Map<String, Object>> listU = getThreephaseDataMapper.getHThreephase(condition, "V");

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

        return list;
    }

    private void transformHData(List<ReportValue> list, List<Map<String, Object>> listThreephase, String str) {
        for (Map<String, Object> map : listThreephase) {
            ReportValue reportValue1 = new ReportValue();
            setHData(str, "UNBALANCE", list, map, reportValue1);
        }
    }

    private void setHData(String str1, String str, List<ReportValue> list, Map<String, Object> map, ReportValue reportValue) {
        reportValue.setFmaxValue(new Float(((Double) map.get(str1 + "_" + str + "_MAX")).floatValue()));
        reportValue.setMinValue(new Float(((Double) map.get(str1 + "_" + str + "_MIN")).floatValue()));
        reportValue.setMeanValue(new Float(((Double) map.get(str1 + "_" + str)).floatValue()));
        try {
            reportValue.setStrTime(PubUtils.date2String((Date) map.get("TIMEID"), "yyyy-MM-dd"));
        } catch (Exception e) {
           logger.error(e.getMessage());
        }

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

        list.add(reportValue);
    }

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

        //电压三相不平衡度
        List<Map<String, Object>> listU = getThreephaseDataMapper.getThreephase(condition, "V");
        //电流三相不平衡度
        List<Map<String, Object>> listI = getThreephaseDataMapper.getThreephase(condition, "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<ReportValue> list) {
        for (int i = 0; i < 4; i++) {
            List<ReportValue> list1 = new ArrayList<>();
            RegroupData.regroupData(list1, false);
            list.addAll(list1);
        }
    }

    private void transformData(List<ReportValue> list, List<Map<String, Object>> listThreephase, String str) {
        for (Map<String, Object> map : listThreephase) {
            ReportValue reportValue1 = new ReportValue();
            ReportValue reportValue2 = new ReportValue();
            ReportValue reportValue3 = new ReportValue();
            ReportValue reportValue4 = new ReportValue();
            setData(str, "UNBALANCE", list, map, reportValue1);
            setData(str, "POS", list, map, reportValue2);
            setData(str, "NEG", list, map, reportValue3);
            setData(str, "ZERO", list, map, reportValue4);
        }
    }

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

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

        list.add(reportValue);
    }
}
