package com.njcn.serviceimpl.excelreport;

import com.njcn.mapper.excelreport.GetVirtualMapper;
import com.njcn.pojo.excelreport.ConditionData;
import com.njcn.pojo.excelreport.ReportValueData;
import com.njcn.service.excelreport.GetVirtualService;
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;

/**
 * @Author: Sunwei 【sunW2016@163.com】
 * @Description:
 * @Date: Create in 10:46 2018/6/7
 * @Modified By:
 */
@Service
public class GetVirtualServiceImpl implements GetVirtualService {
    @Autowired
    protected GetVirtualMapper getVirtualMapper;

    @Override
    public List<ReportValueData> getVirtualData(ConditionData conditionData) {
        List<ReportValueData> list = new ArrayList<>();

        // 获取电流有效值
        List<ReportValueData> listI = getVirtualMapper.getVirtualData(conditionData, "i");

        //获取电压有效值
        List<ReportValueData> listV = getVirtualMapper.getVirtualData(conditionData, "v");

        //获取线电压有效值
        Map<String, Object> resultMap = getVirtualMapper.getVVirtualData(conditionData);
        List<ReportValueData> listVV = new ArrayList<>();

        if (resultMap != null && !resultMap.isEmpty()) {
            listVV = transform(resultMap);
        }

        Regroup.regroupData(listV, true);
        Regroup.regroupData(listI, true);
        Regroup.regroupData(listVV, true);
        list.addAll(listV);
        list.addAll(listI);
        list.addAll(listVV);

        return list;
    }

    @Override
    public List<ReportValueData> getVirtualData(ConditionData conditionData, String tableName) {
        List<ReportValueData> list = getVirtualMapper.getVirtualData(conditionData, tableName);
        Regroup.regroupData(list, true);
        return list;
    }

    @Override
    public List<ReportValueData> getVVirtualData(ConditionData conditionData) {
        Map<String, Object> resultMap = getVirtualMapper.getVVirtualData(conditionData);
        List<ReportValueData> listVV = new ArrayList<>();

        if (resultMap != null && !resultMap.isEmpty()) {
            listVV = transform(resultMap);
        }

        Regroup.regroupData(listVV, true);
        return listVV;
    }

    private List<ReportValueData> transform(Map<String, Object> map) {
        List<ReportValueData> list1 = new ArrayList<>();
        transformObject(list1, "AB", map);
        transformObject(list1, "BC", map);
        transformObject(list1, "CA", map);

        return list1;
    }

    private void transformObject(List<ReportValueData> list, String type, Map<String, Object> map) {
        ReportValueData reportValueData = new ReportValueData();
        reportValueData.setPhaseType(type);
        reportValueData.setFmaxValue(new Float(((BigDecimal) map.get("RMS" + type + "_MAX")).floatValue()));
        reportValueData.setMinValue(new Float(((BigDecimal) map.get("RMS" + type + "_MIN")).floatValue()));

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

        reportValueData.setMeanValue(new Float(((BigDecimal) map.get("RMS" + type)).floatValue()));
        list.add(reportValueData);
    }
}
