package com.ecep.contract.service;

import java.util.List;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import com.ecep.contract.vm.UnitViewModel;
import com.ecep.contract.vo.UnitVo;

@Service
@CacheConfig(cacheNames = "unit")
public class UnitService extends QueryService<UnitVo, UnitViewModel> {

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

    @Cacheable(key = "#name")
    public UnitVo findByName(String name) {
        return findOneByProperty("name", name);
    }

    @Caching(put = @CachePut(key = "#entity.id"), evict = @CacheEvict(key = "'all'"))
    @Override
    public UnitVo save(UnitVo entity) {
        return super.save(entity);
    }

    @Caching(evict = {
            @CacheEvict(key = "#entity.id"),
            @CacheEvict(key = "'all'")
    })
    @Override
    public void delete(UnitVo entity) {
        super.delete(entity);
    }

    @Cacheable(key = "'all'", unless = "#result == null")
    public List<UnitVo> findAllUnits() {
        return super.findAll();
    }

    @Override
    public UnitViewModel createNewViewModel() {
        return new UnitViewModel();
    }

}