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

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.cloud.SysUserRoleCloudDao;
import io.renren.modules.sys.entity.SysUserRoleEntity;
import io.renren.modules.sys.service.cloud.SysUserRoleCloudService;
import lombok.Data;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("sysUserRoleCloudService")
@DataSource("cloud")
public class SysUserRoleCloudServiceImpl extends ServiceImpl<SysUserRoleCloudDao, SysUserRoleEntity> implements SysUserRoleCloudService {
    @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);
        }
    }
}
