package com.pqs9000.serviceimpl.report;

import com.pqs9000.mapper.report.GetVirtualDataMapper;
import com.pqs9000.pojo.report.Condition;
import com.pqs9000.pojo.report.ReportValue;
import com.pqs9000.service.report.GetVirtualDataService;
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 GetVirtualDataServiceImpl implements GetVirtualDataService {
    @Autowired
    private GetVirtualDataMapper getVirtualDataMapper;

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

        // 获取电流有效值
        List<ReportValue> listI = getVirtualDataMapper.getVirtualDataI(condition);

        //获取电压有效值
        List<ReportValue> listV = getVirtualDataMapper.getVirtualDataV(condition);

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

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

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

        return list;
    }

    @Override
    public List<ReportValue> getVirtualData(Condition condition, String tableName) {
        List<ReportValue> list;
        if(tableName.equalsIgnoreCase("v")){
            list = getVirtualDataMapper.getVirtualDataV(condition);
        }else{
            list = getVirtualDataMapper.getVirtualDataI(condition);
        }
        RegroupData.regroupData(list, true);
        return list;
    }

    @Override
    public List<ReportValue> getVVirtualData(Condition condition) {
        Map<String, Object> resultMap = getVirtualDataMapper.getVVirtualData(condition);
        List<ReportValue> listVV = new ArrayList<>();

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

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

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

        return list1;
    }

    private void transformObject(List<ReportValue> list, String type, Map<String, Object> map) {
        ReportValue reportValue = new ReportValue();
        reportValue.setPhaseType(type);
        reportValue.setFmaxValue(Float.parseFloat(map.get("RMS" + type + "_MAX").toString()));
        reportValue.setMinValue(Float.parseFloat(map.get("RMS" + type + "_MIN").toString()));

        if (map.containsKey("RMS" + type + "_CP95")) {
            reportValue.setCp95Value(Float.parseFloat(map.get("RMS" + type + "_CP95").toString()));
        } else {
            reportValue.setCp95Value(null);
        }

        reportValue.setMeanValue(Float.parseFloat(map.get("RMS" + type).toString()));
        list.add(reportValue);
    }
}
