package com.community.huiju.service.impl;

import com.community.commom.mode.ResponseBean;
import com.community.huiju.dao.ToSysMenuMapper;
import com.community.huiju.model.ToSysMenu;
import com.community.huiju.service.IToSysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ToSysMenuServiceImpl implements IToSysMenuService {

    @Autowired
    private ToSysMenuMapper toSysMenuMapper;

    @Override
    public ResponseBean getMenuAll() {
        ResponseBean responseBean = new ResponseBean();
        List<ToSysMenu> menuList = getMenu(0, 0);
        responseBean.addSuccess(menuList);
        return responseBean;
    }

    /**
     * 根据 parentId 查询菜单
     * @param parentId 父级菜单ID
     * @param menuType 菜单类型 0 页面菜单  1功能菜单
     * @return
     */
    private List<ToSysMenu> getMenu (Integer parentId, Integer menuType){
        List<ToSysMenu> sysMenuList = toSysMenuMapper.selectParentId(parentId, menuType);
        if (CollectionUtils.isNotEmpty(sysMenuList)) {
            sysMenuList.forEach(e -> {
                e.setChildren(getMenu(e.getId(), menuType));
            });
        }
        return sysMenuList;
    }

}
