package com.ecep.contract.service;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ecep.contract.converter.ProjectSaleTypeStringConverter;
import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.vm.ProjectSaleTypeViewModel;
import com.ecep.contract.vo.ProjectSaleTypeVo;

import javafx.util.StringConverter;

@Service
@CacheConfig(cacheNames = "project-sale-type")
public class ProjectSaleTypeService extends QueryService<ProjectSaleTypeVo, ProjectSaleTypeViewModel> {
    private final StringConverter<ProjectSaleTypeVo> stringConverter = new ProjectSaleTypeStringConverter(this);

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

    public ProjectSaleTypeVo findByCode(String code) {
        return findAll(ParamUtils.builder().equals("code", code).build(), Pageable.ofSize(1)).getContent().getFirst();
    }

    public ProjectSaleTypeVo findByName(String name) {
        return findAll(ParamUtils.builder().equals("name", name).build(), Pageable.ofSize(1)).getContent().getFirst();
    }

    @Caching(evict = {@CacheEvict(key = "#p0.id")})
    @Override
    public ProjectSaleTypeVo save(ProjectSaleTypeVo v) {
        return super.save(v);
    }

    @Caching(evict = {@CacheEvict(key = "#p0.id")})
    @Override
    public void delete(ProjectSaleTypeVo entity) {
        super.delete(entity);
    }

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