package cn.novalon.manage.db.repository;

import cn.novalon.manage.sys.core.domain.SysRolePermission;
import cn.novalon.manage.sys.core.repository.ISysRolePermissionRepository;
import cn.novalon.manage.db.converter.SysRolePermissionConverter;
import cn.novalon.manage.db.dao.SysRolePermissionDao;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * 角色权限关联仓储实现类
 * 
 * @author 张翔
 * @date 2026-03-25
 */
@Repository
public class SysRolePermissionRepository implements ISysRolePermissionRepository {

    private final SysRolePermissionDao sysRolePermissionDao;
    private final SysRolePermissionConverter sysRolePermissionConverter;

    public SysRolePermissionRepository(SysRolePermissionDao sysRolePermissionDao, SysRolePermissionConverter sysRolePermissionConverter) {
        this.sysRolePermissionDao = sysRolePermissionDao;
        this.sysRolePermissionConverter = sysRolePermissionConverter;
    }

    @Override
    public Mono<SysRolePermission> save(SysRolePermission rolePermission) {
        return sysRolePermissionDao.save(sysRolePermissionConverter.toEntity(rolePermission))
                .map(sysRolePermissionConverter::toDomain);
    }

    @Override
    public Mono<Void> deleteById(Long id) {
        return sysRolePermissionDao.deleteById(id);
    }

    @Override
    public Mono<Void> deleteByRoleId(Long roleId) {
        return sysRolePermissionDao.deleteByRoleId(roleId);
    }

    @Override
    public Mono<Void> deleteByPermissionId(Long permissionId) {
        return sysRolePermissionDao.deleteByPermissionId(permissionId);
    }

    @Override
    public Flux<SysRolePermission> findByRoleId(Long roleId) {
        return sysRolePermissionDao.findByRoleId(roleId)
                .map(sysRolePermissionConverter::toDomain);
    }

    @Override
    public Flux<SysRolePermission> findByPermissionId(Long permissionId) {
        return sysRolePermissionDao.findByPermissionId(permissionId)
                .map(sysRolePermissionConverter::toDomain);
    }

    @Override
    public Flux<Long> findPermissionIdsByRoleId(Long roleId) {
        return sysRolePermissionDao.findPermissionIdsByRoleId(roleId);
    }

    @Override
    public Flux<Long> findRoleIdsByPermissionId(Long permissionId) {
        return sysRolePermissionDao.findRoleIdsByPermissionId(permissionId);
    }

    @Override
    public Mono<Void> deleteByRoleIdAndPermissionIds(Long roleId, java.util.List<Long> permissionIds) {
        return sysRolePermissionDao.deleteByRoleIdAndPermissionIds(roleId, permissionIds);
    }

    @Override
    public Mono<Void> deleteByPermissionIdAndRoleIds(Long permissionId, java.util.List<Long> roleIds) {
        return sysRolePermissionDao.deleteByPermissionIdAndRoleIds(permissionId, roleIds);
    }
}