package com.huimv.video.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.huimv.video.domain.PastureArea;
import com.huimv.video.repo.PastureAreaRepo;
import com.huimv.video.service.PastureAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.video.service.impl
 * @Description : TODO
 * @Author : yuxuexuan
 * @Create : 2021/1/7 0007 10:32
 **/
@Service
public class PastureAreaServiceImpl implements PastureAreaService {
    @Autowired
    private PastureAreaRepo pastureAreaRepo;
    @Override
    public void save(PastureArea pastureArea) {
        pastureAreaRepo.save(pastureArea);
    }

    @Override
    public void update(PastureArea pastureArea) {
        pastureAreaRepo.save(pastureArea);
    }

    @Override
    @DS("master")
    public void delete(List<Integer> ids) {
        for (Integer id : ids) {
            List<PastureArea> list = pastureAreaRepo.findAllByParent(id);
            if (list != null && list.size()>0){
                for (PastureArea pastureArea : list) {
                    pastureAreaRepo.deleteById(pastureArea.getId());
                }
            }
            pastureAreaRepo.deleteById(id);
        }
    }

    @Override
    @DS("master")
    public List findAll() {
        List list  = new ArrayList();
        Sort sort = Sort.by(Sort.Direction.ASC, "sort");
        List<PastureArea> allArea = pastureAreaRepo.findAll(sort);
        list = areaUtil(allArea);
        return list;
    }


    public List areaUtil(List<PastureArea> areaList){
        List<Map> list = new ArrayList<>();
        for (PastureArea pastureArea : areaList) {
            if(pastureArea.getParent() == null){
                return null;
            }
            if (pastureArea.getParent()==0){
                Map map  = new HashMap();
                map.put("id", pastureArea.getId());
                map.put("name", pastureArea.getName());
                map.put("parent", pastureArea.getParent());
                map.put("remark", pastureArea.getRemark());
                map.put("sort",pastureArea.getSort());
                List list1 = new ArrayList();
                for (PastureArea area : areaList) {
                    if (pastureArea.getId()==area.getParent()){
                        list1.add(area);
                    }
                }
                map.put("children",list1);

                list.add(map);
            }
        }



        return list;
    }


    private boolean areaEntityIsChildren(PastureArea pastureArea, List<PastureArea> areaEntityList) {
        for (PastureArea pastureAreas : areaEntityList) {
            if (pastureArea.getId()==(pastureAreas.getParent()) ){
                return true;
            }
        }
        return false;
    }

}
