package cn.novalon.manage.sys.core.service.impl;

import cn.novalon.manage.sys.core.domain.SysConfig;
import cn.novalon.manage.sys.core.repository.ISysConfigRepository;
import cn.novalon.manage.sys.core.service.ISysConfigService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * 系统配置服务实现类
 * 
 * @author 张翔
 * @date 2026-03-14
 */
@Service
public class SysConfigService implements ISysConfigService {

    private final ISysConfigRepository repository;

    public SysConfigService(ISysConfigRepository repository) {
        this.repository = repository;
    }

    @Override
    public Flux<SysConfig> findAll() {
        return repository.findByDeletedAtIsNull();
    }

    @Override
    @Cacheable(value = "sysConfig", key = "#id")
    public Mono<SysConfig> findById(Long id) {
        return repository.findById(id);
    }

    @Override
    @Cacheable(value = "sysConfig", key = "#configKey")
    public Mono<SysConfig> findByConfigKey(String configKey) {
        return repository.findByConfigKeyAndDeletedAtIsNull(configKey);
    }

    @Override
    @CacheEvict(value = "sysConfig", allEntries = true)
    public Mono<SysConfig> save(SysConfig config) {
        return repository.save(config);
    }

    @Override
    @CacheEvict(value = "sysConfig", key = "#id")
    public Mono<Void> deleteById(Long id) {
        return repository.deleteByIdAndDeletedAtIsNull(id);
    }

    @Override
    public Mono<String> getConfigValue(String configKey) {
        return findByConfigKey(configKey)
                .map(SysConfig::getConfigValue);
    }
}
