package com.xunmei.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunmei.common.core.web.page.TableDataInfo;
import com.xunmei.system.mapper.SysUserRoleMapper;
import com.xunmei.system.domain.SysUserRole;
import com.xunmei.system.service.ISysUserRoleService;

/**
 * 用户和角色关联Service业务层处理
 *
 * @author xunmei
 * @date 2023-08-10
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public TableDataInfo<SysUserRole> selectPage(SysUserRole sysUserRole) {
        Page<SysUserRole> page;
        if (sysUserRole.getPageNum() != null &&sysUserRole.getPageSize() != null){
            page = new Page<>(sysUserRole.getPageNum(), sysUserRole.getPageSize());
        }else{
            page = new Page<>();
        }
        QueryWrapper<SysUserRole> where = new QueryWrapper<>(sysUserRole);
        baseMapper.selectPage(page, where);
        return TableDataInfo.build(page);

    }


    /**
     * 查询用户和角色关联
     *
     * @param userId 用户和角色关联主键
     * @return 用户和角色关联
     */
    @Override
    public SysUserRole selectSysUserRoleByUserId(Long userId) {
        return sysUserRoleMapper.selectById(userId);
    }

    @Override
    public List<SysUserRole> selectSysUserRoleListByUserId(Long userId) {
        List<SysUserRole> userRole = baseMapper.selectList(new QueryWrapper<SysUserRole>().eq("user_id", userId));
        return userRole;
    }

    /**
     * 查询用户和角色关联列表
     *
     * @param sysUserRole 用户和角色关联
     * @return 用户和角色关联
     */
    @Override
    public List<SysUserRole> selectSysUserRoleList(SysUserRole sysUserRole) {
        return sysUserRoleMapper.selectList(new QueryWrapper<>(sysUserRole));
    }

    /**
     * 新增用户和角色关联
     *
     * @param sysUserRole 用户和角色关联
     * @return 结果
     */
    @Override
    public int insertSysUserRole(SysUserRole sysUserRole) {
            return sysUserRoleMapper.insert(sysUserRole);
    }

    /**
     * 修改用户和角色关联
     *
     * @param sysUserRole 用户和角色关联
     * @return 结果
     */
    @Override
    public int updateSysUserRole(SysUserRole sysUserRole) {
        return sysUserRoleMapper.updateById(sysUserRole);
    }

    /**
     * 批量删除用户和角色关联
     *
     * @param userIds 需要删除的用户和角色关联主键
     * @return 结果
     */
    @Override
    public int deleteSysUserRoleByUserIds(Long[] userIds) {
        return sysUserRoleMapper.deleteUserRole(userIds);
    }

    /**
     * 删除用户和角色关联信息
     *
     * @param userId 用户和角色关联主键
     * @return 结果
     */
    @Override
    public int deleteSysUserRoleByUserId(Long userId) {
        return sysUserRoleMapper.deleteUserRoleByUserId(userId);
    }
}
