package com.jy.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.jy.system.dao.SysRoleMenuDao;
import com.jy.system.model.entity.SysRoleMenu;
import com.jy.system.service.SysRoleMenuService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 角色目录关联 服务实现类
 * </p>
 *
 * @author
 * @since 2024-06-20
 */
@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {

    @Resource
    private SysRoleMenuDao sysRoleMenuDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addRoleLinked(Long roleId, List<Long> menuIdList) {
        if (ObjectUtil.isEmpty(menuIdList)) {
            return;
        }

        List<SysRoleMenu> systemRoleMenuList = menuIdList.stream()
                .map(menuId -> new SysRoleMenu(roleId, menuId))
                .toList();

        sysRoleMenuDao.saveBatch(systemRoleMenuList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void editRoleLinked(Long roleId, List<Long> menuIdList) {
        sysRoleMenuDao.removeByRoleId(roleId);
        addRoleLinked(roleId, menuIdList);
    }

}
