package com.ecep.contract.ds.project.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
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.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.ecep.contract.IEntityService;
import com.ecep.contract.ds.project.repository.ProjectSaleTypeRequireFileTypeRepository;
import com.ecep.contract.model.ProjectSaleTypeRequireFileType;

@Lazy
@Service
@CacheConfig(cacheNames = "project-sale-type-require-file-type")
public class ProjectSaleTypeRequireFileTypeService implements IEntityService<ProjectSaleTypeRequireFileType> {
    @Lazy
    @Autowired
    private ProjectSaleTypeRequireFileTypeRepository repository;

    @Cacheable(key = "#p0")
    public ProjectSaleTypeRequireFileType findById(Integer id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public Page<ProjectSaleTypeRequireFileType> findAll(Specification<ProjectSaleTypeRequireFileType> spec, Pageable pageable) {
        return repository.findAll(spec, pageable);
    }

    @Cacheable(key = "'bySaleType-'+#p0")
    public List<ProjectSaleTypeRequireFileType> findBySaleTypeId(int saleTypeId) {
        return repository.findBySaleTypeId(saleTypeId);
    }

    // findAll
    @Cacheable(key = "'all'")
    public List<ProjectSaleTypeRequireFileType> findAll() {
        return repository.findAll();
    }

    @Override
    public Specification<ProjectSaleTypeRequireFileType> getSpecification(String searchText) {
        return null;
    }

    // save
    @Caching(
            evict = {
                    @CacheEvict(key = "#p0.id"),
                    @CacheEvict(key = "'bySaleType-'+#p0.saleType.id"),
                    @CacheEvict(key = "'all'")
            }
    )
    public ProjectSaleTypeRequireFileType save(ProjectSaleTypeRequireFileType type) {
        return repository.save(type);
    }


    /**
     * delete and evict cache
     */
    @Caching(
            evict = {
                    @CacheEvict(key = "#p0.id"),
                    @CacheEvict(key = "'all'")
            }
    )
    public void delete(ProjectSaleTypeRequireFileType type) {
        repository.delete(type);
    }


}
