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.SysRoleMenu;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author author
 * @since 2022-08-18
 */
public interface ISysRoleMenuService extends IService<SysRoleMenu> {
    @Transactional
    default void saveBatch(Long roleId, Long[] menuIds){
        // 先删除 对应的角色菜单
        remove(new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, roleId));
        if (null != menuIds && menuIds.length > 0){
            List<SysRoleMenu> list = new ArrayList<>();
            for (Long menuId : menuIds) {
                SysRoleMenu rm = new SysRoleMenu();
                rm.setRoleId(roleId);
                rm.setMenuId(menuId);
                list.add(rm);
            }
            saveBatch(list);
        }
    }
    @Transactional
    default void deleteRoleMenu(Collection<Long> roleIds){
        if (CollectionUtil.isNotEmpty(roleIds)){
            roleIds.forEach(this::removeByRoleId);
        }
    }
    @Transactional
    default void removeByRoleId(Long roleId){
        remove(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId));
    }

    default boolean checkMenuExistRole(Long menuId){
        return count(new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getMenuId, menuId)) > 0;
    }
}
