package com.pqs9000.serviceimpl.report;

import com.njcn.utils.PubUtils;
import com.pqs9000.mapper.report.GetFlickerDataMapper;
import com.pqs9000.pojo.report.Condition;
import com.pqs9000.pojo.report.ReportValue;
import com.pqs9000.service.report.GetFlickerDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class GetFlickerDataServiceImpl implements GetFlickerDataService {
    private static final Logger logger = LoggerFactory.getLogger(GetFlickerDataServiceImpl.class);
    @Autowired
    private GetFlickerDataMapper getFlickerDataMapper;

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

        //短时闪变
        List<Map<String, Object>> listFlicker = getFlickerDataMapper.getFlickerData(condition);
        //长时闪变
        List<Map<String, Object>> listLFlicker = getFlickerDataMapper.getLFlickerData(condition);

        //筛选结果为想要的数据格式
        if (0 < listFlicker.size() && null != listFlicker.get(0)) {
            transformData("S", listPst, listFlicker);
        }

        if (0 < listLFlicker.size() && null != listLFlicker.get(0)) {
            transformData("L", listPlt, listLFlicker);
        }

        RegroupData.regroupData(listPst, true);
        RegroupData.regroupData(listPlt, true);
        list.addAll(listPst);
        list.addAll(listPlt);

        return list;
    }

    @Override
    public List<ReportValue> getPFlickerData(Condition condition) {
        List<ReportValue> listPst = new ArrayList<>();
        return listPst;
    }

    @Override
    public List<ReportValue> getLLFlickerData(Condition condition) {
        List<ReportValue> listPlt = new ArrayList<>();
        List<Map<String, Object>> listLFlicker = getFlickerDataMapper.getLLFlickerData(condition);

        if (0 < listLFlicker.size() && null != listLFlicker.get(0)) {
            transformHData("L", listPlt, listLFlicker);
        }

        return listPlt;
    }

    private void transformHData(String str, List<ReportValue> list, List<Map<String, Object>> listMap) {
        for (Map<String, Object> map : listMap) {
            ReportValue value = new ReportValue();
            setHData(str, value, list, map);
        }
    }

    private void setHData(String str, ReportValue reportValue, List<ReportValue> list, Map<String, Object> map) {
        reportValue.setPhaseType(map.get("PHASETYPE").toString());
        reportValue.setMeanValue(Float.parseFloat(map.get("P" + str + "T").toString()));
        reportValue.setFmaxValue(Float.parseFloat(map.get("P" + str + "T_MAX").toString()));
        reportValue.setMinValue(Float.parseFloat(map.get("P" + str + "T_MIN").toString()));

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

        if (map.containsKey("P" + str + "T_CP95")) {
            reportValue.setCp95Value(Float.parseFloat(map.get("P" + str + "T_CP95").toString()));
        } else {
            reportValue.setCp95Value(null);
        }

        list.add(reportValue);
    }

    private void transformData(String str, List<ReportValue> list, List<Map<String, Object>> listMap) {
        for (Map<String, Object> map : listMap) {
            ReportValue value = new ReportValue();
            setData(str, value, list, map);
        }
    }

    private void setData(String str, ReportValue reportValue, List<ReportValue> list, Map<String, Object> map) {
        reportValue.setPhaseType(map.get("PHASETYPE").toString());
        reportValue.setMeanValue(Float.parseFloat(map.get("P" + str + "T").toString()));
        reportValue.setFmaxValue(Float.parseFloat(map.get("P" + str + "T_MAX").toString()));
        reportValue.setMinValue(Float.parseFloat(map.get("P" + str + "T_MIN").toString()));

        if (map.containsKey("P" + str + "T_CP95")) {
            reportValue.setCp95Value(Float.parseFloat(map.get("P" + str + "T_CP95").toString()));
        } else {
            reportValue.setCp95Value(null);
        }

        list.add(reportValue);
    }
}
