package com.xunmei.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunmei.common.core.utils.StringUtils;
import com.xunmei.system.api.domain.Org;
import com.xunmei.system.mapper.OrgMapper;
import com.xunmei.system.service.IOrgService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class OrgServiceImpl extends ServiceImpl<OrgMapper, Org> implements IOrgService {


    private void recursionFn(List<Org> list, Org t)
    {
        // 得到子节点列表
        List<Org> childList = getChildList(list, t);
        t.setChildren(childList);
        for (Org tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Org> list, Org t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

    private List<Org> getChildList(List<Org> list, Org t)
    {
        List<Org> tlist = new ArrayList<Org>();
        Iterator<Org> it = list.iterator();
        while (it.hasNext())
        {
            Org n = (Org) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    @Override
    public Page<Org> selectPage(Page<Org> page, Org org) {
        return baseMapper.selectPage(page,org);
    }
}
