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.Cacheable;
import org.springframework.cache.annotation.Caching;
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.ProjectIndustryViewModel;
import com.ecep.contract.vo.ProjectIndustryVo;

@Service
@CacheConfig(cacheNames = "project-industry")
public class ProjectIndustryService extends QueryService<ProjectIndustryVo, ProjectIndustryViewModel> {
    @Cacheable(key = "#p0")
    @Override
    public ProjectIndustryVo findById(Integer id) {
        return super.findById(id);
    }

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

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

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

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