package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.dictionary.SolidwasteClassificationType;
import com.lightinit.hsdatashow.entity.SolidwasteClassification;
import com.lightinit.hsdatashow.entity.SolidwasteDanger;
import com.lightinit.hsdatashow.entity.SolidwasteIndustry;
import com.lightinit.hsdatashow.entity.SolidwasteLife;
import com.lightinit.hsdatashow.mapper.SolidwasteClassificationMapper;
import com.lightinit.hsdatashow.mapper.SolidwasteDangerMapper;
import com.lightinit.hsdatashow.mapper.SolidwasteIndustryMapper;
import com.lightinit.hsdatashow.mapper.SolidwasteLifeMapper;
import com.lightinit.hsdatashow.model.admin.*;
import com.lightinit.hsdatashow.service.admin.ISolidWasteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class SolidWasteServiceImpl implements ISolidWasteService {

    @Autowired
    private SolidwasteIndustryMapper solidwasteIndustryMapper ;

    @Autowired
    private SolidwasteDangerMapper solidwasteDangerMapper ;

    @Autowired
    private SolidwasteClassificationMapper solidwasteClassificationMapper ;

    @Autowired
    private SolidwasteLifeMapper solidwasteLifeMapper ;

    @Override
    public SolidwasteIndustryResultModel getSolidwasteIndustry() {
        SolidwasteIndustryResultModel solidwasteIndustryResultModel = new SolidwasteIndustryResultModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<SolidwasteIndustry> solidwasteIndustries = solidwasteIndustryMapper.getSolidwasteIndustry(year) ;
        if (solidwasteIndustries.size()>0){
            List<String> names = new ArrayList<>();
            List<BigDecimal> quantity = new ArrayList<>() ;
            List<BigDecimal> quantityManagement = new ArrayList<>();
            List<BigDecimal> utilize = new ArrayList<>() ;
            for (SolidwasteIndustry solidwasteIndustry : solidwasteIndustries) {
                names.add(solidwasteIndustry.getDataYear()+"年") ;
                quantity.add(solidwasteIndustry.getQuantity()) ;
                quantityManagement.add(solidwasteIndustry.getQuantityManagement()) ;
                utilize.add(solidwasteIndustry.getUtilize()) ;
            }
            solidwasteIndustryResultModel.setNames(names);
            solidwasteIndustryResultModel.setQuantity(quantity);
            solidwasteIndustryResultModel.setQuantityManagement(quantityManagement);
            solidwasteIndustryResultModel.setUtilize(utilize);
        }
        return solidwasteIndustryResultModel;
    }

    @Override
    public SolidwasteDangerResultModel getSolidwasteDanger() {
        SolidwasteDangerResultModel solidwasteDangerResultModel = new SolidwasteDangerResultModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<SolidwasteDanger> solidwasteDangers = solidwasteDangerMapper.getSolidwasteDanger(year) ;
        if (solidwasteDangers.size()>0){
            List<String> names = new ArrayList<>();
            List<BigDecimal> quantity = new ArrayList<>() ;
            List<BigDecimal> quantityActual = new ArrayList<>() ;
            for (SolidwasteDanger solidwasteDanger : solidwasteDangers) {
                names.add(solidwasteDanger.getDataYear()+"年") ;
                quantity.add(solidwasteDanger.getQuantity()) ;
                quantityActual.add(solidwasteDanger.getQuantityActual()) ;
            }
            solidwasteDangerResultModel.setNames(names);
            solidwasteDangerResultModel.setQuantity(quantity);
            solidwasteDangerResultModel.setQuantityActual(quantityActual);
        }
        return solidwasteDangerResultModel;
    }

    @Override
    public OneSolidwasteClassificationResultModel getOneSolidwasteClassification() {
        OneSolidwasteClassificationResultModel oneSolidwasteClassificationResultModel = new OneSolidwasteClassificationResultModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year =  calendar.get(Calendar.YEAR) ;
        List<SolidwasteClassification> solidwasteClassifications = solidwasteClassificationMapper.getOneSolidwasteClassification(year) ;
        if (solidwasteClassifications.size()>0){
            List<String> names = new ArrayList<>() ;
            List<SolidWasteNameAndValueModel> values = new ArrayList<>() ;
            for (SolidwasteClassification solidwasteClassification : solidwasteClassifications) {
                names.add(solidwasteClassification.getType()) ;
                values.add(new SolidWasteNameAndValueModel(solidwasteClassification.getType(),solidwasteClassification.getQuantity())) ;
            }
            oneSolidwasteClassificationResultModel.setNames(names);
            oneSolidwasteClassificationResultModel.setValues(values);
        }
        return oneSolidwasteClassificationResultModel;
    }

    @Override
    public SolidwasteClassificationResultModel getSolidwasteClassification() {
        SolidwasteClassificationResultModel solidwasteClassificationResultModel = new SolidwasteClassificationResultModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year =  calendar.get(Calendar.YEAR)-5 ;
        List<SolidwasteClassification> solidwasteClassifications = solidwasteClassificationMapper.getSolidwasteClassification(year) ;
        if (solidwasteClassifications.size()>0){
            List<String> names = new ArrayList<>();
            List<BigDecimal> quantity = new ArrayList<>() ;
            List<BigDecimal> quantity1 = new ArrayList<>() ;
            List<BigDecimal> quantity2 = new ArrayList<>() ;
            for (SolidwasteClassification solidwasteClassification : solidwasteClassifications) {
                if(!names.contains(solidwasteClassification.getDataYear()+"年")) {
                    names.add(solidwasteClassification.getDataYear()+"年") ;
                }
                if (solidwasteClassification.getType().equals(SolidwasteClassificationType.INDUSTRY)){
                    quantity.add(solidwasteClassification.getQuantity()) ;
                }
                if (solidwasteClassification.getType().equals(SolidwasteClassificationType.LIFE)){
                    quantity1.add(solidwasteClassification.getQuantity()) ;
                }
                if (solidwasteClassification.getType().equals(SolidwasteClassificationType.DANGER)){
                    quantity2.add(solidwasteClassification.getQuantity()) ;
                }
            }
            solidwasteClassificationResultModel.setNames(names);
            solidwasteClassificationResultModel.setQuantity(quantity);
            solidwasteClassificationResultModel.setQuantity1(quantity1);
            solidwasteClassificationResultModel.setQuantity2(quantity2);
        }
        return solidwasteClassificationResultModel;
    }

    @Override
    public SolidWasteNameAndValueModel getOneSolidwasteLife() {
        return  solidwasteLifeMapper.getOneSolidwasteLife() ;
    }

    @Override
    public SolidwasteLifeResultModel getSolidwasteLife() {
        SolidwasteLifeResultModel solidwasteLifeResultModel = new SolidwasteLifeResultModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year  =  calendar.get(Calendar.YEAR)-5 ;
        List<SolidwasteLife> solidwasteLives = solidwasteLifeMapper.getSolidwasteLife(year) ;
        if (solidwasteLives.size()>0){
            List<String> names = new ArrayList<>() ;
            List<BigDecimal> quantity = new ArrayList<>() ;
            List<BigDecimal> quantityNo = new ArrayList<>() ;
            List<BigDecimal> untreated = new ArrayList<>() ;
            List<BigDecimal> handle = new ArrayList<>() ;
            for (SolidwasteLife solidwasteLive : solidwasteLives) {
                names.add(solidwasteLive.getDataYear()+"年") ;
                quantity.add(solidwasteLive.getQuantity()) ;
                quantityNo.add(solidwasteLive.getQuantityNo()) ;
                untreated.add(solidwasteLive.getUntreated()) ;
                handle.add(solidwasteLive.getHandle()) ;
            }
            solidwasteLifeResultModel.setNames(names);
            solidwasteLifeResultModel.setHandle(handle);
            solidwasteLifeResultModel.setQuantity(quantity);
            solidwasteLifeResultModel.setQuantityNo(quantityNo);
            solidwasteLifeResultModel.setUntreated(untreated);
        }
        return solidwasteLifeResultModel;
    }
}
