package com.tuoheng.miniprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import com.tuoheng.miniprogram.dao.MenuMapper;
import com.tuoheng.miniprogram.dao.OpPermissionsMapper;
import com.tuoheng.miniprogram.dao.RoleMenuMapper;
import com.tuoheng.miniprogram.entity.Menu;
import com.tuoheng.miniprogram.entity.OpPermissions;
import com.tuoheng.miniprogram.entity.RoleMenu;
import com.tuoheng.miniprogram.entity.dto.RolePermissionDto;
import com.tuoheng.miniprogram.entity.query.RoleClientQuery;
import com.tuoheng.miniprogram.service.OpPermissionsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Author ChengWang
 * @Date 2022/12/23
 */
@Service
@Slf4j
public class OpPermissionsServiceImpl extends ServiceImpl<OpPermissionsMapper, OpPermissions> implements OpPermissionsService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private OpPermissionsMapper opPermissionsMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    /**
     * 根据 RoleId 获取用户权限
     * @param query
     * @return
     */
    @Override
    public JsonResult getRolePermissionByRoleId(RoleClientQuery query) {
        RolePermissionDto rolePermissionDto = new RolePermissionDto();
        //根据角色id查询出对应的菜单列表 -> 无父级
        List<Menu> opMenusDtoList = menuMapper.getOpMenusByRoleId(query.getRoleId(), query.getClientId());
        for (Menu opMenusDto : opMenusDtoList) {
            //根据父级id查询出对应所属的菜单
            extracted(query, opMenusDto);
        }
        rolePermissionDto.setRoleId(query.getRoleId())
                .setOpMenusList(opMenusDtoList)
                //根据角色id查询出对应的权限集合
                .setPermissionsList(opPermissionsMapper.getOpPermissionsByRoleId(query.getRoleId(), query.getClientId()));
        return JsonResult.success(rolePermissionDto);
    }

    /**
     * 封装方法 递归菜单 层级
     *
     * @param query
     * @param item
     */
    private void extracted(RoleClientQuery query, Menu item) {
        List<Menu> list = menuMapper.getChildrenMenuByPid(item.getId(), query.getClientId());
        if (StringUtils.isNotEmpty(list)) {
            QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_id", query.getRoleId());
            queryWrapper.select("menu_id");
            List<RoleMenu> roleMenuList = roleMenuMapper.selectList(queryWrapper);
            List<Integer> menuIds = roleMenuList.stream().map(p -> p.getMenuId()).collect(Collectors.toList());
            List<Menu> collect = list.stream().map(t -> {
                if (menuIds.contains(t.getId())) {
                    t.setChecked(true);
                    t.setOpen(true);
                }
                extracted(query, t);
                return t;
            }).filter(t -> t.isChecked() == true).collect(Collectors.toList());
            item.setChildren(collect);
        }
    }


}
