package com.ecep.contract.service;

import com.ecep.contract.CompanyFileType;
import com.ecep.contract.converter.CompanyFileTypeStringConverter;
import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.vm.CompanyFileTypeLocalViewModel;
import com.ecep.contract.vo.CompanyFileTypeLocalVo;
import javafx.util.StringConverter;
import org.springframework.cache.annotation.*;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@CacheConfig(cacheNames = "company-file-type")
public class CompanyFileTypeService extends QueryService<CompanyFileTypeLocalVo, CompanyFileTypeLocalViewModel> {
    private final StringConverter<CompanyFileTypeLocalVo> stringConverter = new CompanyFileTypeStringConverter(this);

    @Cacheable(key = "#p0")
    @Override
    public CompanyFileTypeLocalVo findById(Integer id) {
        return super.findById(id);
    }

    @Cacheable(key = "'all'")
    @Override
    public List<CompanyFileTypeLocalVo> findAll() {
        return super.findAll();
    }

    @Caching(put = {@CachePut(key = "#p0.id")},
            evict = {
                    @CacheEvict(key = "'type-'+#p0.lang+'-'+#p0.type.name()"),
                    @CacheEvict(key = "'all'")
            })
    @Override
    public CompanyFileTypeLocalVo save(CompanyFileTypeLocalVo entity) {
        return super.save(entity);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            @CacheEvict(key = "'type-'+#p0.lang+'-'+#p0.type.name()"),
            @CacheEvict(key = "'all'")
    })
    @Override
    public void delete(CompanyFileTypeLocalVo entity) {
        super.delete(entity);
    }

    @Cacheable
    public Map<CompanyFileType, CompanyFileTypeLocalVo> findAll(Locale locale) {
        return findAll(ParamUtils.builder().equals("lang", locale.toLanguageTag()).build(), Pageable.unpaged()).stream()
                .collect(Collectors.toMap(CompanyFileTypeLocalVo::getType, Function.identity()));
    }

    @Override
    public StringConverter<CompanyFileTypeLocalVo> getStringConverter() {
        return stringConverter;
    }

    /**
     * 根据语言标签和参数查找单个 CompanyFileTypeLocalVo 对象
     *
     * @param locale 语言区域
     * @param key    参数键
     * @param value  参数值
     * @return 查找到的 CompanyFileTypeLocalVo 对象，未找到则返回 null
     */
    private CompanyFileTypeLocalVo findOneByLang(Locale locale, String key, Object value) {
        return findAll(ParamUtils.builder().equals("lang", locale.toLanguageTag()).equals(key, value).build(),
                Pageable.ofSize(1))
                .stream().findFirst().orElse(null);
    }

    public CompanyFileTypeLocalVo findByLocaleAndValue(Locale locale, String string) {
        return findOneByLang(locale, "value", string);
    }

    @Cacheable(key = "'type-'+#p0.toLanguageTag()+'-'+#p1.name()")
    public CompanyFileTypeLocalVo findByLocaleAndType(Locale locale, CompanyFileType type) {
        return findOneByLang(locale, "type", type);
    }
}
