package cn.novalon.gym.manage.db.repository;

import cn.novalon.gym.manage.sys.core.domain.SysRolePermission;
import cn.novalon.gym.manage.sys.core.repository.ISysRolePermissionRepository;
import cn.novalon.gym.manage.db.converter.SysRolePermissionConverter;
import cn.novalon.gym.manage.db.dao.SysRolePermissionDao;
import cn.novalon.gym.manage.db.entity.SysRolePermissionEntity;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
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;
    private final R2dbcEntityTemplate r2dbcEntityTemplate;

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

    @Override
    public Mono<SysRolePermission> save(SysRolePermission rolePermission) {
        SysRolePermissionEntity entity = sysRolePermissionConverter.toEntity(rolePermission);
        if (entity.isNew()) {
            return r2dbcEntityTemplate.insert(SysRolePermissionEntity.class)
                    .using(entity)
                    .doOnNext(e -> e.markNotNew())
                    .map(sysRolePermissionConverter::toDomain);
        }
        return sysRolePermissionDao.save(entity)
                .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);
    }
}