package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.mapper.*;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.admin.*;
import com.lightinit.hsdatashow.service.admin.IEconomicDevelopmentService;
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.HashMap;
import java.util.List;

@Service
public class EconomicDevelopmentServiceImpl implements IEconomicDevelopmentService {
    @Autowired
    private GdpMapper gdpMapper;
    @Autowired
    private PerCapitaGdpMapper perCapitaGdpMapper;
    @Autowired
    private GdpOccupationRatioMapper gdpOccupationRatioMapper;
    @Autowired
    private LaborProductionMapper laborProductionMapper;
    @Autowired
    private UrbanizationMapper urbanizationMapper;
    @Autowired
    private  DistrictCountyMapper districtCountyMapper;
    @Override
    public PerCapitaGdpListModel getGDP() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        GdpExample example = new GdpExample();
        Calendar calendar = Calendar.getInstance();
        long year = calendar.get(Calendar.YEAR)-5 ;
        List<Gdp> list =gdpMapper.selectList(year);
        PerCapitaGdpListModel<BigDecimal> model = new PerCapitaGdpListModel<>() ;
        List<BigDecimal> growth = new ArrayList<>() ;
        List<BigDecimal> number = new ArrayList<>() ;
        List<Integer> dataYear = new ArrayList<>() ;
        if (list.size()>0){
            for (Gdp gdp : list) {
                number.add(gdp.getActual()) ;
                dataYear.add(gdp.getDataYear());
                growth.add(gdp.getGrowth()) ;
            }
            model.setDataYear(dataYear);
            model.setGrowthRate(growth);
            model.setNumber(number);
        }
        return model;
    }

    @Override
    public PerCapitaGdpListModel getPerCapitaGdp() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        PerCapitaGdpExample example = new PerCapitaGdpExample();
        Calendar calendar = Calendar.getInstance();
        long year = calendar.get(Calendar.YEAR)-5 ;
        List<PerCapitaGdp> list =perCapitaGdpMapper.selectList(year);
        PerCapitaGdpListModel<BigDecimal> model = new PerCapitaGdpListModel<>() ;
        List<BigDecimal> growthRate = new ArrayList<>() ;
        List<BigDecimal> number = new ArrayList<>() ;
        List<Integer> dataYear = new ArrayList<>() ;
        if (list.size()>0){
            for (PerCapitaGdp perCapitaGdp : list) {
                growthRate.add(perCapitaGdp.getGrowthRate()) ;
                number.add(perCapitaGdp.getNumber());
                dataYear.add(perCapitaGdp.getDataYear());
            }
            model.setDataYear(dataYear);
            model.setGrowthRate(growthRate);
            model.setNumber(number);
        }
        return model;
    }

    @Override
    public ResultState getGdpOccupationRatio() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        GdpOccupationRatioExample example = new GdpOccupationRatioExample();
        Calendar calendar = Calendar.getInstance();
        long year = calendar.get(Calendar.YEAR)-1 ;
        List<GdpOccupationRatio> list = gdpOccupationRatioMapper.selectList2(year);
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> names = new ArrayList<>();
        if(list !=null && list.size()>0){
            for (GdpOccupationRatio chartModel : list) {
                names.add(chartModel.getName());
            }
        }
        map.put("names",names);
        map.put("list",list);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public PerCapitaGdpListModel getLaborProduction() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        LaborProductionExample example = new LaborProductionExample();
        List<LaborProduction> list =laborProductionMapper.selectByExample(example);
        PerCapitaGdpListModel<BigDecimal> model = new PerCapitaGdpListModel<>() ;
        List<BigDecimal> growthRate = new ArrayList<>() ;
        List<BigDecimal> number = new ArrayList<>() ;
        List<Integer> dataYear = new ArrayList<>() ;
        if (list.size()>0){
            for (LaborProduction laborProduction : list) {
                growthRate.add(laborProduction.getActual()) ;//实际值
               // number.add(laborProduction.getGrowth());
                dataYear.add(laborProduction.getDataYear());
            }
            model.setDataYear(dataYear);
            model.setGrowthRate(growthRate);
            //model.setNumber(number);
        }
        return model;
    }

    @Override
    public PerCapitaGdpListModel getUrbanization() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        UrbanizationExample example = new UrbanizationExample();
        List<Urbanization> list =urbanizationMapper.selectByExample(example);
        PerCapitaGdpListModel<BigDecimal> model = new PerCapitaGdpListModel<>() ;
        List<BigDecimal> number = new ArrayList<>() ;
        List<Integer> dataYear = new ArrayList<>() ;
        if (list.size()>0){
            for (Urbanization urbanization : list) {
                number.add(urbanization.getNumber());
                dataYear.add(urbanization.getDataYear());
            }
            model.setDataYear(dataYear);
            model.setNumber(number);
            //model.setNumber(number);
        }
        return model;
    }

    @Override
    public GdpOccupationRatioModel getGdpOccupationRatio2() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        GdpOccupationRatioExample example = new GdpOccupationRatioExample();
        Calendar calendar = Calendar.getInstance();
        long year = calendar.get(Calendar.YEAR)-5 ;
        List<GdpOccupationRatio> list = gdpOccupationRatioMapper.selectList(year);
        GdpOccupationRatioModel model = new GdpOccupationRatioModel();
        ArrayList<String> names = new ArrayList<>();
        ArrayList<BigDecimal> value1 = new ArrayList<>();
        ArrayList<BigDecimal> value2 = new ArrayList<>();
        ArrayList<BigDecimal> value3 = new ArrayList<>();
        ArrayList<Long> dataYear = new ArrayList<>();
        if(list !=null && list.size()>0){
            for (GdpOccupationRatio chartModel : list) {
                if(!names.contains(chartModel.getName())) {
                    names.add(chartModel.getName());
                }
                if(!dataYear.contains(chartModel.getDataYear())) {
                    dataYear.add(chartModel.getDataYear());
                }
                if("第一产业".equals(chartModel.getName())) {
                    value1.add(chartModel.getValue());
                }if("第二产业".equals(chartModel.getName())) {
                    value2.add(chartModel.getValue());
                }
                if("第三产业".equals(chartModel.getName())) {
                    value3.add(chartModel.getValue());
                }

            }
        }
        model.setDataYear(dataYear);
        model.setName(names);
        model.setValue1(value1);
        model.setValue2(value2);
        model.setValue3(value3);
        return model;
    }

    @Override
    public ResultState getDistrictCounty() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        GdpOccupationRatioExample example = new GdpOccupationRatioExample();
//        Calendar calendar = Calendar.getInstance();
//        long year = calendar.get(Calendar.YEAR)-1 ;
        List<DistrictCounty> list = districtCountyMapper.getDistrictCounty();
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> names = new ArrayList<>();
        if(list !=null && list.size()>0){
            for (DistrictCounty districtCounty : list) {
                names.add(districtCounty.getName());
            }
        }
        map.put("names",names);
        map.put("list",list);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public GdpOccupationRatioModel getGDPTotal() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        GdpOccupationRatioModel model = new GdpOccupationRatioModel();
        GdpExample example = new GdpExample();
        Calendar calendar = Calendar.getInstance();
        long year = calendar.get(Calendar.YEAR)-1 ;
        List<Gdp> list =gdpMapper.getGDPTotal(year);
        List<GdpOccupationRatio> list1 = gdpOccupationRatioMapper.selectList2(year);
        ArrayList<String> names = new ArrayList<>();
        ArrayList<BigDecimal> value = new ArrayList<>();
        ArrayList<BigDecimal> value1 = new ArrayList<>();
        ArrayList<BigDecimal> value2 = new ArrayList<>();
        ArrayList<BigDecimal> value3 = new ArrayList<>();
        value.add(list.get(0).getActual());
        /*if(list !=null && list.size()>0){
            for (Gdp gdp : list) {
                value=gdp.getActual();
            }
        }*/
        if(list1 !=null && list1.size()>0){
            for (GdpOccupationRatio chartModel : list1) {
                if("第一产业".equals(chartModel.getName())) {
                    BigDecimal bg = new BigDecimal(String.valueOf(chartModel.getValue().divide(new BigDecimal(100)).multiply(list.get(0).getActual())));
                    BigDecimal  v1 = bg.setScale(4, BigDecimal.ROUND_HALF_UP);
                    value1.add(v1);
                }if("第二产业".equals(chartModel.getName())) {
                    BigDecimal bg = new BigDecimal(String.valueOf(chartModel.getValue().divide(new BigDecimal(100)).multiply(list.get(0).getActual())));
                    BigDecimal  v2 = bg.setScale(4, BigDecimal.ROUND_HALF_UP);
                    value2.add(v2);
                }
                if("第三产业".equals(chartModel.getName())) {
                    BigDecimal bg = new BigDecimal(String.valueOf(chartModel.getValue().divide(new BigDecimal(100)).multiply(list.get(0).getActual())));
                    BigDecimal  v3 = bg.setScale(4, BigDecimal.ROUND_HALF_UP);
                    value3.add(v3);
                }
            }
        }
        model.setValue(value);
        model.setValue2(value2);
        model.setValue1(value1);
        model.setValue3(value3);

        return model;
    }
}
