package com.xy.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.entity.SysDeptRole;
import com.xy.mapper.SysDeptRoleMapper;
import com.xy.service.SysDeptRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 部门角色表; 服务实现类
 * </p>
 *
 * @author 谭斌
 * @since 2022-12-15
 */
@Service
public class SysDeptRoleServiceImpl extends ServiceImpl<SysDeptRoleMapper, SysDeptRole> implements SysDeptRoleService {

    @Override
    public void saveDeptRole(Long deptId, List<Long> roleIds, Long sysId) {
        List<SysDeptRole> sysDeptRoles = roleIds.stream().map(roleId -> new SysDeptRole().setSysId(sysId).setDeptId(deptId).setRoleId(roleId)).collect(Collectors.toList());
        saveBatch(sysDeptRoles);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDeptRole(Long deptId, List<Long> roleIds, Long sysId) {
        removeDeptRoleById(deptId);
        saveDeptRole(deptId, roleIds, sysId);
    }

    @Override
    public void removeDeptRoleById(Long deptId) {
        remove(Wrappers.<SysDeptRole>lambdaQuery().eq(SysDeptRole::getDeptId, deptId));
    }
}
