package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.dictionary.DicAtmosphereType;
import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.mapper.*;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.admin.*;
import com.lightinit.hsdatashow.service.admin.IAtmosphereService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class AtmosphereServiceImpl implements IAtmosphereService {

    @Autowired
    private AtmosphereIndexMapper atmosphereIndexMapper ;

    @Autowired
    private AtmosphereYearMapper atmosphereYearMapper ;

    @Autowired
    private AtmosphereEmissionMapper atmosphereEmissionMapper ;

    @Autowired
    private AtmosphereGasificationMapper atmosphereGasificationMapper ;

    @Autowired
    private AtmosphereIndustryMapper atmosphereIndustryMapper ;

     @Autowired
    private HbCityAirQualityIndexMapper hbCityAirQualityIndexMapper;

     @Autowired
    private AreaAirPollutionIndexMapper areaAirPollutionIndexMapper;

    @Override
    public AtmosphereNameAndValueModel getAtmosphereIndex() {
        AtmosphereNameAndValueModel<Integer> atmosphereNameAndValueModel = new AtmosphereNameAndValueModel<>() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR) ;
        List<AtmosphereIndex> atmosphereIndices = atmosphereIndexMapper.getAtmosphereIndex(year) ;
        if (atmosphereIndices.size()>0) {
            List<Integer> names = new ArrayList<>();
            List<BigDecimal> values = new ArrayList<>();
            for (AtmosphereIndex atmosphereIndex : atmosphereIndices) {
                names.add(atmosphereIndex.getDataMonth());
                values.add(atmosphereIndex.getQuality());
            }
            atmosphereNameAndValueModel.setNames(names);
            atmosphereNameAndValueModel.setValues(values);
        }
        return atmosphereNameAndValueModel;
    }

    @Override
    public AtmosphereYearResultModel getAtmosphereYear() {
        AtmosphereYearResultModel atmosphereYearResultModel = new AtmosphereYearResultModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR) ;
        List<AtmosphereYear> atmosphereYears = atmosphereYearMapper.getAtmosphereYear(year) ;
        if (atmosphereYears.size()>0) {
             List<BigDecimal> excellents = new ArrayList<>();
             List<BigDecimal> lights = new ArrayList<>();
             List<BigDecimal> severes = new ArrayList<>() ;
             List<BigDecimal> moderates = new ArrayList<>() ;
             List<BigDecimal> seriouss = new ArrayList<>() ;
            for (AtmosphereYear atmosphereYear : atmosphereYears) {
                if (atmosphereYear.getGrade().equals(DicAtmosphereType.EXCELLENT)){
                    excellents.add(atmosphereYear.getQuality()) ;
                }
                if (atmosphereYear.getGrade().equals(DicAtmosphereType.LIGHT)){
                    lights.add(atmosphereYear.getQuality()) ;
                }
                if (atmosphereYear.getGrade().equals(DicAtmosphereType.MODERATE)){
                    moderates.add(atmosphereYear.getQuality()) ;
                }
                if (atmosphereYear.getGrade().equals(DicAtmosphereType.SERIOUS)){
                    seriouss.add(atmosphereYear.getQuality()) ;
                }
                if (atmosphereYear.getGrade().equals(DicAtmosphereType.SEVERE)){
                    severes.add(atmosphereYear.getQuality()) ;
                }
            }
            atmosphereYearResultModel.setExcellents(excellents);
            atmosphereYearResultModel.setLights(lights);
            atmosphereYearResultModel.setModerates(moderates);
            atmosphereYearResultModel.setSeriouss(seriouss);
            atmosphereYearResultModel.setSeveres(severes);
        }
        return atmosphereYearResultModel;
    }

    @Override
    public AtmosphereEmissionResultModel getAtmosphereEmission() {
        AtmosphereEmissionResultModel atmosphereEmissionResultModel = new AtmosphereEmissionResultModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR) ;
        List<AtmosphereEmission> atmosphereEmissions = atmosphereEmissionMapper.getAtmosphereEmission(year) ;
        if (atmosphereEmissions.size()>0){
             List<String> names = new ArrayList<>() ;
             List<BigDecimal> limit = new ArrayList<>() ;
             List<BigDecimal> actual = new ArrayList<>() ;
            for (AtmosphereEmission atmosphereEmission : atmosphereEmissions) {
                names.add(atmosphereEmission.getType()) ;
                limit.add(atmosphereEmission.getLimitQuality()) ;
                actual.add(atmosphereEmission.getActualQuality()) ;
            }
            atmosphereEmissionResultModel.setNames(names);
            atmosphereEmissionResultModel.setActual(actual);
            atmosphereEmissionResultModel.setLimit(limit);
        }
        return atmosphereEmissionResultModel;
    }

    @Override
    public AtmosphereGasificationResultModel getAtmosphereGasification() {
        AtmosphereGasificationResultModel atmosphereGasificationResultModel = new AtmosphereGasificationResultModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR) - 5 ;
        List<AtmosphereGasification> atmosphereGasifications = atmosphereGasificationMapper.getAtmosphereGasification(year) ;
        if (atmosphereGasifications.size()>0){
             List<Integer> years = new ArrayList<>();
             List<BigDecimal> actualQuality = new ArrayList<>() ;
             List<BigDecimal> forecastQuality =  new ArrayList<>() ;
            for (AtmosphereGasification atmosphereGasification : atmosphereGasifications) {
                years.add(atmosphereGasification.getDataYear()) ;
                actualQuality.add(atmosphereGasification.getActualQuality()) ;
                forecastQuality.add(atmosphereGasification.getForecastQuality()) ;
            }
            atmosphereGasificationResultModel.setYears(years);
            atmosphereGasificationResultModel.setActualQuality(actualQuality);
            atmosphereGasificationResultModel.setForecastQuality(forecastQuality);
        }
        return atmosphereGasificationResultModel;
    }

    @Override
    public AtmosphereIndustryResultModel getAtmosphereIndustry() {
        AtmosphereIndustryResultModel atmosphereIndustryResultModel = new AtmosphereIndustryResultModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR) - 5 ;
        List<AtmosphereIndustry>  atmosphereIndustries= atmosphereIndustryMapper.getAtmosphereIndustry(year) ;
        if (atmosphereIndustries.size()>0){
             List<Integer> years = new ArrayList<>();
             List<BigDecimal> discharge = new ArrayList<>() ;
             List<BigDecimal> standard  = new ArrayList<>();
            for (AtmosphereIndustry atmosphereIndustry : atmosphereIndustries) {
                years.add(atmosphereIndustry.getDataYear()) ;
                discharge.add(atmosphereIndustry.getDischarge()) ;
                standard.add(atmosphereIndustry.getStandard()) ;
            }
            atmosphereIndustryResultModel.setYears(years);
            atmosphereIndustryResultModel.setDischarge(discharge);
            atmosphereIndustryResultModel.setStandard(standard);
        }
        return atmosphereIndustryResultModel;
    }

    @Override
    public Map<String, Object> getHBCityAirQualityIndex() {
        List<String> dateList=hbCityAirQualityIndexMapper.getHBCityAirQualityIndexDate();
        String dates=JSONArray.fromObject(dateList).toString();
        List<LinkedHashMap<String,String>> list=hbCityAirQualityIndexMapper.getHBCityAirQualityIndex();
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("[");
        for (LinkedHashMap<String, String> map : list) {
            stringBuffer.append("'"+map.get(map.keySet().iterator().next())+"',");
        }
        stringBuffer.setCharAt(stringBuffer.lastIndexOf(","),']');
        HashMap<String,Object> map = new HashMap<>();
        map.put("date",dates);
        map.put("cityName",stringBuffer.toString());
        map.put("data",list);
        return map;
    }

    @Override
    public Map<String, Object> getHSAreaAirPollutionIndex() {
        List<String> dateList=areaAirPollutionIndexMapper.getHSAirPollutionIndexDate();
        String dates=JSONArray.fromObject(dateList).toString();
        List<LinkedHashMap<String,String>> list=areaAirPollutionIndexMapper.getHSAreaAirPollutionIndex();
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("[");
        for (LinkedHashMap<String, String> hashMap : list) {
            stringBuffer.append("'"+hashMap.get(hashMap.keySet().iterator().next())+"',");
        }
        stringBuffer.setCharAt(stringBuffer.lastIndexOf(","),']');
        HashMap<String, Object> map = new HashMap<>();
        map.put("date",dates);
        map.put("areaName",stringBuffer.toString());
        map.put("data",list);
        return map;
    }

    @Override
    public ResultState getHbAreaAirFineCount() {
        ResultState resultState = new ResultState();
        List<ChartModel> list=hbCityAirQualityIndexMapper.getHbAreaAirFineCount();
        List<String> citys = new ArrayList<>();
        List<Object> days = new ArrayList<Object>();
        for (ChartModel  chartModel: list) {
            citys.add(chartModel.getName());
            days.add(chartModel.getValue());
        }
        Map<String, List<?>> hashMap = new HashMap<>();
        hashMap.put("citys",citys);
        hashMap.put("days",days);
        resultState.setData(hashMap);
        return resultState;
    }


}

