package com.ningdatech.carapi.sys.manage;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.carapi.datascope.model.DataScopeDto;
import com.ningdatech.carapi.datascope.provider.DataScopeContext;
import com.ningdatech.carapi.sys.entity.Menu;
import com.ningdatech.carapi.sys.entity.Role;
import com.ningdatech.carapi.sys.entity.RoleMenu;
import com.ningdatech.carapi.sys.entity.dto.RolePageQuery;
import com.ningdatech.carapi.sys.entity.vo.MenuRoleVO;
import com.ningdatech.carapi.sys.entity.vo.RoleVO;
import com.ningdatech.carapi.sys.entity.vo.UserInfoVO;
import com.ningdatech.carapi.sys.service.IRoleMenuService;
import com.ningdatech.carapi.sys.service.MenuService;
import com.ningdatech.carapi.sys.service.RoleService;
import com.ningdatech.carapi.user.util.LoginUserUtil;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;

/**
 * @Classname UserRepository
 * @Description
 * @Date 2022/10/12 11:26
 * @Created by PoffyZhang
 */
@Component
@RequiredArgsConstructor
public class RoleManage {

    private final UserManage userManage;
    private final RoleService roleService;
    private final MenuService menuService;

    private final IRoleMenuService roleMenuService;

    public PageVo<RoleVO> queryList(RolePageQuery rolePageQuery) {
        Page<Role> page = rolePageQuery.page();
        LambdaQueryWrapper<Role> wrapper = Wrappers.lambdaQuery(Role.class);
        searchList(page, wrapper, rolePageQuery);
        List<RoleVO> data = CollUtils.convert(page.getRecords(), w -> BeanUtil.copyProperties(w, RoleVO.class));
        return PageVo.of(data, page.getTotal());
    }

    public void searchList(Page<Role> page, LambdaQueryWrapper<Role> wrapper, RolePageQuery rolePageQuery) {
        wrapper.like(StringUtils.isNotBlank(rolePageQuery.getName()), Role::getName, rolePageQuery.getName())
                .orderByAsc(Role::getSort);
        roleService.page(page, wrapper);
    }

    public Optional<DataScopeDto> getCurrentUserDataScoper() {
        UserInfoVO userInfoVO = userManage.queryDetails(LoginUserUtil.getUserId());
        //即便是多个 暂时取一个
        if (Objects.isNull(userInfoVO) || Objects.isNull(userInfoVO.getRole())) {
            return Optional.empty();
        }
        Role role = roleService.getById(userInfoVO.getRole());
        if (Objects.isNull(role) || Objects.isNull(role.getDataScope())) {
            return Optional.empty();
        }
        return DataScopeContext.getDataScope(role.getDataScope());
    }

    public void buildMenu(RoleVO query) {
        //改成直接返回有权限的
        List<Menu> list = menuService.list(Wrappers.lambdaQuery(Menu.class).orderByAsc(Menu::getSort));
        List<RoleMenu> roleMenus = roleMenuService.list(Wrappers.lambdaQuery(RoleMenu.class).eq(RoleMenu::getRoleId, query.getId()));
//        List<MenuRoleVO> menuRoleVOS = CollUtils.convert(list, w -> BeanUtil.copyProperties(w,MenuRoleVO.class));
//        List<MenuRoleVO> menus = TreeUtil.buildTree(menuRoleVOS,roleMenus);
        List<MenuRoleVO> menus = list.stream().map(menu -> {
            for (RoleMenu roleMenu : roleMenus) {
                if (roleMenu.getMenuId().equals(menu.getId())) {
                    return BeanUtil.copyProperties(menu, MenuRoleVO.class);
                }
            }
            return null;
        }).filter(Objects::nonNull).collect(Collectors.toList());
        query.setMenu(menus);
    }
}
