package com.qmth.teachcloud.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.teachcloud.common.bean.result.MenuResult;
import com.qmth.teachcloud.common.entity.SysRole;
import com.qmth.teachcloud.common.entity.SysRolePrivilege;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.entity.SysUserRole;
import com.qmth.teachcloud.common.mapper.SysUserRoleMapper;
import com.qmth.teachcloud.common.service.CommonCacheService;
import com.qmth.teachcloud.common.service.SysUserRoleService;
import com.qmth.teachcloud.common.util.ServletUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户和角色关联表 服务实现类
 * </p>
 *
 * @author xf
 * @since 2021-03-23
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

    @Resource
    CommonCacheService commonCacheService;

    @Override
    public List<SysUserRole> listByRoleId(Long id) {
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserRole::getRoleId, id);
        return this.list(queryWrapper);
    }

    @Override
    public List<SysRole> listRoleByUserId(Long userId) {
        return this.baseMapper.listRoleByUserId(userId);
    }

    @Override
    public void removeByUserId(Long id) {
        UpdateWrapper<SysUserRole> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysUserRole::getUserId, id);
        this.remove(updateWrapper);
    }

    @Override
    public MenuResult listByUserId() {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        return commonCacheService.userMenuCache(sysUser.getId());
    }

    @Override
    public void rebindPrivileges(Long roleId) {
        // 查询绑定该角色的所有用户
        List<SysUserRole> sysUserRoles = this.listByRoleId(roleId);
        Set<Long> userIds = sysUserRoles.stream().map(m -> m.getUserId()).collect(Collectors.toSet());
        //删除
        UpdateWrapper<SysUserRole> userRoleUpdateWrapper = new UpdateWrapper<>();
        userRoleUpdateWrapper.lambda().in(SysUserRole::getUserId, userIds);
        List<SysUserRole> list = new ArrayList<>();

        for (Long userId : userIds) {
            List<SysRole> sysRoles = this.listRoleByUserId(userId);
            for (SysRole role : sysRoles) {
                List<SysRolePrivilege> rolePrivileges = commonCacheService.rolePrivilegeCache(role.getId());
                for (SysRolePrivilege rolePrivilege : rolePrivileges) {
                    SysUserRole userRole = new SysUserRole();
                    userRole.setUserId(userId);
                    userRole.setRoleId(role.getId());
                    userRole.setPrivilegeId(rolePrivilege.getPrivilegeId());
                    list.add(userRole);
                }
            }

        }

        this.saveBatch(list);
    }
}
