package com.tuoheng.service.op.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tuoheng.mapper.op.MenusMapper;
import com.tuoheng.mapper.op.PermissionsMapper;
import com.tuoheng.mapper.op.RoleMenuMapper;
import com.tuoheng.mapper.op.RolePermissionMapper;
import com.tuoheng.model.dto.LoginUser;
import com.tuoheng.model.entity.Menu;
import com.tuoheng.model.entity.Permissions;
import com.tuoheng.model.entity.RoleMenu;
import com.tuoheng.model.entity.RolePermission;
import com.tuoheng.model.query.RoleClientQuery;
import com.tuoheng.model.vo.RolePermissionVo;
import com.tuoheng.service.op.PermissionsService;
import com.tuoheng.until.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author 小影
 * @description 针对表【op_permissions(权限配置表)】的数据库操作Service实现
 * @createDate 2023-03-20 14:55:07
 */
@Service
public class PermissionsServiceImpl extends ServiceImpl<PermissionsMapper, Permissions>
        implements PermissionsService {

    @Autowired
    private MenusMapper menusMapper;
    @Autowired
    private PermissionsMapper permissionsMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;


    @Override
    @Transactional(readOnly = true)
    public JsonResult getRolePermissionByRoleId(RoleClientQuery query) {
        RolePermissionVo vo = new RolePermissionVo();
        //根据角色id查询出对应的菜单列表 -> 无父级
        List<Menu> opMenusDtoList = menusMapper.getOpMenusByRoleId(query.getRoleId());


        for (Menu menus : opMenusDtoList) {
            //根据父级id查询出对应所属的菜单
            extracted(query, menus);
        }
        vo.setRoleId(query.getRoleId())
                .setOpMenusList(opMenusDtoList)
                //根据角色id查询出对应的权限集合
                .setPermissionsList(permissionsMapper.getOpPermissionsByRoleId(query.getRoleId()));
        return JsonResult.success(vo);
    }

    /**
     * 添加或者编辑
     *
     * @return
     */
    @Override
    public JsonResult edit(Permissions opPermissions, LoginUser loginUser) {

        Integer opPermissionsId = opPermissions.getId();
        //添加
        if (null == opPermissionsId) {
            //默认
            opPermissions.setCreateUser(1);
            permissionsMapper.insert(opPermissions);
            //添加对应创建权限时的关联关系
            RolePermission opRolePermission = new RolePermission();
            //新增权限直接讲菜单跟当前用户的租户进行关联
            opRolePermission.setRoleId(loginUser.getRoleId());
            opRolePermission.setPermissionId(opPermissions.getId());
            rolePermissionMapper.insert(opRolePermission);
        } else {
            opPermissions.setUpdateUser(loginUser.getUserId().intValue());
            permissionsMapper.updateById(opPermissions);
        }
        return JsonResult.success();
    }


    /**
     * 封装方法 递归菜单 层级
     *
     * @param query
     * @param item
     */
    private void extracted(RoleClientQuery query, Menu item) {
        List<Menu> list = menusMapper.getChildrenMenuByPid(item.getId());
        if (ObjectUtil.isNotEmpty(list)) {
            // 获取角色菜单列表
            List<RoleMenu> roleMenuList = roleMenuMapper.selectList(Wrappers.<RoleMenu>lambdaQuery()
                    .eq(RoleMenu::getRoleId, query.getRoleId())
                    .select(RoleMenu::getMenuId));
            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);
        }
    }
}




