package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.mapper.*;
import com.lightinit.hsdatashow.model.admin.*;
import com.lightinit.hsdatashow.service.admin.IWaterThingService;
import com.lightinit.hsdatashow.spring.DynamicDataContextHolder;
import org.springframework.beans.BeanUtils;
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;
import java.util.stream.Collectors;

@Service
public class WaterThingServiceImpl implements IWaterThingService {

    @Autowired
    private WaterSourceQualityAnalysisMapper waterSourceQualityAnalysisMapper ;

    @Autowired
    private WaterConsumptionPerCapitaMapper waterConsumptionPerCapitaMapper ;

    @Autowired
    private SewageDischargeMapper sewageDischargeMapper ;

    @Autowired
    private WaterSupplyMainMapper waterSupplyMainMapper ;

    @Autowired
    private WaterSupplyAggregateMapper waterSupplyAggregateMapper ;

    @Autowired
    private PrecipitationAggregateMapper precipitationAggregateMapper ;

    @Autowired
    private WaterResourceAggregateMapper waterResourceAggregateMapper ;

    @Autowired
    private WaterStorageAggregateMapper waterStorageAggregateMapper ;

    @Autowired
    private PrecipitationMapper precipitationMapper ;

    @Autowired
    private WaterAllCityMapper waterAllCityMapper ;

    @Autowired
    private WaterAllCountyMapper waterAllCountyMapper ;

    @Override
    public GroupNameAndValueModel getWaterSourceQualityAnalysis() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        GroupNameAndValueModel<Integer> groupNameAndValueModel = new GroupNameAndValueModel<>() ;
        List<WaterSourceQualityAnalysisListModel> waterSourceQualityAnalysisListModels = waterSourceQualityAnalysisMapper.getWaterSourceQualityAnalysis() ;
        List<String> name = new ArrayList<>() ;
        List<Integer> value = new ArrayList<>() ;
        if (waterSourceQualityAnalysisListModels.size()>0) {
            for (WaterSourceQualityAnalysisListModel waterSourceQualityAnalysisListModel : waterSourceQualityAnalysisListModels) {
                name.add(waterSourceQualityAnalysisListModel.getRegionName()) ;
                value.add(waterSourceQualityAnalysisListModel.getQualityLevel().intValue()) ;
            }
            groupNameAndValueModel.setCategories(name);
            groupNameAndValueModel.setValue(value);
        }
        return groupNameAndValueModel;
    }

    @Override
    public GroupNameAndValueModel getWaterConsumptionPerCapita() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        GroupNameAndValueModel<BigDecimal> groupNameAndValueModel = new GroupNameAndValueModel<>() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR) - 5 ;
        List<WaterConsumptionPerCapita> waterConsumptionPerCapitas = waterConsumptionPerCapitaMapper.getWaterConsumptionPerCapita(year) ;
        List<String> name = new ArrayList<>() ;
        List<BigDecimal> value = new ArrayList<>() ;
        if (waterConsumptionPerCapitas.size()>0){
            for (WaterConsumptionPerCapita waterConsumptionPerCapita : waterConsumptionPerCapitas) {
                name.add(waterConsumptionPerCapita.getDataYear()+"年") ;
                value.add(waterConsumptionPerCapita.getQuantity()) ;
            }
            groupNameAndValueModel.setCategories(name);
            groupNameAndValueModel.setValue(value);
        }
        return groupNameAndValueModel;
    }

    @Override
    public GroupNameAndValueModel getSewageDischarge() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        GroupNameAndValueModel<BigDecimal> groupNameAndValueModel = new GroupNameAndValueModel<>() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR) - 5 ;
        List<SewageDischarge> sewageDischarges = sewageDischargeMapper.getSewageDischarge(year) ;
        List<String> name = new ArrayList<>() ;
        List<BigDecimal> value = new ArrayList<>() ;
        if (sewageDischarges.size()>0){
            for (SewageDischarge sewageDischarge : sewageDischarges) {
                name.add(sewageDischarge.getDataYear()+"年") ;
                value.add(sewageDischarge.getQuantity()) ;
            }
            groupNameAndValueModel.setCategories(name);
            groupNameAndValueModel.setValue(value);
        }
        return groupNameAndValueModel;
    }

    @Override
    public GroupNameAndValueModel getPrecipitationAggregateList() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        GroupNameAndValueModel<BigDecimal> groupNameAndValueModel = new GroupNameAndValueModel<>() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR) - 5 ;
        List<PrecipitationAggregateListModel> precipitationAggregateListModels = precipitationAggregateMapper.getPrecipitationAggregateList(year) ;
        List<String> name = new ArrayList<>() ;
        List<BigDecimal> value = new ArrayList<>() ;
        if (precipitationAggregateListModels.size()>0){
            for (PrecipitationAggregateListModel precipitationAggregateListModel : precipitationAggregateListModels) {
                name.add(precipitationAggregateListModel.getYear()+"年") ;
                value.add(precipitationAggregateListModel.getTotle()) ;
            }
            groupNameAndValueModel.setCategories(name);
            groupNameAndValueModel.setValue(value);
        }
        return groupNameAndValueModel;
    }

    @Override
    public WaterSupplyMainReturnModel getWaterSupplyMain() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        WaterSupplyMainReturnModel waterSupplyMainReturnModel = new WaterSupplyMainReturnModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5   ;
        List<WaterSupplyMain> waterSupplyMains = waterSupplyMainMapper.getWaterSupplyMain(year) ;
        if (waterSupplyMains.size()>0){
             List<String> titles = new ArrayList<>() ;
             List<String> years = new ArrayList<>();
             List<WaterSupplyMainReturnDataModel> series = new ArrayList<>();
            for (WaterSupplyMain waterSupplyMain : waterSupplyMains) {
                if (!titles.contains(waterSupplyMain.getCompanyName())){
                    titles.add(waterSupplyMain.getCompanyName()) ;
                }
                if (!years.contains(waterSupplyMain.getDataYear()+"年")){
                    years.add(waterSupplyMain.getDataYear()+"年") ;
                }
                WaterSupplyMainReturnDataModel dataModel = null;
                for (WaterSupplyMainReturnDataModel model : series){
                    if(model.getName().equals(waterSupplyMain.getCompanyName())){
                        dataModel = model;
                        break;
                    }
                }
                if (dataModel != null){
                    dataModel.getData().add(waterSupplyMain.getQuantity());
                }else {
                    WaterSupplyMainReturnDataModel returnDataModel = new WaterSupplyMainReturnDataModel();
                    returnDataModel.setName(waterSupplyMain.getCompanyName());
                    returnDataModel.setType("line");
                    List<BigDecimal> dataList = new ArrayList<>();
                    dataList.add(waterSupplyMain.getQuantity());
                    returnDataModel.setData(dataList);
                    series.add(returnDataModel);
                }
            }
            waterSupplyMainReturnModel.setSeries(series);
            waterSupplyMainReturnModel.setTitles(titles) ;
            waterSupplyMainReturnModel.setYears(years);
        }

        return waterSupplyMainReturnModel;
    }

    @Override
    public List<WaterSupplyAggregateResultModel> getWaterSupplyAggregate() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        List<WaterSupplyAggregate> waterSupplyAggregates = waterSupplyAggregateMapper.getWaterSupplyAggregate() ;
        if (waterSupplyAggregates.size()>0){
            List<WaterSupplyAggregateResultModel> waterSupplyAggregateResultModels = waterSupplyAggregates.stream().map(waterSupplyAggregate -> {
                WaterSupplyAggregateResultModel waterSupplyAggregateResultModel = new WaterSupplyAggregateResultModel() ;
                waterSupplyAggregateResultModel.setName(waterSupplyAggregate.getDataYear()+"年");
                waterSupplyAggregateResultModel.setValue(waterSupplyAggregate.getQuantity().intValue());
                return waterSupplyAggregateResultModel ;
            }).collect(Collectors.toList()) ;
            return waterSupplyAggregateResultModels;
        }else {
            return null ;
        }
    }

    @Override
    public PrecipitationAggregateResultModel getPrecipitationAggregate() {
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-1   ;
        List<PrecipitationAggregate> precipitationAggregates = precipitationAggregateMapper.getPrecipitationAggregate() ;
        PrecipitationAggregateResultModel precipitationAggregateResultModel = new PrecipitationAggregateResultModel() ;
        if (precipitationAggregates.size()>0){
            BeanUtils.copyProperties(precipitationAggregates.get(0),precipitationAggregateResultModel);
        }
       // PrecipitationAggregateResultModel
        return precipitationAggregateResultModel;
    }

    @Override
    public PrecipitationAggregateResultModel getnewPrecipitationAggregate() {
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-1   ;
        PrecipitationAggregateResultModel precipitationAggregateResultModel = new PrecipitationAggregateResultModel() ;
        BigDecimal bigDecimal = precipitationAggregateMapper.getnewPrecipitationAggregate(year) ;
        precipitationAggregateResultModel.setTotal(bigDecimal);
        return precipitationAggregateResultModel;
    }

    @Override
    public WaterResourceAggregateResultModel getWaterResourceAggregate() {
        List<WaterResourceAggregate> waterResourceAggregates = waterResourceAggregateMapper.getWaterResourceAggregate() ;
        WaterResourceAggregateResultModel waterResourceAggregateResultModel = new WaterResourceAggregateResultModel() ;
        if (waterResourceAggregates.size()>0){
            BeanUtils.copyProperties(waterResourceAggregates.get(0),waterResourceAggregateResultModel);
        }
        return waterResourceAggregateResultModel;
    }

    @Override
    public WaterStorageAggregateResultModel getWaterStorageAggregate() {
        List<WaterStorageAggregate> waterStorageAggregates = waterStorageAggregateMapper.getWaterStorageAggregate() ;
        WaterStorageAggregateResultModel waterStorageAggregateResultModel = new WaterStorageAggregateResultModel() ;
        if (waterStorageAggregates.size()>0){
            BeanUtils.copyProperties(waterStorageAggregates.get(0),waterStorageAggregateResultModel);
        }
        return waterStorageAggregateResultModel;
    }

    @Override
    public List<PrecipitationResultModel> getPrecipitation() {
        return precipitationMapper.getPrecipitation();
    }

    @Override
    public GroupNameAndValueModel getChangeWaterStorageAggregate() {
        GroupNameAndValueModel<BigDecimal> groupNameAndValueModel = new GroupNameAndValueModel<>() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR) - 5 ;
        List<WaterStorageAggregate> waterqualityNameAndValueModels  = waterStorageAggregateMapper.getChangeWaterStorageAggregate(year) ;
        List<String> categories = new ArrayList<>();
        List<BigDecimal> value  = new ArrayList<>();
        for (WaterStorageAggregate waterqualityNameAndValueModel : waterqualityNameAndValueModels) {
            categories.add(waterqualityNameAndValueModel.getDataYear()+"年") ;
            value.add(waterqualityNameAndValueModel.getTotal()) ;
        }
        groupNameAndValueModel.setCategories(categories);
        groupNameAndValueModel.setValue(value);
        return groupNameAndValueModel;
    }

    @Override
    public GroupNameAndValueModel getWaterResourceAggregateList() {
        GroupNameAndValueModel<BigDecimal> groupNameAndValueModel = new GroupNameAndValueModel<>() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR) - 5 ;
        List<WaterStorageAggregate> waterqualityNameAndValueModels  = waterResourceAggregateMapper.getWaterResourceAggregateList(year) ;
        List<String> categories = new ArrayList<>();
        List<BigDecimal> value  = new ArrayList<>();
        for (WaterStorageAggregate waterqualityNameAndValueModel : waterqualityNameAndValueModels) {
            categories.add(waterqualityNameAndValueModel.getDataYear()+"年") ;
            value.add(waterqualityNameAndValueModel.getTotal()) ;
        }
        groupNameAndValueModel.setCategories(categories);
        groupNameAndValueModel.setValue(value);
        return groupNameAndValueModel;
    }

    @Override
    public GroupNameAndValueModel getWaterAllCity() {
        GroupNameAndValueModel<BigDecimal> groupNameAndValueModel = new GroupNameAndValueModel<>() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR) - 5 ;
        List<WaterAllCity> waterAllCities  = waterAllCityMapper.getWaterAllCity(year) ;
        List<String> categories = new ArrayList<>();
        List<BigDecimal> value  = new ArrayList<>();
        for (WaterAllCity waterAllCity : waterAllCities) {
            categories.add(waterAllCity.getDataYear()+"") ;
            value.add(waterAllCity.getTotal()) ;
        }
        groupNameAndValueModel.setCategories(categories);
        groupNameAndValueModel.setValue(value);
        return groupNameAndValueModel;
    }

    @Override
    public GroupNameAndValueModel getWaterAllCounty() {
        GroupNameAndValueModel groupNameAndValueModel = new GroupNameAndValueModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR) - 1 ;
        List<WaterqualityNameAndValueModel> waterAllCountys  = waterAllCountyMapper.getWaterAllCounty(year) ;
        if (waterAllCountys.size()>0){
            List<String> categories = new ArrayList<>();
            List<BigDecimal> value  = new ArrayList<>();
            for (WaterqualityNameAndValueModel waterAllCounty : waterAllCountys) {
                categories.add(waterAllCounty.getName()) ;
                value.add(waterAllCounty.getValue()) ;
            }
            groupNameAndValueModel.setValue(value);
            groupNameAndValueModel.setCategories(categories);
        }

        return groupNameAndValueModel;
    }
}
