package com.backendsys.modules.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.backendsys.exception.CustException;
import com.backendsys.modules.common.utils.TreeUtil;
import com.backendsys.modules.system.dao.SysUserRoleMenuDao;
import com.backendsys.modules.system.dao.SysUserRolePermissionDao;
import com.backendsys.modules.system.entity.SysUserRoleMenu;
import com.backendsys.modules.system.entity.SysUserRolePermission;
import com.backendsys.modules.system.service.SysUserRoleMenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class SysUserRoleMenuServiceImpl implements SysUserRoleMenuService {

    @Autowired
    private SysUserRoleMenuDao sysUserRoleMenuDao;

    @Autowired
    private SysUserRolePermissionDao sysUserRolePermissionDao;

//    // 转为树型结构 (SysUserRoleMenu)
//    public static List<SysUserRoleMenu> buildTree(List<SysUserRoleMenu> sysUserRoleMenuList) {
//        // 创建一个Map来存储id和SysUserRoleMenu对象的映射关系
//        Map<Long, SysUserRoleMenu> map = new HashMap<>();
//        // 遍历sysUserRoleMenuList，将每个SysUserRoleMenu对象放入map中
//        for (SysUserRoleMenu menu : sysUserRoleMenuList) {
//            map.put(menu.getId(), menu);
//        }
//
//        // 创建一个List来存储最父级的SysUserRoleMenu对象
//        List<SysUserRoleMenu> rootMenus = new ArrayList<>();
//
//        // 创建一个List来存储需要删除的SysUserRoleMenu对象
//        List<SysUserRoleMenu> toRemove = new ArrayList<>();
//
//        // 遍历sysUserRoleMenuList，构建树型结构
//        for (SysUserRoleMenu menu : sysUserRoleMenuList) {
//            // 如果parent_id不等于-1，说明该SysUserRoleMenu对象有父节点
//            if (menu.getParent_id().equals(-1)) {
//                // 如果parent_id等于-1，说明该SysUserRoleMenu对象是最父级节点，直接放入rootMenus中
//                rootMenus.add(menu);
//            } else {
//                // 获取其父节点
//                SysUserRoleMenu parentMenu = map.get(menu.getParent_id());
//                // 如果父节点存在，则将该SysUserRoleMenu对象放入父节点的children列表中
//                if (parentMenu != null) {
//                    if (parentMenu.getChildren() == null) {
//                        parentMenu.setChildren(new ArrayList<>());
//                    }
//                    parentMenu.getChildren().add(menu);
//                    // 将该SysUserRoleMenu对象添加到toRemove列表中
//                    toRemove.add(menu);
//                }
//            }
//        }
//
//        // 删除已经被放入children列表中的SysUserRoleMenu对象
//        sysUserRoleMenuList.removeAll(toRemove);
//        return rootMenus;
//    }

//    // 获取系统用户角色菜单列表 (简约)(树型)
//    @Override
//    public List<Map<String, Object>> selectMenuListSimple(List<String> permission_ids) {
//        List<Map<String, Object>> menuList = sysUserRoleMenuDao.selectMenuListSimple(permission_ids);
//        if (menuList.size() > 0) {
//            buildTreeToMap(menuList);
//        }
//        return menuList;
//    }


    // 获取系统用户角色菜单列表
    @Override
    public List<Map<String, Object>> selectUserRoleMenuList(List<String> permission_ids, Integer type) {
        List<Map<String, Object>> sysUserRoleMenuList = sysUserRoleMenuDao.selectUserRoleMenuList(permission_ids, type);
        if (sysUserRoleMenuList.size() > 0) {
            sysUserRoleMenuList = TreeUtil.buildTree(sysUserRoleMenuList);
        }
        return sysUserRoleMenuList;
    }

    // 获取系统用户角色菜单详情
    @Override
    public SysUserRoleMenu selectUserRoleMenuDetail(SysUserRoleMenu sysUserRoleMenu) {
        LambdaQueryWrapper<SysUserRoleMenu> queryWrapper = new LambdaQueryWrapper<SysUserRoleMenu>().eq(SysUserRoleMenu::getId, sysUserRoleMenu.getId());
        SysUserRoleMenu detail = sysUserRoleMenuDao.selectOne(queryWrapper);
        return detail;
    }

    @Override
    public Map<String, Object> insertUserRoleMenu(SysUserRoleMenu sysUserRoleMenu) {

        // 判断权限是否存在
        String permission_id = sysUserRoleMenu.getPermission_id();
        if (StrUtil.isNotEmpty(permission_id)) {
            LambdaQueryWrapper<SysUserRolePermission> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysUserRolePermission::getId, permission_id);
            SysUserRolePermission permission = sysUserRolePermissionDao.selectOne(queryWrapper);
            if (permission == null) throw new CustException("权限不存在");
        }

        sysUserRoleMenuDao.insert(sysUserRoleMenu);

        return Map.of("id", sysUserRoleMenu.getId());
    }

    @Override
    public Map<String, Object> updateUserRoleMenu(SysUserRoleMenu sysUserRoleMenu) {

        SysUserRoleMenu detail = selectUserRoleMenuDetail(sysUserRoleMenu);
        if (detail == null) throw new CustException("菜单不存在");

        // 判断权限是否存在
        String permission_id = sysUserRoleMenu.getPermission_id();
        if (StrUtil.isNotEmpty(permission_id)) {
            LambdaQueryWrapper<SysUserRolePermission> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysUserRolePermission::getId, permission_id);
            SysUserRolePermission permission = sysUserRolePermissionDao.selectOne(queryWrapper);
            if (permission == null) throw new CustException("权限不存在");
        }

        sysUserRoleMenuDao.updateById(sysUserRoleMenu);
        return Map.of("id", sysUserRoleMenu.getId());
    }

    @Override
    public Map<String, Object> deleteUserRoleMenu(SysUserRoleMenu sysUserRoleMenu) {

        SysUserRoleMenu detail = selectUserRoleMenuDetail(sysUserRoleMenu);
        if (detail == null) throw new CustException("菜单不存在");

        sysUserRoleMenuDao.deleteById(sysUserRoleMenu);
        return Map.of("id", sysUserRoleMenu.getId());
    }

}
