package com.qmth.paper.library.common.service.impl;

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

import javax.annotation.Resource;
import java.util.List;

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

    @Resource
    LibraryCommonService libraryCommonService;

    @Resource
    SysUserRoleMapper sysUserRoleMapper;

    @Override
    public List<SysUserRole> listByRoleId(Long id) {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        return sysUserRoleMapper.listByRoleId(id, sysUser.getSchoolId());
    }

    @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 libraryCommonService.getUserMenu(sysUser.getId());
    }

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