package com.guwan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guwan.RBAC.*;
import com.guwan.mapper.*;
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;
import service.SystemUsersService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 12455
* @description 针对表【system_users(用户信息表)】的数据库操作Service实现
* @createDate 2024-11-13 15:58:26
*/
@Service
@RequiredArgsConstructor
public class SystemUsersServiceImpl extends ServiceImpl<SystemUsersMapper, SystemUsers>
    implements SystemUsersService{


    private final SystemUsersMapper systemUsersMapper;

    private final SystemUserRoleMapper systemUserRoleMapper;
    //system_role_menu
    private final SystemRoleMenuMapper systemRoleMenuMapper;
    private final SystemMenuMapper systemMenuMapper;
    private final SystemRoleMapper systemRoleMapper;


    @Override
    public void getRouterAsync() {


      /*  List<String> roleList = ;


        System.out.println("角色list = " + roleList);

        //SELECT * FROM system_menu WHERE id in (SELECT menu_id FROM system_role_menu where role_id in (101, 1 ,2))

        List<String> menuList = ;

        System.out.println("menuList = " + menuList);*/


        List<String> roleList = systemUserRoleMapper
                .selectObjs(new LambdaQueryWrapper<SystemUserRole>()
                        .eq(SystemUserRole::getUserId,
                                systemUsersMapper.selectOne(new LambdaQueryWrapper<SystemUsers>()
                                                .eq(SystemUsers::getUsername, "yudao"))
                                        .getId()).select(SystemUserRole::getRoleId));

        System.out.println("roleList = " + roleList);


        List<String> roleName = systemRoleMapper.selectObjs(new LambdaQueryWrapper<SystemRole>()
                .in(SystemRole::getId, roleList).select(SystemRole::getCode));

        System.out.println("roleName = " + roleName);


        List<SystemMenu> systemMenus = systemMenuMapper
                .selectList(new LambdaQueryWrapper<SystemMenu>()
                .in(SystemMenu::getId, systemRoleMenuMapper
                        .selectObjs(new LambdaQueryWrapper<SystemRoleMenu>()
                                .in(SystemRoleMenu::getRoleId, systemUserRoleMapper
                                        .selectObjs(new LambdaQueryWrapper<SystemUserRole>()
                        .eq(SystemUserRole::getUserId,
                                systemUsersMapper.selectOne(new LambdaQueryWrapper<SystemUsers>()
                                        .eq(SystemUsers::getUsername, "yudao"))
                                        .getId()).select(SystemUserRole::getRoleId)))
                                .select(SystemRoleMenu::getMenuId))));

        //  System.out.println("systemMenus = " + systemMenus);

      /*  systemMenus.stream().filter(systemMenu -> !systemMenu.getDeleted() && systemMenu.getVisible())
                .map(systemMenu -> {

                    // 角色码列表
                    List<String> roleCodeList;
                    roleCodeList = roleName;

                    // 权限码列表
                    List<String> powerCodeList;


                    RouterMeta meta =  new RouterMeta();


                    return null;

                }).distinct().toList();*/

        System.out.println("systemMenus = " + buildMenuTree(systemMenus));



    }

    // 方法：将菜单列表构建为树形结构
    public static List<SystemMenu> buildMenuTree(List<SystemMenu> menuList) {
        // 以 parentId 分组菜单
        Map<Long, List<SystemMenu>> parentIdMap = menuList.stream()
                .collect(Collectors.groupingBy(SystemMenu::getParentId));

        // 从根节点（parentId 为 null 或 0）递归构建树
        return buildTree(parentIdMap, 0L); // 假设顶级节点 parentId 为 0
    }

    // 递归构建树形结构
    private static List<SystemMenu> buildTree(Map<Long, List<SystemMenu>> parentIdMap, Long parentId) {
        List<SystemMenu> children = parentIdMap.getOrDefault(parentId, new ArrayList<>());

        for (SystemMenu menu : children) {
            // 递归设置子菜单
            menu.setChildren(buildTree(parentIdMap, menu.getId()));
        }

        return children;
    }

}




