package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.mapper.EducationalPortraitMapper;
import com.lightinit.hsdatashow.mapper.HealthCarePlanMapper;
import com.lightinit.hsdatashow.mapper.PublicSecurityMapper;
import com.lightinit.hsdatashow.mapper.TrafficTravelMapper;
import com.lightinit.hsdatashow.model.admin.*;
import com.lightinit.hsdatashow.service.admin.IPublishService;
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.List;

@Service
public class PublishServiceImpl implements IPublishService {
    @Autowired
    private EducationalPortraitMapper educationalPortraitMapper;
    @Autowired
    private HealthCarePlanMapper healthCarePlanMapper;
    @Autowired
    private PublicSecurityMapper publicSecurityMapper;
    @Autowired
    private TrafficTravelMapper trafficTravelMapper;

    @Override
    public  EducationalPortraitListModel getEducationalPortrait() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        List<EducationalPortrait> list =educationalPortraitMapper.selectList();
        EducationalPortraitListModel<BigDecimal> groupNameAndValueModel = new EducationalPortraitListModel<>() ;
        List<BigDecimal> value = new ArrayList<>() ;
        List<EducationalPortraitNameModel> ListName = new ArrayList<>();
        if (list.size()>0){
            for (EducationalPortrait educationalPortrait : list) {
                EducationalPortraitNameModel name = new EducationalPortraitNameModel();
                value.add(educationalPortrait.getValue()) ;
                name.setName(educationalPortrait.getName());
                //BeanUtils.copyProperties(educationalPortrait,name);
                ListName.add(name);
            }
            groupNameAndValueModel.setValue(value);
            groupNameAndValueModel.setListName(ListName);

        }
        return groupNameAndValueModel;
    }

    @Override
    public GroupNameAndValueModel getGreening() {
        return null;
    }

    @Override
    public EducationalPortraitListModel geHealthCarePlan() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        HealthCarePlanExample example  = new HealthCarePlanExample();
        List<HealthCarePlan> list =healthCarePlanMapper.selectByExample(example);
        EducationalPortraitListModel<BigDecimal> groupNameAndValueModel = new EducationalPortraitListModel<>() ;
        List<BigDecimal> value = new ArrayList<>() ;
        List<EducationalPortraitNameModel> ListName = new ArrayList<>();
        if (list.size()>0){
            for (HealthCarePlan healthCarePlan : list) {
                EducationalPortraitNameModel name = new EducationalPortraitNameModel();
                value.add(healthCarePlan.getValue()) ;
                name.setName(healthCarePlan.getName());
                //BeanUtils.copyProperties(educationalPortrait,name);
                ListName.add(name);
            }
            groupNameAndValueModel.setValue(value);
            groupNameAndValueModel.setListName(ListName);

        }
        return groupNameAndValueModel;
    }

    @Override
    public List<PublicSecurity> gePublicSecurity() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        return publicSecurityMapper.gePublicSecurity(year);
    }

    @Override
    public TrafficTravelListModel getTrafficTravel() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        TrafficTravelListModel model = new TrafficTravelListModel();
        TrafficTravelExample example = new TrafficTravelExample();
        List<TrafficTravel> list = trafficTravelMapper.selectByExample(example);
        for (TrafficTravel trafficTravel:list
             ) {
            if(trafficTravel.getName().equals("公交车")){
                model.setCar(trafficTravel.getValue());
            }
            if(trafficTravel.getName().equals("出租车")){
                model.setBus(trafficTravel.getValue());
            }
        }
        return model;
    }
}
