package com.ecep.contract.ds.project.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.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.ecep.contract.IEntityService;
import com.ecep.contract.ds.project.repository.ProductDeliverySignMethodRepository;
import com.ecep.contract.model.DeliverySignMethod;
import com.ecep.contract.model.ProjectSaleType;

@Lazy
@Service
@CacheConfig(cacheNames = "project-sign-method")
public class DeliverySignMethodService implements IEntityService<DeliverySignMethod> {
    @Lazy
    @Autowired
    private ProductDeliverySignMethodRepository deliverySignMethodRepository;

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

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

    public DeliverySignMethod findBySaleTypeAndName(ProjectSaleType saleType, String name) {
        return deliverySignMethodRepository.findBySaleTypeAndName(saleType, name).orElse(null);
    }

    @Cacheable(key = "'type-'+#p0.id+'_code-'+#p1")
    public DeliverySignMethod findBySaleTypeAndCode(ProjectSaleType saleType, String code) {
        return deliverySignMethodRepository.findBySaleTypeAndCode(saleType, code).orElse(null);
    }

    public List<DeliverySignMethod> findAll(Specification<DeliverySignMethod> spec, Sort sort) {
        return deliverySignMethodRepository.findAll(spec, sort);
    }

    @Cacheable(key = "'all'")
    public List<DeliverySignMethod> findAll() {
        return deliverySignMethodRepository.findAll();
    }

    public List<DeliverySignMethod> findAllBySaleTypeId(Integer saleTypeId) {
        return deliverySignMethodRepository.findAllBySaleTypeId(saleTypeId);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            // #p0.saleType.id 可能未载入
            @CacheEvict(key = "'type-'+#p0.saleType.id+'_code-'+#p0.code"),
            @CacheEvict(key = "'all'"),
    })
    public DeliverySignMethod save(DeliverySignMethod usage) {
        return deliverySignMethodRepository.save(usage);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            // #p0.saleType.id 可能未载入
            @CacheEvict(key = "'type-'+#p0.saleType.id+'_code-'+#p0.code"),
            @CacheEvict(key = "'all'"),
    })
    public void delete(DeliverySignMethod method) {
        deliverySignMethodRepository.delete(method);
    }

    @Override
    public Specification<DeliverySignMethod> getSpecification(String searchText) {
        return null;
    }

}
