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

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.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.List;
import java.util.Set;

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

    @Resource
    CommonCacheService commonCacheService;

    @Resource
    SysUserRoleMapper sysUserRoleMapper;

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

    /**
     * 根据角色ids和学校id查找
     *
     * @param ids
     * @param schoolId
     * @return
     */
    @Override
    public List<SysUserRole> listByRoleIdsAndSchoolId(Set<Long> ids, Long schoolId) {
        return sysUserRoleMapper.listByRoleIdsAndSchoolId(ids, schoolId);
    }

    @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());
    }
}
