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

import java.util.List;

import com.ecep.contract.IEntityService;
import com.ecep.contract.QueryService;
import com.fasterxml.jackson.databind.JsonNode;
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.ds.vendor.repository.VendorGroupRequireFileTypeRepository;
import com.ecep.contract.model.VendorGroupRequireFileType;

@Lazy
@Service
@CacheConfig(cacheNames = "vendor-group-require-file-type")
public class VendorGroupRequireFileTypeService implements IEntityService<VendorGroupRequireFileType>, QueryService<VendorGroupRequireFileType> {
    @Lazy
    @Autowired
    private VendorGroupRequireFileTypeRepository repository;

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

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

    @Override
    public Page<VendorGroupRequireFileType> findAll(JsonNode paramsNode, Pageable pageable) {
        Specification<VendorGroupRequireFileType> spec = null;
        if (paramsNode.has("searchText")) {
            spec = getSpecification(paramsNode.get("searchText").asText());
        }
        // 添加额外的参数过滤
        return findAll(spec, pageable);
    }

    @Override
    public Specification<VendorGroupRequireFileType> getSpecification(String searchText) {
        return 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);
    }


}
