package com.sw.mes.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sw.mes.auth.domain.AuthUserRole;
import com.sw.mes.auth.service.AuthUserRoleService;
import com.sw.mes.auth.mapper.AuthUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author JS111816
* @description 针对表【auth_user_role(【系统域—系统管理】用户角色表)】的数据库操作Service实现
* @createDate 2022-05-19 14:22:04
*/
@Service
public class AuthUserRoleServiceImpl extends ServiceImpl<AuthUserRoleMapper, AuthUserRole>
    implements AuthUserRoleService{
    @Autowired
    private AuthUserRoleMapper authUserRoleMapper;

    @Override
    public void deleteByUserId(Integer userId) {
        LambdaUpdateWrapper<AuthUserRole> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(AuthUserRole::getUserId,userId);
        authUserRoleMapper.delete(updateWrapper);
    }

    @Override
    public List<AuthUserRole> findByUserId(Integer userId) {
        LambdaQueryWrapper<AuthUserRole> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(AuthUserRole::getUserId,userId);
        return authUserRoleMapper.selectList(queryWrapper);
    }

    @Override
    public List<AuthUserRole> findByUserIds(Integer userId,Integer type) {
        return authUserRoleMapper.findByUserIds(userId,type);
    }

    @Override
    public String getRoleNamesByUserId(Integer id) {
        LambdaQueryWrapper<AuthUserRole> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(AuthUserRole::getId,id);
        List<AuthUserRole> roles = authUserRoleMapper.selectList(queryWrapper);
        if(roles == null || roles.size() == 0){
            return "";
        }
        StringBuilder ids = new StringBuilder();
        for (AuthUserRole userRole : roles) {
            ids.append(",").append(userRole.getRoleId());
        }
        return ids.substring(1);
    }
}




