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

@Service
public class GetICurrentServiceImpl implements GetICurrentService {
    private static final Logger logger = LoggerFactory.getLogger(GetICurrentServiceImpl.class);
    @Autowired
    private GetICurrentMapper getICurrentMapper;

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

        for (int i = 1; i < 51; i++) {
            listValue.add(new Integer(i));
        }

        //获取电流幅值，包含基波
        List<Map<String, Object>> listResult = getICurrentMapper.getICurrent(condition, listValue);

        if (0 == listResult.size() || null == listResult.get(0)) {
            for (int i = 0; i < 50; i++) {
                RegroupData.regroupData(list, true, true);
            }
        } else {
            transformData(list, listResult, listValue);
        }

        return list;
    }

    private void transformData(List<ReportValue> list, List<Map<String, Object>> maps, List<Integer> listValue) {
        for (Integer integer : listValue) {
            for (Map<String, Object> map : maps) {
                ReportValue reportValue = new ReportValue();
                reportValue.setPhaseType(map.get("PHASETYPE").toString());
                reportValue.setFmaxValue(new Float(((BigDecimal) map.get("I_" + integer + "_MAX")).floatValue()));
                reportValue.setMinValue(new Float(((BigDecimal) map.get("I_" + integer + "_MIN")).floatValue()));
                reportValue.setMeanValue(new Float(((BigDecimal) map.get("I_" + integer)).floatValue()));

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

                list.add(reportValue);
            }
        }
    }

    @Override
    public List<ReportValue> getHICurrent(Condition condition, Integer times) {
        List<ReportValue> list = new ArrayList<>();
        condition.setTimes(times);
        List<Map<String, Object>> listResult = getICurrentMapper.getHICurrent(condition);

        if (0 < listResult.size() && null != listResult.get(0)) {
            transformHData(list, listResult, times);
        }

        return list;
    }

    private void transformHData(List<ReportValue> list, List<Map<String, Object>> maps, Integer integer) {
        for (Map<String, Object> map : maps) {
            ReportValue reportValue = new ReportValue();
            reportValue.setPhaseType(map.get("PHASETYPE").toString());
            reportValue.setFmaxValue(new Float(((Double) map.get("I_" + integer + "_MAX")).floatValue()));
            reportValue.setMinValue(new Float(((Double) map.get("I_" + integer + "_MIN")).floatValue()));
            reportValue.setMeanValue(new Float(((Double) map.get("I_" + integer)).floatValue()));

            try {
                reportValue.setStrTime(PubUtils.date2String((Date) map.get("TIMEID"), "yyyy-MM-dd"));
            } catch (Exception e) {
                logger.error(e.getMessage());
            }

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

            list.add(reportValue);
        }
    }
}
