package com.agileboot.system.role.service.impl;

import com.agileboot.system.role.entity.SysRoleMenu;
import com.agileboot.system.role.mapper.SysRoleMenuMapper;
import com.agileboot.system.role.service.ISysRoleMenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements ISysRoleMenuService {

    @Override
    public List<SysRoleMenu> listById(Long roleId) {
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = Wrappers.lambdaQuery(SysRoleMenu.class).eq(SysRoleMenu::getRoleId, roleId);
        return super.list(queryWrapper);
    }

    @Override
    public boolean saveMenus(Long roleId, List<Long> menuIds) {
        List<SysRoleMenu> list = new ArrayList<>();
        if (menuIds != null && !menuIds.isEmpty()) {
            for (Long menuId : menuIds) {
                SysRoleMenu rm = new SysRoleMenu();
                rm.setRoleId(roleId);
                rm.setMenuId(menuId);
                list.add(rm);
            }
            return super.saveBatch(list);
        }
        return false;
    }

    @Override
    public void cleanOldMenus(List<Long> roleIds) {
        if (roleIds == null || roleIds.isEmpty()) {
            return;
        }
        this.baseMapper.deleteAbsoluteByIds(roleIds);
    }
}
