package com.ecep.contract.ds.vendor.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.stereotype.Service;

import com.ecep.contract.ds.vendor.repository.VendorGroupRequireFileTypeRepository;
import com.ecep.contract.model.VendorGroupRequireFileType;

@Lazy
@Service
@CacheConfig(cacheNames = "vendor-group-require-file-type")
public class VendorGroupRequireFileTypeService {
    @Lazy
    @Autowired
    private VendorGroupRequireFileTypeRepository repository;

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

    @Cacheable(key = "'byGroup-'+#p0")
    public List<VendorGroupRequireFileType> findByGroupId(int groupId) {
        return repository.findByGroupId(groupId);
    }

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

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

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


}
