package cn.novalon.manage.db.repository;

import cn.novalon.manage.file.core.domain.SysFile;
import cn.novalon.manage.file.core.repository.ISysFileRepository;
import cn.novalon.manage.db.converter.SysFileConverter;
import cn.novalon.manage.db.dao.SysFileDao;
import cn.novalon.manage.db.entity.SysFileEntity;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * 文件管理仓储实现类
 * 
 * @author 张翔
 * @date 2026-03-13
 */
@Repository
public class SysFileRepository implements ISysFileRepository {

    private final SysFileDao sysFileDao;
    private final SysFileConverter sysFileConverter;

    public SysFileRepository(SysFileDao sysFileDao, SysFileConverter sysFileConverter) {
        this.sysFileDao = sysFileDao;
        this.sysFileConverter = sysFileConverter;
    }

    @Override
    public Flux<SysFile> findByDeletedAtIsNullOrderByCreatedAtDesc() {
        return sysFileDao.findByDeletedAtIsNullOrderByCreatedAtDesc()
                .map(sysFileConverter::toDomain);
    }

    @Override
    public Flux<SysFile> findByCreateByOrderByCreatedAtDesc(String createBy) {
        return sysFileDao.findByCreateByOrderByCreatedAtDesc(createBy)
                .map(sysFileConverter::toDomain);
    }

    @Override
    public Mono<SysFile> findById(Long id) {
        return sysFileDao.findById(id)
                .map(sysFileConverter::toDomain);
    }

    @Override
    public Flux<SysFile> findByFilePathContaining(String fileName) {
        return sysFileDao.findByFilePathContaining(fileName)
                .map(sysFileConverter::toDomain);
    }

    @Override
    public Mono<SysFile> save(SysFile sysFile) {
        SysFileEntity entity = sysFileConverter.toEntity(sysFile);
        return sysFileDao.save(entity)
                .map(sysFileConverter::toDomain);
    }

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