package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.dictionary.DicConsumeType;
import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.mapper.ConsumeMapper;
import com.lightinit.hsdatashow.mapper.DealWithTheStandardMapper;
import com.lightinit.hsdatashow.mapper.EnvironmentalGovernanceGdpMapper;
import com.lightinit.hsdatashow.mapper.EnvironmentalQualityMapper;
import com.lightinit.hsdatashow.model.admin.ConsumeListModel;
import com.lightinit.hsdatashow.model.admin.EnvironmentalGovernanceGdpListModel;
import com.lightinit.hsdatashow.model.admin.SpreadListModel;
import com.lightinit.hsdatashow.service.admin.IEcologicalCivilizationService;
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 EcologicalCivilizationServiceImpl implements IEcologicalCivilizationService {
    @Autowired
    private ConsumeMapper consumeMapper;
    @Autowired
    private EnvironmentalQualityMapper environmentalQualityMapper;
    @Autowired
    private EnvironmentalGovernanceGdpMapper environmentalGovernanceGdpMapper;
    @Autowired
    private DealWithTheStandardMapper dealWithTheStandardMapper;
    @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 List<EnvironmentalQuality> getEnvironmentalQuality() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        EnvironmentalQualityExample example = new EnvironmentalQualityExample();
        return environmentalQualityMapper.selectByExample(example);
    }

    @Override
    public EnvironmentalGovernanceGdpListModel getEnvironmentalGovernanceGdp() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<EnvironmentalGovernanceGdp> list =environmentalGovernanceGdpMapper.selectList(year);
        EnvironmentalGovernanceGdpListModel model = new EnvironmentalGovernanceGdpListModel();
        List<BigDecimal> huanjing = new ArrayList<>();
        List<BigDecimal> growth = new ArrayList<>();
        List<Integer> dataYear = new ArrayList<>();
        List<String> name = new ArrayList<>();
        for (EnvironmentalGovernanceGdp environmentalGovernanceGdp:list
             ) {
            if(!dataYear.contains(environmentalGovernanceGdp.getDataYear())) {
                dataYear.add(environmentalGovernanceGdp.getDataYear());
            }
            if(!name.contains(environmentalGovernanceGdp.getName())) {
                name.add(environmentalGovernanceGdp.getName());
            }
            if("环境治理".equals(environmentalGovernanceGdp.getName())) {
                huanjing.add(environmentalGovernanceGdp.getValue());
            }
            if("同比值".equals(environmentalGovernanceGdp.getName())) {
                growth.add(environmentalGovernanceGdp.getValue());
            }

        }
        model.setDataYear(dataYear);
        model.setName(name);
        model.setGrowth(growth);
        model.setHuanjing(huanjing);
        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;
    }
}
