package com.lightinit.hsdatashow.impl.topic.water;

import com.lightinit.hsdatashow.dictionary.DicConsumeType;
import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.mapper.*;
import com.lightinit.hsdatashow.model.admin.*;
import com.lightinit.hsdatashow.service.topic.water.ISyntheticalService;
import com.lightinit.hsdatashow.spring.DynamicDataContextHolder;
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 SyntheticalServiceImpl implements ISyntheticalService{

    @Autowired
    private WaterqualityHandleMapper waterqualityHandleMapper ;

    @Autowired
    private WaterqualityCityHandleMapper waterqualityCityHandleMapper ;

    @Autowired
    private WaterqualityIndustryMapper waterqualityIndustryMapper ;

    @Autowired
    private WaterqualityFocusMapper waterqualityFocusMapper ;

    @Autowired
    private CountrysideWaterMapper countrysideWaterMapper ;

    @Autowired
    private CountrysideGarbageMapper countrysideGarbageMapper ;

    @Autowired
    private ConsumeMapper consumeMapper;
    @Autowired
    private DealWithTheStandardMapper dealWithTheStandardMapper;
    @Autowired
    private WaterqualitySurfaceMapper waterqualitySurfaceMapper ;


    @Override
    public BigDecimal getWaterqualityHandle() {
        return waterqualityHandleMapper.getWaterqualityHandle();
    }

    @Override
    public BigDecimal getWaterqualityCityHandle() {
        return waterqualityCityHandleMapper.getWaterqualityCityHandle();
    }

    @Override
    public WaterqualityIndustryResultModel getWaterqualityIndustry() {
        WaterqualityIndustryResultModel waterqualityIndustryResultModel = new WaterqualityIndustryResultModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR) -5  ;
        List<WaterqualityIndustry> waterqualityIndustries = waterqualityIndustryMapper.getWaterqualityIndustry(year) ;
        if (waterqualityIndustries.size()>0){
            List<Integer> years = new ArrayList<>();
            List<BigDecimal> quantity = new ArrayList<>();
            List<BigDecimal> growth = new ArrayList<>() ;
            for (WaterqualityIndustry waterqualityIndustry : waterqualityIndustries) {
                years.add(waterqualityIndustry.getDataYear()) ;
                quantity.add(waterqualityIndustry.getQuantity()) ;
                growth.add(waterqualityIndustry.getGrowth()) ;
            }
            waterqualityIndustryResultModel.setYears(years);
            waterqualityIndustryResultModel.setGrowth(growth);
            waterqualityIndustryResultModel.setQuantity(quantity);
        }
        return waterqualityIndustryResultModel;
    }

    @Override
    public WaterqualityFocusResultModel getWaterqualityFocus() {
        WaterqualityFocusResultModel waterqualityFocusResultModel = new WaterqualityFocusResultModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR) ;
        List<WaterqualityFocusModel> waterqualityFocus = waterqualityFocusMapper.getWaterqualityFocus(year) ;
        if (waterqualityFocus.size()>0){
            List<String> regionName = new ArrayList<>() ;
            List<BigDecimal> quantity = new ArrayList<>() ;
            List<BigDecimal> quantityNo = new ArrayList<>() ;
            for (WaterqualityFocusModel focus : waterqualityFocus) {
                regionName.add(focus.getRegionName()) ;
                quantity.add(focus.getQuantity()) ;
                quantityNo.add(focus.getQuantityno()) ;
            }
            waterqualityFocusResultModel.setRegionName(regionName);
            waterqualityFocusResultModel.setQuantity(quantity);
            waterqualityFocusResultModel.setQuantityNo(quantityNo);
        }
        return waterqualityFocusResultModel;
    }

    @Override
    public CountrysideNameAndValue getCountrysideWater() {
        CountrysideNameAndValue countrysideNameAndValue = new CountrysideNameAndValue() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<CountrysideWater> countrysideWaters = countrysideWaterMapper.getCountrysideWater(year) ;
        if (countrysideWaters.size()>0){
            List<String> names = new ArrayList<>();
            List<BigDecimal> quantity = new ArrayList<>() ;
            for (CountrysideWater countrysideWater : countrysideWaters) {
                names.add(countrysideWater.getDataYear()+"年") ;
                quantity.add(countrysideWater.getQuantity()) ;
            }
            countrysideNameAndValue.setNames(names);
            countrysideNameAndValue.setQuantity(quantity);
        }
        return countrysideNameAndValue;
    }

    @Override
    public CountrysideNameAndValue getCountrysideGarbage() {
        CountrysideNameAndValue countrysideNameAndValue = new CountrysideNameAndValue() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<CountrysideGarbage> countrysideGarbages = countrysideGarbageMapper.getCountrysideGarbage(year) ;
        if (countrysideGarbages.size()>0){
            List<String> names = new ArrayList<>();
            List<BigDecimal> quantity = new ArrayList<>() ;
            for (CountrysideGarbage countrysideGarbage : countrysideGarbages) {
                names.add(countrysideGarbage.getDataYear()+"年") ;
                quantity.add(countrysideGarbage.getQuantity()) ;
            }
            countrysideNameAndValue.setNames(names);
            countrysideNameAndValue.setQuantity(quantity);
        }
        return countrysideNameAndValue;
    }
    @Override
    public ConsumeListModel getConsume() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<Consume> list =consumeMapper.selectList(year);
        ConsumeListModel model = new ConsumeListModel();
        List<BigDecimal> energyValue = new ArrayList<>();//能耗
        List<BigDecimal> waterValue = new ArrayList<>();//水耗
        List<BigDecimal> constructValue = new ArrayList<>();//建设用地
        List<BigDecimal> co2Value = new ArrayList<>();//co2消耗
        List<Integer> dataYear = new ArrayList<>();
        for (Consume consume:list
                ) {
            if(!dataYear.contains(consume.getDataYear())) {
                dataYear.add(consume.getDataYear());
            }
            if(DicConsumeType.CO2.equals(consume.getConsumeType())) {
                co2Value.add(consume.getNumber());
            }
            if(DicConsumeType.CONSTRUCT.equals(consume.getConsumeType())) {
                constructValue.add(consume.getNumber());
            }
            if(DicConsumeType.ENERGY.equals(consume.getConsumeType())) {
                energyValue.add(consume.getNumber());
            }
            if(DicConsumeType.WATER.equals(consume.getConsumeType())) {
                waterValue.add(consume.getNumber());
            }
        }
        model.setDataYear(dataYear);
        model.setCo2Value(co2Value);
        model.setConstructValue(constructValue);
        model.setEnergyValue(energyValue);
        model.setWaterValue(waterValue);
        return model;
    }

    @Override
    public SpreadListModel getDealWithTheStandard() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<DealWithTheStandard> list =dealWithTheStandardMapper.selectList(year);
        SpreadListModel model = new SpreadListModel();
        List<BigDecimal> sanfei = new ArrayList<>();
        List<BigDecimal> wushui = new ArrayList<>();
        List<BigDecimal> laji = new ArrayList<>();
        List<Integer> dataYear = new ArrayList<>();
        List<String> name = new ArrayList<>();
        for (DealWithTheStandard dealWithTheStandard:list
                ) {
            if(!dataYear.contains(dealWithTheStandard.getDataYear())) {
                dataYear.add(dealWithTheStandard.getDataYear());
            }
            if(!name.contains(dealWithTheStandard.getName())) {
                name.add(dealWithTheStandard.getName());
            }
            if("工业三废".equals(dealWithTheStandard.getName())) {
                sanfei.add(dealWithTheStandard.getValue());
            }
            if("生活污水".equals(dealWithTheStandard.getName())) {
                wushui.add(dealWithTheStandard.getValue());
            }
            if("生活垃圾".equals(dealWithTheStandard.getName())) {
                laji.add(dealWithTheStandard.getValue());
            }

        }
        model.setDataYear(dataYear);
        model.setName(name);
        model.setReads(laji);
        model.setWifi(wushui);
        model.setSiG(sanfei);
        return model;
    }

    @Override
    public WaterqualitySurfaceResultModel getWaterqualitySurface() {
        WaterqualitySurfaceResultModel waterqualitySurfaceResultModel = new WaterqualitySurfaceResultModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR) ;
        List<WaterqualitySurface> waterqualitySurfaces= waterqualitySurfaceMapper.getWaterqualitySurface(year) ;
        if (waterqualitySurfaces.size()>0){
            List<String> names = new ArrayList<>();
            List<WaterqualityNameAndValueModel> values = new ArrayList<>();
            for (WaterqualitySurface waterqualitySurface : waterqualitySurfaces) {
                names.add(waterqualitySurface.getCategory()) ;
                values.add(new WaterqualityNameAndValueModel(waterqualitySurface.getCategory(),waterqualitySurface.getQuantity())) ;
            }
            waterqualitySurfaceResultModel.setNames(names);
            waterqualitySurfaceResultModel.setValues(values);
        }
        return waterqualitySurfaceResultModel;
    }
}
