package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.dictionary.DicCoalConsumeCategory;
import com.lightinit.hsdatashow.dictionary.DicNewEnergyCategory;
import com.lightinit.hsdatashow.mapper.*;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.admin.ChartModel;
import com.lightinit.hsdatashow.model.admin.ChartModel2;
import com.lightinit.hsdatashow.service.admin.IEnergyService;
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.HashMap;
import java.util.List;

/**
 * @Author: sunxiang
 * @Date: 2018/8/6 11:32
 * @Description:
 **/
@Service
public class EnergyServiceImpl implements IEnergyService {

    @Autowired
    private EnergyConsumptionMapper energyConsumptionMapper;
    @Autowired
    private EnergyProportionMapper energyProportionMapper;
    @Autowired
    private CleanPolluteProportionMapper cleanPolluteProportionMapper;
    @Autowired
    private NewenergyInfrastructureMapper newenergyInfrastructureMapper;
    @Autowired
    private CoalConsumeMapper coalConsumeMapper;
    @Autowired
    private ElectricConsumeMapper electricConsumeMapper;
    @Override
    public ResultState getEngeryTotal() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        List<ChartModel> list=energyConsumptionMapper.getEngeryTotal();
        ArrayList<String> years = new ArrayList<>();
        ArrayList<BigDecimal> amount = new ArrayList<>();
        //计算总量
        BigDecimal total=new BigDecimal(0);
        //取最后一年的“同比数据值”
        BigDecimal growth=null;
        if(list !=null && list.size()>0){
            for (ChartModel chartModel : list) {
                years.add(chartModel.getName());
                amount.add((BigDecimal) chartModel.getValue());
                total=total.add((BigDecimal)chartModel.getValue());
                growth=(BigDecimal)chartModel.getTemp();
            }
        }
        map.put("years",years);
        map.put("amount",amount);
        map.put("total",total);
        map.put("growth",growth);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getEngeryScale() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        List<ChartModel> list=energyProportionMapper.getEngeryScale();
        ArrayList<String> names = new ArrayList<>();
        if(list !=null && list.size()>0){
            for (ChartModel chartModel : list) {
                names.add(chartModel.getName());
            }
        }
        map.put("names",names);
        map.put("list",list);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getCleanAndpollute() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> years = new ArrayList<>();
        ArrayList<BigDecimal> cleanAmount = new ArrayList<>();
        ArrayList<BigDecimal> polluteAmount = new ArrayList<>();
        List<ChartModel> list=cleanPolluteProportionMapper.getCleanAndpollute();
        if(list !=null && list.size()>0){
            for (ChartModel chartModel : list) {
                if(!years.contains(chartModel.getName())){
                    years.add(chartModel.getName());
                }
                if(chartModel.getTemp().equals("清洁")){
                    cleanAmount.add((BigDecimal) chartModel.getValue());
                }else{
                    polluteAmount.add((BigDecimal) chartModel.getValue());
                }
            }
        }
        map.put("years",years);
        map.put("cleanAmount",cleanAmount);
        map.put("polluteAmount",polluteAmount);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getNewenergyInfrastructure() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        double lampTotal=0,lampGrowth=0;
        double voliateTotal=0,voliateGrowth=0;
        double carTotal=0,catGrowth=0;
        ArrayList<Integer> years = new ArrayList<>();
        ArrayList<Double> lampAmount = new ArrayList<>();
        ArrayList<Double> voliateAmount = new ArrayList<>();
        ArrayList<Double> carAmount = new ArrayList<>();
        List<ChartModel2> list=newenergyInfrastructureMapper.getNewenergyInfrastructure();
        if(list !=null && list.size()>0){
            for (ChartModel2 chartModel : list) {
                if(!years.contains(chartModel.getTemp1())){//年份
                    years.add((Integer)chartModel.getTemp1());
                }
                if(DicNewEnergyCategory.SUNLAMP.equals(chartModel.getName())){
                    lampAmount.add((Double)chartModel.getValue());
                    lampTotal+=(Double) chartModel.getValue();
                    lampGrowth=(Double)chartModel.getTemp2();//最新同比数据值
                }else if(DicNewEnergyCategory.PHOTOVOLTAICELECTRIC.equals(chartModel.getName())){
                    voliateAmount.add((Double)chartModel.getValue());
                    voliateTotal+=(Double) chartModel.getValue();
                    voliateGrowth=(Double)chartModel.getTemp2();//最新同比数据值
                }else{
                    carAmount.add((Double)chartModel.getValue());
                    carTotal+=(Double) chartModel.getValue();
                    catGrowth=(Double)chartModel.getTemp2();//最新同比数据值
                }
            }
        }
        map.put("years",years);
        map.put("lampAmount",lampAmount);
        map.put("voliateAmount",voliateAmount);
        map.put("carAmount",carAmount);
        //总数
        map.put("lampTotal",lampTotal);
        map.put("voliateTotal",voliateTotal);
        map.put("carTotal",carTotal);
        //最新同比数据值
        map.put("lampGrowth",lampGrowth);
        map.put("voliateGrowth",voliateGrowth);
        map.put("catGrowth",catGrowth);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getCoalconsume() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        Double electricNow=null,industyNow=null,livingNow=null;
        ArrayList<String> years = new ArrayList<>();
        ArrayList<String> names = new ArrayList<>();
        ArrayList<Double> electricAmount = new ArrayList<>();
        ArrayList<Double> industryAmount = new ArrayList<>();
        ArrayList<Double> livingAmount = new ArrayList<>();
        List<ChartModel> list=coalConsumeMapper.getCoalconsume();
        if(list !=null && list.size()>0) {
            for (ChartModel chartModel : list) {
                if(!years.contains(chartModel.getName())){//年份
                    years.add(chartModel.getName());
                }
                if(!names.contains(chartModel.getTemp())){//种类
                    names.add(chartModel.getTemp().toString());
                }
                if(DicCoalConsumeCategory.ELECTRICCAR.equals(chartModel.getTemp())){
                    electricAmount.add((Double)chartModel.getValue());
                    electricNow=(Double)chartModel.getValue();//最新同比数据值
                }else if(DicCoalConsumeCategory.INDUSTRY.equals(chartModel.getTemp())){
                    industryAmount.add((Double)chartModel.getValue());
                    industyNow=(Double)chartModel.getValue();//最新同比数据值
                }else if(DicCoalConsumeCategory.LIVING.equals(chartModel.getTemp())){
                    livingAmount.add((Double)chartModel.getValue());
                    livingNow=(Double)chartModel.getValue();//最新同比数据值
                }
            }
        }
        map.put("years",years);
        map.put("names",names);
        map.put("livingAmount",livingAmount);
        map.put("industryAmount",industryAmount);
        map.put("electricAmount",electricAmount);
        //最新数量值
        map.put("livingNow",livingNow);
        map.put("industyNow",industyNow);
        map.put("electricNow",electricNow);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getElectricConsume() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        Double electricNow=null;
        ArrayList<String> months = new ArrayList<>();
        ArrayList<Double> lastAmount = new ArrayList<>();
        ArrayList<Double> newAmount = new ArrayList<>();
        List<ChartModel> list=electricConsumeMapper.getElectricConsume();
            if(list !=null && list.size()>0) {
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            for (ChartModel chartModel : list) {
                if(!months.contains(chartModel.getTemp()+"月")){
                    months.add(chartModel.getTemp()+"月");
                }
                if(year==Integer.parseInt(chartModel.getName())){
                    newAmount.add((Double)chartModel.getValue());
                    electricNow=(Double) chartModel.getValue();
                }else{
                    lastAmount.add((Double)chartModel.getValue());
                }
            }
        }
        map.put("electricNow",electricNow);
        map.put("newAmount",newAmount);
        map.put("lastAmount",lastAmount);
        map.put("months",months);
        resultState.setData(map);
        return resultState;
    }

}
