package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.mapper.*;
import com.lightinit.hsdatashow.model.admin.ServiceExpenditureListModel;
import com.lightinit.hsdatashow.model.admin.SpreadListModel;
import com.lightinit.hsdatashow.model.admin.UnemploymentListModel;
import com.lightinit.hsdatashow.service.admin.IProgressService;
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 ProgressServiceImpl implements IProgressService {
    @Autowired
    private ServiceExpenditureMapper serviceExpenditureMapper;
    @Autowired
    private YearsEducationMapper yearsEducationMapper;
    @Autowired
    private SocialSecurityMapper socialSecurityMapper;
    @Autowired
    private SocialSafetyMapper socialSafetyMapper;
    @Autowired
    private ChildMortalityMapper childMortalityMapper;
    @Override
    public ServiceExpenditureListModel getServiceExpenditure() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ServiceExpenditureListModel model = new ServiceExpenditureListModel();
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<String> name =new ArrayList<>();
        List<BigDecimal> total = new ArrayList<>();
        List<BigDecimal> expenditure = new ArrayList<>();
        List<Integer> dataYear = new ArrayList<>();
        List<ServiceExpenditure>   list  = serviceExpenditureMapper.selectList(year);
        for (ServiceExpenditure serviceExpenditure:list
             ) {
            if(!name.contains(serviceExpenditure.getName())) {
                name.add(serviceExpenditure.getName());
            }
            if("总支出".equals(serviceExpenditure.getName())) {
                total.add(serviceExpenditure.getValue());
            }else if("公共支出".equals(serviceExpenditure.getName())) {
                expenditure.add(serviceExpenditure.getValue());
            }
            if(!dataYear.contains(serviceExpenditure.getDataYear())) {
                dataYear.add(serviceExpenditure.getDataYear());
            }

        }
        model.setDataYear(dataYear);
        model.setName(name);
        model.setExpenditure(expenditure);
        model.setTotal(total);
        return model;
    }

    @Override
    public UnemploymentListModel getYearsEducation() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        UnemploymentListModel model = new UnemploymentListModel();
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<YearsEducation> list = yearsEducationMapper.selectList(year);
        List<Integer> dataYear = new ArrayList<>();
        List<BigDecimal> value = new ArrayList<>();
        for (YearsEducation yearsEducation:list
             ) {
            dataYear.add(yearsEducation.getDataYear());
            value.add(yearsEducation.getYearsEducation());

        }
        model.setDataYear(dataYear);
        model.setNumber(value);
        return model;
    }

    @Override
    public SpreadListModel getSocialSecurity() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        SpreadListModel model = new SpreadListModel();
        List<Integer> dataYear = new ArrayList<>();
        List<String> name = new ArrayList<>();
        List<BigDecimal> insurance = new ArrayList<>();//保险
        List<BigDecimal> rural  = new ArrayList<>();//农村低保
        List<BigDecimal> urban  = new ArrayList<>();//城市低保
        List<SocialSecurity> list = socialSecurityMapper.selectList();
        for (SocialSecurity socialSecurity:list
             ) {
            if(!dataYear.contains(socialSecurity.getDataYear())) {
                dataYear.add(socialSecurity.getDataYear());
            }
            if("保险".equals(socialSecurity.getName())) {
                insurance.add(socialSecurity.getValue());
            }else if("农村低保".equals(socialSecurity.getName())) {
                rural.add(socialSecurity.getValue());
            }
            else if("城市低保".equals(socialSecurity.getName())) {
                urban.add(socialSecurity.getValue());
            }
            if(!name.contains(socialSecurity.getName())) {
                name.add(socialSecurity.getName());
            }

        }
        model.setName(name);
        model.setDataYear(dataYear);
        model.setWifi(insurance);
        model.setSiG(urban);
        model.setReads(rural);
        return model;
    }

    @Override
    public UnemploymentListModel getChildMortality() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<ChildMortality> list =childMortalityMapper.selectList(year);
        UnemploymentListModel<BigDecimal> model = new UnemploymentListModel<>() ;
        List<BigDecimal> number = new ArrayList<>() ;
        List<Integer> dataYear = new ArrayList<>() ;
        if (list.size()>0){
            for (ChildMortality childMortality : list) {
                number.add(childMortality.getMortality());
                dataYear.add(childMortality.getDataYear());
            }
            model.setDataYear(dataYear);
            model.setNumber(number);
        }
        return model;
    }

    @Override
    public List<SocialSafety> getSocialSafety() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        SocialSafetyExample example =new SocialSafetyExample();
        return socialSafetyMapper.selectByExample(example);
    }
}
