import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.chushang.common.core.util.StringUtils;
import com.chushang.security.service.TokenService;
import com.chushang.system.SystemApplication;
import com.chushang.system.entity.po.SysMenu;
import com.chushang.system.entity.po.SysUser;
import com.chushang.security.entity.vo.LoginUser;
import com.chushang.system.entity.vo.RouterVo;
import com.chushang.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.*;

/**
 * @author by zhaowenyuan create 2022/8/22 15:20
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SystemApplication.class)
public class DemoTest {

    @Autowired
    ISysMenuService menuService;
    @Autowired
    ISysDeptService sysDeptService;
    @Autowired
    ISysUserService sysUserService;
    @Autowired
    ISysRoleService sysRoleService;
    @Autowired
    ISysPermissionService permissionService;
    @Autowired
    TokenService tokenService;

//    @Test
//    public void  test(){
//        String username = "zhaowenyuan@31gamestudio.com";
////        String username = "admin";
//
//        SysUser sysUser = sysUserService.selectUserByUserName(username);
//        if (StringUtils.isNull(sysUser)) {
//            log.error("密码错误");
//        }
//
//        // 角色集合
//        Set<String> roles = permissionService.getRolePermission(sysUser);
//        // 权限集合
//        Set<String> permissions = permissionService.getMenuPermission(sysUser);
//
//        LoginUser sysUserVo = new LoginUser();
//        sysUserVo.setUserId(sysUser.getUserId());
//        sysUserVo.setUsername(sysUser.getUsername());
//        sysUserVo.setSysUser(sysUser);
//        sysUserVo.setRoles(roles);
//        sysUserVo.setPermissions(permissions);
//
//        List<SysUserData> userDataList = userDataService.listUserData(sysUser);
//        // 默认传递一个空Map
//        sysUserVo.setAuthDataMap(new HashMap<>());
//        if (CollectionUtil.isNotEmpty(userDataList)){
//            Map<String, Set<String>> authDataMap = new HashMap<>();
//            // 对应的数据权限
//            for (SysUserData sysUserData : userDataList) {
//                authDataMap.put(sysUserData.getDataType().getCodeType(),
//                        new HashSet<>(JSONUtil.toList(sysUserData.getDataValue(), String.class)));
//            }
//            sysUserVo.setAuthDataMap(authDataMap);
//        }
//
//        log.info("{}", JSONUtil.toJsonStr(sysUserVo));
//    }


    @Test
    public void menuTest(){
//        List<SysMenu> menus = menuService.list(
//                new LambdaQueryWrapper<SysMenu>()
//                        .eq(SysMenu::getMenuId, 2136)
//        );
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(new SysUser(1l));
        List<RouterVo> routerVos = menuService.buildMenus(menus);
        log.info("{}", JSONUtil.toJsonStr(routerVos));
    }

}
