package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.entity.GarbageTreatment;
import com.lightinit.hsdatashow.entity.GarbageTrend;
import com.lightinit.hsdatashow.entity.GarbagedangerTrend;
import com.lightinit.hsdatashow.mapper.*;
import com.lightinit.hsdatashow.model.admin.GarbageRegionResultModel;
import com.lightinit.hsdatashow.model.admin.GarbageTrendResultModel;
import com.lightinit.hsdatashow.model.admin.GarbagedangerModel;
import com.lightinit.hsdatashow.model.admin.GarbagedangerTrendResultModel;
import com.lightinit.hsdatashow.service.admin.IGarbageServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

@Service
public class GarbageServerImpl implements IGarbageServer {

    @Autowired
    private GarbageTreatmentMapper garbageTreatmentMapper ;

    @Autowired
    private GarbageRegionMapper garbageRegionMapper ;

    @Autowired
    private GarbagedangerRegionMapper garbagedangerRegionMapper ;

    @Autowired
    private GarbageTrendMapper garbageTrendMapper ;

    @Autowired
    private GarbagedangerTrendMapper garbagedangerTrendMapper ;

    @Override
    public GarbageTreatment getGarbageTreatment() {
        return garbageTreatmentMapper.getGarbageTreatment() ;
    }

    @Override
    public GarbageRegionResultModel getGarbageRegion() {
        List<GarbagedangerModel> garbagedangerModels = garbageRegionMapper.getGarbageRegion() ;
        GarbageRegionResultModel garbageRegionResultModel = new GarbageRegionResultModel() ;
        List<String> names = new  ArrayList<>() ;
        List<BigDecimal> values = new ArrayList<>() ;
        for (GarbagedangerModel garbagedangerModel : garbagedangerModels) {
            names.add(garbagedangerModel.getRegionName()) ;
            values.add(garbagedangerModel.getTreatmentRate()) ;
        }
        garbageRegionResultModel.setNames(names);
        garbageRegionResultModel.setValues(values); ;
        return garbageRegionResultModel;
    }

    @Override
    public BigDecimal getGarbagedangerRegion() {
        BigDecimal bd = new BigDecimal(100) ;
        BigDecimal bd0 = garbagedangerRegionMapper.getGarbagedangerRegion() ;
        BigDecimal bd1 = bd0.divide(bd) ;
        return bd1;
    }

    @Override
    public GarbageTrendResultModel getGarbageTrend() {
        GarbageTrendResultModel garbageTrendResultModel = new GarbageTrendResultModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<GarbageTrend> garbageTrends = garbageTrendMapper.getGarbageTrend(year) ;
        if (garbageTrends.size()>0) {
            List<Integer> years = new ArrayList<>() ;
            List<BigDecimal>  produceCounts = new ArrayList<>();
            List<BigDecimal> treatment = new ArrayList<>();
            List<BigDecimal> treatmentRate = new ArrayList<>();
            for (GarbageTrend garbageTrend : garbageTrends) {
                years.add(garbageTrend.getDataYear()) ;
                produceCounts.add(garbageTrend.getProducecount()) ;
                treatment.add(garbageTrend.getTreatment()) ;
                treatmentRate.add(garbageTrend.getTreatmentRate()) ;
            }
            garbageTrendResultModel.setYears(years);
            garbageTrendResultModel.setProduceCounts(produceCounts);
            garbageTrendResultModel.setTreatment(treatment);
            garbageTrendResultModel.setTreatmentRate(treatmentRate);
        }
        return garbageTrendResultModel ;
    }

    @Override
    public GarbagedangerTrendResultModel getGarbagedangerTrend() {
        GarbagedangerTrendResultModel garbageTrendResultModel = new GarbagedangerTrendResultModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<GarbagedangerTrend> garbagedangerTrends = garbagedangerTrendMapper.getGarbagedangerTrend(year) ;
        if (garbagedangerTrends.size()>0) {
            List<Integer> years = new ArrayList<>() ;
            List<BigDecimal>  produceCounts = new ArrayList<>();
            List<BigDecimal> treatment = new ArrayList<>();
            List<BigDecimal> treatmentRate = new ArrayList<>();
            for (GarbagedangerTrend garbagedangerTrend : garbagedangerTrends) {
                years.add(garbagedangerTrend.getDataYear()) ;
                produceCounts.add(garbagedangerTrend.getProducecount()) ;
                treatment.add(garbagedangerTrend.getTreatment()) ;
                treatmentRate.add(garbagedangerTrend.getTreatmentRate()) ;
            }
            garbageTrendResultModel.setYears(years);
            garbageTrendResultModel.setProduceCounts(produceCounts);
            garbageTrendResultModel.setTreatment(treatment);
            garbageTrendResultModel.setTreatmentRate(treatmentRate);
        }
        return garbageTrendResultModel ;
    }
}
