package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.entity.GarbageCan;
import com.lightinit.hsdatashow.entity.GarbageCanExample;
import com.lightinit.hsdatashow.mapper.GarbageCanMapper;
import com.lightinit.hsdatashow.model.admin.GarbageCanModel;
import com.lightinit.hsdatashow.model.admin.GroupNameAndValueModel;
import com.lightinit.hsdatashow.service.admin.IGarbageCanService;
import com.lightinit.hsdatashow.spring.DynamicDataContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class GarbageCanServiceImpl implements IGarbageCanService {
    @Autowired
    private GarbageCanMapper  garbageCanMapper;



    @Override
    public  List<GarbageCan> getGarbageCan() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        GarbageCanExample example = new GarbageCanExample();
        List<GarbageCan> list = garbageCanMapper.selectList();
//        GroupNameAndValueModel model = new GroupNameAndValueModel();
//        List<String> categories =new ArrayList<>() ;
//        List<Long> value = new ArrayList<>() ;
//        if(list.size()>0) {
//            for(GarbageCan garbageCan :list) {
//                categories.add(garbageCan.getStreet());
//                value.add(garbageCan.getNumber());
//            }
//        }
//        model.setCategories(categories);
//        model.setValue(value);
        return list;
    }

    @Override
    public GroupNameAndValueModel getGarbageCan2(String name,Integer falg) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        List<GarbageCan> list = new ArrayList<>();
        GroupNameAndValueModel model = new GroupNameAndValueModel();
        List<String> categories =new ArrayList<>() ;
        List<Long> value = new ArrayList<>() ;
        if(falg!=null){
            if(name==null || name.equals("")){
                name ="前进街";
            }
            GarbageCan garbageCan=  garbageCanMapper.selectByName(name);
            list = garbageCanMapper.selectByparentId(garbageCan.getId());

            if(list.size()>0) {
                for(GarbageCan garbageCan1 :list) {
                    categories.add(garbageCan1.getStreet());
                    if(garbageCan1.getNumber()==null||garbageCan1.getNumber()==0) {
                        value.add(Long.valueOf(0));
                    }else{
                        value.add((long) Math.round(100/((Long)garbageCan1.getStreetLength()/garbageCan1.getNumber())));
                        Collections.sort(value);
                        Collections.reverse(value);
                    }
                }
            }
        }else {
            if(name!=null && name!="") {
                GarbageCan garbageCan=  garbageCanMapper.selectByName(name);
                list = garbageCanMapper.selectByparentId(garbageCan.getId());

            }else {
                list = garbageCanMapper.selectList2();

            }
            if(list.size()>0) {
                for(GarbageCan garbageCan1 :list) {
                    categories.add(garbageCan1.getStreet());
                    value.add(garbageCan1.getNumber());
                }
            }
        }

        model.setCategories(categories);
        model.setValue(value);

        return model;
    }

    @Override
    public List<GarbageCanModel> getGarbageCanList() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        List<GarbageCanModel> list = new ArrayList<>();
        List<GarbageCan> list2 = garbageCanMapper.selectList3();
        for (GarbageCan garbageCan:list2
             ) {
            GarbageCanModel model = new GarbageCanModel();
            model.setName(garbageCan.getStreet());
            model.setValue(garbageCan.getNumber());
            list.add(model);
        }
        return list;
    }

    @Override
    public GroupNameAndValueModel getTop() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);

        GroupNameAndValueModel model = new GroupNameAndValueModel();
        List<String> categories =new ArrayList<>() ;
        List<Long> value = new ArrayList<>() ;
        List<String> categories1 =new ArrayList<>() ;
        List<Long> value1 = new ArrayList<>() ;
        List<GarbageCan>  list =  garbageCanMapper.getTop();
        List<GarbageCan>  list2 =  garbageCanMapper.getBottom();
        if(list.size()>0) {
            for(GarbageCan garbageCan1 :list) {
                categories.add(garbageCan1.getStreet());
                value.add(garbageCan1.getNumber());
            }
        }
        if(list2.size()>0) {
            for(GarbageCan garbageCan :list2) {
                categories1.add(garbageCan.getStreet());
                if(garbageCan.getNumber()==null) {
                    value1.add((long)0);
                }else {
                    value1.add(garbageCan.getNumber());
                }
            }
        }
        model.setCategories(categories);
        model.setValue(value);
        model.setCategories1(categories1);

        model.setValue1(value1);

        return model;
    }
//    @Override
//    public GroupNameAndValueModel getBottom() {
//        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
//
//        GroupNameAndValueModel model = new GroupNameAndValueModel();
//        List<String> categories =new ArrayList<>() ;
//        List<Long> value = new ArrayList<>() ;
//        List<GarbageCan>  list =  garbageCanMapper.getTop();
//        if(list.size()>0) {
//            for(GarbageCan garbageCan1 :list) {
//                categories.add(garbageCan1.getStreet());
//                value.add(garbageCan1.getNumber());
//            }
//        }
//        model.setCategories(categories);
//        model.setValue(value);
//
//        return model;
//    }
}
