package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.mapper.*;
import com.lightinit.hsdatashow.model.admin.EducationalPortraitListModel;
import com.lightinit.hsdatashow.model.admin.EducationalPortraitNameModel;
import com.lightinit.hsdatashow.model.admin.UnemploymentListModel;
import com.lightinit.hsdatashow.model.admin.UrbanRuralGdpListModel;
import com.lightinit.hsdatashow.service.admin.ILivelihoodImproveService;
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.List;

@Service
public class LivelihoodImproveServiceImpl implements ILivelihoodImproveService {
    @Autowired
    private UnemploymentMapper unemploymentMapper;
    @Autowired
    private UrbanRuralGdpMapper urbanRuralGdpMapper;
    @Autowired
    private UrbanRuralContrastMapper urbanRuralContrastMapper;
    @Autowired
    private QualityMapper qualityMapper;
    @Autowired
    private CoefficientMapper coefficientMapper;
    @Override
    public UnemploymentListModel getUnemployment() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        UnemploymentExample example = new UnemploymentExample();
        List<Unemployment> list =unemploymentMapper.selectByExample(example);
        UnemploymentListModel<BigDecimal> model = new UnemploymentListModel<>() ;
        List<BigDecimal> number = new ArrayList<>() ;
        List<Integer> dataYear = new ArrayList<>() ;
        if (list.size()>0){
            for (Unemployment unemployment : list) {
                number.add(unemployment.getNumber());
                dataYear.add(unemployment.getDataYear());
            }
            model.setDataYear(dataYear);
            model.setNumber(number);
        }
        return model;
    }

    @Override
    public UrbanRuralGdpListModel getUrbanRuralGdp() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        UrbanRuralGdpExample example = new UrbanRuralGdpExample();
        List<UrbanRuralGdp> list =urbanRuralGdpMapper.selectByExample(example);
        UrbanRuralGdpListModel<BigDecimal> model = new UrbanRuralGdpListModel<>() ;
        List<BigDecimal> gdp = new ArrayList<>() ;
        List<BigDecimal> urbanRural = new ArrayList<>() ;
        List<String> city = new ArrayList<>() ;
        List<String> name = new ArrayList<>() ;
        if (list.size()>0){
            for (UrbanRuralGdp urbanRuralGdp : list) {
//                gdp.add(urbanRuralGdp.getGdp());
//                city.add(urbanRuralGdp.getCity());
//                urbanRural.add(urbanRuralGdp.getUrbanRural());
                if(!city.contains(urbanRuralGdp.getCity())){
                    city.add(urbanRuralGdp.getCity());
                }
                if(!name.contains(urbanRuralGdp.getName())){
                    name.add(urbanRuralGdp.getName());
                }
                if("GDP".equals(urbanRuralGdp.getName())) {
                    gdp.add(urbanRuralGdp.getValue());
                }else if("城乡居民收入".equals(urbanRuralGdp.getName())){
                    urbanRural.add(urbanRuralGdp.getValue());
                }
            }
            model.setCity(city);
            model.setGdp(gdp);
            model.setUrbanRural(urbanRural);
            model.setName(name);
        }
        return model;
    }

    @Override
    public UrbanRuralGdpListModel getUrbanRuralContrast() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        UrbanRuralContrastExample example = new UrbanRuralContrastExample();
        List<UrbanRuralContrast> list =urbanRuralContrastMapper.selectByExample(example);
        UrbanRuralGdpListModel<BigDecimal> model = new UrbanRuralGdpListModel<>() ;
        List<BigDecimal> city = new ArrayList<>() ;
        List<BigDecimal> farmer = new ArrayList<>() ;
        List<Integer> dataYear = new ArrayList<>() ;
        List<String> name = new ArrayList<>() ;
        if (list.size()>0){
            for (UrbanRuralContrast urbanRuralContrast : list) {
                if(!dataYear.contains(urbanRuralContrast.getDataYear())){
                    dataYear.add(urbanRuralContrast.getDataYear());
                }
                if(!name.contains(urbanRuralContrast.getName())){
                    name.add(urbanRuralContrast.getName());
                }
                if("农村居民收入".equals(urbanRuralContrast.getName())) {
                    farmer.add(urbanRuralContrast.getValue());
                }else if("城市居民收入".equals(urbanRuralContrast.getName())){
                    city.add(urbanRuralContrast.getValue());
                }
            }
            model.setDataYear(dataYear);
            model.setGdp(city);
            model.setUrbanRural(farmer);
            model.setName(name);
        }
        return model;
    }

    @Override
    public EducationalPortraitListModel getQuality() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        QualityExample example  = new QualityExample();
        List<Quality> list =qualityMapper.selectByExample(example);
        EducationalPortraitListModel<BigDecimal> groupNameAndValueModel = new EducationalPortraitListModel<>() ;
        List<BigDecimal> value = new ArrayList<>();
        List<BigDecimal> value1 = new ArrayList<>();
        List<BigDecimal> value2 = new ArrayList<>();
        List<Integer> dataYear = new ArrayList<>();
        List<EducationalPortraitNameModel> ListName = new ArrayList<>();
//        List<EducationalPortraitNameModel> ListName1 = new ArrayList<>();
//        List<EducationalPortraitNameModel> ListName2 = new ArrayList<>();
        if (list.size()>0){
            for (Quality Quality : list) {
                if(!dataYear.contains(Quality.getDataYear())){
                    dataYear.add(Quality.getDataYear());
                }
                EducationalPortraitNameModel name = new EducationalPortraitNameModel();
                if(Quality.getDataYear()==2016) {
                    value.add(Quality.getNumber()) ;
                    name.setName(Quality.getName());
                    ListName.add(name);
                }
                if(Quality.getDataYear()==2017) {
                    value1.add(Quality.getNumber()) ;
                }
                if(Quality.getDataYear()==2018) {
                    value2.add(Quality.getNumber()) ;
                }


            }
            groupNameAndValueModel.setValue(value);
            groupNameAndValueModel.setValue1(value1);
            groupNameAndValueModel.setListName(ListName);
            groupNameAndValueModel.setValue2(value2);
            groupNameAndValueModel.setDataYear(dataYear);
        }

        return groupNameAndValueModel;
    }

    @Override
    public List<Coefficient> getCoefficient() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        String name = "基尼系数";
        return coefficientMapper.selectList(name);
    }

    @Override
    public List<Coefficient> getCoefficient1() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        String name = "恩格尔系数";
        return coefficientMapper.selectList(name);
    }

}
