package io.renren.modules.sys.service.impl.alg;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.MapUtils;
import io.renren.datasource.annotation.DataSource;
import io.renren.modules.sys.dao.alg.SysUserRoleAlgDao;
import io.renren.modules.sys.entity.SysUserRoleEntity;
import io.renren.modules.sys.service.alg.SysUserRoleAlgService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("sysUserRoleAlgService")
@DataSource("alg")
public class SysUserRoleAlgServiceImpl extends ServiceImpl<SysUserRoleAlgDao, SysUserRoleEntity> implements SysUserRoleAlgService {
    @Override
    public void saveOrUpdate(Long userId, List<Long> roleIdList) {
        //先删除用户与角色关系
        this.removeByMap(new MapUtils().put("user_id", userId));

        if(roleIdList == null || roleIdList.size() == 0){
            return ;
        }

        //保存用户与角色关系
        for(Long roleId : roleIdList){
            SysUserRoleEntity sysUserRoleEntity = new SysUserRoleEntity();
            sysUserRoleEntity.setUserId(userId);
            sysUserRoleEntity.setRoleId(roleId);

            this.save(sysUserRoleEntity);
        }
    }
}
