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

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.bigdata.SysUserRoleBigDataDao;
import io.renren.modules.sys.entity.SysUserRoleEntity;
import org.springframework.stereotype.Service;
import io.renren.modules.sys.service.bigdata.SysUserRoleBigDataService;

import java.util.List;

@Service("sysUserRoleBigDataService")
@DataSource("bigData")
public class SysUserRoleBigDataServiceImpl extends ServiceImpl<SysUserRoleBigDataDao, SysUserRoleEntity> implements SysUserRoleBigDataService {
    @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);
        }
    }
}
