package com.pqs9000.serviceimpl.report;

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

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

@Service
public class GetInharmServiceImpl implements GetInharmService {
    private static final Logger logger = LoggerFactory.getLogger(GetInharmServiceImpl.class);
    @Autowired
    private GetInHarmMapper getInHarmMapper;

    @Override
    public List<ReportValue> getHInharm(Condition condition) {
        List<ReportValue> list = new ArrayList<>();
        Integer times = condition.getTimes();
        List<Map<String, Object>> listRate = getInHarmMapper.getHInharm(condition);

        if (0 < listRate.size() && null != listRate.get(0)) {
            transformHData(list, listRate, times);
        }
        return list;
    }

    private void transformHData(List<ReportValue> list, List<Map<String, Object>> maps, Integer integer) {
        for (Map<String, Object> map : maps) {
            ReportValue reportValue = new ReportValue();
            reportValue.setPhaseType(map.get("PHASETYPE").toString());
            reportValue.setFmaxValue(Float.parseFloat(map.get("V_" + integer + "_MAX").toString()));
            reportValue.setMinValue(Float.parseFloat(map.get("V_" + integer + "_MIN").toString()));
            reportValue.setMeanValue(Float.parseFloat(map.get("V_" + integer).toString()));

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

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

            list.add(reportValue);
        }
    }
}
