package com.pqs9300.serviceimpl.report;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pqs9300.mapper.report.GetInICurrentMapper;
import com.pqs9300.pojo.report.Condition;
import com.pqs9300.pojo.report.ReportValue;
import com.pqs9300.service.report.GetInICurrentService;

@Service
public class GetInICurrentServiceImpl implements GetInICurrentService {
    @Autowired
    private GetInICurrentMapper getICurrentMapper;

    @Override
    public List<ReportValue> getInICurrent(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);
            }
        }
    }
}
