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.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.vm.ProductUsageViewModel;
import com.ecep.contract.vo.ProductUsageVo;

@Service
@CacheConfig(cacheNames = "productUsageCache")
public class ProductUsageService extends QueryService<ProductUsageVo, ProductUsageViewModel> {

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

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

    @Override
    @CacheEvict(allEntries = true)
    public ProductUsageVo save(ProductUsageVo usage) {
        return super.save(usage);
    }

    @Override
    @CacheEvict(allEntries = true)
    public void delete(ProductUsageVo usage) {
        super.delete(usage);
    }

    public ProductUsageVo findByCode(String code) {
        Page<ProductUsageVo> page = findAll(ParamUtils.builder().equals("code", code).build(), Pageable.ofSize(1));
        if (page.isEmpty()) {
            return null;
        }
        return page.getContent().getFirst();
    }

}
