package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.dictionary.DicSpreadType;
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.ITourismCultureService;
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.HashMap;
import java.util.List;

@Service
public class TourismCultureServiceImpl implements ITourismCultureService {
    @Autowired
    private CulturalFacilitiesMapper culturalFacilitiesMapper;
    @Autowired
    private SpreadMapper spreadMapper;
    @Autowired
    private ConsumptionMapper consumptionMapper;
    @Autowired
    private OutputValueRatioMapper outputValueRatioMapper;
    @Autowired
    private IndustryMapper industryMapper;

    @Override
    public CulturalFalitiesListModel getCulturalFalities() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        CulturalFacilitiesExample example  = new CulturalFacilitiesExample();
        List<CulturalFacilities> list =culturalFacilitiesMapper.selectByExample(example);
        CulturalFalitiesListModel<BigDecimal> groupNameAndValueModel = new CulturalFalitiesListModel<>() ;
        List<String> name = new ArrayList<>() ;
        List<EducationalPortraitNameModel> ListValue = new ArrayList<>();
        if (list.size()>0){
            for (CulturalFacilities culturalFacilities : list) {
                EducationalPortraitNameModel value = new EducationalPortraitNameModel();
                value.setValue(culturalFacilities.getValue()); ;
                name.add(culturalFacilities.getCulturalName());
                //BeanUtils.copyProperties(educationalPortrait,name);
                ListValue.add(value);
            }
            groupNameAndValueModel.setValue(ListValue);
            groupNameAndValueModel.setListName(name);

        }
        return groupNameAndValueModel;
    }

    @Override
    public SpreadListModel getSpread() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        SpreadListModel<BigDecimal> model = new SpreadListModel<>();
        SpreadExample example = new SpreadExample();
        List<Spread> list = spreadMapper.getSpread();
        List<String> name = new ArrayList<>();
        List<Integer> dataYear = new ArrayList<>();
        List<BigDecimal> siG = new ArrayList<>();
        List<BigDecimal> radioBroadcast = new ArrayList<>();
        List<BigDecimal> television = new ArrayList<>();
        List<BigDecimal> wifi = new ArrayList<>();
        List<BigDecimal> reads = new ArrayList<>();
        for (Spread spread : list) {
            if(!name.contains(spread.getSpreadType())){
                name.add(spread.getSpreadType());
            }
            if(!dataYear.contains(spread.getDataYear())){
                dataYear.add(spread.getDataYear());
            }
            if(DicSpreadType.WIFI.equals(spread.getSpreadType())) {
                wifi.add(spread.getValue());
            }else if(DicSpreadType.SIG.equals(spread.getSpreadType())){
                siG.add(spread.getValue());
            }else if(DicSpreadType.RADIOBROADCAST.equals(spread.getSpreadType())){
                radioBroadcast.add(spread.getValue());
            }else if(DicSpreadType.TELEVISION.equals(spread.getSpreadType())){
                television.add(spread.getValue());
            }else if(DicSpreadType.READING.equals(spread.getSpreadType())){
                reads.add(spread.getValue());
            }
            }

//        if (list.size()>0){
////            for (Spread spread : list) {
////                name.add(spread.getSpreadType());
////                siG.add(spread.getSig());
////                dataYear.add(spread.getDataYear());
////                radioBroadcast.add(spread.getRadioBroadcast());
////                television.add(spread.getTelevision());
////                wifi.add(spread.getWifi());
////                reads.add(spread.getReading());
////            }
            model.setName(name);
            model.setRadioBroadcast(radioBroadcast);
            model.setDataYear(dataYear);
            model.setReads(reads);
            model.setSiG(siG);
            model.setWifi(wifi);
            model.setTelevision(television);
//        }
        return model;
    }

    @Override
    public ConsumptionListModel getConsumption() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ConsumptionListModel<BigDecimal> model = new ConsumptionListModel<>();
        ConsumptionExample example = new ConsumptionExample();
        List<Consumption> list = consumptionMapper.getConsumption();

        List<Integer> dataYear = new ArrayList<>();

        List<String> name = new ArrayList<>();
        List<BigDecimal> tourism = new ArrayList<>();
        List<BigDecimal> cultural = new ArrayList<>();
        if (list.size()>0){
            for (Consumption consumption : list) {
                if(!name.contains(consumption.getName())){
                    name.add(consumption.getName());
                }
                if(!dataYear.contains(consumption.getDataYear())){
                    dataYear.add(consumption.getDataYear());
                }
                if("文旅消费".equals(consumption.getName())) {
                    tourism.add(consumption.getValue());
                }else if("旅游消费".equals(consumption.getName())){
                    cultural.add(consumption.getValue());
                }
            }
            model.setTourism(tourism);
            model.setDataYear(dataYear);
            model.setCultural(cultural);
        }
        return model;
    }

    @Override
    public OutPutValueRatioListMOdel getRatio() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        OutPutValueRatioListMOdel<BigDecimal> model = new OutPutValueRatioListMOdel<>();
        OutputValueRatioExample example = new OutputValueRatioExample();

        List<OutputValueRatio> list = outputValueRatioMapper.selectByExample(example);

        List<Integer> dataYear = new ArrayList<>();

        List<String> name = new ArrayList<>();
        List<BigDecimal> number = new ArrayList<>();
        if (list.size()>0){
            for (OutputValueRatio outputValueRatio : list) {
                name.add(outputValueRatio.getName());
                number.add(outputValueRatio.getValue());
            }
            model.setName(name);
            model.setNumber(number);
        }
        return model;
    }

    @Override
    public IndustryListModel getIndustry() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        IndustryListModel<BigDecimal> model = new IndustryListModel<>();
        IndustryExample example = new IndustryExample();

        List<Industry> list = industryMapper.getIndustry();
        List<Integer> dataYear = new ArrayList<>();
        List<BigDecimal> number = new ArrayList<>();
        List<BigDecimal> enterprise = new ArrayList<>();
        List<BigDecimal> practitioners = new ArrayList<>();
        List<BigDecimal> value = new ArrayList<>();
        List<BigDecimal> enumber = new ArrayList<>();
        List<BigDecimal> pnumber = new ArrayList<>();
        if (list.size()>0){
            for (Industry industry : list) {
                if(!dataYear.contains(industry.getDataYear())){
                    dataYear.add(industry.getDataYear());
                }
                if("产值".equals(industry.getName())) {
                    value.add(industry.getValue());
                    number.add(industry.getNumber());
                }else if("骨干企业".equals(industry.getName())){
                    enterprise.add(industry.getValue());
                    enumber.add(industry.getNumber());
                }else if("从业人员".equals(industry.getName())){
                    practitioners.add(industry.getValue());
                    pnumber.add(industry.getNumber());
                }
            }
            model.setDataYear(dataYear);
            model.setEnterprise(enterprise);
            model.setPractitioners(practitioners);
            model.setValue(value);
            model.setNumber(number);
            model.setPnumber(pnumber);
            model.setEnumber(enumber);
        }
        return model;
    }

    @Override
    public ResultState getOutPutValue() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        OutputValueRatioExample example = new OutputValueRatioExample();
        List<OutputValueRatio> list = outputValueRatioMapper.selectByExample(example);
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> names = new ArrayList<>();
        if(list !=null && list.size()>0){
            for (OutputValueRatio chartModel : list) {
                names.add(chartModel.getName());
            }
        }
        map.put("names",names);
        map.put("list",list);
        resultState.setData(map);
        return resultState;
    }
}
