package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.mapper.CityGreeningMapper;
import com.lightinit.hsdatashow.mapper.PollutionIndexMapper;
import com.lightinit.hsdatashow.mapper.SocietyDevelopMapper;
import com.lightinit.hsdatashow.mapper.SocietySecurityMapper;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.admin.ChartModel;
import com.lightinit.hsdatashow.model.admin.ChartModel2;
import com.lightinit.hsdatashow.model.admin.SocietySecurityModel;
import com.lightinit.hsdatashow.service.admin.IHomeService;
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/15 11:32
 * @Description:
 **/
@Service
public class HomeServiceImpl implements IHomeService {

    @Autowired
    private SocietyDevelopMapper societyDevelopMapper;
    @Autowired
    private SocietySecurityMapper societySecurityMapper;
    @Autowired
    private PollutionIndexMapper pollutionIndexMapper;
    @Autowired
    private CityGreeningMapper cityGreeningMapper;

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

    @Override
    public ResultState getSocietySecurity() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        List<Double> values=new ArrayList<>();
        List<SocietySecurityModel> list=societySecurityMapper.getSocietySecurity();
        if(list!=null && list.size()>0){
            for (SocietySecurityModel societySecurityModel : list) {
                values.add(societySecurityModel.getValue());
            }
        }
        map.put("list",list);
        map.put("values",values);
        resultState.setData(map);
        return resultState;
    }

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

    @Override
    public ResultState getSocietyDevelop() {
            ResultState resultState = new ResultState();
            HashMap<String, Object> map = new HashMap<>();
            Double currentGdp=null;
            Double currentPer=null;
            String gdbDegree=null;
            String perDegreer=null;
            ArrayList<String> years = new ArrayList<>();
            ArrayList<Double> gdpAmount = new ArrayList<>();
            ArrayList<Double> perIncomeAmount = new ArrayList<>();
            List<ChartModel2> list=societyDevelopMapper.getSocietyDevelop();
            if(list !=null && list.size()>0) {
                for (ChartModel2 chartModel : list) {
                    String[] namesplite = chartModel.getName().split("-");
                    if(!years.contains(namesplite[0]+"年")){
                        years.add(namesplite[0]+"年");
                    }
                    if("人均GDP".equals(namesplite[1])){
                        gdpAmount.add((Double) chartModel.getValue());
                        gdbDegree=(String) chartModel.getTemp1();
                    }else{
                        perIncomeAmount.add((Double) chartModel.getValue());
                        perDegreer=(String) chartModel.getTemp2();
                    }
                    currentGdp=(Double) chartModel.getValue();
                    currentPer=(Double) chartModel.getValue();
                }
            }
            map.put("currentGdp",currentGdp);
            map.put("gdbDegree",gdbDegree);
            map.put("currentPer",currentPer);
            map.put("perDegreer",perDegreer);
            map.put("perIncomeAmount",perIncomeAmount);
            map.put("gdpAmount",gdpAmount);
            map.put("years",years);
            resultState.setData(map);
            return resultState;
    }
}
