package com.njcn.serviceimpl.excelreport;

import com.njcn.mapper.excelreport.GetInICurrentDataMapper;
import com.njcn.pojo.excelreport.ConditionData;
import com.njcn.pojo.excelreport.ReportValueData;
import com.njcn.service.excelreport.GetInICurrentDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class GetInICurrentDataServiceImpl implements GetInICurrentDataService {
    @Autowired
    private GetInICurrentDataMapper getICurrentMapper;

    @Override
    public List<ReportValueData> getInICurrent(ConditionData conditionData) {
        List<ReportValueData> 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(conditionData, listValue);

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

        return list;
    }

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

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

                list.add(reportValueData);
            }
        }
    }
}
