package com.sky.ioc.service.system.impl;

import com.sky.ioc.entity.domain.system.Menus;
import com.sky.ioc.entity.domain.system.SystemMenus;
import com.sky.ioc.mapper.system.SystemMenusMapper;
import com.sky.ioc.service.system.SystemMenusService;
import com.sky.ioc.tool.ReturnMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class SystemMenusServiceImpl implements SystemMenusService {

    @Autowired
    SystemMenusMapper systemMenusMapper;


    @Override
    public List<SystemMenus> listWithTree(List<SystemMenus> list, long parentId) {
        List<SystemMenus> result = new ArrayList<>();
        for (SystemMenus Menus : list) {
            if (Menus.getParentId()==parentId) {
                result.add(Menus);
            }
            for (SystemMenus child : list) {
                if (child.getParentId().equals(Menus.getId())) {
                    List<SystemMenus> children = Menus.getChildren();
                    if (children == null) {
                        children = new ArrayList<>();
                        Menus.setChildren(children);
                    }
                    children.add(child);
                }
            }
        }
        return result;
    }

    @Override
    public ReturnMsg getSystemMenusInfoList(long parentId) {
        List<SystemMenus> lists = systemMenusMapper.getList();
        return  ReturnMsg.ok(listWithTree(lists,parentId));
    }
}
