package com.chushang.system.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.chushang.system.entity.po.SysRoleDept;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author author
 * @since 2022-08-18
 */
public interface ISysRoleDeptService extends IService<SysRoleDept> {
    @Transactional
    default void saveBatch(Long roleId, Long[] deptIds){

        remove(new LambdaQueryWrapper<SysRoleDept>().eq(SysRoleDept::getRoleId, roleId));

        // 新增角色与部门（数据权限）管理
        List<SysRoleDept> list = new ArrayList<>();
        for (Long deptId : deptIds)
        {
            SysRoleDept rd = new SysRoleDept();
            rd.setRoleId(roleId);
            rd.setDeptId(deptId);
            list.add(rd);
        }
        if (!list.isEmpty())
        {
            saveBatch(list);
        }
    }
    @Transactional
    default void deleteRoleDept(List<Long> roleIds){
        if (CollectionUtil.isNotEmpty(roleIds)){
            roleIds.forEach(this::removeByRoleId);
        }
    }
    @Transactional
    default void removeByRoleId(Long roleId){
        remove(new LambdaQueryWrapper<SysRoleDept>().eq(SysRoleDept::getRoleId, roleId));
    }
}
